Format Text within Extruded Part Feature of Assembly Component

Format Text within Extruded Part Feature of Assembly Component

rogerJDLE5
Contributor Contributor
645 Views
6 Replies
Message 1 of 7

Format Text within Extruded Part Feature of Assembly Component

rogerJDLE5
Contributor
Contributor

Hi,

 

I'm in need of help to traverse assembly tree from the assembly file to edit a sketch within two specific parts.

 

Below is code that loops through all assembly occurrences and filters out two labels that are modelled as extruded text. I would like to be able to edit text and text height from a form within the assembly document. The Label model tree is also shown below for reference.

 

I figure there needs to be some reference to a PartComponentDefinition object to be able to access the Sketch object that I want to edit, but am unsure how to do this step... 

 

rogerJDLE5_0-1625454722272.png

 

 

Dim doc As AssemblyDocument = ThisDoc.Document  
Dim oAsmCompDef As ComponentDefinition  
oAsmCompDef = doc.ComponentDefinition
Dim oCompOcc As Inventor.ComponentOccurrence
Dim oPartDoc As PartComponentDefinition

For Each oCompOcc In oAsmCompDef.Occurrences
	
'	Filter for LABELS
	If oCompOcc.Name = "LABEL BCL:1" Then
		
		oSketchBCL = oCompOcc.Sketches.Item("LabelSketch")
		oSketchBCL.TextBoxes.Item(1).FormattedText ="<StyleOverride Font='FRUTIGER' FontSize='" & _
		doc.Parameter("HL")/10 & "'>" & "Baker Cranes" & "</StyleOverride>"
		
	ElseIf oCompOcc.Name = "LABEL RC:1" Then
		
		oSketchRC = oCompOcc.Sketches.Item("LabelSketch")
		oSketchRC.TextBoxes.Item(1).FormattedText ="<StyleOverride Font='FRUTIGER' FontSize='" & _
		doc.Parameter("HL")/10 & "'>" & doc.Parameter("RC_RATING") & "</StyleOverride>"
		
	End If	

Next

ThisDoc.Document.Rebuild()

 

Kind regards.

 

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

FINET_Laurent
Advisor
Advisor

Hi,

 

You could try something like this :

 

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oAssCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oOcc As ComponentOccurrence
Dim oSketch As Sketch 	

oOcc = oAssCompDef.Occurrences.Item(1)

oSketch = oOcc.Definition.Sketches.Item(1)

 Hope this helps,

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 7

rogerJDLE5
Contributor
Contributor

Hi,

 

Thanks @FINET_Laurent for the code suggestion, it seems to have helped me over that hurdle well.

 

Still getting an error message though and wonder if it has anything to do with passing the text parameter RC_RATING in the assembly level as the text for the component sketch..?

 

The text formatting code seems to work fine if run as a rule from within the part file. I have tried to keep the text formatting code in the part file and run the rule from the assembly using iLogic.Vb.Runrule, but that also falls over. 

 

Dim doc As AssemblyDocument = ThisDoc.Document  
Dim oAsmCompDef As ComponentDefinition  
oAsmCompDef = doc.ComponentDefinition
Dim oCompOcc As Inventor.ComponentOccurrence
Dim oSketch As Sketch

For Each oCompOcc In oAsmCompDef.Occurrences
	
'	Filter for LABELS
	If oCompOcc.Name = "LABEL BCL:1" Then
		
		oSketch = oCompOcc.Definition.Sketches.Item(1)
		oSketch.TextBoxes.Item(1).FormattedText ="<StyleOverride Font='FRUTIGER' FontSize='" & _
		HL/10 & "'>" & "Company" & "</StyleOverride>"
		
	ElseIf oCompOcc.Name = "LABEL RC:1" Then
		
		oSketch = oCompOcc.Definition.Sketches.Item(1)
		oSketch.TextBoxes.Item(1).FormattedText ="<StyleOverride Font='FRUTIGER' FontSize='" & _
		HL/10 & "'>" & RC_RATING & "</StyleOverride>"
		
	End If	

Next

ThisDoc.Document.Rebuild()

 

rogerJDLE5_0-1625523093787.png

 

rogerJDLE5_2-1625523186375.png

 

Kind regards.

0 Likes
Message 4 of 7

FINET_Laurent
Advisor
Advisor

Hi,

 

I managed to get to the textbox the way you did like so :

 

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oAssCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oOcc As ComponentOccurrence

oOcc = oAssCompDef.Occurrences.Item(1)

Dim oSketch As Sketch = oOcc.Definition.sketches.Item(1)
Dim oTextBox As TextBox = oSketch.TextBoxes.Item(1)

oTextBox.Text = "Hi"

 Maybe the text box isn't always on the same sketch ? It must be on sketch 1 for each occurrences. Maybe there is no text box in each occurrences ?

Regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 5 of 7

WCrihfield
Mentor
Mentor

The error message basically says that it ran into trouble at the last line where it tries to rebuild the document.  In your code, that line is rebuilding the main assembly, not the part document.  If you comment out that line, run it, then check that part file, did it make the needed changes to the part correctly?  If it updated the part OK, but maybe it just didn't update within the main assembly, you may just need to replace ThisDoc.Document.Rebuild() with doc.Update(), to simply update the assembly.  Update only updates those things which are out of date, while Rebuild recomputes everything, whether it needs it or not.  Or maybe insert insert 'oCompOcc.Definition.Document.Update' after changing the FormattedText within the loop, to update the part individually.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 7

rogerJDLE5
Contributor
Contributor

Hi,

 

Thanks for looking into this and for your suggestions, its taken a while for me to get back onto this task.

 

If I run the rules below locally in each of the two part files it seems to work pretty well. 

 

For the BCL Label:

oSketch = ThisApplication.ActiveDocument.ComponentDefinition.Sketches.Item("LabelSketch")
oSketch.TextBoxes.Item(1).FormattedText ="<StyleOverride Font='FRUTIGER' FontSize='" & _
		Parameter("HL")/10 & "'>" & "Company Name" & "</StyleOverride>"

For the RC Label:

oSketch = ThisApplication.ActiveDocument.ComponentDefinition.Sketches.Item("LabelSketch")

RC_Rating = Parameter("PARAMETERS.ipt", "RC_RATING_TEXT")

oSketch.TextBoxes.Item(1).FormattedText ="<StyleOverride Font='Arial Black' FontSize='" & _
		Parameter("HL") / 10 & "'>" & RC_Rating & " </StyleOverride>"

 

However, if I run the rules using iLogicVb.RunRule it kicks out the error below for both part files. 

Code to run the rules from assembly doc:

Parameter("PARAMETERS.ipt", "HL_1") = HL
Parameter("PARAMETERS.ipt", "RC_RATING_TEXT") = RC_RATING
iLogicVb.RunRule("LABEL_RC.ipt",“UpdateSketch”)
iLogicVb.RunRule("LABEL_BCL.ipt",“UpdateSketch”)

 

rogerJDLE5_1-1626942500889.png

 

Any thoughts?

 

Best regards.

 

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Due to how the first line of code in those two rules within the part files are set-up, they are trying to retrieve the sketch object from the 'active' document.  When you run those rules from the main assembly, the main assembly is the 'active' document.  That's probably the main problem.  There may be multiple things you could do to fix this though.  One suggestion would be to change that first line of code in those two (local to the part file) rules

from this:

 

oSketch = ThisApplication.ActiveDocument.ComponentDefinition.Sketches.Item("LabelSketch")

 

to this:

 

oSketch = ThisDoc.Document.ComponentDefinition.Sketches.Item("LabelSketch")

 

or this:

 

Dim oPDoc As PartDocument = ThisDoc.Document
Dim oSketch As PlanarSketch = oPDoc.ComponentDefinition.Sketches.Item("LabelSketch")

 

Because the terms 'ThisDoc, ThisAssembly, ThisDrawing' will always point to the 'local' document first, if there is one, and if there isn't a 'local' document, it defaults to whichever document was 'active' when the rule first started.

 

Other than those changes, I might also suggest using the other method for running the rules, but that's up to you.

Instead of using iLogicVb.RunRule you could be using iLogicVb.Automation.RunRule(), which allows you to specify a document object directly.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)