how to get 3D coordinates of a sketch segment in the global coordinate system?

Programming and macros
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

how to get 3D coordinates of a sketch segment in the global coordinate system?

Unread post by mihkov »

Initial parameters: There is a test part file PRUG_test_dim_on_SM-FP. It has a sheet metal rectangle located in space not orthogonal to any of the sides - by accident.
Why sheet metal? To have a Boundary_Box sketch. Why a rectangle? To make the Boundary_Box sketch coincide with the edge of the rectangle.
In addition, there is a 2D sketch on this rectangle in which there is a line segment that coincides with the edge, and there is also a 3D sketch in which there is also a line segment that coincides with the edge.
Now I have 3 different sketches in which there are segments whose starting and ending points coincide with the edge of the rectangle.
Now if I programmatically get the coordinates of the beginning and end of these geometric objects in the global coordinate system, they will coincide and I will understand that I am defining them correctly.
Now the problem.
There is a GetCurveParams2 function that only works with edges. And it works perfectly, accurately producing the correct coordinates of the beginning and end of the edge (result 3 in the Immediate window).
There is a function Evaluate2 that should give the coordinates of a point on a segment, the point is specified as a percentage along the length of the segment from 0.0 to 1.0.
The Evaluate2 function works with Curve, which can be obtained from both a sketch segment and an Edge.
Using the Evaluate2 function I only get the first point (0.0) with the correct coordinates, and the end point (1.0) is somewhere far in space.
If I select a segment from any sketch and run Sub main_with_SketchSeg() then I get the result 1 from the Immediate window.
If I select an edge and run Sub main_with_edge() I get a semi-correct Curve-based result (2 in the Immediate window) and a correct Edge-based result (3 in the Immediate window).
How to correctly get 3D coordinates in the global coordinate system for the start and end points of a sketch segment?

Code: Select all

Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSketchSeg As SldWorks.SketchSegment
Dim swEdge      As SldWorks.Edge
Dim swCurve As SldWorks.Curve
Dim startPoint As Variant
Dim endPoint As Variant


'====== Before running this module, select one straight edge
Sub main_with_edge()
' Get the active SolidWorks application
Set swApp = Application.SldWorks
' Get the active model
Set swModel = swApp.ActiveDoc
' Getting the selection manager
Set swSelMgr = swModel.SelectionManager
' Get the selected Edge
Set swEdge = swSelMgr.GetSelectedObject5(1)
If Not swEdge Is Nothing Then
    ' Get the curve object
         Set swCurve = swEdge.GetCurve
        get_coordinatsCurve swCurve
        get_coordinatsEdge swEdge
Else
    MsgBox "Nothing is selected, or the Edge selected is invalid.", vbExclamation
End If
End Sub


'====== Before running this module, select one sketch segment, and only a straight segment.
Sub main_with_SketchSeg()

' Get the active SolidWorks application
Set swApp = Application.SldWorks
' Get the active model
Set swModel = swApp.ActiveDoc
' Getting the selection manager
Set swSelMgr = swModel.SelectionManager
' Get the selected segment
Set swSketchSeg = swSelMgr.GetSelectedObject5(1)
If Not swSketchSeg Is Nothing Then
    ' Get the curve object
         Set swCurve = swSketchSeg.GetCurve
        get_coordinatsCurve swCurve
Else
    MsgBox "Nothing is selected, or the SketchSeg selected is invalid.", vbExclamation
End If
End Sub



Function get_coordinatsCurve(swCurve As SldWorks.Curve)
         If Not swCurve Is Nothing Then
             ' Setting parameters for evaluation on the curve
             Dim params(1 To 2) As Double
             params(1) = 0# ' Initial parameter
             params(2) = 1# ' End parameter
             ' Calculation of points on a curve using parameters
             startPoint = swCurve.Evaluate2(params(1), 0)
             endPoint = swCurve.Evaluate2(params(2), 0)
             ' Using the results
             Debug.Print "Result based on Curve and Evaluate2"
             Debug.Print "Start      = (" & startPoint(0) * 1000# & "/ " & startPoint(1) * 1000# & "/ " & startPoint(2) * 1000# & ") mm "
             Debug.Print "End        = (" & endPoint(0) * 1000# & "/ " & endPoint(1) * 1000# & "/ " & endPoint(2) * 1000# & ") mm "
         End If
End Function

Function get_coordinatsEdge(swEdge As SldWorks.Edge)
         If Not swEdge Is Nothing Then
            Dim vCurveParam As Variant
            'Get points of edge
            vCurveParam = swEdge.GetCurveParams2
            
            'results
            Debug.Print "Result based on Edge and GetCurveParams2"
            Debug.Print "Start      = (" & vCurveParam(0) * 1000# & "/ " & vCurveParam(1) * 1000# & "/ " & vCurveParam(2) * 1000# & ") mm "
            Debug.Print "End        = (" & vCurveParam(3) * 1000# & "/ " & vCurveParam(4) * 1000# & "/ " & vCurveParam(5) * 1000# & ") mm "
        End If
End Function
GetCoordinatesOfEndpointsSketchSegment01.swp
(48 KiB) Downloaded 69 times
result.png
PRUG_test_dim_on_SM-FP.SLDPRT
(81.79 KiB) Downloaded 52 times
User avatar
josh
Posts: 304
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 514

Re: how to get 3D coordinates of a sketch segment in the global coordinate system?

Unread post by josh »

Multiply the point by the inverse of the sketch's transform.
Post Reply