Files
Associated model(s) Get start and end points of sketch segment
Code
'This cycles through each segment in a sketch and gets the start and end location if it _
is a circle or line.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
'Precondition: sketch is selected in feature manager tree
'Results: see immediate window
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSketch As SldWorks.Sketch
Dim swSelMgr As SldWorks.SelectionMgr
Dim vSkSegs As Variant
Dim swSkSeg As SldWorks.SketchSegment
Dim swCurve As SldWorks.Curve
Dim i As Integer
Dim vCurveParams As Variant
Dim dblStart As Double
Dim dblEnd As Double
Dim bClosed As Boolean
Dim bPeriodic As Boolean
Dim dblX As Double
Dim dblY As Double
Dim dblZ As Double
Dim dblLength As Double
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
'make sure a sketch is selected
If swSelMgr.GetSelectedObjectType3(1, -1) <> 9 Then
MsgBox "Select a sketch."
End
End If
'open sketch
swModel.InsertSketch2 True
'swModel.Insert3DSketch2 True 'works for 3d sketches as well
Set swSketch = swModel.GetActiveSketch2
'get sketch segments
vSkSegs = swSketch.GetSketchSegments
For i = 0 To UBound(vSkSegs)
Set swSkSeg = vSkSegs(i)
If swSkSeg.ConstructionGeometry = 0 Then 'ignore construction geometry
'get underlying curve
Set swCurve = swSkSeg.GetCurve
swCurve.GetEndParams dblStart, dblEnd, bClosed, bPeriodic
dblLength = Round(swSkSeg.GetLength, 6)
'get the curve data depending on what type of curve
If swCurve.Identity = 3001 Then 'line
Debug.Print "Type: Line (" & swCurve.Identity & ")"
Debug.Print "Length: " & dblLength
vCurveParams = swCurve.LineParams
Debug.Print " Start: " & Round(vCurveParams(0), 6) & ", " & _
Round(vCurveParams(1), 6) & ", " & Round(vCurveParams(2), 6)
dblX = Round(vCurveParams(0) + dblLength * vCurveParams(3), 6)
dblY = Round(vCurveParams(1) + dblLength * vCurveParams(4), 6)
dblZ = Round(vCurveParams(2) + dblLength * vCurveParams(5), 6)
Debug.Print " End: " & dblX & ", " & dblY & ", " & dblZ
ElseIf swCurve.Identity = 3002 Then 'circle
Debug.Print "Type: Circle (" & swCurve.Identity & ")"
Debug.Print "Length: " & dblLength
vCurveParams = swCurve.CircleParams
Debug.Print "Radius: " & Round(vCurveParams(6), 6)
Else 'other
Debug.Print "Sketch segment is not a line or circle; data will not be retrieved."
End If
Debug.Print "========="
End If
Next i
End Sub
'Sketch Segments can be sketch arc, line, ellipse, parabola or spline.
'ISketchSegment::GetCurve will return null unless sketch arc, line, ellipse, parabola or spline.
'If using ICurve::Identity...
'Line: 3001
'Circle: 3002
'Ellipse: 3003
'Spline (B-curve): 3005
'Once the curve type is detected, use the following member to get the data
'Line: LineParams
'Circle: CircleParams
'Ellipse: GetEllipseParams
'Spline: GetBCurveParams5
'To get the length of a line, use ISketchSegment::GetLength, not ICurve::GetLength3
'LineParams:
'X: cos(deg in radians)
'Y: sin(deg in radians)
'Z: ?
'radians = degrees * pi / 180
'note that 1 deg below X is 359 is -1 not 1