Kean Walmsley

July 2009

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 31  

Twitter Updates

    follow me on Twitter



    « 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:

    Comments

    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

    Hi Mark,

    Here you go.

    Cheers,

    Kean

    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

    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:

    http://through-the-interface.typepad.com/through_the_interface/multileaders/index.html

    Regards,

    Kean

    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,

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

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search