直线——line,圆——circle当然还有其的类,圆弧、多段线、样条曲线、椭圆、文字、标注、块、点。想画一条线的话就需要先给出两个点。
Point3d pt1 = new Point3d(0, 0, 0);
Point3d pt2 = new Point3d(10, 0, 0);
Line l1 = new Line(pt1,pt2);
代码中的直线画出来后,运行程序,cad的模型空间中并不会出现这条直线。此时需要将直线添加到模型空间中。下面是将图元添加到模型空间的函数。
public static ObjectId AddToModelSpace(this Database db, Entity ent)
{
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
下面是完整的画线&圆代码。
Point3d pt1 = new Point3d(0, 0, 0);
Point3d pt2 = new Point3d(100, 0, 0);
Line l1 = new Line(pt1,pt2);
Vector3d ve = new Vector3d(0, 0, 1);
Circle cir = new Circle(pt1,ve,120);
//cir.ColorIndex = 2;
//l1.ColorIndex = 2;
db.AddToModelSpace(l1);
db.AddToModelSpace(cir);
实际上,画线/圆之类的,固定的复杂形状,cad本身可以用块的方法解决。二次开发可以实现,但是没有明显简便很多。书上还有详细的各种类及函数的用法,同时也有复制,阵列,缩放,移动图元的介绍,按需查找使用即可。