Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 516

Parent of Mirror Solid

$
0
0

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


Viewing all articles
Browse latest Browse all 516

Trending Articles