24 June 2012

How to draw line in AutoCAD using VB.NET | Draw AutoCAD with VB.Net 2005 | Code to add a line to AutoCAD drawing using VB.NET

                      Hi Friends, in this article I will explain about how to draw line in AutoCAD using VB.NET.
                      We can write the programs For AutoCAD in VBA, AutoLISP, DCL and VB.NET etc....
First of all open Visual Studio 2005/2008/2010 what you have, otherwise downloaded the Microsoft Visual Studio.

Add project:
File—new—project—class library---enter the class library name—ok






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).
Add reference to classlibrary :
Right Click the Reference tab and select "Add Reference".




Add the reference in the COM Add reference to AutoCAD Type Library (acax17enu.tlb), AutoCAD / ObjectDBX Common Type Library (axdb17enu.tlb)
Add the reference in the Browse C:\Program Files\AutoCAD 2007 add acdbmgd.dll and acmgd.dll.

After that write the below code in the class library code.
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Public Class Class1
  
    <CommandMethod("MyLine")> _
    Public Shared Sub CreateLine()

        Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
        Dim acaddoc As Autodesk.AutoCAD.Interop.AcadDocument
        Dim pt As Object
        Dim pt1 As Object
        AcadApp = GetObject(, "Autocad.Application")
        acaddoc = AcadApp.ActiveDocument

        pt = acaddoc.Utility.GetPoint(, "Pick point:")
        pt1 = acaddoc.Utility.GetPoint(pt, "Pick second point:")

         acaddoc.ModelSpace.AddLine(pt, pt1)

    End Sub
End Class

Build the solution:
Go to Browse tab—Build then .dll file is created in the bin\debug folder of the project.
Go to AutoCAD in the command enter netload then one popup will come.select the your .dll file  and click Open .
Give the MyLine then it will ask the pick point and second pick point and draw the line.
The below figure will show the process of how to load .dll file.



Thank you for  Reading My Blog.If you like my blog then please share with your friends.

3 comments:

  1. Little Help need here , i could not find the AutoCAD Library

    ReplyDelete
    Replies
    1. Hi Ganesh if u install AutoCAD in your system then it will show in COM tab.

      Delete
  2. Do you have an example on how to draw a horizontal line starting from a given point (known coordinates) and ending automatically when intersecting with another line

    ReplyDelete