Creating an AutoCAD linetype programmatically using .NET

Happy New Year, everyone!

I had a very relaxing break over the holiday period: during the better part of two weeks I managed to stay away from my PC and even my BlackBerry, which I admit I'm generally not very good at ignoring. So now I'm easing back into the swing of things, remembering how to type and open modelspaces, among other things. 🙂

To save my brain from unnecessary stress during the first few weeks of 2008, I've decided to take inspiration from the ADN website. Today's post is based on this DevNote, which shows how to create a custom linetype using ObjectARX.

Here's some equivalent code in C#:

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.EditorInput;

namespace Linetype

{

  public class Commands

  {

    [CommandMethod("CL")]

    public void CreateLinetype()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        // Get the linetype table from the drawing

        LinetypeTable lt =

          (LinetypeTable)tr.GetObject(

            db.LinetypeTableId,

            OpenMode.ForWrite

          );

        // Create our new linetype table record...

        LinetypeTableRecord ltr =

          new LinetypeTableRecord();

        // ... and set its properties

        ltr.AsciiDescription = "T E S T -";

        ltr.PatternLength = 0.75;

        ltr.NumDashes = 2;

        ltr.SetDashLengthAt(0, 0.5);

        ltr.SetDashLengthAt(1,-0.25);

        ltr.Name = "TESTLINTEYPE";

        // Add the new linetype to the linetype table

        ObjectId ltId = lt.Add(ltr);

        tr.AddNewlyCreatedDBObject(ltr,true);

        // Create a test line with this linetype

        BlockTable bt =

          (BlockTable)tr.GetObject(

            db.BlockTableId,

            OpenMode.ForRead

          );

        BlockTableRecord btr =

          (BlockTableRecord)tr.GetObject(

            bt[BlockTableRecord.ModelSpace],

            OpenMode.ForWrite

          );

        Line ln =

          new Line(

            new Point3d(0, 0, 0),

            new Point3d(10, 10, 0)

          );

        ln.SetDatabaseDefaults(db);

        ln.LinetypeId = ltId;

        btr.AppendEntity(ln);

        tr.AddNewlyCreatedDBObject(ln, true);

        tr.Commit();

      }

    }

  }

}

Once you've run the CL command, Zoom Extents to see a line that has been created with our new custom linetype.

11 responses to “Creating an AutoCAD linetype programmatically using .NET”

  1. Kean,

    i tried you code and it works great and it also got me thinking... is it possible to programmitically add text in as well? I've tried using ltr.SetTextAt(1, "TEST") but so far i've had no luck, any suggestions???

    Cheers

    Mark

  2. Hi Mark,

    Here you go.

    Cheers,

    Kean

  3. Hi Kean,

    In AutoCAD 2008 there is the new multi leaders. I have placed a box around the text but it sits too close to the text.
    Is there a way the distance between the box and the text can be controlled? Programatically and also direct in AutoCAD?

    Cheers

    Ben

  4. Hi Ben,

    I only see that you can enable the frame - I don't see what controls its distance from the text. I'd suggest posting something on the discussion groups - I'm sure someone there will be able to help.

    Here are some multi-leader posts, by the way:

    keanw.com/multileaders/index.html

    Regards,

    Kean

  5. Mohamad ElCharfa Avatar

    Greetings,

    I would like to first thank you for the great blog. I have been following your posts and they are helping me a lot, especially that I am a new .NET AutoCAD developer.

    My Question:

    I am creating new layerRecords and I would like to set the linetypes based on the definitions set in the acadeiso.lin file. Since this file can be loaded from AutoCAD I believe I do not need to re-create any linetype defined inside it. Is there a way to specify the linetype from the acadeiso.lin file without recreating it?

    Best Regards,

  6. You should be able to use Database.LoadLineTypeFile(), specifying the linetype name and the file path.

    Kean

  7. Hi Kean,

    I am new to Autocad programming. I am looking to create an application using autocad .NET API to generate a drawing (in a new drawing file) based on user inputs / inputs read from a file.

    Can you please provide some sample applications which can help me in understanding how to start the application or please provide some pointers.

    Thanks a lot in advance.

    Thanks,
    Ravi K.

  8. Kean Walmsley Avatar

    Hi Ravi,

    I suggest creating a command that reads the information in from a file (using System.IO) and then creates corresponding geometry in the model/paperspace of the current drawing. You can then either save out the current drawing programmatically or rely on the SAVE command to do so.

    If you search this blog for "AppendEntity" you should find plenty of examples of creating geometry inside an AutoCAd drawing.

    Regards,

    Kean

  9. Hi Kean;
    I have an error in the line
    "Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;"

    The error is:System.InvalidProgramException was unhandled
    HResult=-2146233030
    Message=Common Language Runtime detected an invalid program.
    Source=accoremgd
    StackTrace:
    at Autodesk.AutoCAD.ApplicationServices.Core.Application.get_DocumentManager().....

    I am using AutoCAD 2013 with Visual Studio 2010...
    what is the reason of this error...
    Thanks for your help....
    Regards,
    Hasan

    1. Did you ever figure this out? I'm running into this too.

  10. Hi Hasan,

    Sorry - I don't know, off the top of my head.

    I suggest posting your question to the ADN team or on the AutoCAD .NET Discussion Group.

    Regards,

    Kean

Leave a Reply to Ravi Cancel reply

Your email address will not be published. Required fields are marked *