症状
Inventor VBAでSketchedSymbols.Add()メソッドで正しい引数を渡しているにもかかわらず「プロシージャの呼び出し、または引数が不正です。」エラーが発生する。
再現手順
1.Inventorを起動し、図面ファイルを新規作成します。
2. VBAエディタで以下のコードを実行し、スケッチシンボルを追加します。
Public Sub CreateSketchedSymbolDefinition()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Create the new sketched symbol definition.
Dim oSketchedSymbolDef As SketchedSymbolDefinition
Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Add("Circular Callout")
' Open the sketched symbol definition's sketch for edit. This is done by calling the Edit
' method of the SketchedSymbolDefinition to obtain a DrawingSketch. This actually creates
' a copy of the sketched symbol definition's and opens it for edit.
Dim oSketch As DrawingSketch
Call oSketchedSymbolDef.Edit(oSketch)
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Use the functionality of the sketch to add sketched symbol graphics.
Dim oSketchLine As SketchLine
Set oSketchLine = oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(0, 0), oTG.CreatePoint2d(20, 0))
Dim oSketchCircle As SketchCircle
Set oSketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(22, 0), 2)
Call oSketch.GeometricConstraints.AddCoincident(oSketchLine.EndSketchPoint, oSketchCircle)
' Make the starting point of the sketch line the insertion point
oSketchLine.StartSketchPoint.InsertionPoint = True
' Add a prompted text field at the center of the sketch circle.
Dim sText As String
sText = "<Prompt>Enter text 1</Prompt>"
Dim oTextBox As TextBox
Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(22, 0), sText)
oTextBox.VerticalJustification = kAlignTextMiddle
oTextBox.HorizontalJustification = kAlignTextCenter
Call oSketchedSymbolDef.ExitEdit(True)
End Sub
3.作成したスケッチシンボルをシートに追加する以下のコードを実行すると、oSketchedSymbols.Add()の行でエラーが発生します。
Public Sub InsertSketchedSymbolOnSheet()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Obtain a reference to the desired sketched symbol definition.
Dim oSketchedSymbolDef As SketchedSymbolDefinition
Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("Circular Callout")
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
' This sketched symbol definition contains one prompted string input. An array
' must be input that contains the strings for the prompted strings.
Dim sPromptStrings(0) As String
sPromptStrings(0) = "A"
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Add an instance of the sketched symbol definition to the sheet.
' Rotate the instance by 45 degrees and scale by .75 when adding.
' The symbol will be inserted at (0,0) on the sheet. Since the
' start point of the line was marked as the insertion point, the
' start point should end up at (0,0).
Dim oSketchedSymbols As Object
Set oSketchedSymbols = oSheet.SketchedSymbols
Dim oSketchedSymbol As SketchedSymbol
Set oSketchedSymbol = oSketchedSymbols.Add(oSketchedSymbolDef, oTG.CreatePoint2d(0, 0), (3.14159 / 4), 0.75, sPromptStrings)
End Sub
原因
上述のスケッチシンボルをシートに追加するコードで、SketchedSymbolsオブジェクトを格納する変数oSketchedSymbolsがObject型で宣言されていることエラーの原因です。
SketchedSymbols.Add()メソッドは、第五引数のオプショナル引数に文字列の配列を指定可能ですがSketchedSymbolsオブジェクトがObject型の変数で宣言されている場合、VBAはこの引数を文字列の配列ではなく、IUnknown型の配列に変換してメソッド呼び出し行います。
このため、指定している引数の型不一致と判断されエラーとなります。
対処法
以下サンプルコードの様にSketchedSymbolsオブジェクトを格納する変数oSketchedSymbolsを明示的にSketchedSymbols型として宣言をすることで、このエラーは発生しなくなります。
Public Sub InsertSketchedSymbolOnSheet()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Obtain a reference to the desired sketched symbol definition.
Dim oSketchedSymbolDef As SketchedSymbolDefinition
Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("Circular Callout")
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
' This sketched symbol definition contains one prompted string input. An array
' must be input that contains the strings for the prompted strings.
Dim sPromptStrings(0) As String
sPromptStrings(0) = "A"
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Add an instance of the sketched symbol definition to the sheet.
' Rotate the instance by 45 degrees and scale by .75 when adding.
' The symbol will be inserted at (0,0) on the sheet. Since the
' start point of the line was marked as the insertion point, the
' start point should end up at (0,0).
Dim oSketchedSymbols As SketchedSymbols
Set oSketchedSymbols = oSheet.SketchedSymbols
Dim oSketchedSymbol As SketchedSymbol
Set oSketchedSymbol = oSketchedSymbols.Add(oSketchedSymbolDef, oTG.CreatePoint2d(0, 0), (3.14159 / 4), 0.75, sPromptStrings)
End Sub
なお、この現象はメソッドをチェイン実行した場合にも発生します。例えば上記サンプルで以下のようにoSheet変数をObject型として宣言して、oSheet.SketchedSymbols.Add()と実行した場合などです。この場合もoSheetがObject型として宣言されているため以降のメソッド呼び出しが、VBAの実行時バインディング
を用いて呼び出されるため、VBAが第五引数の変数の型をIUnknown型の配列に変換してしまうためです。
Dim oSheet As Object
...
Set oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTG.CreatePoint2d(0, 0), (3.14159 / 4), 0.75, sPromptStrings)
記事全体を表示