Hello there!
I am writing a programm to automate some workflows on our drawing.
Now i am stuck at connecting a Text from a BOM Balloon to a Text Node.
In Solidworks it is possible to edit a text node and klick a BOM Balloon to
get the Position Number to the Text Node.
If you now change the order of the components in dem Assembly, the BOM Balloon
is updated and also the Text in the Text Node.
Is it possible to do this via API? i tried to record a macro, but the macrorecorder doesn't
record this
I hope somebody can help me.
Thank you!
SolidworksApi macros
You can use Note.PropertyLinkedText to set it to something like
But I don't know how to find the number you should use for the OBJECT ID
I think the API Call GetPersistReference3 returns the ID of the selected Object. But it returns a byte array. As i am no so good in working with byte arrays my next question is: how do i get the Integer OBJECT ID out of the Array?
No - the GetPersistReference3 returns a string which is nearly 400 characters long!
The Object ID we are looking for is something like the number of the DetailItem
No, thats not correct:
This returns a byte array. I found out that the OBJECT ID of the bomballoon is an 16BIT Integer of the bytearray starting at index 132.
So if you use this code:
Dim persistRef As Object
Dim selObj As Object
selObj = SelMgr.GetSelectedObject6(1, -1)
persistRef = swdoc.Extension.GetPersistReference3(selObj)
Dim x As Int16
x = BitConverter.ToInt16(persistRef, 132)
you will get the OBJECT ID of the boomballon. Give it a try! :-)
Ok, it is not so easy as i thought. the lenght of the byte array is not always the same.
I was going to ask how you found the bytes at number 132,
Here is a snippet I use to convert the byte array to a string:
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim selMgr As SelectionMgr
Sub main()
Dim myNote As Note
Dim pID As Variant
Dim myPID As String
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set selMgr = Part.SelectionManager
Set myNote = selMgr.GetSelectedObject6(1, -1)
pID = Part.Extension.GetPersistReference3(myNote)
myPID = GetStringFromID(pID)
Debug.Print myPID
End Sub
Function GetStringFromID(ByVal vPIDarr As Variant) As String
Dim vPID As Variant
GetStringFromID = ""
For Each vPID In vPIDarr
GetStringFromID = GetStringFromID & Format(vPID, "###000")
Next vPID
End Function
So now i have tried on 4 different drawings to get the right bytes out of the array. The bytes are always bytearray.Lenght - 8
This is my codesnippet. It returns all OBJECT IDs of the given BomBalloons.
Function GetOBJECT_IDsofSwObjects(ByVal ObjectArray() As Object, ByVal swDrawingDoc As sldworks.DrawingDoc) As Array
Dim persistRef() As Byte
Dim IDArray(ObjectArray.Length) As Int16
For i = 0 To ObjectArray.Length - 1
persistRef = swDrawingDoc.Extension.GetPersistReference3(ObjectArray(i))
IDArray(i) = BitConverter.ToInt16(persistRef, persistRef.Length - 8)
Next
Return IDArray
End Function
I am getting the same as well. Looks like a solution!
Yes, i hope this will help other people. Thanks Simon for your Help
This solution is for Solidworks 2012 SP5
Hi Georg
I am working on a marco to connect text of bom balloon to a text note. But i am just starting to do macros and do not understand
all the code you have in your snippet, is anyway you could provide me more of the macro code? or the whole macro? I am tring to add the item number and the quanity of the part in a text note
Thanks
My Snippet is a part of a vb.net project. the code is not for use in macros. if u want to use it in a macro you have to change the code to the vba code, so you have to know vb.net and vba.
My snippet is a function where you pass all bom balloons as nodes. The code is a function u can use in your macro, but you (or somebody) have to port it to vba (vb6).
Hello, tell me please how can I declare this function in the procedure?
Function GetOBJECT_IDsofSwObjects(ByVal ObjectArray() As Object, ByVal swDrawingDoc As sldworks.DrawingDoc) As Array
Dim persistRef() As Byte
Dim IDArray(ObjectArray.Length) As Int16
For i = 0 To ObjectArray.Length - 1
persistRef = swDrawingDoc.Extension.GetPersistReference3(ObjectArray(i))
IDArray(i) = BitConverter.ToInt16(persistRef, persistRef.Length - 8)
Next
Return IDArray
End Function
in this code:
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim selMgr As SelectionMgr
Sub main()
Dim myNote As Note
Dim pID As Variant
Dim myPID As String
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set selMgr = Part.SelectionManager
Set myNote = selMgr.GetSelectedObject6(1, -1)
pID = Part.Extension.GetPersistReference3(myNote)
myPID = GetStringFromID(pID)
Debug.Print myPID
End Sub
Function GetStringFromID(ByVal vPIDarr As Variant) As String
Dim vPID As Variant
GetStringFromID = ""
For Each vPID In vPIDarr
GetStringFromID = GetStringFromID & Format(vPID, "###000")
Next vPID
End Function
You can use the function with your selected objects.
In my case i got a view with many notes. i put all the notes
in an array and use my function with this array.
You musst store all selected items into an array and pass them to my function,
but you have to convert my code to vba, because i used vb.net.
GetOBJECT_IDsofSwObjects(selectedItems, yourSwDrawingDoc)
Thank you for reply, but i have a error.
Sub Main()
Dim myNote As Note
Dim pID As Object
Dim myPID As Object
swApp = CreateObject("SldWorks.Application")
Part = swApp.ActiveDoc
selMgr = Part.SelectionManager
myNote = selMgr.GetSelectedObject6(1, -1)
pID = Part.Extension.GetPersistReference3(myNote)
myPID = GetOBJECT_IDsofSwObjects(Items, DrawingDoc)
Console.WriteLine(myPID)
Console.ReadKey()
End Sub
Function GetOBJECT_IDsofSwObjects(ByVal ObjectArray() As Object, ByVal swDrawingDoc As DrawingDoc) As Array
Dim persistRef() As Byte
Dim IDArray(ObjectArray.Length) As Int16
For i = 0 To ObjectArray.Length - 1
persistRef = swDrawingDoc.Extension.GetPersistReference3(ObjectArray(i))
IDArray(i) = BitConverter.ToInt16(persistRef, persistRef.Length - 8)
Next
Return IDArray
End Function
Can you check how many items you have in the ObjectArray before code line 34 is executed?
Dim persistRef() As Byte
Dim IDArray(ObjectArray.Length) As Int16
For i = 0 To ObjectArray.Length - 1
persistRef = swDrawingDoc.Extension.GetPersistReference3(ObjectArray(i))
IDArray(i) = BitConverter.ToInt16(persistRef, persistRef.Length - 8)
Next
Return IDArray
Set a breakpoint at "Dim persistRef() as Byte" and tell me how many items you have in ObjectArray
ObjectArray: Nothing
You have to pass your selected optects when you call the Function "GetOBJECT_IDsofSwObjects"
Can you show me some codelines where you call the Function?
Dim Items As Object
Dim DrawingDoc As DrawingDoc
myPID = GetOBJECT_IDsofSwObjects(Items, DrawingDoc)
And where do you set the Items and the DrawingDoc Variables?
you can't only past the empty variables to the function.
You have to get first the selected items with the selectionmanager from solidworks and the drawing document.
Yeah, sry) you are right, but now i have new error)
You have to past the notes.
Do something like this:
Dim myNotes as new Arraylist
Dim OBJECT_IDs()
For i = 0 to selMgr.GetSelectedObjectCount2(-1) - 1
myNote = selMgr.GetSelectedObject6(i+1, -1)
myNotes.add(myNote)
Next
OBJECT_IDs = GetOBJECT_IDsofSwObjects(myNotes.toArray, swDrawingDoc)
New error)
Did you select some notes in solidworks?
DrawingDoc = CType(Part, DrawingDoc)
You have to open a drawing in solidworks. select some notes.
Use Code
DrawingDoc = swapp.ActiveDoc
Yea Georg, i select notes. Guys, maybe i do something wrong, It doesn t work. Can you look at my project ? Thanks in advance!
Try this....
<p>Sub Main()</p><p></p><p> Dim myNote As Note<br> Dim drawingDoc As DrawingDoc</p><p> Dim swDoc as ModelDoc2</p><p></p><p> swApp = CreateObject("SldWorks.Application")</p><p> swDoc = swApp.ActiveDoc<br> drawingDoc = swDoc<br> selMgr = swDoc.SelectionManager<br> myNote = selMgr.GetSelectedObject6(1, -1)</p><p></p><p> Dim myNotes As New ArrayList<br> Dim OBJECT_IDs()</p><p></p><p> For i = 0 To selMgr.GetSelectedObjectCount2(-1) - 1<br> myNote = selMgr.GetSelectedObject6(i + 1, -1)<br> myNotes.Add(myNote)<br> Next</p><p></p><p> <br> OBJECT_IDs = GetOBJECT_IDsofSwObjects(myNotes.ToArray, drawingDoc)</p><p></p><p> Console.WriteLine(OBJECT_IDs)<br> Console.ReadKey()<br> End Sub</p><p></p><p><br> Function GetOBJECT_IDsofSwObjects(ByVal ObjectArray() As Object, ByVal swDrawingDoc As DrawingDoc) As Array</p><p></p><p> Dim persistRef() As Byte<br> Dim IDArray(ObjectArray.Length) As Int16</p><p></p><p> For i = 0 To ObjectArray.Length - 1</p><p></p><p> persistRef = swDrawingDoc.Extension.GetPersistReference3(ObjectArray(i))</p><p></p><p> IDArray(i) = BitConverter.ToInt16(persistRef, persistRef.Length - 8)</p><p></p><p> Next</p><p></p><p> Return IDArray<br> End Function</p>Not working(
I don't know what you select in your drawing but for me the code works exactly as i posted.
Can you post a screenshot of your selection?
You need to cast the ModelDoc2 to DrawingDoc:
DrawingDoc = CType(swDoc, DrawingDoc)
As i know this is nice but not really necessary.
you only need to store the modeldoc into an drawingdoc variable
I know this can cause errors but for function it is not needet to CType the ModelDoc2.
@Nazar Chaykin
But give it a try ;-)
I think it depends if you have Option Strict set in your project options. With that on, I don't think you can implicitly cast from a ModelDoc2 to a DrawingDoc
Nothing(
If you have option strict on many of the codelines will not work ;-)
I agree!
(That's why you need to cast with CType)
Change Dim OBJECT_IDs()
to
Dim OBJECT_IDs() As Int16
Yes - you are right - this is the mistake.
There are no mistakes, console throws now "System.Int16[]". Should I point option strict off?
Did you Change
Dim OBJECT_IDs()
to
Dim OBJECT_IDs() As Int16
as is said?
yeah
you can't write the array to the console like you did. you need to loop to the array.
Thanks for you help. Georg, can you say about loop to the array in greater detail? Particular in my case.
Remove the line Console.WriteLine(OBJECT_IDs)
and use
For i = 0 To OBJECT_IDs.Length - 1
Console.WriteLine(OBJECT_IDs(i))
Next
Thank you for you help again Georg Draxler, exactly!