Filling TextBoxes in Titleblock, if possible with nested loops

Filling TextBoxes in Titleblock, if possible with nested loops

Daan_M
Collaborator Collaborator
291 Views
3 Replies
Message 1 of 4

Filling TextBoxes in Titleblock, if possible with nested loops

Daan_M
Collaborator
Collaborator

Hello,

 

1. I have a TitleBlock in my Drawing Document with multiple TextBoxes, i filled these TextBoxes with strings so I can Identify what textbox i'm working with and change the Text of them with a specific variable from (2).

 

2. I have a big list of variables: 1 to n

 

What i want to do is fill the TextBox with a specific variable, but i have problems doing this:

 

  • I managed to Identify a TextBox, but i am unable to change the text
  • Since the number of TextBoxes and the number of Variables is quite big, i prefer not to make very long if-else statement. If it's possible to do this with two collections/arrays (one for the textboxes and one for the vairables)  and nest them somehow it would be perfect. But my programming knowledge on how to do this is limited

Here is my code with some comments

 

 

Dim oDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = oDoc.Sheets(1)
oSheet.Activate

Dim Var1, Var2 As String 'variables 1 to n
Var1 = "xxxx"
Var2 = "yyyy"

Dim oTitleBlock As TitleBlock = oSheet.TitleBlock
Dim oTitleBlockDefinition As TitleBlockDefinition = oTitleBlock.Definition
Dim oSketch As DrawingSketch

Dim oTB As Inventor.TextBox 'textboxes 1 to n
oTitleBlockDefinition.Edit(oSketch)

For Each oTB In oSketch.TextBoxes
	oTBT = oTB.Text
	If oTBT = "Var1" Then
	oTBT = Var1 'THE CONDITION WORKS FINE BUT THIS LINE DOESN'T ACTUALLY CHANGE THE TEXT
	Else If oTBT = "Var2" Then
	oTBT = Var2 'THE CONDITION WORKS FINE BUT THIS LINE DOESN'T ACTUALLY CHANGE THE TEXT
	'n
	End If
Next

oTitleBlockDefinition.ExitEdit()

 

 

Basically if the Text from the TextBox in my Titleblock equals "Var1" i want to change it's text to the actual Var1 variable.

 

 

0 Likes
Accepted solutions (1)
292 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor

Are you sure you want to change the TitleBlock definition every time you want to update the values? In my opinion it is much better solution to replace static textboxes in TitleBlock definition with textboxes with prompted text. In this case you can update the values multiple times because the PromptedText in its definition stay unchanged.  Also you can use the same TitleBlock on more then one sheet in the same drawing document with different values.

Message 3 of 4

Daan_M
Collaborator
Collaborator

Hi @Michael.Navara ,prompted textboxes sound like a good idea if it keeps it more flexible.

I've managed to prompt a variable into the textboxes, but the problems where i have to assign each variable manually to a specific textbox remains;

 

 

Dim oDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = oDoc.Sheets(1)
oSheet.Activate

Dim Var1, Var2 As String 'variables 1 to n
Var1 = "variable_1"
Var2 = "variable_2"

Dim oTitleBlock As TitleBlock = oSheet.TitleBlock
Dim oTitleBlockDefinition As TitleBlockDefinition = oTitleBlock.Definition
Dim oSketch As DrawingSketch

Dim oTB As Inventor.TextBox 'textboxes 1 to n
Dim oTBV1, oTBV2 As Inventor.TextBox

oTitleBlockDefinition.Edit(oSketch)

For Each oTB In oSketch.TextBoxes
	If oTB.FormattedText.Contains("Var1") Then
		oTBV1 = oTB
	ElseIf oTB.FormattedText.Contains("Var2") Then
		oTBV2 = oTB
	End If
Next

oTitleBlockDefinition.ExitEdit()

oTitleBlock.SetPromptResultText(oTBV1, Var1)
oTitleBlock.SetPromptResultText(oTBV2, Var2)

 

 

Is there a way how i can loop through my batch of variables without having to assign them to each individual textbox? 

 

To give an example (i know this doesn't work, but to give an idea of what i try to achieve):

 

 

Dim oDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = oDoc.Sheets(1)
oSheet.Activate

Dim Variables(0 To 1) 'variables 0 to n
Variables(0) = "Test1"
Variables(1) = "Test2"

Dim TotalVariables = 1
Dim Counter = 0

Dim oTitleBlock As TitleBlock = oSheet.TitleBlock
Dim oTitleBlockDefinition As TitleBlockDefinition = oTitleBlock.Definition
Dim oSketch As DrawingSketch

Dim oTB As Inventor.TextBox 'textboxes 1 to n

oTitleBlockDefinition.Edit(oSketch)

For Each oTB In oSketch.TextBoxes
	
	Do While Counter < TotalVariables
		If oTB.Text = "Var" & i
			oTitleBlock.SetPromptResultText(oTB, "Variables(" & i & ")")
		End If
		Counter = Counter + 1
	Loop
	Counter = 0
Next

oTitleBlockDefinition.ExitEdit()

 

0 Likes
Message 4 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

You can use this code for it. It expects the prompted text is the same as your variable name.

For example for variable name "Var1" you set PromptedText to "Var1".

 

 

Imports System.Text.RegularExpressions
Sub Main
	Dim variables As String() = {"Var1", "Var2", "etc..." }
	Dim drawing As DrawingDocument = ThisDoc.Document
	Dim sheet As Sheet = drawing.ActiveSheet

	Dim titleBlock As TitleBlock = sheet.TitleBlock
	For Each txtBox As Inventor.TextBox In titleBlock.Definition.Sketch.TextBoxes
		Dim formattedText As String = txtBox.FormattedText
		Dim match = Regex.Match(formattedText, "<Prompt.*>(?<varName>.*)</Prompt>")
		If match.Success Then
			Dim varName As String = match.Groups.Item("varName").Value
			If variables.Contains(varName) Then
				Dim value As String = GetValue(varName)
				titleBlock.SetPromptResultText(txtBox, value)
			End If
		End If
	Next

End Sub

Private Function GetValue(varName As String) As String
	Return String.Format("Value for {0}", varName)
End Function

 

 

0 Likes