Setting prompted entries text after title block creation possible?

Setting prompted entries text after title block creation possible?

J_Pfeifer_
Advocate Advocate
1,240 Views
7 Replies
Message 1 of 8

Setting prompted entries text after title block creation possible?

J_Pfeifer_
Advocate
Advocate

Good morning, 

 

Working through some program stuff that allows the users to select the correct title block from a form. During this it removes the names of the sheets themselves. So I am attempting to re-establish those names according to the name of the sheets in the model browser. 

 

In doing so I think I'm tackling the problem in the wrong direction, either leading me to an index to array size mismatch or a unspecified error.

 

The question is, are you unable to modify the sketch.textbox text after the prompted entries have been passed into the title block creation? See code for more information (Code erroring out at line 23 of block 1).

 

 

	Call SelectTitleBlock(TitleBlockDef)

	Call CollectedTitleEntries(TitleBlockDef, PromptStrings)
	
	Call CheckAndRemoveTitleblocks()
	
	For Each osheet1 As Sheet In oDoc.Sheets 
		Dim Titleblock As TitleBlock = osheet1.AddTitleBlock(TitleBlockDef, , PromptStrings)
	Next
	
	Dim PageNameStrings(3) As String
	
	PageNameStrings(0) = oPage1.Name
	PageNameStrings(1) = oPage2.Name
	PageNameStrings(2) = oPage3.Name
	PageNameStrings(3) = oPage4.Name
	
	Dim TextIndex As Integer = 0 
		
	For Each osheet1 As Sheet In oDoc.Sheets
		For Each oTB As Inventor.TextBox In TitleBlockDef.Sketch.Textboxes
			If oTB.Text.Contains("<SHEET NAME>") = True Then 
				oTB.Text = "Hello world"   'PageNameStrings(TextIndex)<-- This will error depending on location of the text index increase. Two locations = unspecified error, one location = array to index size mismatch. 
				TextIndex = TextIndex + 1
			End If 
		Next
	Next			
	
Sub CollectedTitleEntries(ByVal TitleBlockDef As TitleBlockDefinition, ByRef PromptStrings() As String)
	
	Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
	
	Dim oPage1 As Sheet = oDrawDoc.Sheets.Item(1)
	
	'Dim TitleBlockDef As TitleBlockDefinition = oDrawDoc.TitleBlockDefinitions.Add(TitleSelection) 'Title selection will need to be a sub that passes the selected title block definition string name into this sub for collecting prompted entires. 
	
	
	Dim PromptedTitleFields As New List(Of String) 'This establishes an array of strings 
	
		For Each oTB As Inventor.TextBox In TitleBlockDef.Sketch.TextBoxes 'Searching through each text box on the nozzelfittingsketchdefinition 
			If oTB.Text.Contains("<") AndAlso oTB.Text.Contains(">") Then 'As it searches it finds any text within a box to contain both these items it adds it to the collection ------->'If oTB.Text.Contains("<SHEET NAME>") Then 	
				PromptedTitleFields.Add(oTB.Text)
			End If
		Next
		
		'This section below is establishing the uservalue of an inputbox to the prompted strings of the above sketched object. This is required for any sketched symbol with a prompted text need. 
		
		'Dim PromptStrings(PromptedTitleFields.Count - 1) As String
		
		ReDim PromptStrings(PromptedTitleFields.Count - 1)
		
		
		For iIndex1 = 0 To PromptedTitleFields.Count - 1
			PromptStrings(iIndex1) = PromptedTitleFields(iIndex1)
			Logger.Info(PromptStrings(iIndex1) & " At the index: " & iIndex1)
		Next

End Sub

 

 

 

0 Likes
Accepted solutions (2)
1,241 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @J_Pfeifer_.  Yes, it is possible.  However, if you are adding a TitleBlock to a Sheet, and the TitleBlockDefinition does contain some Prompted Entries, you must supply an Array of Strings for those Prompted Entries at that point in time.  And I believe the Array of Strings you supply must have the correct number of elements in it also (one for each Prompted Entry).  You can just provide empty Strings in the Array of Strings though.  Then you can go back afterwards and change their values, either manually, or by code.  There are lots of code examples for this type of task here on this forum, if you use the Search functionality of this forum to find them.  I can most likely help you with filling them in also, but it can be difficult for someone else to do for you properly, because we must be very familiar with your TitleBlockDefinition, such as what specific 'prompts' (with correct spelling and capitalization) are included, and what values should be put into them.  The code solution can be made more dynamic, requesting manual user interactions, but then you start loosing the benefits of automation.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

WCrihfield
Mentor
Mentor

Hi @J_Pfeifer_.  Just as additional tools for these types of situations, I have a few custom Sub routines designed specifically for dealing with 'Prompted Entries' within either TitleBlock, Border, or SketchedSymbol objects.  All three types of drawing based objects can have prompted entries in them, and they are all handled in the same way, so the primary 'input' for these custom Sub routines is a generic Object, which can be any of those 3 things.  The two routines that either 'get' or 'set' all prompted entries in the object, use an Inventor.NameValueMap as the two factor storage type of variable, which can store a list of name / value pairs.  Within each 'entry' in the NameValueMap, the 'Name' will be the 'prompt name', and the 'Value' will be the current value of that prompt (what you see in the TextBox).  The third Sub routine is just or setting a single prompted entry value.  There is already a routine for setting the value of a single one, bot it requires us to specify the TextBox object as 'input', which is complicated to do.  So this routine simply ask you to specify the prompt name instead.  I also have a custom 'Function' routine just for getting the current value of a specific prompt, which returns a String, but I can only attach 3 things to one response.

 

These 3 codes are attached as text files.  Just remember, they do not work on their own, but must be included within your rule somewhere, then called to run from somewhere within your Sub Main...End Sub block of code.  And yes, you must enclose your 'main' code within the 'Sub Main' and 'End Sub' block, first, then put one of these types of blocks of code below that point, outside of that 'main' block of code to be able to use it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

And here is that custom Function, just for 'getting' the current value of a prompted entry.  As with the 'set' variation, there is also an existing / built-in function for this also, but it asks us to specify the TextBox object as 'input', which is complicated to do.  So, this version only asks for the main object itself (TextBox or Border or SketchedSymbol), then the name of the prompt, and returns a String value.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

J_Pfeifer_
Advocate
Advocate

 

 

Editted: Wrote this before your most recent messages, gimme a second to read through. 

 

That's what I thought my code was trying to do. The code currently has the ability to apply the title block change to each sheet as intended. However when I search through the sheets, then through the text boxes apart of the title block on the that iterations of a sheet. I only need to change one value "<SHEET NAME>". This would be the only value that ends up being a prompted entry. 

 

There are about 12 text boxes in each title block, only one of which needs to be changed to match the page name. 

 

The way its written to how I interpret it. I don't see where the issue is. I'm directly getting down to the text box that holds the value, but when I attempt to set it we get a failure. Even when I'm only attempting to pass a single string to all the sheets in the "hello world" note. Can you see anything in the code that's not allowing me to change this value?

 

I've tried something like: oTB.text = oTB.text.Replace("<SHEET NAME>", "Hello world"). Which also fails with a unspecified error. For some reason it seems that the index controling the strings to pass is being compared to the number of textboxes. If those values don't match it fails with the array to size error. With the way the code targets the specific box, the array size shouldn't be being compared to the number of overall textboxes on the item (This is also a presumption as to what's failing, in which I'm still unsure of).

 

Depending on where I place the TextIndex inside the for loops I get different results. The one that I expect to work would be in either of the options that lead to an unspecified error. 

 

Thinking about it though, I could just remove the prompted entry from the title blocks entirely. Once this has been programmed we wouldn't need to manipulate this value further using a form or what not. So long as the other <text> locations can still be modified using a form. 

 

 

0 Likes
Message 6 of 8

J_Pfeifer_
Advocate
Advocate

Might take me awhile to understand everything you've sent so far. I think I'm getting an idea of what to change. 

 

I've done something similar to this with a sketched symbol. In which I used to have a manual user inputbox to change the value of a specific text box on the sketched symbol. Below is the order and working code of me doing so. This is why I came to the conclusion I needed to change the value before establishing the symbol itself. (Line 25)

	Dim UpperleftQuadrantPoint As Point2d = oTG.CreatePoint2d(LeftHalfCenter - LeftquadCenter, TopHalfCenter + TopQuadCenter)
	Dim NozzelFittingSketchDef As SketchedSymbolDefinition = oDoc.SketchedSymbolDefinitions.Item("NEW NOZZLE DETAIL 1")
'scale will need to be adjusted to a proper size. The width of this will be set by the attachment chart insert. The vertical dimenison will need to grow by the number of fittings + manway number

'Below is the user value that controls the prompted entry of the nozzel fitting chart. 
	Dim UserValue As String = Nothing
'UserValue = InputBox("Enter the laminate sketch symbol value '3M,V': ", "Prompted Entry")
	UserValue = "***CHANGE VALUE****"

'MessageBox.Show(UserValue.ToString) 'This messagebox will check the values of the entered value into the input box before applying new value. 

	Dim PromptedFields As New List(Of String) 'This establishes an array of strings 



		For Each oTB As Inventor.TextBox In NozzelFittingSketchDef.Sketch.TextBoxes 'Searching through each text box on the nozzelfittingsketchdefinition 
			If oTB.Text.Contains("<") And oTB.Text.Contains(">") Then 'As it searches it finds any text within a box to contain both these items it adds it to the collection 
				PromptedFields.Add(oTB.Text)
			End If
		Next
'This section below is establishing the uservalue of an inputbox to the prompted strings of the above sketched object. This is required for any sketched symbol with a prompted text need. 
	Dim PromptStrings(PromptedFields.Count - 1) As String

		For iIndex1 = 0 To PromptedFields.Count - 1
			PromptStrings(iIndex1) = UserValue '<------ value established before the prompted entries are sent into the sketched symbol creation. 
			Logger.Info(PromptStrings(iIndex1) & " At the index: " & iIndex1)
	Next

	Dim NozzelFittingSketch As SketchedSymbol = oPage4.SketchedSymbols.Add(NozzelFittingSketchDef, UpperleftQuadrantPoint, 0, 1, PromptStrings)

 

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Edit:  Sent before reading your previous response.

OK.  I suspect that the main issue in your code examples above, is that you can not 'get' or 'set' the value of a prompted entry using the TextBox.Text property.  What you see as the value of the TextBox.Text property is often the result of whatever may be going on within the TextBox.FormattedText property's value.  See this online help page to learn more about 'FormattedText':

XML Tags for FormattedText 

When the FormattedText is for a prompted entry, then the value of that prompt must be obtained or set using the provided methods, such as the following:

TitleBlock.GetResultText 

TitleBlock.SetPromptResultText 

We can find the TextBox object for a specific prompted entry in a couple ways.  My example codes show one of those two ways (checking within FormattedText for the XML tag "</Prompt>", then if that is present, getting the value of TextBox.Text, and eliminating the leading "<" character, and the following ">" character, to get the true prompt name.  Then check that against a prompt name we are expecting, to know which value to set for it.  The way you are showing works too, when you have very few prompted entries and know what they look like in the text editor.  Then we use that TextBox object we found for that specific prompt, in one of those two methods listed above.  It looks to me like your index system is OK, because you seem to be well aware that both List(Of String) and String() both start with element zero, instead of one, and are using Count - 1, for end index of enumerations.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

J_Pfeifer_
Advocate
Advocate
Accepted solution

That formatted text reference is amazing, thank you. It cleared up just about everything. 

 

Once you told me we needed to target that box in a different manner I was able to find a really simple fix. One that I may need to refine later. However it's working perfectly fine. Below is the changes I've made to the code to allow me to set these values. 

 

First I established a new collection that would hold the textboxes, such that I would be able to reference them by index in the main. This was required due to the TitleBlock.SetPromptResultText requires you pass the textbox object itself inside first. For anyone in the future, take care and listen to Wcrihfield for more specific targeting of which box will be used. My example will lead to issues anytime the <SHEET NAME> happens to be a different number in TextBoxCollection. Here we have them setup that all those sheet names are the first index on each of our title blocks. 

 

 

	Call SelectTitleBlock(TitleBlockDef)

	Call CollectedTitleEntries(TitleBlockDef, PromptStrings, TextBoxCollection)'-added collection to sub to hold all textboxes for indexing later
	
	Call CheckAndRemoveTitleblocks()
	
	For Each osheet1 As Sheet In oDoc.Sheets 
		Dim Titleblock As TitleBlock = osheet1.AddTitleBlock(TitleBlockDef, , PromptStrings)
	Next
	
	Dim PageNameStrings(3) As String
	
	PageNameStrings(0) = oPage1.Name
	PageNameStrings(1) = oPage2.Name
	PageNameStrings(2) = oPage3.Name
	PageNameStrings(3) = oPage4.Name
	
	Dim TextIndex As Integer = 0 
		
	For Each osheet1 As Sheet In oDoc.Sheets
		For Each oTB As Inventor.TextBox In TitleBlockDef.Sketch.Textboxes
			If oTB.Text.Contains("<SHEET NAME>") = True Then 
				osheet1.TitleBlock.SetPromptResultText(TextBoxCollection.Item(1), PageNameStrings(TextIndex)) 
'Passed the collection index into the correct way to maipulate the box's value. 
				TextIndex = TextIndex + 1
			End If 
		Next
	Next		

 

0 Likes