Changing A text box within a drawing sketch (Inventor 2021, iLogic)

Changing A text box within a drawing sketch (Inventor 2021, iLogic)

Mario.van.der.raaf
Enthusiast Enthusiast
371 Views
4 Replies
Message 1 of 5

Changing A text box within a drawing sketch (Inventor 2021, iLogic)

Mario.van.der.raaf
Enthusiast
Enthusiast

I am trying to change the contents of a text box using Ilogic. I can't get the reference to the sketch working. The sketch is located inside view1 of my drawing. There I want to loop through each textbox, search its contents in order to know what to place, and then continue to the next textbox.

 

Here is my code which is currently not working, gives out no errors but also does not change anything:

Dim oSketches As DrawingSketches = ThisDrawing.Document.ActiveSheet.Sketches
Dim oSketch As DrawingSketch
Dim oTBox As TextBox

For Each oTBox In ThisDrawing.Document.ActiveSheet.Sketches
		
	If oTBox.Contains("N5")
		oSketch.Edit
		oTBox = oSketch.TextBoxes.Item("Test N5")
	End If
Next

 

 

0 Likes
372 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @Mario.van.der.raaf.  When declaring a variable for a TextBox in Inventor, you must declare it as Inventor.TextBox, otherwise it will be understood as a System.Windows.Forms.TextBox, by default.  Here is a corrected version of your sample code:

 

Dim oSketches As DrawingSketches = ThisDrawing.Document.ActiveSheet.Sketches
Dim oSketch As DrawingSketch
Dim oTBox As Inventor.TextBox
Dim oMyTBox As Inventor.TextBox
For Each oSketch In oSketches
	Dim oTBoxes As Inventor.TextBoxes = oSketch.TextBoxes
	For Each oTBox In oTBoxes
		If oTBox.Text.Contains("N5") Then
		'If oTBox.FormattedText.Contains("N5") Then
			oMyTBox = oTBox
		End If
	Next
Next

 

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 5

Mario.van.der.raaf
Enthusiast
Enthusiast

Maybe I am using the corrected script wrong but i cannot get it to edit a text box. Also when I add a message box anywhere below "For Each oSketch In oSketches" is doesn't show. It is possible my original script is missing a part. For example which view to select the sketch from.

 

The weird thing is that I got it to work with Leadernotes, but that is not what I want since they are not as easily moved as sketch entities. This is the script that works and edits a leadernote text from "N5" to "test N5".

 

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

Dim oLeaderNote As LeaderNote

For Each oLeaderNote In oDoc.ActiveSheet.DrawingNotes.LeaderNotes
	
	If oLeaderNote.FormattedText.Contains("N5")
	oLeaderNote.FormattedText = "test test N5"
	End If 
	
Next

 

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Hi @Mario.van.der.raaf.  The info you have provided so far has me a little confused about how I can help you.  In a drawing, a DrawingView object generally does not contain any sketches of its own unless it is a draft view.  Is your view a draft view?  I know that the 3D model document being represented within a drawing view can contain sketches, and you can make those sketches visible in a drawing view, so is the sketch you want actually within the model document?  Or is the sketch just a regular drawing sketch, not actually created within a draft view, but on the sheet?  If it is not a draft view, and you are sure the sketch belongs to the view, but is only in the drawing, not the model, then you should be able to get the sketch from the DrawingView.Sketches.Item(1) property.  But if there are more than one view on the sheet, you need a way to specify which view to target.  If there is just one view, or the sketch is in the first view that was placed on the sheet, then you can simply specify that view by something like:

oView = ThisDoc.Document.ActiveSheet.DrawingViews.Item(1)

oSketch = oView.Sketches.Item(1)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

Mario.van.der.raaf
Enthusiast
Enthusiast

To hopefully answer some questions about my model. Here is a picture of the feature tree of the drawing:

Mariovanderraaf_1-1663310336166.png

View1 contains a sketch that is made within the drawing environment.

This sketch then contains the following text box:

Mariovanderraaf_2-1663310409494.png

I want to change the text to for example N5 = 90 degrees. But since this angle can very depending on customer requirements I need a way to later on change the text in the text box that contains N5 to N5 = 45 degrees.

 

Now the script i got to work does this with leader text:

Mariovanderraaf_3-1663310630609.png

Mariovanderraaf_4-1663310650198.png

After some work this morning using the API help I found a sollution to my problem.

First I used the select command to make sure the right view is selected. Then the Sketch.edit command to make sure the right sketch was selected. After that the right way of searching the text string was found using the API. Now for the implementation. Thanks so much for you time and help!

'Replaces the text within a text leader with a given text. Filters the leaders so the right text is placed in the right leader.
Dim doc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = doc.ActiveSheet
Dim oView As DrawingView = oSheet.DrawingViews.Item(1) 'Works for selecting view1 
ThisApplication.CommandManager.DoSelect(oView)'Select View
oSketch = oView.Sketches.Item(1) 'Works for selecting the sketch inside the view1
Dim oTBoxes As Inventor.TextBoxes = oSketch.TextBoxes

oSketch.Edit

For Each oTBox In oTBoxes
	MsgBox(oTBox.text)
	If oTBox.Text.Contains("N5")
		oTBox.Text = ("N5 =  80 degrees")
	End If
Next

oSketch.ExitEdit

 

 

 

0 Likes