Connect Text of BOMBalloon to a Text Node

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
Answers 46
ST
ST 2013-11-21 Best Answer

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

SF
SF 2013-11-21

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?

ST
ST 2013-11-21

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

SF
SF 2013-11-21

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! :-)

SF
SF 2013-11-21

Ok, it is not so easy as i thought. the lenght of the byte array is not always the same.

ST
ST 2013-11-21

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

SF
SF 2013-11-21

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

ST
ST 2013-11-21

I am getting the same as well. Looks like a solution!

SF
SF 2013-11-21

Yes, i hope this will help other people. Thanks Simon for your Help

This solution is for Solidworks 2012 SP5

SF
SF 2013-11-27

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

SF
SF 2013-11-27

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).

SF
SF 2014-01-14

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

SF
SF 2014-01-14

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)

SF
SF 2014-01-14

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

SF
SF 2014-01-14

Can you check how many items you have in the ObjectArray before code line 34 is executed?

SF
SF 2014-01-14

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

SF
SF 2014-01-14

Set a breakpoint at "Dim persistRef() as Byte" and tell me how many items you have in ObjectArray

SF
SF 2014-01-14

ObjectArray: Nothing

SF
SF 2014-01-14

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?

SF
SF 2014-01-14

Dim Items As Object

Dim DrawingDoc As DrawingDoc

myPID = GetOBJECT_IDsofSwObjects(Items, DrawingDoc)

SF
SF 2014-01-14

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.

SF
SF 2014-01-14

Yeah, sry) you are right, but now i have new error)

SF
SF 2014-01-14

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)

SF
SF 2014-01-14

New error)

SF
SF 2014-01-14

Did you select some notes in solidworks?

ST
ST 2014-01-14

DrawingDoc = CType(Part, DrawingDoc)

SF
SF 2014-01-14

You have to open a drawing in solidworks. select some notes.

Use Code

DrawingDoc = swapp.ActiveDoc

SF
SF 2014-01-14

Yea Georg, i select notes. Guys, maybe i do something wrong, It doesn t work.  Can you look at my project ? Thanks in advance!

SF
SF 2014-01-14

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>
SF
SF 2014-01-14

Not working(

SF
SF 2014-01-14

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?

ST
ST 2014-01-14

You need to cast the ModelDoc2 to DrawingDoc:

DrawingDoc = CType(swDoc, DrawingDoc)

SF
SF 2014-01-14

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 ;-)

ST
ST 2014-01-14

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

SF
SF 2014-01-14

Nothing(

SF
SF 2014-01-14

If you have option strict on many of the codelines will not work ;-)

ST
ST 2014-01-14

I agree!

(That's why you need to cast with CType)

SF
SF 2014-01-14

Change Dim OBJECT_IDs()

to

Dim OBJECT_IDs() As Int16

ST
ST 2014-01-14

Yes - you are right - this is the mistake.

SF
SF 2014-01-14

There are no mistakes, console throws now "System.Int16[]". Should I point option strict off?

SF
SF 2014-01-14

Did you Change

Dim OBJECT_IDs()

to

Dim OBJECT_IDs() As Int16

as is said?

SF
SF 2014-01-14

yeah

SF
SF 2014-01-14

you can't write the array to the console like you did. you need to loop to the array.

SF
SF 2014-01-14

Thanks for you help. Georg, can you say about loop to the array in greater detail? Particular in my case.

SF
SF 2014-01-14

Remove the line Console.WriteLine(OBJECT_IDs)

and use

For i = 0 To OBJECT_IDs.Length - 1

            Console.WriteLine(OBJECT_IDs(i))

Next

SF
SF 2014-01-15

Thank you for you help again Georg Draxler, exactly!