AnsweredAssumed Answered

How to get the X Y coordinates of the vertex of each component in assembly drawing space

Question asked by Alek 77 on Sep 11, 2020

Hello!
Can you please tell me how to take the X Y coordinates of the vertex of each component in the space of the assembly drawing?
Transformation is a very difficult science to understand, and I just cannot find a simple example to study this subject.
The topic "How to take X Y coordinates of a vertex in drawing space" helped me with the drawing for the part.
https://forum.solidworks.com/message/1071056
To move on, I need to figure out what to do with the assembly drawing.
In my macro, the vertex coordinates of the components are the same. They are wrong. There is also no example how to use the Transform2 (IComponent2) property for drawing.
Please help me. Thank!

 

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim Comp As SldWorks.Component2
Dim eachComp 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
Dim swSelMgr As SldWorks.SelectionMgr
Dim selData As SldWorks.SelectData
Dim i As Long
Dim itr As Long
Dim vOutline() As Variant
Dim vPos() As Variant
Dim swDispDim As SldWorks.DisplayDimension
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swModelDocExt = swDraw.Extension
Set swSelMgr = swDraw.SelectionManager

swDraw.ClearSelection2 True

Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView

Set selData = swSelMgr.CreateSelectData
Debug.Print "View Name = " + swView.GetName2
vComps = swView.GetVisibleComponents
Debug.Print "Comp count = " & swView.GetVisibleComponentCount
For i = LBound(vComps) To UBound(vComps)
Set eachComp = vComps(i)
Debug.Print
Debug.Print "Comp (" & i & ") - " & eachComp.Name2


' Dim swXform As SldWorks.MathTransform
' Set swXform = eachComp.Transform2

vVerts = swView.GetVisibleEntities2(eachComp, swViewEntityType_e.swViewEntityType_Vertex)
For itr = 0 To UBound(vVerts)

Dim currentVertex As SldWorks.Vertex
Set currentVertex = vVerts(itr)

Dim vertexCoord As Variant
vertexCoord = currentVertex.GetPoint
'Debug.Print "Model space Point-" & itr & " (X,Y) - (" & vertexCoord(0) * 1000 & ", " & vertexCoord(1) * 1000 & ", " & vertexCoord(2) * 1000 & ")"

Dim swModelToViewXForm As SldWorks.MathTransform
Set swModelToViewXForm = swView.ModelToViewTransform
Dim swViewTransform As SldWorks.MathTransform
Set swViewTransform = drawingToViewTransform(swView).Inverse
Dim swMathUtils As MathUtility
Set swMathUtils = swApp.GetMathUtility
Dim swMathPt As SldWorks.MathPoint
Set swMathPt = swMathUtils.CreatePoint(vertexCoord)
Set swMathPt = swMathPt.MultiplyTransform(swViewTransform)
Set swMathPt = swMathPt.MultiplyTransform(swModelToViewXForm)

Dim currentPtCoord As Variant
currentPtCoord = swMathPt.ArrayData
' Set swMathPt = swMathUtils.CreatePoint(currentPtCoord)
' Set swMathPt = swMathPt.MultiplyTransform(swXform)
' currentPtCoord = swMathPt.ArrayData

boolstatus = currentVertex.Select4(True, selData)
boolstatus = swDraw.GraphicsRedraw2
Debug.Print "Drawing Space Point-" & itr & " (X,Y) - (" & currentPtCoord(0) * 1000 & ", " & currentPtCoord(1) * 1000 & ")"

Next itr
Next i
Set vComps = Nothing
Set vVerts = Nothing
swModel.ClearSelection2 True
Debug.Print "Ok!"
End Sub
Function drawingToViewTransform(swView As SldWorks.View) As SldWorks.MathTransform
Dim swMathUtil As SldWorks.MathUtility
Dim transformData(15) As Double
Set swMathUtil = swApp.GetMathUtility
transformData(0) = Cos(swView.Angle)
transformData(1) = Sin(swView.Angle)
transformData(2) = 0#
transformData(3) = -Sin(swView.Angle)
transformData(4) = Cos(swView.Angle)
transformData(5) = 0#
transformData(6) = 0#
transformData(7) = 0#
transformData(8) = 1#
transformData(9) = swView.Position(0)
transformData(10) = swView.Position(1)
transformData(11) = 0#
transformData(12) = swView.ScaleDecimal
transformData(13) = 0#
transformData(14) = 0#
transformData(15) = 0#

Set drawingToViewTransform = swMathUtil.CreateTransform(transformData)

End Function

Outcomes