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
{
/// <summary>
/// Enhanced Line Command.
/// Allows user to change current layer and
/// linetype from within the command.
/// </summary>
[CommandMethod("ELINE")]
public static void DrawEnhancedLine()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointOptions prPO = new PromptPointOptions("\nPick start point of line : ");
prPO.AllowNone = true;
prPO.UseBasePoint = false;
PromptPointResult ppr;
ppr = ed.GetPoint(prPO);
//Get start point
Point3d SPoint = ppr.Value;
//Remeber it for the CLOSE option
Point3d CPoint = SPoint;
prPO.Keywords.Add("LAyer");
prPO.Keywords.Add("LType");
prPO.Keywords.Add("Close");
// Continue line command until the user interrupts
while (ppr.Status == PromptStatus.OK ppr.Status ==
PromptStatus.Keyword)
{
prPO.UseBasePoint = true;
//Use the previously picked point as the base point for new line
prPO.BasePoint = SPoint;
prPO.Message = "\nPick next point or ";
ppr = ed.GetPoint(prPO);
Point3d NPoint = ppr.Value;
if (ppr.Status == PromptStatus.OK ppr.Status ==
PromptStatus.Keyword)
{
//Exit command on enter key
if (ppr.Status == PromptStatus.None) break;
//Declare ObjectId variable for common use
ObjectId objID;
// Check to see whether any keyword is choosen
if (ppr.Status == PromptStatus.Keyword)
{
PromptResult prs;
switch (ppr.StringResult)
{
case "LAyer":
prs = ed.GetString("\nEnter a layer name to make it current: ");
if (prs.Status == PromptStatus.OK)
{
//Loop back on Enter
if (prs.StringResult == "")
continue ;
try
{
objID =
SetCurrentLayer(prs.StringResult);
//Check whether the operation was successful
if (objID != ObjectId.Null)
ed.WriteMessage("\nCurrent layer is '"
+ prs.StringResult+"'");
else
ed.WriteMessage("\nThe layer '" +
prs.StringResult + "' was not found.");
continue;
}
catch
{
ed.WriteMessage("\nUnable to change current
layer. An error occured...!!");
continue;
}
}
return ;
case "LType":
prs = ed.GetString("\nEnter a linetype name to make it current: ");
if (prs.Status == PromptStatus.OK)
{
//Loop back on Enter
if (prs.StringResult == "")
continue;
try
{
objID =
SetCurrentLType(prs.StringResult);
//Check whether the operation was successful
if (objID != ObjectId.Null)
ed.WriteMessage("\nCurrent linetype is "
+ prs.StringResult +"'");
else
ed.WriteMessage("\nThe linetype '"
+ prs.StringResult + "' was not found.");
continue;
}
catch
{
ed.WriteMessage("\nUnable to change current linetype. An error occured...!!");
continue;
}
}
return ;
case "Close":
try
{
objID =
CreateLine(SPoint, CPoint);
if (objID != ObjectId.Null)
return;
break;
}
catch {}
return;
}
}
try
{
objID = CreateLine(SPoint, NPoint);
//The last end point will serve as the next base point
if (objID != ObjectId.Null) SPoint = NPoint;
}
catch { return; }
}
}
}
private static ObjectId SetCurrentLayer(string LayerName)
{
ObjectId layerId=ObjectId.Null ;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction tr = tm.StartTransaction())
{
LayerTable lt = (LayerTable)tm.GetObject(db.LayerTableId, OpenMode.ForRead, false);
if (lt.Has(LayerName))
{
layerId = lt[LayerName];
db.Clayer = layerId;
}
tr.Commit();
}
return layerId;
}
private static ObjectId SetCurrentLType(string LineTypeName)
{
ObjectId ltypeId = ObjectId.Null;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction tr = tm.StartTransaction())
{
LinetypeTable ltt = (LinetypeTable)tm.GetObject(db.LinetypeTableId, OpenMode.ForRead, false);
if (ltt.Has(LineTypeName))
{
ltypeId = ltt[LineTypeName];
db.Celtype = ltypeId;
}
tr.Commit();
}
return ltypeId;
}
private static ObjectId CreateLine(Point3d SPoint,Point3d EPoint)
{
ObjectId lineId = ObjectId.Null;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Line oLine = new Line(SPoint, EPoint);
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction tr = tm.StartTransaction())
{
BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
using (oLine)
{
lineId = btr.AppendEntity(oLine);
tm.AddNewlyCreatedDBObject(oLine, true);
}
tr.Commit();
}
return lineId;
}
}
}
Sunday, August 19, 2007
Putting Everything Together - An Enhanced Line Command
I have experienced in the past that the best way to learn something new is to apply it in our own way. So it's time to apply whatever we have learned so far, in a practical way. The result is an enhanced line command which allows the user to change current layer and linetype on the fly. I have also tried to have logical seperations inside the code using reusable methods(See SetCurrentLayer, SetCurrentLType and CreateLine methods).
Labels:
AutoCAD Programming,
AutoCAD.Net,
Sample Code
Subscribe to:
Post Comments (Atom)
1 comment:
It would be helpful if you can provide a link of the source code for user to download. When copy and paste from your blog, the code formatting is bad. Thanks.
Post a Comment