How To Remove All Text Style Overrides From All Text

How To Remove All Text Style Overrides From All Text

jniehaus
Participant Participant
2,631 Views
8 Replies
Message 1 of 9

How To Remove All Text Style Overrides From All Text

jniehaus
Participant
Participant

I've searched here and Google and haven't found anything that does exactly what I need, or that works.

I'm updating the styles on our drawings with a different font (Arial) and some text will not update. The text that is not updating is view labels, leader text, text boxes, and sketch text. I believe it's because the style is overridden in those items. My own attempts at coding a fix have not worked.

I would appreciate any help you can give.

0 Likes
Accepted solutions (1)
2,632 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @jniehaus.  That is obviously going to be a pretty complicated task to do entirely by code, because there are lots of different types of objects involved, and lots of different types of styles involved, each with its own unique set of properties and methods available to them.  Some things also simply can not be easily accessed, because they may be buried under multiple levels, such as within SketchedSymbolDefinitions, TitleBlockDefinitions, BorderDefinitions, and similar, that must be set to an 'edit mode' before you can even access 'a copy of' their sketch, because the original sketch is protected from immediate changes, other than linked properties and prompted entries.

 

Below is one of the codes I have in my collection that you can try out.  But even this long one is still not going to be able to cover every possible entity in an entire drawing document all at once, because not every entity is directly available in these main collections for direct edit.  Some are locked away in sub definitions.  You may simply need replace all existing 'definitions' with newly created ones, then purge out all the old ones.

This code example primarily tries to change the styles of all immediately accessible entities on all sheets of the drawing to the active standard default styles for those Types of objects.  Good for when updating older drawings from a old template resources to a new ones, and have put in the effort to create new/updated styles for them to use.

Sub Main
	Dim oDDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	UpdateToDefaultStyles(oDDoc)
	oDDoc.Update2(True)
End Sub

Sub UpdateToDefaultStyles(oDDoc As DrawingDocument)
	If (oDDoc Is Nothing) OrElse (Not oDDoc.IsModifiable) Then Return
	Dim oActiveStd As DrawingStandardStyle = oDDoc.StylesManager.ActiveStandardStyle
	Dim oDStyles As ObjectDefaultsStyle = oActiveStd.ActiveObjectDefaults
	For Each oSheet As Sheet In oDDoc.Sheets
		Dim oDDims As DrawingDimensions = oSheet.DrawingDimensions
		For Each oGDim As GeneralDimension In oDDims.GeneralDimensions
			If TypeOf oGDim Is LinearGeneralDimension Then
				oGDim.Style = oDStyles.LinearDimensionStyle
			ElseIf TypeOf oGDim Is AngularGeneralDimension Then
				oGDim.Style = oDStyles.AngularDimensionStyle
			ElseIf TypeOf oGDim Is DiameterGeneralDimension Then
				oGDim.Style = oDStyles.DiameterDimensionStyle
			ElseIf TypeOf oGDim Is RadiusGeneralDimension Then
				oGDim.Style = oDStyles.RadialDimensionStyle
			End If
		Next oGDim
		For Each oODim As OrdinateDimension In oDDims.OrdinateDimensions
			oODim.Style = oDStyles.OrdinateDimensionStyle
		Next
		For Each oODS As OrdinateDimensionSet In oDDims.OrdinateDimensionSets
			oODS.Style = oDStyles.OrdinateSetDimensionStyle
		Next
		For Each oCDS As ChainDimensionSet In oDDims.ChainDimensionSets
			oCDS.Style = oDStyles.ChainDimensionStyle
		Next
		For Each oBDS As BaselineDimensionSet In oDDims.BaselineDimensionSets
			oBDS.Style = oDStyles.BaselineDimensionStyle
		Next
		Dim oDNotes As DrawingNotes = oSheet.DrawingNotes
		For Each oCN As ChamferNote In oDNotes.ChamferNotes
			oCN.DimensionStyle = oDStyles.ChamferNoteStyle
		Next
		For Each oGN As GeneralNote In oDNotes.GeneralNotes
			oGN.TextStyle = oDStyles.GeneralNoteStyle
		Next
		For Each oHTN As HoleThreadNote In oDNotes.HoleThreadNotes
			oHTN.Style = oDStyles.HoleNoteStyle
		Next
		For Each oLN As LeaderNote In oDNotes.LeaderNotes
			oLN.DimensionStyle = oDStyles.LeaderTextStyle
		Next
		For Each oPN As PunchNote In oDNotes.PunchNotes
			oPN.DimensionStyle = oDStyles.PunchNoteStyle
		Next
		oSheet.Update
	Next oSheet
End Sub

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

jniehaus
Participant
Participant

This changed text boxs, but not sketch text, view labels, or leader text.

All of the items are set to the correct style, but the text is overridden with a different font.

0 Likes
Message 4 of 9

Michael.Navara
Advisor
Advisor

When somebody change the text font manually, you need to look at property FormattedText. This is available on various objects. In this property you can see formatting XML tags. You need to remove/modify StyleOverride tag to fix the font. But be careful, because some StyleOverride is not added by user but Inventor itself. For example diameter sign is letter 'n' in font AIGDT

MichaelNavara_0-1725516722035.png

 

 

FormattedText : "default font <StyleOverride Font='Arial'>ARIAL FONT</StyleOverride> text and <StyleOverride Font='AIGDT'>n</StyleOverride>"

 

 

0 Likes
Message 5 of 9

g.georgiades
Advocate
Advocate

I have attached the code I use to remove size overrides. It can be adapted to remove font overrides and skip the AIGDT font. You can use the built in .net XML library if you wrap the Formatted Text with a root node, then remove it after along with a couple clean up actions.

 

Imports Inventor

Imports System.Text.RegularExpressions
Imports System.Xml

#If False Then 'Hides VB.Net Compiler Error in Visual Studio, but still works with iLogic
AddReference "System.Xml.dll"
#End If

'''''''''''''''''''''''''''''''''''''''
''' Author: Greg G.
'''''''''''''''''''''''''''''''''''''''
'''
''' Goes through all sheets
'''		For all text objects, remove fontsize style override xml attribute. This should make it easier to change the font sizes.

Public Class All_Text___Remove_Font_Overrides
	Sub Main()
		If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
		Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Remove Font Size Overrides")

		Dim doc As DrawingDocument = ThisDoc.Document
		Try
			For Each sht As Sheet In doc.Sheets
				sht.Activate()
				ThisApplication.UserInterfaceManager.DoEvents()
				Dim lst = sht.DrawingNotes.Cast(Of Object).Concat(sht.DrawingDimensions.Cast(Of Object)).Concat(sht.DrawingViews.Cast(Of DrawingView))
				For Each ob In lst
					Dim textobj As Object = Nothing
					If TypeOf ob Is DrawingNote Then
						textobj = ob
					ElseIf TypeOf ob Is DrawingDimension Then
						textobj = ob.Text
					ElseIf TypeOf ob Is DrawingView Then
						textobj = ob.Label
					End If
					Dim ns As String = removeFontSizeOverrides(textobj.FormattedText)
					If ns <> "" Then
						textobj.FormattedText = ns
					End If
				Next
			Next
		Finally
			oTrans.End()
		End Try
	End Sub

	Function removeFontSizeOverrides(inputXml As String) As String
		Dim xmldoc = New XmlDocument()
		xmldoc.LoadXml("<xm>" & inputXml & "</xm>") 'have to add xml root for xml to work...
		Dim vs As XmlNodeList = xmldoc.GetElementsByTagName("StyleOverride") 'get all nodes with style override tag
		For Each nd As XmlElement In vs
			nd.RemoveAttribute("FontSize") 'removes all fontsize overrides. dont touch underline/bold/font type/italic
		Next
		Dim newstring = Regex.Replace(xmldoc.InnerXml, "<StyleOverride>(.*?)</StyleOverride>", "$1", RegexOptions.IgnoreCase).Replace("<xm>", "").Replace("</xm>", "").Replace("<Br />", "<Br/>") 'removes empty styleoverride tags, otherwise they will repopulate which is no good. Also remove xml root. Also fix newline tag as inventor doesn't like the extra space.

		If newstring IsNot Nothing AndAlso newstring <> "" Then
                Return Regex.Replace(newstring, "\r?\n", "") 'Remove blank lines - is not the same as removing <Br/> Tags
		End If
		Return ""
	End Function
End Class

 

0 Likes
Message 6 of 9

jniehaus
Participant
Participant

This does not work for me either.

It only changes text boxes but not sketch text, view labels, or leader text.

0 Likes
Message 7 of 9

jniehaus
Participant
Participant

Here is some code I cobbled together for that demonstrates what I'm trying to do.

This searches a selected view label for font override text and removes it. This seems to reset it to the style it's supposed to be.

Is there an easier way to do this to all text?

Dim oSSet As SelectSet = ThisDoc.Document.SelectSet
Dim oView As DrawingView = TryCast(oSSet.Item(1), DrawingView)
Dim tOld As String = "Font='Simplex_IV50'"
Dim tNew As String = ""

oView.ShowLabel = True
Dim sLabelText As String = oView.Label.FormattedText
sLabelText = Replace(sLabelText,tOld,tNew)
oView.Label.FormattedText = sLabelText
oView.Label.LineSpacing = 1

 

0 Likes
Message 8 of 9

g.georgiades
Advocate
Advocate
Accepted solution

I do not recommend simply deleting the font attribute from the styleoverride. It can leave a blank override tag behind which can cause issues.

 

My original code was not set up for changing fonts - only font sizes. It also had some issue with notes... (it only worked for dimension text...)

 

Here is an updated code that works with all notes and text boxes in drawing sketches. I have the hole note portion commented out as it has some weird behavior with those. This code will remove the font override attribute only if the font is not the Inventor Symbol font that @Michael.Navara described.

 

For text in sketches - you have to enter and exit the sketch environment before you can change them.

 

Imports Inventor

Imports System.Text.RegularExpressions
Imports System.Xml

#If False Then 'Hides VB.Net Compiler Error in Visual Studio, but still works with iLogic
AddReference "System.Xml.dll"
#End If

'''''''''''''''''''''''''''''''''''''''
''' Author: Greg G.
'''''''''''''''''''''''''''''''''''''''
'''
''' Goes through all sheets
'''		For all text objects, remove font type style override xml attribute.
'''		Has mixed effectiveness on hole thread notes.

Public Class All_Text___Remove_FontF_Overrides
	Sub Main()
		If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
		Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Remove Font Size Overrides")

		Dim doc As DrawingDocument = ThisDoc.Document
		Try
			For Each sht As Sheet In doc.Sheets
				sht.Activate()
				ThisApplication.UserInterfaceManager.DoEvents()
				Dim lst = sht.DrawingNotes.BendNotes.Cast(Of Object).
					Concat(sht.DrawingNotes.ChamferNotes.Cast(Of Object)).
					Concat(sht.DrawingNotes.GeneralNotes.Cast(Of Object)).
					Concat(sht.DrawingNotes.HoleThreadNotes.Cast(Of Object)).
					Concat(sht.DrawingNotes.LeaderNotes.Cast(Of Object)).
					Concat(sht.DrawingNotes.PunchNotes.Cast(Of Object)).
					Concat(sht.DrawingDimensions.Cast(Of Object)).
					Concat(sht.DrawingViews.Cast(Of DrawingView))

				For Each ob In lst
					Dim textobj As Object = Nothing
					If TypeOf ob Is DrawingNote Then
						textobj = ob
					ElseIf TypeOf ob Is DrawingDimension Then
						textobj = ob.Text
					ElseIf TypeOf ob Is DrawingView Then
						textobj = ob.Label
					End If
					Dim ns As String = removeFontTypeOverrides(textobj.FormattedText)
					If ns <> "" Then
						textobj.FormattedText = ns
					End If


					'If TypeOf ob Is HoleThreadNote Then 'this seems to affect the spaces and tolerance?
					'	Dim ns1 As String = removeFontSizeOverrides(ob.FormattedHoleThreadNote)
					'	If ns1 <> "" Then
					'		ob.FormattedHoleThreadNote = ns1
					'	End If
					'End If
				Next

				'handle font overrides in sketch textboxes

				'collect all sketch textboes from sheets and views
				Dim sketchTextBoxes As IEnumerable(Of TextBox) =
					sht.Sketches.Cast(Of DrawingSketch).SelectMany(Function(s) s.TextBoxes.Cast(Of TextBox)).
					Concat(sht.DrawingViews.Cast(Of DrawingView).SelectMany(Function(d) d.Sketches.Cast(Of DrawingSketch)).SelectMany(Function(s1) s1.TextBoxes.Cast(Of TextBox)))

				For Each tb As TextBox In sketchTextBoxes
					tb.Parent.Edit()
					Try
						Dim ns As String = removeFontTypeOverrides(tb.FormattedText)
						If ns <> "" Then
							tb.FormattedText = ns
						End If
					Catch
					Finally
						tb.Parent.ExitEdit()
					End Try
				Next
			Next
		Finally
			oTrans.End()
		End Try
	End Sub

	Function removeFontTypeOverrides(inputXml As String) As String
		Dim xmldoc = New XmlDocument()
		xmldoc.LoadXml("<xm>" & inputXml & "</xm>") 'have to add xml root for xml to work...
		Dim vs As XmlNodeList = xmldoc.GetElementsByTagName("StyleOverride") 'get all nodes with style override tag
		For Each nd As XmlElement In vs
			Dim ftattr = nd.GetAttribute("Font")
			If Not String.IsNullOrWhiteSpace(ftattr) AndAlso ftattr <> "AIGDT" Then
				nd.RemoveAttribute("Font") 
			End If
		Next
		Dim newstring = Regex.Replace(xmldoc.InnerXml, "<StyleOverride>(.*?)</StyleOverride>", "$1", RegexOptions.IgnoreCase).Replace("<xm>", "").Replace("</xm>", "").Replace("<Br />", "<Br/>") 'removes empty styleoverride tags, otherwise they will repopulate which is no good. Also remove xml root. Also fix newline tag as inventor doesn't like the extra space.

		If newstring IsNot Nothing AndAlso newstring <> "" Then
			Return Regex.Replace(newstring, "\r?\n", "") 'Remove blank lines - is not the same as removing <Br/> Tags
		End If

		Return ""
	End Function
End Class

 

0 Likes
Message 9 of 9

jniehaus
Participant
Participant
Wow. That is amazing. Thank you very much.
0 Likes