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

 

    Try

      ' 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

      oBTR.AppendEntity(oLine)

 

      ' Add the line object to the drawing database

      oTrans.AddNewlyCreatedDBObject(oLine, True)

 

      ' Close the open transaction (very important)

      oTrans.Commit()

 

      ' Return true if everything worked

      retVal = True

    Catch ex As System.Exception

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

    Finally

      ' Clean up the transaction object (very important)

      oTrans.Dispose()

    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()

    Try

      ' 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.

No comments:

Disclaimer

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.