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      







« Creating an AutoCAD linetype programmatically using .NET | Main | Understanding the properties of textual linetype segments in AutoCAD »

January 09, 2008

Creating a complex AutoCAD linetype containing text using .NET

In my last post we saw some code to create a simple linetype using .NET. As a comment on that post, Mark said:

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???

It turned out to be quite a bit more complicated to make a linetype containing text than merely calling SetTextAt() on one of the segments. In order to understand what properties needed setting, I first loaded the HOT_WATER_SUPPLY linetype from acad.lin (using the LINETYPE command):

Loaded_linetype

I then looked at the contents of the linetype table using ArxDbg (the ObjectARX SDK sample that is very helpful for understanding drawing structure). Here's what the SNOOPDB command - defined by the ArxDbg application - showed for the loaded linetype:

Snooped_linetype

From there it was fairly straightforward to determine the code needed to create our own complex linetype containing text segments. I decided to call the new linetype "COLD_WATER_SUPPLY", and have it resemble the original in every way but placing "CW" in the middle segment, rather than "HW" (with the descriptions updated to match, of course). As I've simply copied the properties of an existing linetype, please don't ask me to explain what they all mean. :-)

Here's the C# code:

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("CCL")]

    public void CreateComplexLinetype()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        // We'll use the textstyle table to access

        // the "Standard" textstyle for our text

        // segment


        TextStyleTable tt =

          (TextStyleTable)tr.GetObject(

            db.TextStyleTableId,

            OpenMode.ForRead

          );


        // 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.Name = "COLD_WATER_SUPPLY";

        ltr.AsciiDescription =

          "Cold water supply ---- CW ---- CW ---- CW ----";

        ltr.PatternLength = 0.9;

        ltr.NumDashes = 3;


        // Dash #1


        ltr.SetDashLengthAt(0, 0.5);


        // Dash #2


        ltr.SetDashLengthAt(1, -0.2);

        ltr.SetShapeStyleAt(1, tt["Standard"]);

        ltr.SetShapeNumberAt(1, 0);

        ltr.SetShapeOffsetAt(1, new Vector2d(-0.1,-0.05));

        ltr.SetShapeScaleAt(1, 0.1);

        ltr.SetShapeIsUcsOrientedAt(1, false);

        ltr.SetShapeRotationAt(1, 0);

        ltr.SetTextAt(1, "CW");


        // Dash #3


        ltr.SetDashLengthAt(2, -0.2);


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

      }

    }

  }

}

And here's the result of calling the CCL command and zooming in on the created line:

Linetype_with_text

TrackBack

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

Listed below are links to weblogs that reference Creating a complex AutoCAD linetype containing text using .NET:

blog comments powered by Disqus

10 Random Posts