Kean Walmsley


  • About the Author
    Kean on Google+

April 2014

Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      







« Launching AutoCAD with a specific profile using .NET | Main | Creating a complex AutoCAD linetype containing text using .NET »

January 07, 2008

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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200e54fd6dc648834

Listed below are links to weblogs that reference Creating an AutoCAD linetype programmatically using .NET:

blog comments powered by Disqus

10 Random Posts