0

There is a solidworks assembly containing a point "P" and a coordinate system frame "F". The goal is to find x,y,z coordinates of the point "P" wrt the frame "F". The VB code looks like

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim bstatus As Boolean
    Dim swMeasure As SldWorks.Measure
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    swModel.ClearSelection2 True
    bstatus = swModel.Extension.SelectByID2("P", "DATUMPOINT", 0, 0, 0, False, 0, Nothing, 0)
    Debug.Assert bstatus

    Set swMeasure = swModel.Extension.CreateMeasure
    bstatus = swMeasure.Calculate(Nothing)
    Debug.Assert bstatus

    Debug.Print ("X: " & swMeasure.X)
    Debug.Print ("Y: " & swMeasure.Y)
    Debug.Print ("Z: " & swMeasure.Z)
End Sub

The script works, but it gives coordinates in recently used coordinate system frame. It can be the frame "F" or the default frame, so the result is unpredictable. I couldn't find the properties of the Measure class to force it to use the necessary frame. Is there a way to explicitly specify the frame? Is there a workaround?

1 Answer 1

0

You can get the point coordinates like this example method, then you might need to multiply that with the matrix of the coordinate system with GetCoordinateSystemTransformByName or GetDefinition ot the Coordinate System Feature, and if the point P is in a component, you will need to multiply previous matrix with the component matrix transform Transform2.

See also this related post

Sign up to request clarification or add additional context in comments.

4 Comments

It's completely unclear in which coordinate system frame the example gives the coordinates. In my case the coordinates are not in default frame, neither in frame F. It seems it's an auxiliary frame related to a part in assembly. So, the workaround doesn't work.
If the point is on the assembly, the example will give the coordinates in the default coordinates system of the assembly. If the point is on a part of the assembly, the example will give the coordinates in the default coordinates system of this part. From there you will have to convert it with the relevant matrix transforms, depending on where the point is, and where is your frame F.
The function GetCoordinateSystemTransformByName requires me to specify name of the frame. What is the name of the default frame of a part? How can I get it?
GetCoordinateSystemTransformByName is only for a custom coordinate system. Again: To get the coordinate in the default coordinate system just use the 'method' or 'getdefinition' links in my original reply. Then convert those coordinates with the transform matrix of the part (see original reply), and/or those of the sub-assemblies if applicable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.