GetVisibleEntities and process vertex position

Hi guys,
I need your advice. I am trying to get all vertices from visible entites and evalute their position in sheet coordinates. the following code works fine for a part drawing.
when i process the macro in in assembly drawings the result is wrong → the coordinates doesn’t fit. what am i doing wrong? how can I get the matrix from the assembly transformation? When I use Component2.Transform2 it always returns null…
does anyone have an idea?
I am working with SW 2022 SP5

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swView As View
Dim swDrawing As DrawingDoc
Dim selMgr As SelectionMgr
Dim status As Boolean

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDrawing = swModel
Set selMgr = swModel.SelectionManager

status = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
Set swView = selMgr.GetSelectedObject6(1, -1)

'Fetch visible Entities
Dim vComps As Variant
vComps = swView.GetVisibleComponents

Dim vComp As Variant
Dim comp As Component2

For Each vComp In vComps
    
    Set comp = vComp

    Dim vVertexes As Variant
    vVertexes = swView.GetVisibleEntities2(comp, swViewEntityType_e.swViewEntityType_Vertex)
    
    Dim vVertex As Variant
    Dim swVertex As Vertex
    Dim swEntity As Entity
    Dim point As Variant
    
    Dim modelToViewTransform As MathTransform
    Dim modelToSketchTransform As MathTransform
    Dim swScaleTransform As MathTransform
    Dim swPositionTransform As MathTransform
   
    Dim swSketch As Sketch
    Set swSketch = swView.GetSketch
    
    Dim swMathUtility As MathUtility
    Set swMathUtility = swApp.GetMathUtility

    Set modelToSketchTransform = swSketch.modelToSketchTransform
    Set swScaleTransform = ScaleTransform(swView)
    Set modelToViewTransform = swView.modelToViewTransform
    Set swPositionTransform = PositionTransform(swView)

    Debug.Print ""
    Dim swMathPoint As MathPoint
    
    For Each vVertex In vVertexes
    
        Set swVertex = vVertex
        point = swVertex.GetPoint
                     
        Set swMathPoint = swMathUtility.CreatePoint(point)
        Set swMathPoint = swMathPoint.MultiplyTransform(modelToSketchTransform) 'Dimension to Part-Origin
        Set swMathPoint = swMathPoint.MultiplyTransform(swScaleTransform)
        Set swMathPoint = swMathPoint.MultiplyTransform(modelToViewTransform)

        Debug.Print "TransformedMathpoint => " & swMathPoint.ArrayData(0) * 1000 & " | " & swMathPoint.ArrayData(1) * 1000
    
        swVertex.Select4 True, Nothing
    Next vVertex
Next vComp


swModel.ClearSelection2 True
End Sub


Public Function ScaleTransform(swView As SldWorks.View) As SldWorks.MathTransform

    Dim swMathUtil  As SldWorks.MathUtility
    Dim transformData(15) As Double
    
    Set swMathUtil = swApp.GetMathUtility

    transformData(0) = 1#
    transformData(1) = 0#
    transformData(2) = 0#
    transformData(3) = 0#
    transformData(4) = 1#
    transformData(5) = 0#
    transformData(6) = 0#
    transformData(7) = 0#
    transformData(8) = 1#
    transformData(9) = 0#
    transformData(10) = 0#
    transformData(11) = 0#
    transformData(12) = swView.ScaleDecimal
    transformData(13) = 0#
    transformData(14) = 0#
    transformData(15) = 0#

    Set ScaleTransform = swMathUtil.CreateTransform(transformData)

End Function

Public Function PositionTransform(swView As SldWorks.View) As SldWorks.MathTransform

    Dim swMathUtil  As SldWorks.MathUtility
    Dim transformData(15) As Double
    
    Set swMathUtil = swApp.GetMathUtility
    
    Dim pos As Variant
    pos = swView.GetViewXform
    

    transformData(0) = pos(0)
    transformData(1) = pos(1)
    transformData(2) = pos(2)
    transformData(3) = pos(3)
    transformData(4) = pos(4)
    transformData(5) = pos(5)
    transformData(6) = pos(6)
    transformData(7) = pos(7)
    transformData(8) = pos(8)
    transformData(9) = pos(9)
    transformData(10) = pos(10)
    transformData(11) = pos(11)
    transformData(12) = pos(12)
    transformData(13) = 0#
    transformData(14) = 0#
    transformData(15) = 0#

    Set PositionTransform = swMathUtil.CreateTransform(transformData)

End Function

ah, finally got it. it works for assembly drawings, for single part drawings I need to adjust the process a little bit.

I think you may have forgotten to transform the coordinates of the vertex from the component coordinate system to the top of the level assembly.

I’ve tried to reach the MathTransform of a component, but swComp.Transform2 always returns null…

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swMathUtil = swApp.GetMathUtility
Set swDrawing = swModel
Set selMgr = swModel.SelectionManager

status = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
Set swView = selMgr.GetSelectedObject6(1, -1)

Dim comps As Variant
comps = swView.GetVisibleDrawingComponents

Dim drawingComp As DrawingComponent
Set drawingComp = comps(0)

Dim swComp As Component2
Set swComp = drawingComp.Component

Dim swMathTransform As MathTransform
Set swMathTransform = swComp.Transform2

Dim vPos(2) As Double
vPos(0) = drawingComp.Component.Transform2.ArrayData(9)
vPos(1) = drawingComp.Component.Transform2.ArrayData(10)
vPos(2) = drawingComp.Component.Transform2.ArrayData(11)

End Sub