做镜像时,你可以对特征镜像,也可对一个实体镜像。API的MirrorFeature对象的属性MirrorOfBody告知是否实体镜像。而对实体镜像,有两种场景,1)要么新生成的特征挂在原实体上,2)要么创建一个新实体。对于场景1, MirrorFeature.SurfaceBody返回原实体,而对于场景2,没有直接的API,但可通过对原实体面进行定位来获知原实体,因为每个镜像特征面和原实体面都有个位置变换关系,可以用LocateUsingPoint得到原实体面,从而得知是哪个实体。
以下VBA代码假定选择了一个镜像特征,它包括了以上两个场景的处理。
Sub FindOriginalBody()
Dim oPart As PartDocument
Set oPart = ThisApplication.ActiveDocument
Dim oPdef As PartComponentDefinition
Set oPdef = oPart.ComponentDefinition
Dim oMirrorF As MirrorFeature
Set oMirrorF = ThisApplication.ActiveDocument.SelectSet(1)
Dim oFPE As FeaturePatternElement
Dim oMirrorMatrix As Matrix
If oMirrorF.MirrorOfBody Then
Dim oSB As SurfaceBody
‘set end of the feature below the mirror feature
oMirrorF.SetEndOfPart True
Set oSB = oMirrorF.SurfaceBody
‘set the end of feature back
oPdef.Features(oPdef.Features.Count).SetEndOfPart False
‘scenario 1
' if the mirror feature is built in the same solid
If Not oSB Is Nothing Then
Debug.Print "the original body for the mirror feature is:" & oSB.Name
Exit Sub
End If
‘scenario 2
'if the mirror feature is built in a new solid
Dim oIdentityM As Matrix
Set oIdentityM = ThisApplication.TransientGeometry.CreateMatrix
oIdentityM.SetToIdentity
‘ iterate each item of the mirror elements
For Each oFPE In oMirrorF.PatternElements
' actually only two elements with Mirror feature.
' one is the original body, the other is the mirror
If oFPE.Transform.IsEqualTo(oIdentityM) = False Then
' this is the mirror element
Set oMirrorMatrix = oFPE.Transform
End If
Next
oMirrorMatrix.Invert
Dim oEachF As Face
Dim oEachBody As SurfaceBody
' MirrorFeature.Faces contains the faces of mirror feature only
'just check one face
Dim oFaceOnMirror As Face
Set oFaceOnMirror = oMirrorF.Faces(1)
Dim oPtInFace As Point
Set oPtInFace = oFaceOnMirror.PointOnFace
Call oPtInFace.TransformBy(oMirrorMatrix)
On Error Resume Next
'find which body contains the orignal face
For Each oEachBody In oPdef.SurfaceBodies
Dim oOrignFace As Face
Set oOrignFace =
oEachBody.LocateUsingPoint(kFaceObject, oPtInFace)
If Err.Number = 0 Then
If Not oOrignFace Is Nothing Then
Debug.Print "the original body for the mirror feature is:" & oEachBody.Name
'Exit For
End If
Err.Clear
End If
Next
End If
End Sub