Change the font of text in a textbox of a sketch

Change the font of text in a textbox of a sketch

ciaran.gibson
Contributor Contributor
716 Views
10 Replies
Message 1 of 11

Change the font of text in a textbox of a sketch

ciaran.gibson
Contributor
Contributor

With this code it will replace the part numbers for an assembly, which are punched on the surface. for some parts when it updates the existing part number it stays as the old font 'punch' which is what i need, but sometimes it reverts back to the default tahoma. i have been trying to get the font to change to the text style i already have set up but cant manage to get it. 

 

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

For Each oOccurrence In oDoc.ComponentDefinition.Occurrences
SearchForSketch(oOccurrence)
Next
End Sub

Private Sub SearchForSketch(ByVal oOccurrence As ComponentOccurrence)
If TypeOf oOccurrence.Definition Is PartComponentDefinition Then
Dim oPartDoc As PartDocument
oPartDoc = oOccurrence.Definition.Document
Dim fileName As String
fileName = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)

For Each oSketch In oOccurrence.Definition.Sketches
If oSketch.Name = "Part_Number" Then
For Each oTextBox In oSketch.TextBoxes

"This is where i have been trying to change the font"
oTextBox.Text = fileName
Next
End If
Next
ElseIf TypeOf oOccurrence.Definition Is AssemblyComponentDefinition Then
For Each oSubOccurrence In oOccurrence.Definition.Occurrences
SearchForSketch(oSubOccurrence)
Next
End If
iLogicVb.UpdateWhenDone = True
End Sub

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

WCrihfield
Mentor
Mentor

Hi @ciaran.gibson.  You said that you already have a 'font' named "punch" prepared, and that it is the one you want to set it to, but have you created a TextStyle to use that 'font' within yet?  If not, you should do so, before running this rule, then you can use something like the following lines of code at that point in your existing code.  You can create a new TextStyle by getting some random existing one, then using TextStyle.ConvertToLocal, which makes a 'local' copy of it in your part file, then you can edit the properties of it (including its Name), then use that one.  You could additionally use TextStyle.SaveToGlobal, to save it for later use in other documents.

 

oTextBox.Text = fileName
oTextBox.Style = oPartDoc.TextStyles.Item("punch")

 

Edit:  That TextStyle will need to be copied locally to each document you want to use it in though, so you may need to put that code for finding/creating it within your loop.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 11

ciaran.gibson
Contributor
Contributor

i do have a text style created, shown here. 

ciarangibson_0-1675263935974.png

 

 

As for fileName, it is to just use the name of the file that the sketch occurrence is on as its new text for the part number. I didn't make a parameter for it, it just worked.

 

I have added the line you provided as below but still get errors,

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

For Each oOccurrence In oDoc.ComponentDefinition.Occurrences
SearchForSketch(oOccurrence)
Next
End Sub

Private Sub SearchForSketch(ByVal oOccurrence As ComponentOccurrence)
If TypeOf oOccurrence.Definition Is PartComponentDefinition Then
Dim oPartDoc As PartDocument
oPartDoc = oOccurrence.Definition.Document
Dim fileName As String
fileName = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)

For Each oSketch In oOccurrence.Definition.Sketches
If oSketch.Name = "Part_Number" Then
For Each oTextBox In oSketch.TextBoxes

oTextBox.Text = fileName
oTextBox.Style = oPartDoc.TextStyles.Item("punch")
Next
End If
Next
ElseIf TypeOf oOccurrence.Definition Is AssemblyComponentDefinition Then
For Each oSubOccurrence In oOccurrence.Definition.Occurrences
SearchForSketch(oSubOccurrence)
Next
End If
iLogicVb.UpdateWhenDone = True
End Sub

Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

 

 

 

I am still learning ilogic, I think it is having issues setting or finding the style. it has no problem making changes to the textbox but sometimes it remembered what the previous font was and sometimes it resets it to the default. That is why I want to set it to the same style all the time. This is the only text style I will need in any sketch so i have also tried setting the style earlier in the code to no avail. 

0 Likes
Message 4 of 11

WCrihfield
Mentor
Mentor

OK.  I think that TextStyle is found in the global library, instead of having a local copy of it within each part document.  In order for your code to apply that TextStyle to that TextBox within each document, that global/library one will have to be copied to the part document each time, to ensure that a local copy exist, before it will succeed at applying it.  I will work on a copy of your code, in an attempt to add some more code for this purpose, if I have time.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 11

WCrihfield
Mentor
Mentor

Hi @ciaran.gibson.  I have not tested this yet, but I think I have something for you to try out.  The code for copying/creating that TextStyle when it is not found, may be a bit excessive, but I wanted to be thorough, just in case.  When that TextStyle is not found, it copies the first TextStyle it finds in the part, then makes sure all its properties/settings are set the way you have them in your dialog image, but you will have to check that those settings are OK after the face, and maybe make any adjustments, if needed.

Private Sub Main()
	Dim oDoc As AssemblyDocument = ThisDoc.Document
	'this reaches every level of the assembly, so no need for recursive routine
	For Each oRefDoc As Document In oDoc.AllReferencedDocuments
		'only process part documents
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		ChangeSketch(oRefDoc, "Part_Number", "punch")
	Next
End Sub

Private Sub ChangeSketch(oPDoc As PartDocument, sSketchName As String, sFontName As String)
	If IsNothing(oPDoc) OrElse oPDoc.IsModifiable = False Then Exit Sub
	Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(oPDoc.FullFileName)
	Dim oPunchTStyle As TextStyle = Nothing
	Try
		oPunchTStyle = oPDoc.TextStyles.Item(sFontName)
	Catch
		'not found, so create a new one locally with same specs
		Dim oNewTStyle As TextStyle = oPDoc.TextStyles.Item(1).Copy(sFontName)
		oNewTStyle.Bold = False
		oNewTStyle.Color = ThisApplication.TransientObjects.CreateColor(255, 255, 255)
		oNewTStyle.Comments = sFontName
		oNewTStyle.Font = sFontName
		oNewTStyle.FontSize = 11.00 mm 'this might be understood as centimeters instead
		'oNewTStyle.FontSize = 1.10 cm 'this might work better
		oNewTStyle.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextLeft
		oNewTStyle.Italic = False
		'oNewTStyle.LineSpacing = 1.00
		'oNewTStyle.LineSpacingType = TextLineSpacingTypeEnum.kMultipleLineSpacing
		oNewTStyle.Rotation = 0.0
		oNewTStyle.Underline = False
		oNewTStyle.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextUpper
		oNewTStyle.WidthScale = 1.0
		oPunchTStyle = oNewTStyle
	End Try
	If IsNothing(oPunchTStyle) Then
		MsgBox("Could not get local copy of '" & sFontName & "' TextStyle in:  " & fileName, vbCritical, "iLogic")
		Exit Sub
	End If
	For Each oSketch As PlanarSketch In oPDoc.ComponentDefinition.Sketches
		If oSketch.Name <> sSketchName Then Continue For
		For Each oTextBox As Inventor.TextBox In oSketch.TextBoxes
			Try : oTextBox.Text = fileName : Catch : End Try
			Try : oTextBox.Style = oPunchTStyle : Catch : End Try
		Next
	Next
	If oPDoc.RequiresUpdate Then oPDoc.Update2(True)
	If oPDoc.Dirty Then oPDoc.Save
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 11

ciaran.gibson
Contributor
Contributor

code runs and changes the text but the font will still revert back to tahoma 3mm.

 

Not sure what else to try, could there be any other property of the part that is hindering this? Do you know of any reason why with some assemblies it changes the text and the font stays the same but on other assemblies it will change the font back to the default instead of keeping the same font as before?

0 Likes
Message 7 of 11

WCrihfield
Mentor
Mentor

I was going to ask about what version of Inventor you were using, and if ModelStates may be involved, but if that were the case, some document would not get any change at all, due to being a 'member' (ReadOnly), but if all documents are getting changes, just not both changes, that rules that out.  You mentioned that the text is getting extruded, right&  And you know that 'punch' font extrudes OK, right?  Well, do you use the 'Convert To Geometry' tool, before you extrude that text?  If so, maybe after you change the TextBox.Text &/or TextBox.Style, maybe you have to apply that 'Convert To Geometry' tool to that TextBox again, to get it to stay there.  I know that when you use that tool, it also asks you to "Choose SHX Font" from a list of available ones, with a checkbox for "Use Big Fond", and another drop-down list under "Big Font".  Maybe we are missing that extra step?  Does that sound right to you.  I do not often do that process, so I am a bit rusty.

WCrihfield_0-1675269535414.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 11

ciaran.gibson
Contributor
Contributor

I am using inventor professional 2020, and the punch font is loaded in by being in the design data folder location. 

 

I tried one there from scratch, all i have to do is sketch>tectbox>text(select punch from dropdown, make it size 11mm)>extrude cut, it does not convert to geometry. 

0 Likes
Message 9 of 11

ciaran.gibson
Contributor
Contributor
Accepted solution

So I have worked on this longer and found a solution. Instead of changing with code which text style the text box uses to punch I just changed the properties of the text style that the text box defaults with, to match the properties of the punch style, and saved that to the part and assembly template. 

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor

That sounds reasonable.  I also created a file to test this issue out on, and did some additional testing yesterday, but it simply will not allow changing the TextBox.Style to a different TextStyle after the fact.  Not even manually, which should have been a good indicator.  Usually if you can not do it manually, you will not be able to do it by code.  I was starting to simply record the position of the TextBox, and its contents, then delete it, then create a new one at the same position, with the same contents, but set its Style to the new Style when creating the new one.  But then you also have to fix the definition of the Extrusion feature, because it looses its 'Profile'.  Certainly not an ideal solution.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 11

ciaran.gibson
Contributor
Contributor

Yeah its a funny one, when I started this I didn't foresee the font changing being the part that holds me up. Works well now but there are some older assemblies that I use that were made before I started to use more ilogic and styles and such that it does not work on. will be updating them when I find one and eventually there will be no old parts left.

0 Likes