Hey guys. I'm building an add-in(used the SW template) and trying to get some simple code to run via a command button. I copied this code from the macro recorder and made some edits, such as removing the 'set' function and adding parenthesis around arguments. I can't figure out how to get rid of the "swApp not declared", "Application not declared" and "SWVerify not declared". I've played around with some statements I've found by searching, but when I run the code, SW just power exits. I think if I can learn how to fix this problem, I can trouble shoot my other VBA macros. I have some nice utilities built using the SW VBA editor. Currently looking for a good book on SW and .NET
Dim Part As Object
Dim boolstatus As Boolean
swApp = Application.SldWorks
Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch(True)
Part.ClearSelection2(True)
Dim skSegment As Object
skSegment = Part.SketchManager.CreateCircle(0#, 0#, 0#, 0.019991, 0.015967, 0#)
You didn't get rid of the actual error. You simply added a variable declaration to keep the compiler quiet. Think of variables as envelopes. In the ConnectToSW method of the addin, SOLIDWORKS puts a letter (object) in the iSwApp envelope (variable). The letter is the actual SOLIDWORKS object your addin is connected to. If you declare a variable named iSwApp in your Form1 class, it is not the same envelope. It is just a new, empty envelope. It doesn't have a letter in it. You can't read from it or write to it. There is nothing there. In order to make your Form1 class aware of the SOLIDWORKS object, you need to make a copy of the letter and put it in the envelope in Form 1. The easiest way to do it is to add a constructor in Form1 that takes an argument:
Public Class Form1
Dim mySWApp As SldWorks.SldWorks 'an empty envelope
Public Sub New(ByVal swApp As SldWorks.SldWorks)
mySWApp = swApp 'put the copy of the letter in the envelope
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mDoc As ModelDoc2
mDoc = mySWApp.ActiveDoc
MessageBox.Show(mDoc.GetTitle())
End Sub
End Class
Then in your code that creates the form, you pass in the object:
Dim myForm As New Form1(iSwApp)
myForm.Show()