MACRO LIBRARY

Get center mark's owning edge

Free public access. No account or subscription required.

Files

Get center mark's owning edge

Code

Module: mMain

'Contains function for selecting the edge associated with a center mark in a drawing view. _
    Since IAnnotation:: GetAttachedEntities will not return the annotation, this code is _
    needed instead.
    
'Preconditions: center mark is selected
'Results: center mark's edge is selected
    
'Notes:
'If the edge is very small and the correct edge is not being selected then you might _
    modify the tolerance arguments in ICurve::GetTessPts.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swCenterMark As SldWorks.centermark
    Dim swEdge As SldWorks.Edge
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    'get pre-selected center mark
    Set swSelMgr = swModel.SelectionManager
    If swSelMgr.GetSelectedObjectType3(1, -1) <> 100 Then
        MsgBox "Select a center mark."
        End
    Else
        Set swCenterMark = swSelMgr.GetSelectedObject6(1, -1)
        Set swEdge = GetCenterMarksEdge(swApp, swCenterMark)    'run our function
    End If
    
    'select the edge
    Dim swEnt As SldWorks.Entity
    Dim swView As SldWorks.view
    Dim swAnn As SldWorks.Annotation
    
    Set swEnt = swEdge
    Set swAnn = swCenterMark.GetAnnotation
    Set swView = swAnn.Owner
    swView.SelectEntity swEnt, False
End Sub

Module: mGetCenterMarksEdge

Function GetCenterMarksEdge(swApp As SldWorks.SldWorks, swCenterMark As SldWorks.centermark) As Edge
    'get center mark's location
    Dim swAnn As SldWorks.Annotation
    Dim vCoords As Variant
    
    Set swAnn = swCenterMark.GetAnnotation
    vCoords = swAnn.GetPosition 'this doesn't get the origin but a point on the edge, _
        which is why later we can simply search for an edge that has those exact coords
    
    'transform center mark position to component space
    Dim swMathUtil As SldWorks.MathUtility
    Dim swMathPt As SldWorks.MathPoint
    Dim swTransform As SldWorks.MathTransform
    Dim swView As SldWorks.view
    Dim dblX As Double, dblY As Double, dblZ As Double
    
    Set swMathUtil = swApp.GetMathUtility
    Set swMathPt = swMathUtil.CreatePoint(vCoords)
    Set swView = swAnn.Owner
    Set swTransform = swView.ModelToViewTransform
    Set swTransform = swTransform.Inverse
    Set swMathPt = swMathPt.MultiplyTransform(swTransform)
    vCoords = swMathPt.ArrayData
    dblX = Round(vCoords(0), 3)
    dblY = Round(vCoords(1), 3)
    dblZ = Round(vCoords(2), 3)
    'Debug.Print dblX & ", " & dblY & ", " & dblZ
    
    'Next, traverse bodies of referenced models. What is going on is a little confusing. _
        So, working backwards: ultimately we are trying to select the edge with IView:: _
        SelectEntity. However for this to work, the entity needs to be belong to the _
        model referenced by the view. So in the case of a part, the part itself, and in _
        the case of an assembly, the assembly itself. IView::GetVisibleComponents does _
        not return the assembly but rather the components so it cannot be used if the _
        referenced model is an assembly. Instead we need to use IView::RootDrawingComponent _
        to get the assembly and then IDrawingComponent::GetChildren to get the children. _
        This method nor IDrawingComponent::Component will work to get the component if the _
        referenced model is simply a part, however. So instead for a part we do have to use _
        IView::GetVisibleComponents. At this point you might think, why not just use _
        IComponent2::GetBodies2 to get the bodies. This will not return the bodies however _
        from the visible components in a drawing view because this method actually looks for _
        the bodies in the active configuration of the component's owner, which in our case _
        happens to be the drawing itself, which has no configurations. Consequently we need _
        to use IComponent2::GetModelDoc2 and then cast the IModelDoc2 to IPartDoc and then _
        use IPartDoc::GetBodies2 to get the bodies.
        
    Dim swDrawComp As SldWorks.DrawingComponent
    Dim swComp As SldWorks.Component2
    Dim vComps As Variant
    Dim vEdges As Variant
    Dim swCompModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim swBody As SldWorks.Body2
    Dim swEnt As SldWorks.Entity
    Dim i As Integer
    
    Set swDrawComp = swView.RootDrawingComponent
    Set swComp = swDrawComp.Component
    If swComp Is Nothing Then   'if the referenced model is a part and not an assembly...
        vComps = swView.GetVisibleComponents
        Set swComp = vComps(0)  'since the ref'ed model is a part it should only have one comp
        Set swCompModel = swComp.GetModelDoc2
        Set swPart = swCompModel
        vBodies = swPart.GetBodies2(swSolidBody, False)
        Set swBody = vBodies(0) 'assume one body in component
        Set GetCenterMarksEdge = GetEntity(swBody, dblX, dblY, dblZ)
    Else
        vComps = swComp.GetChildren
        For i = 0 To UBound(vComps)
            Set swComp = vComps(i)
            vBodies = swComp.GetBodies2(swSolidBody)
            Set swBody = vBodies(0) 'assume one body in component
            Set GetCenterMarksEdge = GetEntity(swBody, dblX, dblY, dblZ)
            If Not GetCenterMarksEdge Is Nothing Then Exit For
        Next i
    End If
End Function
    
Function GetEntity(swBody As SldWorks.Body2, dblX As Double, dblY As Double, dblZ As Double) _
    As SldWorks.Entity
    Dim swEdge As SldWorks.Edge
    Dim swCurve As SldWorks.Curve
    Dim swCurveParamData As SldWorks.CurveParamData
    Dim vStartPt As Variant, vEndPt As Variant
    Dim vTessPts As Variant
    Dim j As Integer, k As Integer
    Dim dblTessX As Double, dblTessY As Double, dblTessZ As Double
    Dim vBodies As Variant
    
    vEdges = swBody.GetEdges
    For j = 0 To UBound(vEdges)
        Set swEdge = vEdges(j)
        Set swCurveParamData = swEdge.GetCurveParams3
        vStartPt = swCurveParamData.StartPoint
        vEndPt = swCurveParamData.EndPoint
        Set swCurve = swEdge.GetCurve
        vTessPts = swCurve.GetTessPts(0, 0.0001, vStartPt, vEndPt)
        For k = 0 To UBound(vTessPts) Step 3
            dblTessX = Round(vTessPts(k), 3)
            dblTessY = Round(vTessPts(k + 1), 3)
            dblTessZ = Round(vTessPts(k + 2), 3)
            'Debug.Print Round(vTessPts(k), 3) & ", " & Round(vTessPts(k + 1), 3) _
                & ", " & Round(vTessPts(k + 2), 3)
            If dblX = dblTessX And dblY = dblTessY And dblZ = dblTessZ Then
                Set GetEntity = swEdge
                Exit Function
            End If
        Next k
    Next j
End Function