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.