pl.AddVertexAt(i, pc[i], 0.5, 5, 5); }
BlockTable bt = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pl);
trans.AddNewlyCreatedDBObject(pl, true); trans.Commit(); } }
[CommandMethod(\)] public void CreatePoliyline2d() {
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction()) {
//创建直线
Point3d[] pPoint3dArr = new Point3d[3]; pPoint3dArr[0] = new Point3d(5, 5, 0); pPoint3dArr[1] = new Point3d(7, 8, 0); pPoint3dArr[2] = new Point3d(15, 16, 0);
Point3dCollection pPoint3dColl = new Point3dCollection(pPoint3dArr); double[] pBugles = new double[] { 0, 0, 0 };
DoubleCollection pDoubleCol = new DoubleCollection(pBugles); Polyline2d pPl2d = new Polyline2d(
Poly2dType.FitCurvePoly, pPoint3dColl, 100, false, 1, 1, pDoubleCol); pPl2d.Thickness = 3; pPl2d.CurveFit();
BlockTable bt = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pPl2d);
trans.AddNewlyCreatedDBObject(pPl2d, true); trans.Commit(); } }
[CommandMethod(\)] public void CreatePolyline() {
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction()) {
25
Polyline pPl = new Polyline();
pPl.AddVertexAt(0, new Point2d(20, 20), 0, 0.5, 0.5); pPl.AddVertexAt(0, new Point2d(30, 30), 0, 0.5, 0.5); pPl.AddVertexAt(0, new Point2d(40, 50), 30, 0.5, 0.5); pPl.AddVertexAt(0, new Point2d(50, 45), 0, 0.5, 0.5); pPl.Thickness = 3;
Polyline2d pPl2d = pPl.ConvertTo(false); pPl2d.CurveFit();
System.Collections.IEnumerator pEnumerator = pPl2d.GetEnumerator(); bool pHas = pEnumerator.MoveNext(); while (pHas) {
object pObj = pEnumerator.Current;
pEd.WriteMessage(pObj.GetType().ToString()); pHas = pEnumerator.MoveNext(); }
int segN = pPl.NumberOfVertices - 1; for (int i = 0; i < segN; i++) {
SegmentType pSegmentType = pPl.GetSegmentType(i); if (pSegmentType.Equals(SegmentType.Arc)) {
CircularArc2d pCircularArc2d = pPl.GetArcSegment2dAt(i); }
if (pSegmentType.Equals(SegmentType.Line)) { } }
BlockTable bt = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pPl); btr.AppendEntity(pPl2d);
trans.AddNewlyCreatedDBObject(pPl, true); trans.AddNewlyCreatedDBObject(pPl2d, true); trans.Commit(); }
}
26
4.5 创建椭圆
Autodesk.AutoCAD.DatabaseServices.Ellipse
构造函数:Ellipse(Autodesk.AutoCAD.Geometry.Point3d center, Autodesk.AutoCAD.Geometry.Vector3d unitNormal,
Autodesk.AutoCAD.Geometry.Vector3d majorAxis, double radiusRatio, double startAngle, double endAngle) ①创建一个长轴平行于X轴的椭圆
②创建一个长轴非平行于X轴和Y轴的椭圆(只要设置unitNormal变量的个分量都不为0)
代码:
[CommandMethod(\)]//创建椭圆 public void Ellipse() {
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction()) {
Ellipse pEllipse = new Ellipse(new Point3d(150, 150, 0), new Vector3d(0, 0, 50), new Vector3d(50, 50, 0), 0.8, 0, 0); BlockTable bt = (BlockTable)trans.GetObject(
27
db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pEllipse);
trans.AddNewlyCreatedDBObject(pEllipse, true); trans.Commit(); } }
4.6 样条曲线
样条曲线详见:http://blog.csdn.net/tuqu/article/details/5366701
开(open )B-样条曲线 clamped B-样条曲线 闭(closed)B-样条曲线
使用:Spline(int degree, bool rational, bool closed, bool periodic, Point3dCollection controlPoints, DoubleCollection knots, DoubleCollection weights, double controlPointTolerance, double knotTolerance)创建样条曲线
代码:
[CommandMethod(\)]//创建样条曲线 public void CreateSpline() {
28
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction()) {
Point3d p1 = new Point3d(50, 30, 0); Point3d p2 = new Point3d(100, 60, 0); Point3d p3 = new Point3d(150, 520, 0); Point3d p4 = new Point3d(200, 240, 0); Point3d p5 = new Point3d(250, 300, 0);
Point3dCollection pc = new Point3dCollection(); pc.Add(p1); pc.Add(p2); pc.Add(p3); pc.Add(p4); pc.Add(p5);
Spline spline = new Spline(pc, 2, 5); BlockTable bt = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(spline);
trans.AddNewlyCreatedDBObject(spline, true); trans.Commit(); } }
4.7 创建面域
面域是由一个或多个闭合曲线组成的几何对象。
代码:
[CommandMethod(\)]//创建面域 public void CreatePolygon() {
Database db = HostApplicationServices.WorkingDatabase;
29