2

I'm trying to run a very simple function that takes inputs for a two points and Solidworks makes a line from it.

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

Whenever I try to run this, I get the error 'User defined type not defined'. How would I fix this?

2
  • is your error occurring on this line: value = instance.CreateLine(X1, Y1, Z1, X2, Y2, Z2)? I'm guessing it is because instance never actually gets set to anything. Commented Jul 29, 2020 at 17:04
  • Where do you get the error? Note System is not a thing in VBA unless that's part of the SolidWorks host library. Also, you've hidden your function parameters by declaring local variables with the same name, and your function doesn't return anything. And you need to use Set when assigning object-typed variables Commented Jul 29, 2020 at 17:04

1 Answer 1

3

Try this with a part opened:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.