Hello everyone,
I want to change the width of a textbox used in a Sketched Symbol.
I know there is the Width property inside the TextBox Object. The syntax is given as TextBox.Width() As Double from the Inventor 2022 API Help. The value is given as read/write property.
My question: Has anyone managed to write back the value? And if so, how did you do it?
My code should be used at drawings. Check all Sketched Symbols. If a Sketched Symbole with special name is existing, check the value of textbox <Number>. Get the length of the value and calculate the needed space for it. The textbox <Number> is created as Type Prompted Entry to make it changeable by user.
Finding and reading are working, but write back the value into TextBox.Width is not possible for me.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim oSketchedSymbol As SketchedSymbol
Dim oSketchDef As Sketch
Dim oTextBoxes As TextBoxes
Dim iTextBox As Inventor.TextBox
For Each oSketchedSymbol In oSheet.SketchedSymbols
If oSketchedSymbol.Name = "NameOfSketchedSymbol" Then
oSketchDef = oSketchedSymbol.Definition.Sketch
oTextBoxes = oSketchDef.TextBoxes
Dim jEintrag As String
For j = 1 To oTextBoxes.Count
iTextBox = oTextBoxes.Item(j)
If iTextBox.Text = "<Number>" Then
jEintrag = oSketchedSymbol.GetResultText(iTextBox)
Dim iLengthOfValue As Integer
iLengthOfValue = Strings.Len(jEintrag)
Dim dLengthValue, dFittedLengthValue, dValue As Double
'Calculate needed width
dLengthValue = iLengthOfValue * 1.5
'Set value of TextBox.Width
' iTextBox.Width = dLengthValue 'that is what I want
' iTextBox.Width = iTextBox.Width * 0.1 'try 1
' iTextBox.Width = 30.5623 'try 2
'Check value of Textbox.Width
dValue = iTextBox.Width
MessageBox.Show("Length of string " & iLengthOfValue & vbNewLine & "Calculated width of Textbox " & dLengthValue & vbNewLine & "Width value of Textbox " & dValue)
End If
Next
End If
Next
The messagebox is only for debugging used.
Line 34 runs into an error with this message.
System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.TextBox.set_Width(Double )
at ThisRule.Main() in rule: Forum, in document SketchedSymbolTest.idw:line 34
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
Line 35: writing a value direct at the property runs in an error, too.
System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.TextBox.set_Width(Double )
at ThisRule.Main() in rule: Forum, in document SketchedSymbolTest.idw:line 35
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
Any help is appreciated. Thanks.
Greetings,
Jana
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Hi @JanaHamann. I believe I see the problem. A SketchedSymbolDefinition's sketch can not be modified (ReadOnly) when accessed normally. In order to be able to make edits to it, you need to use
SketchedSymbolDefinition.Edit(oInputDrawingSketchObject). Where the oInputDrawingSketchObject is a variable of Type DrawingSketch that you have already declared, but has no value yet, then that method will set its value with a copy of the definition's sketch. Then after you have made any edits, use the SketchedSymbolDefinition.ExitEdit(True), to save those edits, which saves that copied sketch over the existing one.
Wesley Crihfield
(Not an Autodesk Employee)
Hi @WCrihfield ,
thanks for the fast reply.
I added both lines in the code:
oSketchedSymbol.Definition.Edit(oSketchDef)
oSketchedSymbol.Definition.ExitEdit(True)
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim oSketchedSymbol As SketchedSymbol
Dim oSketchDef As Sketch
Dim oTextBoxes As TextBoxes
Dim iTextBox As Inventor.TextBox
For Each oSketchedSymbol In oSheet.SketchedSymbols
If oSketchedSymbol.Name = "NameOfSketchedSymbol" Then
oSketchDef = oSketchedSymbol.Definition.Sketch
oTextBoxes = oSketchDef.TextBoxes
'this is added
oSketchedSymbol.Definition.Edit(oSketchDef)
Dim jEintrag As String
For j = 1 To oTextBoxes.Count
iTextBox = oTextBoxes.Item(j)
If iTextBox.Text = "<Number>" Then
jEintrag = oSketchedSymbol.GetResultText(iTextBox)
Dim iLengthOfValue As Integer
iLengthOfValue = Strings.Len(jEintrag)
Dim dLengthValue, dFittedLengthValue, dValue As Double
'Calculate needed width
dLengthValue = iLengthOfValue * 1.5
'Set value of TextBox.Width
' iTextBox.Width = dLengthValue 'that is what I want
' iTextBox.Width = iTextBox.Width * 0.1 'try 1
iTextBox.Width = 30.5623 'try 2
'Check value of Textbox.Width
dValue = iTextBox.Width
MessageBox.Show("Length of string " & iLengthOfValue & vbNewLine & "Calculated width of Textbox " & dLengthValue & vbNewLine & "Width value of Textbox " & dValue)
End If
Next
'this is added
oSketchedSymbol.Definition.ExitEdit(True)
End If
Next
Unfortunately the error by writing a value as double into the width property stays the same. System.ArgumentException: The parameter is incorrect.
Did you have another idea?
Hi @JanaHamann. Here try this version of your code. I condensed it a bit, and set the variable for the DrawingSketch object the proper way, so that should enable the edit. But I have not tested it, because I do not have your drawing.
Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Inventor.Sheet = oDrawDoc.ActiveSheet
Dim oSSs As SketchedSymbols = oSheet.SketchedSymbols
If oSSs.Count = 0 Then Exit Sub
For Each oSS As SketchedSymbol In oSSs
If oSS.Name <> "NameOfSketchedSymbol" Then Continue For
Dim oSSD As SketchedSymbolDefinition = oSS.Definition
Dim oSSDSketch As DrawingSketch = Nothing 'create variable here, with no value
oSSD.Edit(oSSDSketch) 'this sets the value of the variable to a copy of the DrawingSketch
Dim oTBoxes As Inventor.TextBoxes = oSSDSketch.TextBoxes
If oTBoxes.Count = 0 Then Continue For
For Each oTBox As Inventor.TextBox In oTBoxes
If oTBox.Text <> "<Number>" Then Continue For
Dim jEintrag As String = oSS.GetResultText(oTBox)
Dim iLengthOfValue As Integer = Strings.Len(jEintrag)
Dim dLengthValue, dFittedLengthValue, dValue As Double
'Calculate needed width
dLengthValue = iLengthOfValue * 1.5
'Set value of TextBox.Width
' iTextBox.Width = dLengthValue 'that is what I want
' iTextBox.Width = iTextBox.Width * 0.1 'try 1
oTBox.Width = 30.5623 'try 2
'Check value of Textbox.Width
dValue = oTBox.Width
MessageBox.Show("Length of string " & iLengthOfValue & vbNewLine & "Calculated width of Textbox " & dLengthValue & vbNewLine & "Width value of Textbox " & dValue)
Next 'oTBox
'oSSDSketch.Solve
oSS.Definition.ExitEdit(True)
Next 'oSS
oSheet.Update
If oDrawDoc.RequiresUpdate Then oDrawDoc.Update2(True)
Wesley Crihfield
(Not an Autodesk Employee)
That did exactly what I want to do!
@WCrihfield: Thank you very much!!!
Have a nice weekend. (I will have it, now. 🙂 )
Jana
Can't find what you're looking for? Ask the community or share your knowledge.