- 积分
- 7540
- 明经币
- 个
- 注册时间
- 2006-4-6
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- using System;
- using System.Text;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- namespace CsMgd1
- {
- public class Class1
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- [CommandMethod("Test")]
- public void Test()
- {
- ed.WriteMessage("\n绘制剖断线 By carrot1983");
- PromptPointResult proPtRes1;
- PromptPointOptions proPtOpts1 = new PromptPointOptions("\n指定第一点: ");
- proPtRes1 = ed.GetPoint(proPtOpts1);
- Point3d pt1 = proPtRes1.Value;
- PromptPointResult proPtRes2;
- PromptPointOptions proPtOpts2 = new PromptPointOptions("\n指定第二点: ");
- proPtOpts2.UseDashedLine = true;
- proPtOpts2.UseBasePoint = true;
- proPtOpts2.BasePoint = pt1;
- proPtRes2 = ed.GetPoint(proPtOpts2);
- Point3d pt2 = proPtRes2.Value;
- Point2dCollection pts = GetPoDuanPoints(pt1, pt2);
- ObjectId pline = AddPline(pts, 0.0);
- }
- //取得两点间角度
- public static double Get2PointsAngle(Point3d pt1, Point3d pt2)
- {
- Vector3d v1 = pt1.GetVectorTo(pt2);
- return v1.GetAngleTo(Vector3d.XAxis);
- }
- //按方向和距离求取点
- public static Point3d PolarPoint(Point3d basePt, double angle, double distance)
- {
- double[] pt = new double[3];
- pt[0] = basePt[0] + distance * Math.Cos(angle);
- pt[1] = basePt[1] + distance * Math.Sin(angle);
- pt[2] = basePt[2];
- Point3d point = new Point3d(pt[0], pt[1], pt[2]);
- return point;
- }
- //计算剖断线的顶点表
- public static Point2dCollection GetPoDuanPoints(Point3d pt1, Point3d pt2)
- {
- double ang = Get2PointsAngle(pt1, pt2);
- Point3d leftPt = PolarPoint(pt1, ang+Math.PI, 56);
- Point3d rightPt = PolarPoint(pt2, ang, 56);
- Point3d middlePt = new Point3d((leftPt.X + rightPt.X) * 0.5, (leftPt.Y + rightPt.Y) * 0.5, (leftPt.Z + rightPt.Z) * 0.5);
- Point3d leftPt1 = PolarPoint(middlePt, ang + Math.PI, 26);
- Point3d leftPt2 = PolarPoint(PolarPoint(middlePt, ang + Math.PI, 12), ang + Math.PI * 0.5, 44);
- Point3d rightPt1 = PolarPoint(middlePt, ang, 26);
- Point3d rightPt2 = PolarPoint(PolarPoint(middlePt, ang, 12), ang + Math.PI * 1.5, 44);
- Point2dCollection pts = new Point2dCollection();
- pts.Add(new Point2d (leftPt.X,leftPt.Y));
- pts.Add(new Point2d(leftPt1.X, leftPt1.Y));
- pts.Add(new Point2d(leftPt2.X, leftPt2.Y));
- pts.Add(new Point2d(rightPt2.X, rightPt2.Y));
- pts.Add(new Point2d(rightPt1.X, rightPt1.Y));
- pts.Add(new Point2d(rightPt.X, rightPt.Y));
- return pts;
- }
- // 由二维点集合和线宽创建二维优化多段线的函数.
- public static ObjectId AddPline(Point2dCollection pts, double width)
- {
- try
- {
- int n = pts.Count;
- Polyline ent = new Polyline();
- for (int i = 0; i < n; i++)
- ent.AddVertexAt(i, pts[i], 0, width, width);
- ObjectId entId = AppendEntity(ent);
- return entId;
- }
- catch
- {
- ObjectId nullId = ObjectId.Null;
- return nullId;
- }
- }
- // 将图形对象加入到模型空间的函数.
- public static ObjectId AppendEntity(Entity ent)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId entId;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- entId = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- trans.Commit();
- }
- return entId;
- }
- }
- }
绘制剖断线,我是先用LISP写一遍,再用C#将LISP翻译过来当做练习。
结果不知道为什么。LISP执行的是OK的。
C#执行过程中在两点角度为[0 PI] 是OK的,但是在(PI 2PI)点位错掉了。
呵呵,果然C#很难走顺畅。。。
将LISP附上:- (defun C:PD (/ PT1 PT2 PTS)
- (princ "\n★绘制剖断线 By carrot1983")
- (if (and (setq PT1 (getpoint "\n指定第一点: "))
- (setq PT2 (getpoint PT1 "\n指定第二点: "))
- (setq PTS (C-DRAW_PODUAN PT1 PT2))
- )
- (progn
- (command "_.PLINE")
- (foreach X PTS
- (command "_NON" X)
- )
- (command "")
- )
- )
- (princ)
- )
- (defun C-DRAW_PODUAN
- (PT1 PT2 / ANG LPT LPT1 LPT2 MPT PTS RPT RPT1 RPT2 X Y)
- (setq ANG (angle PT1 PT2))
- (setq LPT (polar PT1 (+ ANG pi) 56))
- (setq RPT (polar PT2 ANG 56))
- (setq MPT (mapcar
- '(lambda (X Y) (* (+ X Y) 0.5))
- LPT
- RPT
- )
- )
- (setq LPT1 (polar MPT (+ ANG pi) 26))
- (setq LPT2 (polar MPT (+ ANG pi) 12))
- (setq LPT2 (polar LPT2 (+ ANG (* 0.5 pi)) 44))
- (setq RPT1 (polar MPT ANG 26))
- (setq RPT2 (polar MPT ANG 12))
- (setq RPT2 (polar RPT2 (+ ANG (* 1.5 pi)) 44))
- (setq PTS (list LPT LPT1 LPT2 RPT2 RPT1 RPT))
- )
|
评分
-
查看全部评分
|