Transform multipy a point to get measurements in reference coordinate system

Programming and macros
Woodsish
Posts: 1
Joined: Wed Apr 10, 2024 10:19 am
Answers: 0

Transform multipy a point to get measurements in reference coordinate system

Unread post by Woodsish »

Hello!
I am having issues transforming the XYZ assembly coordinates of a point to be relative to a reference coordinate system in the assembly. Users of this macro will build their assemblies in any orientation, however, the reference coordinate system will always be oriented the same way so it would be helpful for further calculations to translate the points.

I have already calculated the Transform Matrix for the reference coordinate system, and found the XYZ coordinates of the points I need to convert, but when I try and use the .MultiplyTransform function, the new values it gives for the points are not correct.

The reference coordinate system “MTV” is located at the point “FrontRight”, so the new XYZ values should be 0, 0, 0. Instead I am getting 0.05715, 0.62865, 24.867602.

Image of the Origin Coordinate System and the MTV Coordinate System:
Image

VBA Code:

Code: Select all

Option Explicit

Sub main()
    '-----------------------------------------------
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelDocExt As SldWorks.ModelDocExtension
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim status As Boolean
    Dim myPoints As Variant
    Dim point As Variant
    Dim pointFeature As Variant
    Dim swMathPt As SldWorks.MathPoint
    Dim swFeat As SldWorks.Feature
    Dim swAssy As SldWorks.AssemblyDoc
        
    '-----------------------------------------------
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager

    '-----------------------------------------------
    Dim FrontRight As Variant
    Dim RearRight As Variant
    Dim FrontLeft As Variant
    Dim PushHeight As Variant
    Dim MTVCoords As Variant
    Dim PointArray As Variant
        
    FrontRight = Array(0, 0, 0)
    RearRight = Array(0, 0, 0)
    FrontLeft = Array(0, 0, 0)
    PushHeight = Array(0, 0, 0)
    MTVCoords = Array(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
    PointArray = Array(FrontRight, RearRight, FrontLeft, PushHeight, MTVCoords)
    
    '-----------------------------------------------
    Dim MomentofInertiaLyy As Long
    Dim MomentofInertiaLxx As Long
    
    MomentofInertiaLyy = 0
    MomentofInertiaLxx = 0

    '-----------------------------------------------
    Dim CartMassCG As Variant
    Dim LoadMassCG As Variant
    
    CartMassCG = Array(0, 0, 0, 0)
    LoadMassCG = Array(0, 0, 0, 0)
    
    '-----------------------------------------------
    Dim i As Integer
    Dim j As Integer
    
    '-----------------------------------------------
    Dim CartLengthValue As Long
    Dim CartWidthValue As Long
    Dim PushHeightValue As Long
    Dim selByIdStr As String
    Dim boolstatus As Boolean
    
    '-----------------------------------------------
    Dim swComp As SldWorks.Component2
    Dim swMathUtils As SldWorks.MathUtility
    Dim coordSysFeature As Variant

    '-----------------------------------------------
    'FIND MTV COORDINATE TRANSLATION----------------
    'https://www.codestack.net/solidworks-api/geometry/transformation/get-coordinate-system-transform/
    boolstatus = swApp.ActiveDoc.Extension.SelectByID2("MTV", "COORDSYS", 0, 0, 0, False, 0, Nothing, 0)
    Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
    
    'Get the Transform Matrix of the Coordiante System
    If Not swFeat Is Nothing Then
        Dim swCoordSys As SldWorks.CoordinateSystemFeatureData
        Set swCoordSys = swFeat.GetDefinition
        
        Dim swMathTransform As SldWorks.MathTransform
        Set swMathTransform = swCoordSys.Transform
        
        Dim vMatrix As Variant
        vMatrix = swMathTransform.ArrayData
        
        i = 0
        For i = 0 To 15
            MTVCoords(i) = vMatrix(i)
        Next i
    End If
    Debug.Print "MTV Coordiante System Transform Matrix:"
    Debug.Print vbTab & MTVCoords(0) & ", " & MTVCoords(1) & ", " & MTVCoords(2) & ", " & MTVCoords(13)
    Debug.Print vbTab & MTVCoords(3) & ", " & MTVCoords(4) & ", " & MTVCoords(5) & ", " & MTVCoords(14)
    Debug.Print vbTab & MTVCoords(6) & ", " & MTVCoords(7) & ", " & MTVCoords(8) & ", " & MTVCoords(15)
    Debug.Print vbTab & MTVCoords(9) & ", " & MTVCoords(10) & ", " & MTVCoords(11) & ", " & MTVCoords(12)
    
    '-----------------------------------------------
    'GET POINT COORDINATES--------------------------
    myPoints = Array("FrontRight", "RearRight", "FrontLeft", "PushHeight")
    i = 0
    For Each point In myPoints
        status = swModelDocExt.SelectByID2(point & "@" & swModel.GetTitle, "DATUMPOINT", 0, 0, 0, False, 1, Nothing, 0)
        Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
        Set pointFeature = swFeat.GetSpecificFeature2
        
        Debug.Print point & " Coordinates:"
        If Not pointFeature Is Nothing Then
            Set swMathPt = pointFeature.GetRefPoint
            Debug.Print vbTab & "x: " & swMathPt.ArrayData(0) * 1000# / 25.4 & "in"
            Debug.Print vbTab & "y: " & swMathPt.ArrayData(1) * 1000# / 25.4 & "in"
            Debug.Print vbTab & "z: " & swMathPt.ArrayData(2) * 1000# / 25.4 & "in"
            j = 0
            For j = 0 To 2
                PointArray(i)(j) = swMathPt.ArrayData(j) * 1000# / 25.4
            Next j
        Else
            MsgBox "Point not found"
        End If
    i = i + 1
    Next point
    
    '-----------------------------------------------
    'THEN TRANSFORM COORDIANTES TO MTV--------------
    Set swMathUtils = swApp.GetMathUtility
            
    i = 0
    For Each point In PointArray
        Set swMathPt = swMathUtils.CreatePoint(point)
        Set swMathTransform = swMathTransform.Inverse
        Set swMathPt = swMathPt.MultiplyTransform(swMathTransform)
        
        
        Dim vCompMTVPoint As Variant
        vCompMTVPoint = swMathPt.ArrayData
        j = 0
        For j = 0 To 2
            PointArray(i)(j) = swMathPt.ArrayData(j)
        Next j
    Debug.Print myPoints(i) & ": "
    Debug.Print vbTab & Join(PointArray(i), ", ")
    i = i + 1
    Next point
        
    '-----------------------------------------------
    'THEN CALCULATE LENGTHS-------------------------
    CartLengthValue = 0
    CartWidthValue = 0
    PushHeightValue = 0
    
    '-----------------------------------------------
    'THEN Next Step---------------------------------


    '-----------------------------------------------
    'THEN Next Step---------------------------------


End Sub
Results Window:

Code: Select all

MTV Coordiante System Transform Matrix:
    3.56327999173599E-17, 1.55218636003651E-18, -1, 0
    -1, 1.18775999724533E-17, -3.56327999173599E-17, 0
    1.18775999724533E-17, 1, 1.55218636003651E-18, 0
    0.62865, -0.117601999999998, 0.05715, 1
FrontRight Coordinates:
    x: 24.75in
    y: -4.62999999999994in
    z: 2.25in
RearRight Coordinates:
    x: 22.75in
    y: -4.62999999999994in
    z: 39.75in
FrontLeft Coordinates:
    x: 1.25in
    y: -4.62999999999994in
    z: 2.25in
PushHeight Coordinates:
    x: 11.9973570414032in
    y: 31.38439107887in
    z: 47.6945927106677in
FrontRight: 
    0.05715, 0.62865, 24.867602
RearRight: 
    -22.12135, -0.117601999999998, 5.71499999999992E-02
FrontLeft: 
    0.05715, 0.62865, 1.367602
PushHeight: 
    -11.3687070414032, -0.117601999999998, 5.71499999999995E-02
Any help would be appreciated! Thank you in advance!
Attachments
Screenshot 2024-04-10 071259.jpg
Post Reply