Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sw2003 API Problem

Status
Not open for further replies.

MMisztal

Mechanical
Aug 20, 2007
4
PL
Hey all,

I'm trying to write a macro that will collect information about all points in the selected sketch, will find the points' XYZ and finally will determine their IDs.

Once the data is collected, I'm expecting the macro to create a note at each sketch point, anchored at each point's XYZ. The notes' text field contains the points' ID.

So here's the macro :
(SW2003 / VBA 6)
===========================================================
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeat As SldWorks.feature
Dim swSketch As SldWorks.sketch
Dim vSketchPt As Variant
Dim vSketchSeg As Variant
Dim swSketchPt As SldWorks.SketchPoint
Dim swSketchSeg As SldWorks.SketchSegment
Dim vID As Variant
Dim i As Long
Dim bRet As Boolean
Dim Note As Object
Dim Part As Object


Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swFeat = swSelMgr.GetSelectedObject3(1)
Set swSketch = swFeat.GetSpecificFeature
Set Part = swApp.ActiveDoc

Debug.Print "Feature = " + swFeat.Name

vSketchPt = swSketch.GetSketchPoints
If Not IsEmpty(vSketchPt) Then
Debug.Print " Sketch Points:"

For i = 0 To UBound(vSketchPt)

Set swSketchPt = vSketchPt(i)
vID = swSketchPt.GetId

Debug.Print (Trim(" Pt(" + Str(i) + ") = [" + Str(vID(0)) + "," + Str(vID(1)) + "]"))

boolstatus = _
Part.Extension.SelectByID(RTrim("Point") & LTrim(Str(i)), _
"SKETCHPOINT", vSketchPt(i).x, vSketchPt(i).y, 0, _
False, 0, Nothing)
'MsgBox (RTrim("Point") & LTrim(Str(i)))

Set Note = Part.InsertNote(Trim("Pt(" + Str(i) + ")=[" + Str(vID(0)) + "," + Str(vID(1)) + "]"))

If Not Note Is Nothing Then

Note.angle = 0
boolstatus = Note.SetBalloon(0, 0)
Set Annotation = Note.GetAnnotation()

If Not Annotation Is Nothing Then
longstatus = Annotation.SetLeader2(True, 0, True, False, False, False)
boolstatus = Annotation.SetPosition(0.15, 0.15 - i * 0.005, 0)
End If
End If
Next i
End If
Debug.Print ""


vSketchSeg = swSketch.GetSketchSegments
If Not IsEmpty(vSketchSeg) Then
Debug.Print " Sketch Segments:"
For i = 0 To UBound(vSketchSeg)
Set swSketchSeg = vSketchSeg(i)

vID = swSketchSeg.GetId
Debug.Print (Trim(" Seg(" + Str(i) + ") = [" + Str(vID(0)) + "," + Str(vID(1)) + "]"))
Next i
End If
End Sub
===========================================================

The result is : All notes are anchored at the origin point, but are displaying correct point IDs. I've been trying to find a solution to this for 4 days now, can anyone tell me - what in the Sweet Chocolate Christ, have I done wrong ?
 
Replies continue below

Recommended for you

You have 99.8% correct. However, I've never had much luck getting SelectByID to work worth a flip. Try replacing that cumbersome line with:

swSketchPt.Select2 False, Empty

If you already have an object for an entity, you can usually use direct selection rather than SelectByID.

I'm pretty sure this should work for you. I'm currently on 2007, though, so post again if it doesn't do the trick for you.
 
Wow it worked !

If I may, I have another question.

Let's assume that :

-I know specific point numbers and their coords.
-The sketch has N points in it
-Points <n1...n2> are going to be used to create a spline

How should I use them, since selectByID doesn't usually work?
 
You already have an array of pointers to each point in the sketch. Until you change the sketch or rebuild the model those pointers in that array are still valid. I've picked most of this up on my own, so my terminology may be a bit off, but as I understand it the SelectByID statement tells the document to ask SolidWorks to add something to the selection set. You have to tell the document which of its entities you want it to add. When you use the Select2 statement, you are telling the entity itself (the sketch point in your case) to ask SolidWorks to add it to the selection set.

As far as inserting a spline goes, you should check the help file for ModelDoc2:CreateSpline. Basically, you won't use those points directly anyway.
 
Hi..

Can "ModelDoc2.SketchSpline" method be used to create spline in this case if we store x , y and z cords in different arrays.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top