3 Replies Latest reply on Nov 14, 2018 11:31 PM by Ben Wollenburg

    API macro, get data from vertices in drawing/view

    Ben Wollenburg

      Hello,

       

      I am very new to macros, especially in SW. I am trying to get the coordinates from vertices in drawings. I know this has been asked in the past, but I don't believe it has been resolved... at least to my liking,

       

      I have gone through various examples, and I am able to count the vertices, but I just don't know how to grab the coordinates. I don't know the classes or members to call.

       

      Once again, I want to do this from drawings, not parts or assemblies.

       

      I know there is a ::SelectById2, but how do I use that if I don't know the Id's?

       

      Thanks,

      Ben

        • Re: API macro, get data from vertices in drawing/view
          Alex Burnett

          I haven't tried this with a drawing yet so don't take this as gospel.

           

          I would start by programatically getting a view (swView) on the drawing.

          From there, I would select the displayed model (swRootComponent = swView.RootDrawingComponent).

          Once you have that, you can get a list of child components with (swChildComponentArray = swRootComponent.GetChildren)

           

          Here's where I'm guessing a bit. I would then try to sort through each component and make sure it's not an assembly. I'd get an array of the body/bodies and then use the GetVertices function of the Body2 class.

           

          I'm not sure where to go from there with the data but this should get you started a bit on how to approach this. If you have any issues along the way, post your code and I as well as others will help point you in a direction that should work.

          • Re: API macro, get data from vertices in drawing/view
            Fifi Riri

            Hello, try this:

             

            Option Explicit

            Dim swApp As SldWorks.SldWorks

            Dim swDraw As SldWorks.DrawingDoc

            Dim swView As SldWorks.View

            Dim Comp As SldWorks.Component2

            Dim swVertex As SldWorks.Vertex

            Dim vComps As Variant

            Dim vComp As Variant

            Dim vVerts As Variant

            Dim vVert As Variant

            Dim vEdges As Variant

            Dim vEdge As Variant

            Dim boolstatus As Boolean

             

            Sub main()

            Set swApp = Application.SldWorks

            Set swDraw = swApp.ActiveDoc

            Set swView = swDraw.ActiveDrawingView

            vComps = swView.GetVisibleComponents

            For Each vComp In vComps

                Set Comp = vComp

                'Get all vertices

                Debug.Print "Vertices"

                vVerts = swView.GetVisibleEntities2(Comp, swViewEntityType_e.swViewEntityType_Vertex)

                For Each vVert In vVerts

                    Set swVertex = vVert

                    FindCoordinates swView, swVertex

                Next

                'Get start and end points of all edges

                Debug.Print "Edges"

                vEdges = swView.GetVisibleEntities(Comp, swViewEntityType_e.swViewEntityType_Edge)

                For Each vEdge In vEdges

                    ' Get the "Start" vertex

                    Set swVertex = vEdge.GetStartVertex

                    FindCoordinates swView, swVertex

               

                    ' Get the "End" vertex

                    Set swVertex = vEdge.GetEndVertex

                    FindCoordinates swView, swVertex

                Next

            Next

            End Sub

             

            Sub FindCoordinates(ByVal subView As SldWorks.View, ByVal subVertex As SldWorks.Vertex)

                If subVertex Is Nothing Then Exit Sub

                Dim swPt As Variant

                Dim subpoint

                swPt = subVertex.GetPoint

                Dim swMathUtils As SldWorks.MathUtility

                Dim swXform As SldWorks.MathTransform

                Dim swMathPt As SldWorks.MathPoint

                Set swMathUtils = swApp.GetMathUtility

                Set swXform = subView.ModelToViewTransform

                Debug.Print "  Coordinates in Model Space " & swPt(0) * 1000 & " " & swPt(1) * 1000 & " " & swPt(2) * 1000

                ' transform absolute coordinate to view coordinate

                Set swMathPt = swMathUtils.CreatePoint(swPt)

                Set swMathPt = swMathPt.MultiplyTransform(swXform)

                swPt = swMathPt.ArrayData

                Debug.Print "  Coordinates in Drawing Space " & swPt(0) * 1000 & " " & swPt(1) * 1000

                Debug.Print

            End Sub

            • Re: API macro, get data from vertices in drawing/view
              Ben Wollenburg

              Fifi Riri
              Thanks Fifi Riri,

               

              You got me headed in the right direction...

               

              Sorry for the bloated macro...I have taken bits and pieces and combined things...I have gone through so many iterations, at this point, I am not sure where to "trim the fat"...

               

              Any how with this macro, you select a vertex, run the macro and the coordinates are added as a note with a leader on the drawing. The coordinates are based off the origin of your part.

              If you want to shift the coordinates, (perhaps you want to shift the origin - we do for machining purposes) you would need to account for that in your code @ strNote. (you can also use round here to get rid of some decimal places...)

              I would eventually like to be able to select a point on the drawing and have that be the new origin, then get the notes to reference this new origin.....

              I basically would love the SW Hole table, but for vertices...Maybe one of these days

              -----------------------------------------------------------------------------

              Option Explicit

              Dim swApp As SldWorks.SldWorks

              Dim swDraw As SldWorks.DrawingDoc

              Dim SelMgr As SldWorks.SelectionMgr

              Dim swView As SldWorks.View

              Dim swEdge As SldWorks.Edge

              Dim swVertex As SldWorks.Vertex

              Dim swPt1 As Variant, swPt2 As Variant, swPt3 As Variant, swPt4 As Variant

              Dim i As Long

              Dim swDatumOrigin As SldWorks.DatumOrigin

              Dim myNote As SldWorks.Note     'Add note

              Dim myAnnotation As Object      'Add note

              Dim myTextFormat As Object      'Add note

              Dim boolstatus As Boolean       'Add note

              Dim longstatus As Long          'Add note

              Dim subPoint As Variant

              Dim strNote As String

              Dim subView As SldWorks.View

               

              Sub main()

               

              Set swApp = Application.SldWorks

               

              Set swDraw = swApp.ActiveDoc

               

              Set swView = swDraw.ActiveDrawingView

               

              Set SelMgr = swDraw.SelectionManager

               

              For i = 0 To SelMgr.GetSelectedObjectCount

                  If SelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelVERTICES Then

                      Set swVertex = SelMgr.GetSelectedObject6(i, 0)

                     

                      '----- Start Point ---------

                      Debug.Print "start point: " & i

                      'Set swVertex = swEdge.GetStartVertex

                      swPt1 = swVertex.GetPoint

                      'Dim swPt10 As Long

                      'Dim swPt11 As Long

                      'Dim swPt12 As Long

                      'Set swPt10 = swPt1(0)

                      'Set swPt11 = swPt1(1)

                      'Set swPt12 = swPt1(2)

                      Debug.Print "X: " & (swPt1(0) * 1000 / 25.4)

                      Debug.Print "Y: " & (swPt1(1) * 1000 / 25.4)

                      Debug.Print "Z: " & (swPt1(2) * 1000 / 25.4)

                          

                      'Dim swMathUtils As SldWorks.MathUtility

                      'Dim swXform As SldWorks.MathTransform

                      'Dim swMathPt As SldWorks.MathPoint

                 

                      'Set swMathUtils = swApp.GetMathUtility

                      'Set swXform = subView.ModelToViewTransform

                      'Debug.Print "  Model   coordinates: " & "X: " & subPoint(0) * 1000 / 25.4 & " " & "Y: " & subPoint(1) * 1000 / 25.4 & " " & "Z: " & subPoint(2) * 1000 / 25.4

                                     

                      strNote = "( X:  " & swPt1(0) * 1000 / 25.4 & ", Y: " & swPt1(1) * 1000 / 25.4 & ", Z: " & swPt1(2) * 1000 / 25.4 & ")"

                      swDraw.InsertNewNote2 strNote, "", False, True, _

                          swCLOSED_ARROWHEAD, swLS_SMART, 0#, swBS_None, swBF_Tightest, 0, 0

                      'swPt2 = CoordinateInDrawingView(swPt1, swView)

                     

                     'Set myNote = swDraw.InsertNote(i)

                        

                  'swDraw.ClearSelection2 True   '---> True clears all selections,  false clears only the items in the active selection list

                 

                  End If

               

                  Debug.Print

               

              Next i      'if more than one vertex was selected, it will loop back and get the next vertex

               

              End Sub

               

              Function CoordinateInDrawingView(ByVal subPoint As Variant, ByVal subView As SldWorks.View) As Variant

                  Dim swMathUtils As SldWorks.MathUtility

                  Dim swXform As SldWorks.MathTransform

                  Dim swMathPt As SldWorks.MathPoint

                  Set swMathUtils = swApp.GetMathUtility

                  Set swXform = subView.ModelToViewTransform

                  Debug.Print "  Model   coordinates: " & "X: " & subPoint(0) * 1000 / 25.4 & " " & "Y: " & subPoint(1) * 1000 / 25.4 & " " & "Z: " & subPoint(2) * 1000 / 25.4

                         

                  '' transform absolute coordinate to view coordinate

                  ''Set swMathPt = swMathUtils.CreatePoint(subPoint)

                  ''Set swMathPt = swMathPt.MultiplyTransform(swXform)

                  ''subPoint = swMathPt.ArrayData

                  ''Debug.Print "  Drawing coordinates: " & "X: " & subPoint(0) * 1000 / 25.4 & " " & "Y: " & subPoint(1) * 1000 / 25.4 & " " & "Z: " & subPoint(2) * 1000 / 25.4

                  ''CoordinateInDrawingView = subPoint

                     

              End Function