Edit text box in sketch with iLogic

Edit text box in sketch with iLogic

leandronicolas_albarracin
Participant Participant
299 Views
6 Replies
Message 1 of 7

Edit text box in sketch with iLogic

leandronicolas_albarracin
Participant
Participant

Hello all,

I am trying to create an iLogic rule that access to every part within an assembly, look for a specific sketch, where a text box is created, and replace the text in it with the standard Part Number iProperty.

I tried using AI help and it managed to do by access to the Part Number iProperty value and copying it into the text box as a text (like manually written).

 

Private Sub Main()
    Dim oDoc As AssemblyDocument
    oDoc = ThisDoc.Document

    ' Loop through all components in the assembly
    Dim oOccurrence As ComponentOccurrence
    For Each oOccurrence In oDoc.ComponentDefinition.Occurrences
        SearchForSketch(oOccurrence)
    Next

    ' Ensure updates happen after all modifications
    iLogicVb.UpdateWhenDone = True
End Sub

Private Sub SearchForSketch(ByVal oOccurrence As ComponentOccurrence)
    If TypeOf oOccurrence.Definition Is PartComponentDefinition Then
        Dim oPartDoc As PartDocument
        oPartDoc = oOccurrence.Definition.Document

        ' Ensure the document is open
        If oPartDoc Is Nothing Then Exit Sub

        ' Loop through sketches to find "Part_Number"
        Dim oSketch As PlanarSketch
        For Each oSketch In oPartDoc.ComponentDefinition.Sketches
            If oSketch.Name = "Part_Number" Then
                Dim oTextBox As TextBox
                For Each oTextBox In oSketch.TextBoxes
                    ' Access the custom property "Part Number" using PropertySets
                    Dim oProperty As [Property]
                    oProperty = oPartDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")

                    ' Link the text box to the custom "Part Number" property
                    oTextBox.Text = oProperty.Value
                Next
            End If
        Next

    ElseIf TypeOf oOccurrence.Definition Is AssemblyComponentDefinition Then
        ' If it's an assembly, iterate through subcomponents
        Dim oSubOccurrence As ComponentOccurrence
        For Each oSubOccurrence In oOccurrence.Definition.Occurrences
            SearchForSketch(oSubOccurrence)
        Next
    End If
End Sub

 

I was wondering if there is some way to, instead of insert text, insert the actual link of the iProperty, like it can be done when editing the text manually.

Screenshot 2025-02-25 115141.png

0 Likes
Accepted solutions (2)
300 Views
6 Replies
Replies (6)
Message 2 of 7

marcin_otręba
Advisor
Advisor
Accepted solution

hi, check this post with the answer:

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/giving-sheet-metal-a-mark-in-the-flat-pat...

 

and the answer:

 

oTextBox.FormattedText = "<StyleOverride FontSize='" & oFontSize & "'><Property Document='model' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>Part Number</Property></StyleOverride>"

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 3 of 7

JelteDeJong
Mentor
Mentor
Accepted solution

You could try something like this:

Dim doc As AssemblyDocument = ThisDoc.Document

' Find all refrenced part documents
Dim refDocs = doc.AllReferencedDocuments.Cast(Of Document).
    Where(Function(d) d.DocumentType = DocumentTypeEnum.kPartDocumentObject)

' Loop through all part documents
For Each refDoc As PartDocument In refDocs
    ' Find sketches with name "Part_Number" end select textboxes
    Dim textBoxes = doc.ComponentDefinition.Sketches.
    Cast(Of PlanarSketch).Where(Function(s) s.Name = "Part_Number").
    SelectMany(Function(s) s.TextBoxes.Cast(Of Inventor.TextBox))

    ' Loop through textboxes
    For Each textBox As Inventor.TextBox In textBoxes
        textBox.FormattedText = "<StyleOverride FontSize='0,2'><Property Document='model' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>PART NUMBER</Property></StyleOverride>"
    Next
Next

' Ensure updates happen after all modifications
iLogicVb.UpdateWhenDone = True

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 7

leandronicolas_albarracin
Participant
Participant

Thank you both!

It worked perfectly!

0 Likes
Message 5 of 7

TechInventor20
Advocate
Advocate

is there a way to retrieve the value of the property?

My vba only gets the formatted text not the actual value of the custom propertie.

0 Likes
Message 6 of 7

leandronicolas_albarracin
Participant
Participant

Hi, TechInventor20

with this coding you retrieve the actual value of the iProperty, so it changes every time the iProperty value changes. Is not just a plane text copied from the iProperty. Both solutions provided should work fine.

Message 7 of 7

marcin_otręba
Advisor
Advisor

if you want to retrive value of iproperty directly from this formatted text use  

dim value as string = textBox.Text 

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders