.NET CAD二次开发学习 对称画线功能

时间:2021-02-28 05:41:52

[CommandMethod("CBline")] //对称画线
public void CBline()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor edit = doc.Editor;
Database db = doc.Database;
using(Transaction trans=doc.TransactionManager.StartTransaction())
{

PromptEntityOptions peo = new PromptEntityOptions("请指定基准线\n");
PromptEntityResult per = edit.GetEntity(peo);
Entity ent = trans.GetObject(per.ObjectId, OpenMode.ForWrite) as Entity;
//if (ent.GetType()!=typeof(Line))
if(!(ent is Line))
{
edit.WriteMessage("指定的实体不是基准线!");
return;

}
Line line = (Line)ent;
line.ColorIndex = 1;
PromptPointOptions ppo = new PromptPointOptions("请指定第一点\n");
PromptPointResult ppr = edit.GetPoint(ppo);
Point3d pt1=ppr.Value;
PromptPointOptions ppo1 = new PromptPointOptions("请指定第二点\n");
PromptPointResult ppr1 = edit.GetPoint(ppo);
Point3d pt2=ppr1.Value;
Point3d pt3 = line.GetClosestPointTo(pt1,true);
Vector3d vt1 = pt3 - pt1;
Point3d pt4 = new Point3d(vt1.X+pt3.X,vt1.Y+pt3.Y,0);
Point3d pt5 = line.GetClosestPointTo(pt2, true);
Vector3d vt2 = pt5 - pt2;
Point3d pt6 = new Point3d(vt2.X + pt5.X, vt2.Y + pt5.Y, 0);
Line line1 = new Line(pt1, pt2);
Line line2 = new Line(pt4, pt6);
ObjectId idModelSpace = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord btr = trans.GetObject(idModelSpace, OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(line1);
btr.AppendEntity(line2);
trans.AddNewlyCreatedDBObject(line1, true);
trans.AddNewlyCreatedDBObject(line2, true);
trans.Commit();


}

}