27 | 07 | 2017

Get Started With AutoCad and VB.NET

Using Visual Basic 2008 Express Edition you can create applications that can communicate with AutoCad. Download Visual Basic 2008 Express from the microsoft site for free (here).

NOTE you do need the ObjectArx to complete this tutorial.

1. OPEN VISUAL BASIC .NET

Open .NET and select Class Library.

"Impression of the New Project Window"

2. SELECT SHOW ALL

On the right in the Solution Explorer select the Show All button. (If the Solution Explorer is not visible then hit CTRL+ALT+L).

"Show All Button"

This will make the Reference tab visible.

3. SHOW REFERENCE WINDOW

Right Click the Reference tab and select "Add Reference".

"Set Reference"

4. SELECT AUTOCAD REFERENCE

In the reference window goto the BROWSE tab and browse to your AutoCad installation folder and add acdbmgd.dll and acmgd.dll. Both files are located in the C:\Program Files\AutoCad XXXX\ directory)

reference

"Reference Window"

5. IMPORT NAMESPACES

If you do not want to fully qualify everything then you can import the namespaces. Add the namespaces below to your class.

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Interop.Common

6. READY FOR PROGRAMMING

Now that all references are set and namespaces are imported you can use VB.NET to communicate with AutoCad.

Add the code below to a class or Form with commandbutton to test the application.

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

 

 



Public Class Class2

    Public Function StartAutoCADSession() As Object

        '*********************************************
        '*********Code from VisibleVisual.com*********
        '*********************************************

        On Error Resume Next

        Dim AcadApp As Object
        'Check if a AutoCad session Exists
        AcadApp = GetObject(, "AutoCAD.Application")
        If Err.Number <> 0Then
            Err.Clear()
            'If there is no AutoCad session active then there will be one created
            AcadApp = CreateObject("AutoCAD.Application")
        End If

        If Err.Number <> 0 Then
            StartAutoCADSession = Nothing
            Exit Function
        End If

        'if successful set visibility status
        AcadApp.Visible = 1
        'opened OK
        Return AcadApp

    End Function

    Sub TestNETprogram()

        Dim AcadApp As Object
        Dim AcadDoc As Object
        Dim prompt1 As String
        Dim pnt As Object

       AcadApp = StartAutoCADSession()
       AcadApp.Visible = True

        AcadDoc = AcadApp.Activedocument

        prompt1 = vbCrLf & "Enter block insert point: "
        pnt = AcadApp.Activedocument.Utility.GetPoint(, prompt1)

        MsgBox("You have selected the folowing coordinates:" _
                & vbCr & "X: " & pnt(0) _
                & vbCr & "Y: " & pnt(1) _
                & vbCr & "Z: " & pnt(2))
    End Sub

End Class


test

 

 

 

 
 
 
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2311473
Latest Articles