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

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:

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

{

publicclassCommands

{

[CommandMethod("CCL")]

publicvoid 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 =

newLinetypeTableRecord();

// ... 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, newVector2d(-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 =

newLine(

newPoint3d(0, 0, 0),

newPoint3d(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: