Parent of Mirror Solid

By Xiaodong Liang

We have two choices when mirroring a solid, either the mirrored body is attached with the original solid, or a new solid is created.

image

We have two scenarios when mirroring a solid,

1) either the mirrored body is attached with the original solid

2) or a new solid is created

MirrorFeature.MirrorOfBody tells if the mirror feature is from a solid or the features. For scenario 1 above, MirrorFeature.SurfaceBody returns the original solid. For scenario 2, there is no direct API, but we can workaround it by checking the original face of the mirrored face, by which we can get the original solid.

The code below is a small demo. It assumes a mirror feature is selected. It covers the two scenarios.

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 original 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

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading