find and replace text for active sheet

find and replace text for active sheet

chris_blessing
Advocate Advocate
2,937 Views
20 Replies
Message 1 of 21

find and replace text for active sheet

chris_blessing
Advocate
Advocate

Can someone help me modify this code to only search active sheet please

Sub main
        oDoc = ThisDoc.Document
        Dim oSheets As Sheets
        Dim oSheet As Sheet
        Dim oGeneralNotes As GeneralNotes
        Dim oGeneralNote As GeneralNote
        Dim oSymbol As SketchedSymbol
        Dim oSymbols As SketchedSymbols
        Dim oTitleBlock As TitleBlock
        Dim oTextBox As TextBox

        Dim ooTXT2Find As String
        Dim oNewTXT As String

        'get user input
        oTXT2Find = InputBox("Enter Text To Find:", "iLogic", "XXX")
        'look for blank value
        If oTXT2Find = "" Then
               Return 'exit rule
        End If

        oNewTXT = InputBox("Enter Text To Replace   '" & oTXT2Find _
        & "'  with.", "iLogic", "ZZZ")
        'look for blank value
        If oNewTXT = "" Then
               Return 'exit rule
        End If

        oSheets = oDoc.Sheets
        For Each oSheet In oSheets

               'handle errors
               On Error Resume Next

               'look at General Notes
               oGeneralNotes = oSheet.DrawingNotes.GeneralNotes
               For Each oGeneralNote In oGeneralNotes
                       oText = oGeneralNote.FormattedText
                       oText = ReplaceText(oText, oTXT2Find, oNewTXT)
                       oGeneralNote.FormattedText = oText
               Next

               'look at leader notes 
               oLeaderNotes = oSheet.DrawingNotes.LeaderNotes
               For Each oLeaderNote In oLeaderNotes
                       oText = oLeaderNote.FormattedText
                       oText = ReplaceText(oText, oTXT2Find, oNewTXT)
                       oLeaderNote.FormattedText = oText
               Next

               'look at title blocks
               oTitleBlock = oSheet.TitleBlock
               For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
                       oText = oTitleBlock.GetResultText(oTextBox)
                       oText = ReplaceText(oText, oTXT2Find, oNewTXT)
                       oTitleBlock.SetPromptResultText(oTextBox, oText)
               Next

               'look at sketched symbols    
               oSymbols = oSheet.SketchedSymbols
               For Each oSymbol In oSymbols
                       For Each oTextBox In oSymbol.Definition.Sketch.TextBoxes
                               oText = oSymbol.GetResultText(oTextBox)
                               oText = ReplaceText(oText, oTXT2Find, oNewTXT)
                               oSymbol.SetPromptResultText(oTextBox, oText)
                       Next
               Next
        Next

End Sub


Function ReplaceText(oText As String, oTXT2Find As String, oNewTXT As String)

        If oText = oTXT2Find Or oText.Contains(oTXT2Find) Then
               oText = Replace(oText, oTXT2Find, oNewTXT)
        End If
        Return oText

End Function
0 Likes
Accepted solutions (1)
2,938 Views
20 Replies
Replies (20)
Message 2 of 21

WCrihfield
Mentor
Mentor
Accepted solution

Here you go:

Sub Main
	oDrawingDoc = ThisDrawing.Document
	Dim oSheet As Sheet = oDrawingDoc.ActiveSheet
	Dim oGeneralNotes As GeneralNotes
	Dim oGeneralNote As GeneralNote
	Dim oSymbol As SketchedSymbol
	Dim oSymbols As SketchedSymbols
	Dim oTitleBlock As TitleBlock
	Dim oTextBox As TextBox
	
	Dim oTXT2Find As String
	Dim oNewTXT As String

	'get user input
	oTXT2Find = InputBox("Enter Text To Find:", "iLogic", "XXX")
	'look for blank value
	If oTXT2Find = "" Then
		Return 'exit rule
	End If
	
	oNewTXT = InputBox("Enter Text To Replace   '" & oTXT2Find _
	& "'  with.", "iLogic", "ZZZ")
	'look for blank value
	If oNewTXT = "" Then
		Return 'exit rule
	End If

	'handle errors
	On Error Resume Next
	
	'look at General Notes
	oGeneralNotes = oSheet.DrawingNotes.GeneralNotes
	For Each oGeneralNote In oGeneralNotes
		oText = oGeneralNote.FormattedText
		oText = ReplaceText(oText, oTXT2Find, oNewTXT)
		oGeneralNote.FormattedText = oText
	Next

	'look at leader notes 
	oLeaderNotes = oSheet.DrawingNotes.LeaderNotes
	For Each oLeaderNote In oLeaderNotes
		oText = oLeaderNote.FormattedText
		oText = ReplaceText(oText, oTXT2Find, oNewTXT)
		oLeaderNote.FormattedText = oText
	Next

	'look at title blocks
	oTitleBlock = oSheet.TitleBlock
	For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
		oText = oTitleBlock.GetResultText(oTextBox)
		oText = ReplaceText(oText, oTXT2Find, oNewTXT)
		oTitleBlock.SetPromptResultText(oTextBox, oText)
	Next

	'look at sketched symbols    
	oSymbols = oSheet.SketchedSymbols
	For Each oSymbol In oSymbols
		For Each oTextBox In oSymbol.Definition.Sketch.TextBoxes
			oText = oSymbol.GetResultText(oTextBox)
			oText = ReplaceText(oText, oTXT2Find, oNewTXT)
			oSymbol.SetPromptResultText(oTextBox, oText)
		Next
	Next
End Sub


Function ReplaceText(oText As String, oTXT2Find As String, oNewTXT As String)
        If oText = oTXT2Find Or oText.Contains(oTXT2Find) Then
               oText = Replace(oText, oTXT2Find, oNewTXT)
        End If
        Return oText
End Function

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 :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 21

brianM328K
Enthusiast
Enthusiast

Again my friend @WCrihfield you have preformed a miracle.

What a game changer, something so simple but not as standard in inventor.

A quick Question though, is there a way to highlight and conform what is being edited?

 

Regards Brian 

0 Likes
Message 4 of 21

WCrihfield
Mentor
Mentor

Hi @brianM328K.  I am not sure what you mean.  Are you saying that you would like to pause the process and highlight each object just before it edits them, and ask the user if they want this item to be edited?  Are you wanting that after all editing has been done, that it will highlight all the stuff that was edited?  I am not sure that would be possible.  There is a Document.CreateHighlightSet method that will create a HighlightSet object, but using them can be finicky.  Some types of objects can be added to one of these, and some can not be added to them.  If some stuff is inside of a sketch, and some stuff is not, then they may not be able to coexist as visibly highlighted at the same time.  The sketch may need to be in edit mode before some stuff within it can be actively highlighted, and may need to remain in edit mode when reviewed by the user.  Other stuff that is outside of the sketch may not be available when in edit mode of a specific sketch also.  Plus, the highlight effect can sometimes only last momentarily after the code has finished, because sometimes it can easily be interrupted, but other times it stays active pretty well.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 21

brianM328K
Enthusiast
Enthusiast

Hi Wesley

 

In my office it would be beneficial to have the ability to find and replace text in mainly general notes and leader notes.

For example we might have 10 general notes and 20 leaders all with say "steel" in them but 5 notes and 3 leaders should now say "aluminium". If we could jump to (preferred option) or see each text selection and pick yes or no to confirm the change. 

 

does that make sense.

0 Likes
Message 6 of 21

WCrihfield
Mentor
Mentor

Yes, that makes sense.  And I do believe that those objects would highlight just fine.  So, I am thinking that you want to first run the rule, then have it prompt you for what text to find, and what test to replace that text with, then do the search, and highlight all of the general notes and leader notes which have the 'search for' text in them.  Then show a simple question about if you want to proceed, so you can see all the entities it would be effecting.  If you say No, exit the rule, but if you say Yes, loop back through them all, and replace that test as planned.  Does that sound about right?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 21

brianM328K
Enthusiast
Enthusiast

Hi Wesley

 

Yes something like that thanks. As long as you could pick or deselect the ones you don't want to edit.

 

Regards Brian

PS its now 17:00 in the UK so I will have to knock off until 8:00 tomorrow .

0 Likes
Message 8 of 21

WCrihfield
Mentor
Mentor

I am also leaving for the day, but did not get a chance to finish the code I started creating for you.  But I will post what I have so far anyways.  The requested functionality where you need to be able to manually select/unselect highlighted notes kind of put a halt to my progress, to work on other things, because I know it would take more time to design than I had at the moment.  Busy day at work.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Return
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	'<<< set initial values to 'shared' variables >>>
	oGNotes = oSheet.DrawingNotes.GeneralNotes
	oLNotes = oSheet.DrawingNotes.LeaderNotes
	oTO = ThisApplication.TransientObjects
	oHLS = oDDoc.CreateHighlightSet
	oHLS.Color = oTO.CreateColor(255, 0, 0, 1) 'Red - Opaque
	oFoundGNotesColl = oTO.CreateObjectCollection
	oFoundLNotesColl = oTO.CreateObjectCollection
	sFind = ""
	sNew = ""
	'<<< done setting initial values >>>
	'<<< following is a designated marker for a place to jump to later (using GoTo) >>>
	StartOfRepeat :
	'<<< all following code may repeat, so clear the 3 collections >>>
	oHLS.Clear
	oFoundGNotesColl.Clear
	oFoundLNotesColl.Clear
	'<<< get required inputs from user  - exit if nothing entered >>>
	sFind = InputBox("Enter Text To Find.", "Text To Find", "")
	If sFind = "" Then Return
	sNew = InputBox("Enter Replacement Text.", "Replacement Text", "")
	If sNew = "" Then Return
	'<<< conduct searches >>>
	SearchOnly() 'runs the Sub routine defined below Sub Main...End Sub block
	'<<< inspect findings, react appropriately >>>
	If oHLS.Count > 0 Then
		Dim oAns As DialogResult = MessageBox.Show("The highlighted notes contained the text to search for." _
		& vbCrLf & "Do you want to proceed with the replacements? [YES]" _
		& vbCrLf & "Or do you want to remove some of the highlighted objects from the edit group? [NO]" _
		& vbCrLf & "Or do you want to cancel these edits? [CANCEL]", "Matching Notes Found", _
		MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
		If oAns = DialogResult.Cancel Then
			GoTo MaybeFinished 'jump to specified location within code
		ElseIf oAns = DialogResult.Yes Then
			'loop back through them and replace the text (simple)
			DoReplacements() 'runs the Sub routine defined below Sub Main...End Sub block
		ElseIf oAns = DialogResult.No Then
			'allow user to manually select ones they want to remove from editing
			'then replace the text in the remaining ones (complex)
			'<<<< !!! NEED TO FINISH CODE HERE !!! >>>>
		End If
	End If
	'<<< following is another marker for a location to jump to (using GoTo) >>>
	MaybeFinished :
	Dim oAgain As DialogResult = MessageBox.Show("Change Search Terms And Go Again? [YES]" _
	& vbCrLf & "Or Exit This Rule? [NO]", "Repeat Rule Process?", MessageBoxButtons.YesNo, _
	MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
	If oAgain = DialogResult.Yes Then GoTo StartOfRepeat 'jump to specified location within code
End Sub

'declare variables here that all routines will need access to
Dim oGNotes As Inventor.GeneralNotes
Dim oLNotes As Inventor.LeaderNotes
Dim oTO As TransientObjects
Dim oHLS As Inventor.HighlightSet
Dim oFoundGNotesColl As ObjectCollection
Dim oFoundLNotesColl As ObjectCollection
Dim sFind As String
Dim sNew As String

Sub SearchOnly()
	If oGNotes.Count > 0 Then
		For Each oGNote As Inventor.GeneralNote In oGNotes
			If oGNote.FormattedText.Contains(sFind) Then
				oFoundGNotesColl.Add(oGNote)
				oHLS.AddItem(oGNote)
			End If
		Next 'oGNote
	End If
	If oLNotes.Count > 0 Then
		For Each oLNote As Inventor.LeaderNote In oGNotes
			If oLNote.FormattedText.Contains(sFind) Then
				oFoundLNotesColl.Add(oLNote)
				oHLS.AddItem(oLNote)
			End If
		Next 'oLNote
	End If
End Sub

Sub DoReplacements()
	If oFoundGNotesColl.Count > 0 Then
		For Each oGNote As Inventor.GeneralNote In oFoundGNotesColl
			Try : oGNote.FormattedText.Replace(sFind, sNew) : Catch : End Try
			Try : oHLS.Remove(oGNote) : Catch : End Try
		Next 'oGNote
	End If
	If oLNotes.Count > 0 Then
		For Each oLNote As Inventor.LeaderNote In oFoundLNotesColl
			Try : oLNote.FormattedText.Replace(sFind, sNew) : Catch : End Try
			Try : oHLS.Remove(oLNote) : Catch : End Try
		Next 'oLNote
	End If
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 21

brianM328K
Enthusiast
Enthusiast
Looks good so far.
Line 75 though 'For Each oLNote As Inventor.LeaderNote In oGNotes'
you have put 'oGNotes' should this be 'oLNote' ?
As I have said before I can read the code just don't know how to put it together.
Brian
0 Likes
Message 10 of 21

WCrihfield
Mentor
Mentor

Hi @brianM328K.  Give this variation of the code a try, and let me know how it works for you.  I have not tested it yet myself, so if it still does not work as expected, let me know the details of how, and I may be able to adjust it again for you.  I attempted to add the extra functionality you wanted...being able to remove some of the drawing notes from the edit group, before it does the replacement.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Return
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	'<<< set initial values to 'shared' variables >>>
	oDNotes = oSheet.DrawingNotes
	oTO = ThisApplication.TransientObjects
	oHLS = oDDoc.CreateHighlightSet
	oHLS.Color = oTO.CreateColor(255, 0, 0, 1) 'Red - Opaque
	oFoundDNotesColl = oTO.CreateObjectCollection
	oDNotesToRemove = oTO.CreateObjectCollection
	sFind = ""
	sNew = ""
	'<<< done setting initial values >>>
	'<<< following is a designated marker for a place to jump to later (using GoTo) >>>
	StartOfRepeat :
	'<<< all following code may repeat, so clear the 3 collections >>>
	oHLS.Clear
	oFoundDNotesColl.Clear
	'<<< get required inputs from user  - exit if nothing entered >>>
	sFind = InputBox("Enter Text To Find.", "Text To Find", "")
	If sFind = "" Then Return
	sNew = InputBox("Enter Replacement Text.", "Replacement Text", "")
	If sNew = "" Then Return
	'<<< conduct searches >>>
	'runs the Sub routine defined below Sub Main...End Sub block
	FindReplace(False) 'False means SearchOnly, no Replace
	'<<< inspect findings, react appropriately >>>
	If oHLS.Count > 0 Then
		Dim oAns As DialogResult = MessageBox.Show("The highlighted notes contained the text to search for." _
		& vbCrLf & "Do you want to proceed with the replacements? [YES]" _
		& vbCrLf & "Or do you want to remove some of the highlighted objects from the edit group? [NO]" _
		& vbCrLf & "Or do you want to cancel these edits? [CANCEL]", "Matching Notes Found", _
		MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
		If oAns = DialogResult.Cancel Then
			GoTo MaybeFinished 'jump to specified location within code
		ElseIf oAns = DialogResult.Yes Then
			'loop back through them and replace the text (simple)
			'runs the Sub routine defined below Sub Main...End Sub block
			FindReplace(True) 'True means do Replace process
		ElseIf oAns = DialogResult.No Then
			'allow user to manually select ones they want to remove from editing
			'then replace the text in the remaining ones (complex)
			GetSelected 'runs Sub for user selection
			RemoveSelected 'runs Sub for removing those from edit group
			FindReplace(True) 'runs Replace process on edit group
		End If
	End If
	'<<< following is another marker for a location to jump to (using GoTo) >>>
	MaybeFinished :
	Dim oAgain As DialogResult = MessageBox.Show("Change Search Terms And Go Again? [YES]" _
	& vbCrLf & "Or Exit This Rule? [NO]", "Repeat Rule Process?", MessageBoxButtons.YesNo, _
	MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
	If oAgain = DialogResult.Yes Then GoTo StartOfRepeat 'jump to specified location within code
End Sub

'declare variables here that all routines will need access to
Dim oDNotes As Inventor.DrawingNotes
Dim oTO As TransientObjects
Dim oHLS As Inventor.HighlightSet
Dim oFoundDNotesColl As ObjectCollection
Dim oDNotesToRemove As ObjectCollection
Dim sFind As String
Dim sNew As String

Sub FindReplace(ByVal bReplace As Boolean)
	If bReplace = False Then
		If oDNotes.Count > 0 Then
			oFoundDNotesColl.Clear
			For Each oDNote As Inventor.DrawingNote In oDNotes
				If oDNote.FormattedText.Contains(sFind) Then
					oFoundDNotesColl.Add(oDNote)
				End If
			Next 'oGNote
			oHLS.Clear
			Try : oHLS.AddMultipleItems(oFoundDNotesColl) : Catch : End Try
		End If
	Else 'bReplace = True
		If oFoundDNotesColl.Count > 0 Then
			For Each oDNote As Inventor.DrawingNote In oFoundDNotesColl
				Try : oDNote.FormattedText.Replace(sFind, sNew) : Catch : End Try
				Try : oFoundDNotesColl.RemoveByObject(oDNote) : Catch : End Try
				Try : oHLS.Remove(oDNote) : Catch : End Try
			Next 'oGNote
		End If
	End If
End Sub

Sub GetSelected()
	oDNotesToRemove.Clear
	Dim oSelDNote As DrawingNote = Nothing
	Do
		oSelDNote = Nothing
		oSelDNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select Note To Remove From Edit Group")
		If oSelDNote Is Nothing Then Exit Do
		oDNotesToRemove.Add(oSelDNote)
	Loop Until oSelDNote Is Nothing
End Sub

Sub RemoveSelected()
	If oDNotesToRemove.Count > 0 Then
		For Each oDNoteToRemove As Inventor.DrawingNote In oDNotesToRemove
			For Each oDNote As Inventor.DrawingNote In oFoundDNotesColl
				If oDNote Is oDNoteToRemove Then
					Try : oFoundDNotesColl.RemoveByObject(oDNote) : Catch : End Try
				End If
			Next 'oDNote
		Next 'oDNoteToRemove
		oHLS.Clear
		Try : oHLS.AddMultipleItems(oFoundDNotesColl) : Catch : End Try
	End If
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 11 of 21

WCrihfield
Mentor
Mentor

I made a couple more minor changes to simplify two routines into one.  I attached the resulting code as a text file this time, because of how long these rules are.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 21

WCrihfield
Mentor
Mentor

Hi @brianM328K.  I should have tested that before posting it, because I see that one line of code in there still needed to be tweaked to make it work.

The following line of code:

Try : oDNote.FormattedText.Replace(sFind, sNew) : Catch : End Try

...should have been like the line below, because you must set a new value to the FormattedText.  You can not just directly change its contents, without setting a new value to it.  What the line of code above was doing is just retrieving a new String value from the altered contents.

Try : oDNote.FormattedText = oDNote.FormattedText.Replace(sFind, sNew) : Catch : End Try

Also, just in case you were not familiar with the way the Pick function works, if you do not want to make a selection while being prompted to, you can just tap the escape key on your keyboard, to exit that command.  That action will result in Nothing being returned to the code, instead of a selection, then the code will exit the selection loop, and move on with the rest of the code.  I guess we could add some additional text into the selection prompt instructions, telling that detail to the user.  To include that extra instruction, we would need to alter the following line of code a bit.

ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select Note To Remove From Edit Group")

The quoted text as the second input into this Pick function is where we specify what text that will be showing in the prompt for the user during selection.  Since that line of code is already pretty long, we could change it a bit, and create a String type variable on a line before that line.  Set its value with the longer selection prompt/instructions, then use that String type variable as the second input in that line.  Sort of like the following.

Dim sPrompt As String = "Select Note To Remove From Edit Group." _
& vbCrLf & "To exit selection mode, tap the Escape keyboard key."
oSelDNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, sPrompt)

After changing those two details, I have re-attached the updated code as a text file again.  And this time I did conduct some testing, which seemed to be working as planned.  Give it a try and let me know how it is working for you.

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 21

brianM328K
Enthusiast
Enthusiast

Thank you @WCrihfield 

This looks like its working great. You are a true god.

I know this has taken up some of your time and I appreciate that thank you.

It bothers me that simple functions like this are not built in as standard.

Thumbs up.png

0 Likes
Message 14 of 21

vianney_cheneau
Participant
Participant

Tanks all, the rule work for me

Exactlly what I expect, and more

 

This function have to be in IV vanilla

0 Likes
Message 15 of 21

Vickimcl
Enthusiast
Enthusiast

Hi @WCrihfield 

 

Can this be modified to search and replace text in a view label?

Nothing fancy, does not need to highlight.

Thx.

0 Likes
Message 16 of 21

WCrihfield
Mentor
Mentor

Hi @Vickimcl.  I will post something in a code window below for you to try out.  You can modify this as needed.  Right now it will first prompt you to pick the drawing view label.  Then ask you to enter what test to find.  Then it will ask you to enter what to replace that text with.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oCmdMgr As Inventor.CommandManager = oInvApp.CommandManager
	Dim sPrompt As String = "Select A Drawing View Label - Or Press Esc Key To Exit"
	Dim oViewLabel As DrawingViewLabel = oCmdMgr.Pick(SelectionFilterEnum.kDrawingViewLabelFilter, sPrompt)
	If oViewLabel Is Nothing Then Return
	Dim sFind As String = InputBox("Enter Text To Find.", "To Find", "")
	If sFind = "" Then Return
	Dim sNew As String = InputBox("Enter Text To Replace What Was Found.", "New - Replacement", "")
	Try
		oViewLabel.FormattedText = oViewLabel.FormattedText.Replace(sFind, sNew)
	Catch oEx As Exception
		MsgBox(oEx.ToString)
		'Logger.Error(oEx.ToString)
	End Try
End Sub

And below is another very similar version of this code, but instead of asking you to enter test to find, and text to replace that with, it simply presents you with the current value of its FormattedText, and lets you edit that existing value.  Then the edited value gets applied to the drawing view label.  A bit simpler way to do something like this, but usually only works good on a single selection basis, rather than iterating through many of them.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oCmdMgr As Inventor.CommandManager = oInvApp.CommandManager
	Dim sPrompt As String = "Select A Drawing View Label - Or Press Esc Key To Exit"
	Dim oViewLabel As DrawingViewLabel = oCmdMgr.Pick(SelectionFilterEnum.kDrawingViewLabelFilter, sPrompt)
	If oViewLabel Is Nothing Then Return
	Dim sEdited As String = InputBox("Edit current FormattedText as needed.", "Edit Value", oViewLabel.FormattedText)
	Try
		oViewLabel.FormattedText = sEdited
	Catch oEx As Exception
		MsgBox(oEx.ToString)
		'Logger.Error(oEx.ToString)
	End Try
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 17 of 21

Vickimcl
Enthusiast
Enthusiast

Thank you! @WCrihfield 

I will see what I can do to modify. I have a lot of view labels with text to replace so having to pick the view label will not save much time.

0 Likes
Message 18 of 21

WCrihfield
Mentor
Mentor

Sorry, been super busy at work lately, so less time to help others online with custom, complex Inventor automation solutions.  The text file in my earlier post contained pretty complex code, so I did not want to recreate all of that for a different type of object, that is not obtained the same way, so no simple/easy change to make it happen.  Try code below instead.  It asks for the 'Find' & 'New' up front, then will iterate all views on active sheet.  If label not visible, then it will skip it.  Have 2 Try/Catch statements, both with two options for feedback, if any (Msg or iLogic Log entry), with Msg active right now, but you can change that, if annoying. 

 

On my way out for the day right now, so I may check back tomorrow, if not swamped then also.  

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	Dim oViews As DrawingViews = oSheet.DrawingViews
	If oViews.Count = 0 Then Return
	Dim sFind As String = InputBox("Enter Text To Find.", "To Find", "")
	If sFind = "" Then Return
	Dim sNew As String = InputBox("Enter Text To Replace Is Found.", "New - Replacement", "")
	For Each oView As DrawingView In oViews
		If Not oView.ShowLabel Then Continue For 'skip it if not visible
		Dim oLabel As DrawingViewLabel = oView.Label
		Dim sFText As String = oLabel.FormattedText
		If Not sFText.Contains(sFind) Then Continue For 'skip if does not contain text to replace
		Dim bChanged As Boolean = False
		Try
			sFText = sFText.Replace(sFind, sNew)
			bChanged = True
		Catch oEx As Exception
			MsgBox(oEx.ToString)
			'Logger.Error(oEx.ToString)
		End Try
		If Not bChanged Then Continue For
		Try
			oLabel.FormattedText = sFText
		Catch oEx As Exception
			MsgBox(oEx.ToString)
			'Logger.Error(oEx.ToString)
		End Try
	Next oView
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)

Message 19 of 21

Vickimcl
Enthusiast
Enthusiast

@WCrihfield you are the best! Works perfectly.

I was still working on the code, not even close (..still learning).

This will save me tons of time, much appreciated.

0 Likes
Message 20 of 21

jlarose
Explorer
Explorer

You, my friend, are a genius!!

This has come in very handy and has already saved me hours of work.

Thank you very much!

0 Likes