Tuesday, August 14, 2007

Creating AutoCAD Geometry

I think it's time to get into AutoCAD geometry. So, in this session, we are going to create a line command . We will also see using prompt options in this sample code. The command name used here is DRAWLINE.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace AcadNetSamples
{
public class Commands
{
[CommandMethod("DrawLine")]
public static void DrawLine()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointOptions prPO = new PromptPointOptions("\nPick start point of line: ");
prPO.AllowNone = false;
prPO.UseBasePoint = false;
PromptPointResult ppr1 = ed.GetPoint(prPO);
if (ppr1.Status == PromptStatus.OK)
{
prPO.UseBasePoint = true;
prPO.BasePoint = ppr1.Value;
prPO.Message = "\nPick end point: ";
PromptPointResult ppr2 = ed.GetPoint(prPO);
if (ppr2.Status == PromptStatus.OK)
{
Point3d SPoint = ppr1.Value;
Point3d EPoint = ppr2.Value;
Line oLine = new Line(SPoint, EPoint);
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
Autodesk.AutoCAD.DatabaseServices.Transaction tr = tm.StartTransaction();
using
(tr)
{
BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
using
(oLine)
{
btr.AppendEntity(oLine);
tm.AddNewlyCreatedDBObject(oLine, true);
}
tr.Commit();
}
}
}
}
}
}

I don't want to confine this blog to learning AutoCAD.Net only. I have got some Tips & Tricks to share with you. You may encounter these in between the dotnet posts (If you see one, just understand that I didn't get enough time to learn next dotnet topic :-).

No comments: