27 June 2008

Adding a line to modelspace

I'll be starting out by posting some basic articles. Easy stuff like adding a line, creating a layer. I'll move onto more complex articles soon.

For today, I'll show you a routine that adds a line to modelspace. Here's the main routine:

  Public Function AddLine(ByVal pt1 As Geometry.Point3d, ByVal pt2 As Geometry.Point3d) As Boolean

    ' Get the active document as an object

    Dim oDWG As Document = Application.DocumentManager.MdiActiveDocument


    ' Get the database in the active drawing

    Dim oDB As Database = oDWG.Database


    ' Open a transaction so we can modify the drawing

    Dim oTrans As DatabaseServices.Transaction = oDB.TransactionManager.StartTransaction


    ' Get the block table for the active drawing

    Dim oBT As BlockTable = CType(oDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead), BlockTable)


    ' Get the modelspace block table record

    Dim oBTR As BlockTableRecord = CType(oBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite), BlockTableRecord)


    ' Create the new line object

    Dim oLine As DatabaseServices.Line = Nothing


    ' Set our return value to false

    Dim retVal As Boolean = False



      ' Create the new line given the start and end point passed into the function

      oLine = New DatabaseServices.Line(pt1, pt2)


      ' Add the line to the block table



      ' Add the line object to the drawing database

      oTrans.AddNewlyCreatedDBObject(oLine, True)


      ' Close the open transaction (very important)



      ' Return true if everything worked

      retVal = True

    Catch ex As System.Exception

      MsgBox(ex.Message, MsgBoxStyle.Information, "Error in AddLine")


      ' Clean up the transaction object (very important)


    End Try


    ' Return true if everything worked, false if not

    Return retVal

  End Function

To call this routine, add it to a new vb.net class file. Be sure to reference acdbmgd.dll and acmgd.dll (not copied local) in your project settings and add these five lines at the top of the class file:

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.Runtime

It's good practice to add this to the very top of every file in your project as it will cut down on the number of errors later:

Option Explicit On

Option Strict On

All that's left now is to add a function that will call our AddLine function to create a line in our active drawing.

  <CommandMethod("AddLine")> _

  Public Sub AddLineToModelSpace()


      ' Define our first point at 0,0,0

      Dim oPt1 As New Point3d(0, 0, 0)


      ' Define our second point at 5,5,0

      Dim oPt2 As New Point3d(5, 5, 0)


      ' Call the AddLine function, passing it our two defined points

      If Not AddLine(oPt1, oPt2) Then Throw New System.Exception("Error calling AddLine.")

    Catch ex As System.Exception

      MsgBox(ex.Message, MsgBoxStyle.Information, "Error in WoPrj")

    End Try

  End Sub

If you've copied everything correctly, the program will compile and you can now test out your AddLine function by starting AutoCAD, netloading your DLL and entering AddLine on the command prompt.

Welcome, let the games begin!

Welcome to “Dwg Dot Net”, a blog dedicated to programmers working with AutoCAD and .Net. I've been developing AutoCAD.Net applications for the past few years and hope to reduce the learning curve for anyone just starting out. I’ll be using this blog to archive basic routines, share technical information about any bugs or glitches I find and answer any questions I receive.

A little about me: I have worked with a number of Autodesk’s products and APIs over the years including AutoCAD (Lisp/Diesel/DCL, VB/VBA, VB.Net) , Inventor (VB and VB.Net) and Vault/Productstream (VB.Net). These days I'm working as a full-time AutoCAD.Net programmer and .

If you have suggestions or questions for interesting topics to cover in this blog, please feel free to contact me at dwgdotnet@gmail.com. Neither my blog nor my email account should not be considered channels for support and I make no guarantees that I will be able to address all topics that are proposed. But please do feel free to drop me a line.


All materials on this site are provided "as is" and without any warranty. Any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the authors be liable to any party for any direct, indirect, incidental, special, exemplary, or consequential damages arising in any way out of the use or misuse of this site.