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 ?
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
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
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
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
Can "ModelDoc2.SketchSpline" method be used to create spline in this case if we store x , y and z cords in different arrays.