Transform a point in 3d sketch to a new coordinate system.

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

Transform a point in 3d sketch to a new coordinate system.

Unread post by Jacomuller »

I want to use scan data from a exhaust pipe to create a bend table for our bender. It uses XYZ and not LRA coordinate system. I can postprocess the scan data to generate a center line based on lines and arcs. I have also create a macro that will generate a CSV file with the XYZ coordinates of each point. Everything works very well if the original scan data is aligned correctly before I start post processing. I have now faced a few cases where it will be beneficial to have the ability to manually put a new coordinate system in after post processing. It gives you the ability to manipulate the coordinate system when ever you want. The trick is now to update my macro to, before writing the CSV file, to check for a Custom Coordinate System, and if it exist, transform each point to the new coordinate system.
by ryan-feeley » Thu Dec 14, 2023 7:29 pm
Here are a few references that might help out. You may have have seen these already:

Get the transformation matrix of coordinate system using SOLIDWORKS API
That shows how to do the conditional logic to get the transformation only if an extra coordinate system is available.

Get Points of Repeating Elements in Table-driven Pattern (VBA)
That sort of shows how to use the transform on a 3D point [I think this is the where you're currently stuck]

Here's some incomplete pseudo code

Code: Select all


Dim pointsArray(2) As Double

# Loop through all the features as swFeat, and look for a Coordinate System

If swFeat.GetTypeName2() = "CoordSys" Then
                
    Dim swCoordSys As SldWorks.CoordinateSystemFeatureData
             
    Set swCoordSys = swFeat.GetDefinition
    Dim swMathTransform As SldWorks.MathTransform
    Set swMathTransform = swCoordSys.Transform

    # Now loop through all the points that you are currently writing to CSV.
    # Make a length 3 vector with each, and transform it
    For i = 0 To ...
    
        pointsArray(0) = CURRENT_X: pointsArray(1) = CURRENT_Y: pointsArray(2) = CURRENT_Z
        Set swMathPoint = swMathUtility.CreatePoint(pointsArray)
   
   	# I DON"T KNOW IF YOU NEED THE INVERSE HERE OR NOT. One transform is global --> coordinate system.
   	# The inverse will go the other way.
        Set swMathPoint = swMathPoint.MultiplyTransform(swMathTransform.Inverse)    
        'Print the transformed (x,y,z) 
        point = "x: " & swMathPoint.ArrayData(0) & "   y: " & swMathPoint.ArrayData(1) & "   z: " & swMathPoint.ArrayData(2)    
        
        # Now log the new x,y,z of each swMathPoint in your CSV file.
Good luck!
Go to full post
ryan-feeley
Posts: 82
Joined: Thu Jan 20, 2022 3:35 pm
Answers: 1
x 31
x 91

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by ryan-feeley »

Could you do the math in your macro?

I believe the API allows you to extract the 4x4 spatial transformation matrix from a selected coordinate system. Get that and then do the matrix math to transform each of the points?

Maybe there is a more direct way, but I'm not aware of it.
User avatar
SPerman
Posts: 2053
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2224
x 1874
Contact:

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by SPerman »

The API will do the matrix transformation for you.

A complete overview of matrix transformations in the SOLIDWORKS API
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by Jacomuller »

Thanks @SPerman , @ryan-feeley . I saw those websites but I still struggling :? :lol:

So I have a 3D sketch for the center line of the exhaust pipe. I have a number of points in the 3D sketch required for the bender. I have created the "New" coordinate system in a location that will work with the tube bender.

The macro will grab the X,Y,Z values of each point in the 3D sketch (This work fine and is saved in a CSV file.). The macro step through all items in the feature tree and find the "Coordinate System" inserted by the user. ... and this is where the wheels come off :?

A possible reason for my confusion is that I am nor transforming a "body" but only a point. Also, I am not really transforming it in the Solidworks part, I am only transforming the data for the CSV file.

The more I read, the more I get attacked by confusion. :oops: Please help
User avatar
SPerman
Posts: 2053
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2224
x 1874
Contact:

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by SPerman »

I would attack this a different way. I would create a new part (or preferably an assembly.) In this new assembly, I would insert the part with the 3d sketch, and use constraints to locate the 3d sketch. Then you don't have to mess with any of the matrix nonsense.

Part with random 3d sketch.
image.png
Part inserted in assembly with mates.
image.png
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by Jacomuller »

@SPerman Yip, I have done it that way a few times. I was just thinking of making it easier for other people in the team. Also, by doing it the way I want, I can leave the original part modeled from the scan data and move the coordinate system to suite the Bender. If, for what ever reason, the bender requires a different origin, it can be done by dragging the coordinate system to a new spot and run the macro.
ryan-feeley
Posts: 82
Joined: Thu Jan 20, 2022 3:35 pm
Answers: 1
x 31
x 91

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by ryan-feeley »

Here are a few references that might help out. You may have have seen these already:

Get the transformation matrix of coordinate system using SOLIDWORKS API
That shows how to do the conditional logic to get the transformation only if an extra coordinate system is available.

Get Points of Repeating Elements in Table-driven Pattern (VBA)
That sort of shows how to use the transform on a 3D point [I think this is the where you're currently stuck]

Here's some incomplete pseudo code

Code: Select all


Dim pointsArray(2) As Double

# Loop through all the features as swFeat, and look for a Coordinate System

If swFeat.GetTypeName2() = "CoordSys" Then
                
    Dim swCoordSys As SldWorks.CoordinateSystemFeatureData
             
    Set swCoordSys = swFeat.GetDefinition
    Dim swMathTransform As SldWorks.MathTransform
    Set swMathTransform = swCoordSys.Transform

    # Now loop through all the points that you are currently writing to CSV.
    # Make a length 3 vector with each, and transform it
    For i = 0 To ...
    
        pointsArray(0) = CURRENT_X: pointsArray(1) = CURRENT_Y: pointsArray(2) = CURRENT_Z
        Set swMathPoint = swMathUtility.CreatePoint(pointsArray)
   
   	# I DON"T KNOW IF YOU NEED THE INVERSE HERE OR NOT. One transform is global --> coordinate system.
   	# The inverse will go the other way.
        Set swMathPoint = swMathPoint.MultiplyTransform(swMathTransform.Inverse)    
        'Print the transformed (x,y,z) 
        point = "x: " & swMathPoint.ArrayData(0) & "   y: " & swMathPoint.ArrayData(1) & "   z: " & swMathPoint.ArrayData(2)    
        
        # Now log the new x,y,z of each swMathPoint in your CSV file.
Good luck!
Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by Jacomuller »

>< @ryan-feeley Thanks mate =)
I had to massage it a little but it appears to be working now. I still need to cleanup the code a bit, but I am getting the the transformed values now in the CSV file.
ryan-feeley
Posts: 82
Joined: Thu Jan 20, 2022 3:35 pm
Answers: 1
x 31
x 91

Re: Transform a point in 3d sketch to a new coordinate system.

Unread post by ryan-feeley »

Nicely done!
Post Reply