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.

Subscribe via RSS
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
Posted by: Mark | January 09, 2008 at 05:14 AM
Hi Mark,
Here you go.
Cheers,
Kean
Posted by: Kean | January 09, 2008 at 03:30 PM
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
Posted by: Ben Senior | February 04, 2008 at 12:49 PM
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
Posted by: Kean | February 04, 2008 at 03:00 PM
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,
Posted by: Mohamad ElCharfa | December 11, 2008 at 02:45 PM
You should be able to use Database.LoadLineTypeFile(), specifying the linetype name and the file path.
Kean
Posted by: Kean Walmsley | December 11, 2008 at 03:01 PM