Having trouble assigning a variable string to SketchSymbolDefinition. This works fine in VB.NET.
Anyone know what is going wrong?
Public Sub addnote() Dim Notes As New ArrayList Notes.Add ("Note1") Notes.Add ("Note2") Notes.Add ("Note3") Dim oSketchedSymbol As SketchedSymbol Dim oSketchedSymbolDef As SketchedSymbolDefinition 'This assumes a drawing document is active. Dim oDrawDoc As DrawingDocument Set oDrawDoc = ThisApplication.ActiveDocument ' a reference to the drawing document. Dim oSheet As Sheet Set oSheet = oDrawDoc.ActiveSheet Dim oTG As TransientGeometry Set oTG = Inventor.ThisApplication.TransientGeometry For Each oName In Notes Debug.Print oName Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions(oName) ' Constant Integer or string works fine but string by variable is failing. Dim oTGPoint As Point2d Set oTGPoint = oTG.CreatePoint2d(0, 0) ' Add an instance of the sketched symbol definition to the sheet. Set oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTGPoint) Next 'Call MoveNote(oDrawDoc, oSheet, oTG) End Sub
From the API help
SketchedSymbolDefinitions.Item( Index As Variant ) As SketchedSymbolDefinition
Index | Variant | Input Variant value that specifies the SketchedSymbolDefinition to return. This can be either a numeric value indicating the index of the item in the collection or it can be a string indicating the sketched symbol definition's name. If an out of range index or a name of a non-existent sketched symbol definition is specified, an error occurs. |
Solved! Go to Solution.
Having trouble assigning a variable string to SketchSymbolDefinition. This works fine in VB.NET.
Anyone know what is going wrong?
Public Sub addnote() Dim Notes As New ArrayList Notes.Add ("Note1") Notes.Add ("Note2") Notes.Add ("Note3") Dim oSketchedSymbol As SketchedSymbol Dim oSketchedSymbolDef As SketchedSymbolDefinition 'This assumes a drawing document is active. Dim oDrawDoc As DrawingDocument Set oDrawDoc = ThisApplication.ActiveDocument ' a reference to the drawing document. Dim oSheet As Sheet Set oSheet = oDrawDoc.ActiveSheet Dim oTG As TransientGeometry Set oTG = Inventor.ThisApplication.TransientGeometry For Each oName In Notes Debug.Print oName Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions(oName) ' Constant Integer or string works fine but string by variable is failing. Dim oTGPoint As Point2d Set oTGPoint = oTG.CreatePoint2d(0, 0) ' Add an instance of the sketched symbol definition to the sheet. Set oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTGPoint) Next 'Call MoveNote(oDrawDoc, oSheet, oTG) End Sub
From the API help
SketchedSymbolDefinitions.Item( Index As Variant ) As SketchedSymbolDefinition
Index | Variant | Input Variant value that specifies the SketchedSymbolDefinition to return. This can be either a numeric value indicating the index of the item in the collection or it can be a string indicating the sketched symbol definition's name. If an out of range index or a name of a non-existent sketched symbol definition is specified, an error occurs. |
Solved! Go to Solution.
Solved by Michael.Navara. Go to Solution.
This is because your variable oName is Variant type in general. You need to cast its value to string and then it works
Sub GetSketchedSymbolByName()
Dim drw As DrawingDocument
Set drw = ThisApplication.ActiveDocument
Dim names As New ArrayList
Call names.Add("test")
For Each name In names
Debug.Print name
'Doesn't work because name is Variant in general
'Set sDef1 = drw.SketchedSymbolDefinitions(name)
Dim defName As String
defName = name
Set sDef2 = drw.SketchedSymbolDefinitions(defName)
Debug.Print "RESULT: " & sDef2.name
Next
End Sub
This is because your variable oName is Variant type in general. You need to cast its value to string and then it works
Sub GetSketchedSymbolByName()
Dim drw As DrawingDocument
Set drw = ThisApplication.ActiveDocument
Dim names As New ArrayList
Call names.Add("test")
For Each name In names
Debug.Print name
'Doesn't work because name is Variant in general
'Set sDef1 = drw.SketchedSymbolDefinitions(name)
Dim defName As String
defName = name
Set sDef2 = drw.SketchedSymbolDefinitions(defName)
Debug.Print "RESULT: " & sDef2.name
Next
End Sub
Thanks for the confirmation and solution Michael. I was there when I was testing and had read about needing to change to a string but hadn't implemented correctly. VB.NET is more forgiving on these little details.
Thanks for the confirmation and solution Michael. I was there when I was testing and had read about needing to change to a string but hadn't implemented correctly. VB.NET is more forgiving on these little details.
Can't find what you're looking for? Ask the community or share your knowledge.