SW & VB.Net
SW & VB.Net
(OP)
I'm curious if anyone is using VB.NET for SolidWorks API programming. I saw a post from Sept'03 with no affirmative responses. Has anyone moved on to VB.Net since then? I ran my code throught the VB.Net wizard and I am now left with a lot of error messages each time I try to run it.
I assume there have been syntax changes. Can anyone point me in the right direction?
Thanks,
Pat
I assume there have been syntax changes. Can anyone point me in the right direction?
Thanks,
Pat






RE: SW & VB.Net
There is so much help out there on Visual Basic 6 that I see no reason to upgrade.
Bradley
RE: SW & VB.Net
RE: SW & VB.Net
Other than some small changes you have to make when you exit your .NET application, 99% of the codes comes across clean. You just need to make sure you release the SW COM object properly.
Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...
RE: SW & VB.Net
I never figured out how to use VB.NET to open a SolidWorks document. Could you show us your sample codes to launch and open a SolidWorks document?
Thanks,
Alex
RE: SW & VB.Net
This was my first successful test interfacing with both SW and ACAD via VB.NET (as opposed to VB6 in the past)
You might find it useful, cut out what you don't need and build on what you do need...
Objects:
TextBoxes:
TextBox1
Buttons:
Button1
Button2
Button3
Button4
References:
SldWorks
AutoCAD (optional)
CODE
'Setup Form...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "VB.NET Test"
Button1.Text = "Fill Text"
Button2.Text = "Clear Text"
Button3.Text = "SolidWorks"
Button4.Text = "AutoCAD"
End Sub
'Fill Text Box...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello World"
End Sub
'Clear Text Box...
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
End Sub
'SolidWorks Interface...
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim swApp As SldWorks.SldWorks
Dim Dwg As SldWorks.DrawingDoc
swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
swApp.UserControl = True
Dwg = swApp.NewDrawing(-1)
Dwg.CreateText(TextBox1.Text, 0 * 0.0254, 11 * 0.0254, 0, 0.5 * 0.0254, 0)
End Sub
'AutoCAD Interface...
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim acApp As AutoCAD.AcadApplication
Dim Dwg As AutoCAD.AcadDocument
Dim pt1(2) As Double
Try
acApp = GetObject(, "AutoCAD.Application")
Catch ex As Exception
acApp = CreateObject("AutoCAD.Application")
End Try
acApp.Visible = True
Dwg = acApp.Documents.Add("")
Dwg.ModelSpace.AddText(TextBox1.Text, pt1, 1)
End Sub
End Class
As you can see, it looks VERY similar to the VB6 version...
When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S
RE: SW & VB.Net
CODE
Try
swApp = System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application")
Catch ex As Exception
'if errors out, that means SW was not already running
End Try
If swApp Is Nothing Then
#If DEBUG Then
swApp = New SldWorks.SldWorks
#Else
swApp = CreateObject("SldWorks.Application")
#End If
End If
I use the #IF Debug to allow me to flip between late binding for development purposes and early binding to handle different production versions of SW.
Also, b/c SW is a COM object, you will need to make sure you release it properly. .NET garbage collection does not know how to handle is properly unless you release the references:
CODE
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(swApp)
Catch ex As System.NullReferenceException
Debug.WriteLine("SolidWorks wasn't hooked")
Catch ex2 As Exception
'don't care about any other errors - just exit
End Try
Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...
RE: SW & VB.Net
I tried your codes. But it showed the following errors:
1. Type 'SldWorks.SldWorks' is not defined.
2. Type 'SldWorks.DrawingDoc' is not defined.
3. Type 'AutoCAD.AcadApplication' is not defined.
4. Type 'AutoCAD.AcadDocument' is not defined.
How do I define these objects? Could you recommend some books or web sites on this?
Thanks,
Alex
RE: SW & VB.Net
System.Runtime.InteropServices.Marshal.GetActiveObject is only needed in C#
and for solidworks, 1 instance is all that is allowed... so the Try/Catch is not necessary...
CreateObject will use an existing Instance if found...
AutoCAD, on the otherhand, needs the Try/Catch as shown above...
But there are multiple ways to do everything in programming so take your pick...
I try to stay with the simplest ways...
When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S
RE: SW & VB.Net
First off, do you have SolidWorks AND AutoCAD?
If you only have SW, remove the AutoCAD stuff...
As far as the SW objects go...
In the menu Click Project>Add Reference...
Click the COM tab
Then select SldWorks (and AutoCAD if you have it):
mine are listed as:
SldWorks 2004 Type Library
AutoCAD 2004 Type Library
Hope This Helps
-Josh
When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S
RE: SW & VB.Net
The following two statements from your codes do not work as "SldWorks.SldWorks" and "SldWorks.DrawingDoc" are not difined.
Dim swApp As SldWorks.SldWorks
Dim Dwg As SldWorks.DrawingDoc
How do I define these?
Thanks,
Alex
RE: SW & VB.Net
COM References... (See ABOVE)
In the menu Click Project>Add Reference...
Click the COM tab
Then select SldWorks XXXX Type Library
(such as: SldWorks 2004 Type Library)
When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S
RE: SW & VB.Net
You are right. COM References were not loaded. Thanks for your help. By the way, could you recommend some books or web sites on SolidWorks and VB.net?
Alex
RE: SW & VB.Net
You are not quite right that only one instance of SW is allowed. You can definitely run two instances (or more) - they will just run like crap b/c SW does not like sharing. Typically, your statement is correct, but I have seen problems with that approach enough times that I always use the GetObject first, then use the CreateObject in the second level try.
Plus, you should always wrap a program instantiation in a Try/Catch just in case it fails.
However, you are correct about the system.runtime call - you can use GetObject in VB.NET. However, I use the other syntax at times b/c it makes it easier for a C# person to read.
Evan
Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...
RE: SW & VB.Net
No problem... and no idea...
For VB, any beginner VB book will do, they basically all say the same thing... (try SAM'S learn VB in 21 days)
For SW API, I learned through the following:
Recording MACROS in SW...
The API help doc (SW Menu: Help>SolidWorks API ... Help Topics)
Internet (this site in particular) as well as SolidWorks Page: http:/
And good ole' trial and error
I learn most software by experimenting with them, then using references (books, help files, web) to fill in the Gaps...
Good Luck!!!
EvanBasalik,
Yeah, I know what you mean...
There is no harm in adding the extra code, and in the long run, it may prove useful...
I could have sworn that it used to say somewhere in the documentation that only 1 instance was allowed (at least by the API call)
I know you can physically/manually load a second instance, but the API *shouldn't* be able to...
(that doesn't mean it CAN'T, just under normal circumstances... shouldn't)
Personally, I have yet to see it load a second instance.
Thanks,
-Josh
When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S