How to change font in a sketch on all parts in an assembly

How to change font in a sketch on all parts in an assembly

randersonSCS1
Contributor Contributor
800 Views
10 Replies
Message 1 of 11

How to change font in a sketch on all parts in an assembly

randersonSCS1
Contributor
Contributor

I am trying to write a rule that cycles through all parts in an assembly and changes the font on a sketch that is used for an emboss on the part.

 

There are two emboss features generated from 1 sketch. The emboss features should always be named "LASER LABEL A1" and "LASER LABEL A2". The sketch with two text boxes is named "LASER LABEL SKETCH".

 

I would like to be set the font to Stencil and change the font size to .25 inches.

 

Previously I worked with someone that wrote code to cycle through parts and suppress an emboss feature, I'm thinking that code could be helpful here. I'm new to iLogic and inventor API, trying to learn!

 

Sub Main()
        
    'define the assembly document
	Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisDoc.Document
	
	'Get user input
	If RuleArguments(oBool) <> Nothing
		oBool = RuleArguments(oBool)
	Else
		oBool = InputRadioBox("Toggle TOP A1 Emboss", "TOP A1 OFF", "TOP A1 ON", True, Title :="TOP A1 EMBOSS")
	End If
	
	'define the assembly component definition
	Dim oAsmDef As AssemblyComponentDefinition
	oAsmDef = oAsmDoc.ComponentDefinition 
	
	'define the leaf occurences of the assembly
	Dim oLeafOccs As ComponentOccurrencesEnumerator
	oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences
	
	'define the occurences
	Dim oOcc As ComponentOccurrence
	
	'Define a part component definiton
	Dim oPCD As PartComponentDefinition
	
	'Iterate through the assembly
	For Each oOcc In oLeafOccs
		
		'Set the part component definition equal to the current occurrence
		oPCD = oOcc.Definition
		
		'Define and set an emboss features collection
		Dim oEFs As EmbossFeatures
		oEFs = oPCD.Features.EmbossFeatures
		
		'Define an emboss feature
		Dim oEmboss As EmbossFeature
		
		If oBool = True Then
		
			Try
				'Suppress Emboss1
				oEmboss = oEFs.Item("Emboss1")
                oEmboss.Suppressed = True
			Catch
			End Try
		
		'Checking to see if user input a no
		ElseIf oBool = False
		
			Try
				'Unsuppress Emboss1
				oEmboss = oEFs.Item("Emboss1")
                oEmboss.Suppressed = False
			Catch
			End Try
		
		'Checking to see if something went wrong
		Else
			'do nothing
		End If
		
	Next
	
	RuleParametersOutput()
	InventorVb.DocumentUpdate()
	
End Sub

I have found a few similar solutions working to format text in a part, but I'm struggling to locate the call the text from the part sketch. Here are a couple links

 

Solved: Change text with iLogic - Autodesk Community - Inventor

Solved: Create Text/sketch on a face in a assembly with iLogic - Autodesk Community - Inventor

Solved: Change Font for all text-elements - Autodesk Community - Inventor

0 Likes
Accepted solutions (1)
801 Views
10 Replies
Replies (10)
Message 2 of 11

dalton98
Collaborator
Collaborator

You could insert this into where the 'oPCD as ComponentDefinition' line is:

'Dim oDoc As PartDocument = ThisApplication.ActiveDocument
'Dim oPCD As ComponentDefinition = oDoc.ComponentDefinition

Dim oSketch As Sketch
oSketch = oPCD.Sketches.Item("LASER LABEL SKETCH")

Dim oTextBox As TextBox
For Each oTextBox In oSketch.TextBoxes
Dim oText As String = oTextBox.Text

Dim oFormattedText As String = "<StyleOverride FontSize='0.25'>" & oText & "</StyleOverride>"

oSketch.TextBoxes.Item(1).FormattedText = oFormattedText

Next

ThisApplication.CommandManager.ControlDefinitions.Item("AppLocalUpdateCmd").Execute

 

0 Likes
Message 3 of 11

randersonSCS1
Contributor
Contributor

That got me a little bit closer. I am able to change the font size on the parts, but the emboss feature will not update until manually selected (double click to edit the feature, then click okay) in the individual parts!

 

I tried InventorVb.DocumentUpdate(), is there a way to call the emboss to update?

 

Here is my code now:

Sub Main()
        
    'define the assembly document
	Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisDoc.Document
	
	
	'define the assembly component definition
	Dim oAsmDef As AssemblyComponentDefinition
	oAsmDef = oAsmDoc.ComponentDefinition 
	
	'define the leaf occurences of the assembly
	Dim oLeafOccs As ComponentOccurrencesEnumerator
	oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences
	
	'define the occurences
	Dim oOcc As ComponentOccurrence
	
	
	'Iterate through the assembly
	For Each oOcc In oLeafOccs
		
		'Set the part component definition equal to the current occurrence
		oPCD = oOcc.Definition
				
			Try
				
				Dim oSketch As Sketch
				oSketch = oPCD.Sketches.Item("LASER LABEL SKETCH")
				
				Dim oTextBox As TextBox
				For Each oTextBox In oSketch.TextBoxes
				Dim oText As String = oTextBox.Text
				
				Dim oFormattedText As String = "<StyleOverride FontSize='.25'>" & oText & "</StyleOverride>"
				
				oSketch.TextBoxes.Item(1).FormattedText = oFormattedText
				
				Next
				
				ThisApplication.CommandManager.ControlDefinitions.Item("AppLocalUpdateCmd").Execute
				
				
			Catch
			End Try
		
	Next
	
	InventorVb.DocumentUpdate()
	
End Sub

 

0 Likes
Message 4 of 11

dalton98
Collaborator
Collaborator

Take out the

ThisApplication.CommandManager.ControlDefinitions.Item("AppLocalUpdateCmd").Execute

 and replace it with this: (hopefull it force updates it)

Dim oEmbossFeature As EmbossFeature
For Each oEmbossFeature In oPCD.Features.EmbossFeatures
oEmbossFeature.Suppressed = True
oEmbossFeature.Suppressed = False
Next

 

0 Likes
Message 5 of 11

randersonSCS1
Contributor
Contributor
Good idea, unfortunately turning suppression state on and off doesn't rebuild the emboss feature. I tested it on a part level too, no dice!

From the individual part level UI the "Rebuild All" button works to update the emboss, BUT it doesn't work from the assembly level!
0 Likes
Message 6 of 11

dalton98
Collaborator
Collaborator

u said editing worked. Try this:

oOcc.Edit
oOcc.ExitEdit(ExitTypeEnum.kExitToTop)

 

0 Likes
Message 7 of 11

dalton98
Collaborator
Collaborator

is this the problem  your getting?

https://knowledge.autodesk.com/support/inventor/troubleshooting/caas/sfdcarticles/sfdcarticles/Inven...

 

If so rebuild all seems to usually work. (Place at end of code)

ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyRebuildAllCmd").Execute

 

0 Likes
Message 8 of 11

randersonSCS1
Contributor
Contributor
The parts were edited, but the emboss still did not update.

Is there a way to use the rebuild command? I tried a couple things "ThisDoc.Document.Rebuild()" seems to rebuild the assembly, not the individual parts like I need.

I was also trying "ThisDoc.AllReferencedDocuments.Rebuild()" but that gives me an error

I was getting ideas from this topic:
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-rebuild-part-sub-assembly-reg...
0 Likes
Message 9 of 11

randersonSCS1
Contributor
Contributor
Sorry, I missed this reply before I posted mine. I'm not getting the "update flash symbol" on the parts when I use the rebuild all command, so I don't think that is the issue.

I tried "AssemblyRebuildAllCmd", that didn't change anything.
0 Likes
Message 10 of 11

dalton98
Collaborator
Collaborator
Accepted solution

YOu would need to set it up like "ThisDoc.AllReferencedDocuments.Item(1).Reubild()"

 

If the ".rebuild" works you could just incorperate it into the code like this:

Dim oPCDD As Document
oPCDD = oPCD.Document
oPCDD.Rebuild()
'oPCDD.Rebuild2()

 

Message 11 of 11

randersonSCS1
Contributor
Contributor

That works!

 

Here is my final code

Sub Main()
        
    'define the assembly document
	Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisDoc.Document
	
	
	'define the assembly component definition
	Dim oAsmDef As AssemblyComponentDefinition
	oAsmDef = oAsmDoc.ComponentDefinition 
	
	'define the leaf occurences of the assembly
	Dim oLeafOccs As ComponentOccurrencesEnumerator
	oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences
	
	'define the occurences
	Dim oOcc As ComponentOccurrence
	
	Dim oPCDD As Document
	
	
	'Iterate through the assembly
	For Each oOcc In oLeafOccs
		
		'Set the part component definition equal to the current occurrence
		oPCD = oOcc.Definition
				
			Try
				
				Dim oSketch As Sketch
				oSketch = oPCD.Sketches.Item("LASER LABEL SKETCH")
				
				Dim oTextBox As TextBox
				For Each oTextBox In oSketch.TextBoxes
				Dim oText As String = oTextBox.Text
				
				Dim oFormattedText As String = "<StyleOverride Font='Stencil' FontSize='0.25'>" & oText & "</StyleOverride>"
				'FONT size is in cm!
				
				oSketch.TextBoxes.Item(1).FormattedText = oFormattedText
				oSketch.TextBoxes.Item(2).FormattedText = oFormattedText

				Next
				
			Catch
				
			End Try
			
	oPCDD = oPCD.Document
	oPCDD.Rebuild()
	'oPCDD.Rebuild2()	
	
Next
	
	InventorVb.DocumentUpdate()
		
End Sub