×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Sw2003 API Problem

Sw2003 API Problem

Sw2003 API Problem

(OP)
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 ?

RE: Sw2003 API Problem

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.

RE: Sw2003 API Problem

(OP)
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?

RE: Sw2003 API Problem

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.

RE: Sw2003 API Problem

Hi..

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



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources