2

我正在尝试运行一个非常简单的函数,该函数接受两个点的输入,Solidworks 从中生成一条线。

Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks
line = CreateLine(1, 1, 1, 0, 0, 0)
End Sub

Function CreateLine( _
   ByVal X1 As System.Double, _
   ByVal Y1 As System.Double, _
   ByVal Z1 As System.Double, _
   ByVal X2 As System.Double, _
   ByVal Y2 As System.Double, _
   ByVal Z2 As System.Double _
) As SldWorks.SketchSegment

    Dim instance As ISketchManager
    Dim X1 As System.Double
    Dim Y1 As System.Double
    Dim Z1 As System.Double
    Dim X2 As System.Double
    Dim Y2 As System.Double
    Dim Z2 As System.Double
    Dim value As SketchSegment
     
    value = instance.CreateLine(X1, Y1, Z1, X2, Y2, Z2)

End Function

每当我尝试运行它时,都会收到错误“未定义用户定义的类型”。我将如何解决这个问题?

4

1 回答 1

3

打开一个零件试试这个:

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
Dim skSegment As SldWorks.SketchSegment
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Create new sketch
swModel.SketchManager.Insert3DSketch True
' Create line
Set skSegment = CreateLine(1, 1, 1, 0, 0, 0)
' Close sketch
swModel.SketchManager.InsertSketch True
swModel.ClearSelection2 True
End Sub

Function CreateLine( _
   ByVal X1 As Double, ByVal Y1 As Double, ByVal Z1 As Double, _
   ByVal X2 As Double, ByVal Y2 As Double, ByVal Z2 As Double _
) As SldWorks.SketchSegment

    Set CreateLine = swModel.SketchManager.CreateLine(X1, Y1, Z1, X2, Y2, Z2)
End Function
于 2020-07-30T06:49:02.033 回答