Get Coordinates of Sketch Point by Name

Hello All,

I am new to the solidworks API.  I do most of my programming in Excel VBA so I'm trying to figure out the solidworks object model.  I need to get the x, y, z coordinates of a sketch point based on the name of the sketch and the point.  I have recorded some macros but I just get statements like the one below.

boolstatus = Part.Extension.SelectByID2("Point2", "SKETCHPOINT", 0.02257855154992, 0.05167601790474, 0, False, 0, Nothing, 0)

This only tells me whether the selection of the point was successful.  It won't tell me the point coordinates.

Below is some pseudo code that will show what I'm trying to do.  I want to find the coorinates of Point1 in Sketch1.

Sub GetPointCoords()

Dim swApp As Object, Part As Object, skPoint As Object

Dim dX As Double, dY As Double, dZ As Double

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set skPoint = Part.Sketch("Sketch1").SketchPoint("Point1")

dX = skPoint.X

dY = skPoint.Y

dZ = skPoint.Z

End Sub

Any help would be greatly appreciated. 

Thanks

Kyle

SolidworksApi macros
Answers 4
WM
WM 2011-10-11 Best Answer

I wish it were as easy as your pseudo code!

You can use the selection manager object to get selected objects.

Dim swSelMgr As SelectionMgr

Set swSelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectByID2("Point1@Sketch1", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)

Dim skPoint As SketchPoint

Set skPoint = swSelMgr.GetSelectedObject6(1, -1)

KG
KG 2011-10-11

Wayne,

Awesome, thank you so much

So, there seems to be a fundamental difference in how you use the Excel object model vs the Solidworks object model.  In Excel the idea is to never select anything.  When cleaning up a recorded macro you go through and, instead of selecting an object and then applying objects to the selection, you apply the objects directly (skipping the middle man).

Apparently this is not the way it's done in solidworks?  You need to programatically select the object and then assign an object to the selection before you can get the properties of the object?  Is this correct or am I generalizing too much.

Thanks again

Kyle

WM
WM 2011-10-11

Yes, there are some differenences. The API help file used to include an object model diagram. I wish it were still there for begining SolidWorks programmers.

You do not always need to select an object in the SolidWorks API, It is just sometimes the best way to do it. For example, the sketchPoint object. You could loop through them once you get the sketch object. The API help file does tell you how you can access objects, Just look under Accessors under the Interface of the object.

http://help.solidworks.com/2011/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ISketchPoint.html?id=4d3d862de9d64984a0522fc9f941e97d#Pg0

KG
KG 2011-10-11

Excellent, thank you very much for your help.

Kyle