How to get .SketchPoint object of SketchSegment End Points?

Hello,

I'm working on a macro that is meant to insert a line segment and then add relations to the line endpoints.

So I create a sketch segment with: Set skSegment = swModel.SketchManager.CreateLine(x1, y1, z1, x2, y2, z2)

My goal is to then get the .SketchPoint objects for both endpoints of the line segment. And the use .select4 method to select it.

I know ISketchArc has a GetStartPoint2() method that does what I'm looking for, the problem is though that I have a SketchSegment object not a SketchArc object.

I can't believe there is not a similar method for SketchSegment objects.

I'm trying to avoid using SelectByID, because even though I know the xyz location of the endpoints, there is also another sketchpoint at the same exact location of the endpoints. I'm actually trying to get the endpoints to have coincident relations with those.

Any help is appreciated,

Thank you!

SolidworksApi macros
Answers 3
JB
JB 2017-09-13 Best Answer

There are multiple types of SketchSegments.  A SketchArc is a type of SketchSegment.  So is a SketchLine, SketchSpline, SketchEllipse, etc...

Dim you a SketchLine.  Set it = to the SketchSegment of interest.  Then you can use GetEndPoint2.

See the help for ISketchSegment.GetType.

AJ
AJ 2017-09-14

if you have this in your code :

<span style="color: #333333; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; font-size: 14px;">Set skSegment = swModel.SketchManager.CreateLine(x1, y1, z1, x2, y2, z2)</span>

Then you have obviously if the construction is successfull, the created sketch segment is line and thus you have a sketch line.
A SketchSegment in this case (and only this case) can be casted into a ISketchLine object (otherwise do the type check like Josh said).

Do something like this:

<p>Dim swLine as SldWorks.SketchLine<br>Set swLine = skSegment</p>

Than you can use these methods to get the start and the end:
2016 SOLIDWORKS API Help - GetStartPoint2 Method (ISketchLine)

2015 SOLIDWORKS API Help - IGetEndPoint2 Method (ISketchLine)

Be mindful that these methods can fail under certain circumstances.

Cheers,
Amen
CADHERO consulting SUARL – CAD Design Automation & Training – SOLIDWORKS API

JM
JM 2017-09-14

Set StartPoint = skSegment.GetStartPoint2()

Set EndPoint = skSegment.GetEndPoint2()

Worked. I was looking through the API help for a SketchSegment method to do it but I never saw this. I guess it is inherited.

Thank you Josh!