Editing a text box within a specific sketch within a drawing sheet

Editing a text box within a specific sketch within a drawing sheet

Chris.Truelove
Contributor Contributor
2,275 Views
9 Replies
Message 1 of 10

Editing a text box within a specific sketch within a drawing sheet

Chris.Truelove
Contributor
Contributor

Hi

I hope someone can help with this. I am trying to edit a text box that is located on a sketch called 'TitleSketch'.

I have a working ilogic project and this is the last part of it that needs to be resolved.

What I need to do is edit a text field that is on a sketch called 'TitleSketch' if the sketch does not exist I need to create it.

The creating of the sketch and text is the easy part using the snippet of code shown bellow.

oSketch = oDrawDoc.ActiveSheet.Sketches.Add
		oSketch.Name = SkName
		oSketch.Edit
		sText = InputBox("Enter new Title Text", "Secondary Title ", "")

This part  of a larger piece of code that places the text according to sheet size.

 

The problem I am having is that I cannot get the code write that locates the existing sketch and then allow me to edit the text

I was intending to edit the existing test using the following;

 

R = MessageBox.Show(Question, Title,  _
		MessageBoxButtons.YesNoCancel, _
		MessageBoxIcon.Question, _
		MessageBoxDefaultButton.Button1)
Dim St As String
Select Case R 
	Case vbYes
		sText = InputBox("Enter new Title Text", "Secondary Title ", "",)
		
'		St = "Yes"
	Case vbNo
		Stop
'		St = "No"
	Case vbCancel
		Stop
'		St = "Cancel"
End Select

Or something to that effect.

 

Please help.

 

 

0 Likes
2,276 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

I'm about to leave my office for the day, but here's some code I threw together to hopefully get you started in the right direction.

It first checks to see if a sketch by that specified name already exists or not.  If not, it lets you know, and asks you if you want to create one.  If it is found, it continues to the next block, which searches to see if the TextBox already exists or not.  (I searched for it by its Name property.  If you didn't give it a name, or only have one TextBox in this sketch, you can replace the sample name I used with the Integer 1.  If it is not found, it lets you know, and asks you if you want to create it.  The rest you likely already have code for.

Dim oSketches As DrawingSketches = ThisDrawing.Document.ActiveSheet.Sketches
Dim oSketch As DrawingSketch
If oSketches.Item("TitleSketch") IsNot Nothing Then
	oSketch = oSketches.Item("TitleSketch")
Else
	oAns1 = MsgBox("Sketch not found.  Create it?", vbYesNo + vbQuestion, " ")
	If oAns1 = vbYes Then
		oSketch = oSketches.Add
	Else
		Exit Sub
	End If
End If

Dim oTBox As Inventor.TextBox
If oSketch.TextBoxes.Item("TextBoxName") IsNot Nothing Then
	oTBox = oSketch.TextBoxes.Item("TextBoxName")
Else
	oAns2 = MsgBox("TextBox not found.  Create it?", vbYesNo + vbQuestion, " ")
	If oAns2 = vbYes Then
		'>>>>> fill in the lines below the way you want them <<<<<
		oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(3,4)
		oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(5,6)
		sText = InputBox("Enter new Title Text","Secondary Title")
		oTBox = oSketch.TextBoxes.AddByRectangle(oPoint1,oPoint2,sText)
'		oTBox = oSketch.TextBoxes.AddFitted()
	End If
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

Chris.Truelove
Contributor
Contributor

Hi,

I am having the same problem with this as I have with other attempts to use 'If' Statements. It Errors whenever an attempt check the sketch name is made. Is there another way to do this?

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

It this sketch the only sketch on the sheet, or are there multiple sketches?

If there are multiple sketches, is this sketch the only one with a TextBox in it?

If there are multiple sketches that have TextBoxes in them, is this the only sketch that has just one TextBox in it?

Does the sketch that contains the target TextBox contain more than that one TextBox?

If this Sketch & TextBox already exist, what would the TextBox always contain, if anything?  (I could use this to check if the TextBox.Text.Contains("Expected Text"), to help identify the right one.)

Is the sketch always going to be on the active sheet, or should this code be looping through all sheets.

If identifying the sketch by it's name won't work for you, how else do you believe the correct sketch can be identified?

 

Once more of these questions are answered, a more precise code can be crafted to meet your needs.

For now though, here is a more careful and detailed approach to getting the correct (existing) sketch, and correct (existing) textbox, to allow you to edit it.

Dim oSheet As Inventor.Sheet = ThisDrawing.Document.ActiveSheet
If oSheet.Sketches.Count > 0 Then
	For Each oSketch As DrawingSketch In oSheet.Sketches
		'If oSketch.Name.Contains("TitleSketch") Then ' it's the right one
		If oSketch.TextBoxes.Count > 0 Then
			For Each oTextBox As Inventor.TextBox In oSketch.TextBoxes
				'If the text box may not contain anything, then delete or comment out this next line
				If oTextBox.Text.Contains("Some text that is always in it.") Then '<<<< EDIT THIS
					oNewText = InputBox("Enter new Title Text.", "Secondary Title.", oTextBox.Text)
					'Could check to see if nothing was entered here, before proceeding.
					Try
						oTextBox.Text = oNewText
					Catch oEx As Exception
						MsgBox("Error occurred while trying to set the value of the entered text to the TextBox." & vbCrLf & _
						oEx.Message & vbCrLf & oEx.StackTrace, vbOKOnly, " ")
					End Try
				End If
			Next
		Else
			MsgBox("No TextBoxes were found in the sketch named: " & oSketch.Name,vbOKOnly," ")
		End If
	Next
Else
	MsgBox("No sketches were found on the Active Sheet. Exiting.", vbOKOnly, " ")
	Return 'or Exit Sub
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

Chris.Truelove
Contributor
Contributor
  • This sketch should be the first sketch on the sheet but I cannot guarantee that it would be the only sketch.
  • It should be the only sketch with text but I wont be the only one producing drawings and a lot of the drafty's will be coming from an ACAD background.
  • Hopefully there wont be any other sketches that have text in them. I guess that will be down to Training, Training, Training.
  • The sketch containing the target text box should only have one text box within it.
  • The text box may not always exist although it should do. The text would not always be the same.
  • Only the sketch on the active sheet needs to be edited at any one time. There will be more than one sheet for each drawing. I am trying everyone to move to using multiple sheets instead of a new drawing for each sheet of a drawing.
  • I'm not sure what can be done if using the sketch new wont work. I don't understand why the code wont read the sketch name.

The problem seems to be getting the 'If' statement to read the name of the existing sketch or textbox. If no sketch is present then the code moves forward.

It seems to be something to do with the logic statement the it doesn't like.

I will look into adding the sketch and text box as the sheets are created this may give an option to only look for and existing sketch and text box with specific names.

 

Thanks for your help.

 

0 Likes
Message 6 of 10

Chris.Truelove
Contributor
Contributor

Hi. Further to my earlier reply. The code you suggested appears to work up to writing back to the text box.

I had gone with looking for a specific sketch title.

If oSketch.Name.Contains("Sketch_Title") Then

and commented out the 

'If oTextBox.Text.Contains("Some text that is always in it.") Then 

Line and the text is being written back into the Input box ready to be changed or deleted but, for some reason it will not write back to the textbox. The error statement does pop up 

 

I don't know if the message from Inventor means anything to you but here it is. 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.TextBox.set_Text(String )
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

The programmed error message is pretty much the same as above.

 

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

Hmmm... That's odd.  It's almost like the it's write protected or reserved for some reason then.

That's a tough nut to crack.

All I can likely do at this point is offer a slightly different route to getting to that point that leaves it open for creating them if their not there.

I also put a few extra notes in there for you to consider.

Dim oSheet As Inventor.Sheet = ThisDrawing.Document.ActiveSheet
Dim oSketch As DrawingSketch
Dim oTextBox As Inventor.TextBox
Dim oExistingText As String
Dim oNewText As String

'set the value of the oSketch variable
If oSheet.Sketches.Count = 0 Then
	oNewSketch = MsgBox("No sketches found on Active Sheet. Create one?", vbYesNo + vbQuestion, " ")
	If oNewSketch = vbNo Then Return 'or Exit Sub   (we don't need to check for vbYes now)
	oSketch = oSheet.Sketches.Add
Else
'	oSketch = oSheet.Sketches.Item(1) 'since you said it will be the first sketch on the sheet
	For Each oSketch In oSheet.Sketches
		If oSketch.Name.Contains("Title") Then  'I believe this is Case-Sensitive
			Exit For 'it will retain the right sketch as the value of oSketch
		End If
	Next
End If
'set the value of the oTextBox variable
If oSketch IsNot Nothing Then
	If oSketch.TextBoxes.Count = 0 Then
		oNewTextBox = MsgBox("No TextBoxes exist in this sketch. Create one?", vbYesNo + vbQuestion, " ")
		If oNewTextBox = vbNo Then Return 'or Exit Sub  (we don't need to check for vbYes now)
		oNewText = InputBox("Enter new Title Text.", "Secondary Title.")
'		oTextBox = oSketch.TextBoxes.AddFitted()
		'You could call a Function here that checks sheet size & orientation, border configuration, etc
		'and returns the needed Point2d coordinates for the rectangle.
		'To return more than one object, set your Function's return data type to some sort of collection,
		'then add the points to the collection within the Function, and return the collection
		'then you may or may not need to redefine those objects as Point2d to use them.
		oTextBox = oSketch.TextBoxes.AddByRectangle() '<<<<< FILL THIS IN HOW YOU WANT IT >>>>>>
	Else
		oTextBox = oSketch.TextBoxes.Item(1) 'since it is going to be the only TextBox in the sketch
		If oTextBox.Text = "" Then
			oNewText = InputBox("Enter new Title Text.", "Secondary Title.")
		Else
			oExistingText = oTextBox.Text
	'		MsgBox("A TextBox was found, and it contains the following:" & vbCrLf & oExistingText, vbOKOnly, " ")
			oNewText = InputBox("Enter new Title Text.", "Secondary Title.", oExistingText)
			Try
				oTextBox.Text = oNewText
'				oTextBox.Text.Replace(oTextBox.Text,oNewText)
			Catch oEx As Exception
				'You could ask the user if they want to just delete and recreate the TextBox, then loop or GoTo
				MsgBox("Error occurred while trying to set the value of the entered text to the TextBox." & vbCrLf & _
				"The Error Message Is as follows:" & vbCrLf & _
				oEx.Message & vbCrLf & vbCrLf & _
				"The StackTrace is as follows:" & vbCrLf & _
				oEx.StackTrace, vbOKOnly, " ")
			End Try
		End If
	End If
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor

I don't know why I didn't think of this in the first place.  You have to put the sketch in 'Edit' mode before it will accept edits.

Use oSketch.Edit

To enter edit mode before trying to change the text, or creating a new TextBox within it.  Then use

oSketch.ExitEdit

To exit the sketch edit mode, before exiting the code.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

Chris.Truelove
Contributor
Contributor

That fixed it.

I have made some good progress with this. I will upload the entire code next week.

 

Thanks for all of your help.

0 Likes
Message 10 of 10

Chris.Truelove
Contributor
Contributor

Hi,

Please see below for the final code for now.

 

Public Sub Main SketchTextAdd()
' Confirm Document Type is Drawing
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.", vbOKOnly, "WRONG DOCUMENT TYPE")
Return
End If

' a reference to the drawing document.
' This assumes a drawing document is active.

oDrawDoc = ThisApplication.ActiveDocument

' Create a new sketch on the active sheet.
Dim oSketch As DrawingSketch 
Dim oDrawSketch As DrawingSketch

' Open the sketch for edit so the text boxes can be created.
' This is only required for drawing sketches, not part.

Dim oTextBox As Inventor.TextBox

' Create text with simple string as input.  Since this doesn't use
' any text overrides, it will default to the active text style.

Dim SkName As String 
SkName = "Sketch_Title"

Dim oDoc As DrawingDocument

'Identify border type
Dim oBorder = ActiveSheet.Border
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
Dim sText As String

' Create text with simple string as input.  Since this doesn't use
' any text overrides, it will default to the active text style.

'Trying new sketch catch addition to the program
Dim oSheet As Inventor.Sheet = ThisDrawing.Document.ActiveSheet
Dim oExistingText As String
Dim oNewText As String

'set the value of the oSketch variable
If oSheet.Sketches.Count = 0 Then
	oSketch = oDrawDoc.ActiveSheet.Sketches.add
	oSketch.Name = SkName
Else
	oSketch = oSheet.Sketches.Item(1) 'since you said it will be the first sketch on the sheet
	For Each oSketch In oSheet.Sketches
		If oSketch.Name.Contains(SkName) Then  'I believe this is Case-Sensitive
			Exit For 'it will retain the right sketch as the value of oSketch
		End If
	Next
End If

'Set the value Of the oTextBox variable
If oSketch IsNot Nothing Then
	If oSketch.TextBoxes.Count = 0 Then
		If oNewTextBox = vbNo Then Return 'or Exit Sub  (we don't need to check for vbYes now)
		oSketch.Edit
		

		'Create a new sketch on the active sheet.
	
		If oBorder = "A3" Then
			Dim A3_A4_A3P_Style As TextStyle
			A3_A4_A3P_Style = oDrawDoc.StylesManager.TextStyles.Item("A3_A4_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.5, 3.3), oTG.CreatePoint2d(34.25, 2.5), sText, A3_A4_P_Style)
		
		Else If oBorder = "A4" Then
			Dim A3_A4_A3P_Style As TextStyle
			A3_A4_A3P_Style = oDrawDoc.StylesManager.TextStyles.Item("A3_A4_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.19, 3.3), oTG.CreatePoint2d(22.0, 2.5), sText, A3_A4_P_Style)
		
		Else If oBorder = "A4_P" Then
			Dim A4_P_Style As TextStyle
			A4_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A4_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.95, 6.40), oTG.CreatePoint2d(19.95, 5.5), sText, A4_P_Style)
		
		Else If oBorder = "A3_P" Then
			Dim A3_A4_A3P_Style As TextStyle
			A3_A4_A3P_Style = oDrawDoc.StylesManager.TextStyles.Item("A3_A4_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.19, 3.3), oTG.CreatePoint2d(22, 2.5), sText, A3_A4_P_Style)
		
		Else If oBorder = "A2" Then
			Dim Altrad_MB_A0_A1_A2_P_Style As TextStyle
			A0_A1_A2_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A0_A1_A2_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(47.65, 9.45), oTG.CreatePoint2d(57.8, 8.2), sText, A0_A1_A2_P_Style)
		
		Else If oBorder = "A1" Then
			Dim A0_A1_A2_P_Style As TextStyle
			A0_A1_A2_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A0_A1_A2_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(72.35, 9.45), oTG.CreatePoint2d(82.5, 8.2), sText, A0_A1_A2_P_Style)
		
		Else If oBorder = "A0" Then
			Dim A0_A1_A2_P_Style As TextStyle
			A0_A1_A2_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A0_A1_A2_P_Style")
			sText = InputBox("Enter new Title Text", "Secondary Title ", "")
			oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(107.15, 9.45), oTG.CreatePoint2d(117.3, 8.2), sText, A0_A1_A2_P_Style)


		End If

	
	Else
		oTextBox = oSketch.TextBoxes.Item(1) 'since it is going to be the only TextBox in the sketch
		If oTextBox.Text = "" Then
			oSketch.Edit
			sText = InputBox("Enter new Title Text.", "Secondary Title.")
			oTextBox.Text = sText
		Else
			oExistingText = oTextBox.Text
			oSketch.Edit
			sText = InputBox("Enter new Title Text.", "Secondary Title.", oExistingText)
			Try
				oTextBox.Text = sText
			Catch oEx As Exception
				'You could ask the user if they want to just delete and recreate the TextBox, then loop or GoTo
				MsgBox("Error occurred while trying to set the value of the entered text to the TextBox." & vbCrLf & _
				"The Error Message Is as follows:" & vbCrLf & _
				oEx.Message & vbCrLf & vbCrLf & _
				"The StackTrace is as follows:" & vbCrLf & _
				oEx.StackTrace, vbOKOnly, " ")
			End Try
		End If
	End If
End If

'-----------------------------------------------------------------------------------

'MessageBox.Show("This is going to check the number of character in the second title")
Fitcheck :	

'Set maximum Charactor value
Dim Count As String
'Dim CurrentLength As Integer
Dim MaxCount As Integer

'MessageBox.Show("Getting the MaxCount")
If oBorder = "A4_P" Then 
	MaxCount = 150
ElseIf oBorder = "A4" Then 
	MaxCount = 154
ElseIf oBorder = "A3" Then 
	MaxCount = 154
ElseIf oBorder = "A3_P" Then 
	MaxCount = 154
ElseIf oBorder = "A2" Then 
	MaxCount = 178
ElseIf oBorder = "A1" Then 
	MaxCount = 178
ElseIf oBorder = "A0" Then 
	MaxCount = 178
End If
Count = oTextBox.Text
'MessageBox.Show("Checking the text length against the MaxCount")
If Count.Length > MaxCount Then
	MessageBox.Show("The title currently exceeds the " & MaxCount & " character limit." & vbLf & _sText & "Total count = " & Count.Length & " characters", "iLogic")
	sText = InputBox("Enter new Title Text", "Secondary Title ", oTextBox.Text)
	oTextBox.Text = sText
	GoTo Fitcheck
	ElseIf Count.Length <= MaxCount Then
	Stop
End If


Dim ActSheet As Sheet
Dim SheetNumber As String
Dim aSNum As Integer

'FOr Each ActSheet In ThisApplication.ActiveDocument.Sheets
SheetNumber  = Mid(oSheet.Name, InStr(1, oSheet.Name, ":") + 1)
If SheetNumber<10 Then
	iProperties.Value("Custom", "Second Title Sheet:0" & SheetNumber) = sText
	'aSNum = ("0" & SheetNumber)', oSheet.Name
	Else
	iProperties.Value("Custom", "Second Title Sheet:" & SheetNumber) = sText
	'aSNum = (SheetNumber)', oSheet.Name)
End If
'Next

'MessageBox.Show("This is going to update the custom iProperty")
'iProperties.Value("Custom", "Second Title " & aSNum) = sText
oSketch.ExitEdit
End Sub

 A big thank you  to all that helped.

0 Likes