Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SW & VB.Net

Status
Not open for further replies.

mcguirepm

Mechanical
Oct 9, 2002
33
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
 
Replies continue below

Recommended for you

Pat,
There is so much help out there on Visual Basic 6 that I see no reason to upgrade.


Bradley
 
Unfortunately, this is the direction that I am required to go.
 
I do ALL my SW programming in .NET. I find VB.NET much, much easier to use (especially string functions and losing the useless SET command), plus the IDE is pretty darn cool.

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...
 
Hi, Evan:

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
 
I typically start with a simple project and build from there...

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:
Public Class Form1
    [b]'Setup Form...[/b]
    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

    [b]'Fill Text Box...[/b]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "Hello World"
    End Sub

    [b]'Clear Text Box...[/b]
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
    End Sub

    [b]'SolidWorks Interface...[/b]
    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

    [b]'AutoCAD Interface...[/b]
    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

 
I use:

Code:
 				'Create the SolidWorks application
				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:
		'release handle to SW
		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...
 
Hi, SWVB101:

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
 
GetObject is a VB function...

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

 
rgrayclamps said:
Hi, SWVB101:

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

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

 
Hi, SWVB101:

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
 
Hi, rgrayclamps:

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

 
Hi, Josh:

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
 
SWVB101-

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...
 
rgrayclamps,

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: 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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor