[分享]绘制剖断线(C#)
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;
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;
pt = basePt + distance * Math.Cos(angle);
pt = basePt + distance * Math.Sin(angle);
pt = basePt;
Point3d point = new Point3d(pt, pt, pt);
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, 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#执行过程中在两点角度为 是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))
)
<p>将Lisp的运行结果图片贴上让看看,不知道啥叫剖断线</p> 你的这个剖断线跟剖面图是不是一个东西的 不能设置比例,不太好 路过并表示强烈支持! 角度转换有问题 萝卜兄转战.NET了?你的拉移随心已经成名已久啦!你的剖断线程序,我测试了是后面的角度转换有问题,改过后就行了。不知道你解决没有? public static Point2dCollection GetVertexAndPoints(Point3d pt1, Point3d pt2,double offset_x,double offset_y,double offset_end)
{
Point2d p1 = new Point2d(pt1.X, pt1.Y);
Point2d p2 = new Point2d(pt2.X, pt2.Y);
double ang = GetTwoPoints2DAngle(p1, p2);
double length = p1.GetDistanceTo(p2);
//offset_x = 120.0;
//offset_y = 220.0;
//offset_end=500.0;
//角度定义
double ang_pi = Rad2Ang(180.0) + ang;
double ang_half_pi = Rad2Ang(90.0) + ang;
double ang_half_pi2 = ang - Rad2Ang(90.0);
//起始点
Point2d StartPoint = PolarPoint(p1, ang_pi, 500.0);
Point2d EndPoint = PolarPoint(p2, ang, 500.0);
//中点
Point2d middlePt = PolarPoint(p1, ang, 0.5 * length);
//左右第一点
Point2d leftPt1 = PolarPoint(middlePt, ang_pi, 0.5 * offset_x);
Point2d rightPt1 = PolarPoint(middlePt, ang, 0.5 * offset_x);
//L2、R2投影点
Point2d PtL = PolarPoint(middlePt, ang_pi, 0.25 * offset_x);
Point2d PtR = PolarPoint(middlePt, ang, 0.25 * offset_x);
//左右第二点
Point2d leftPt2 = PolarPoint(PtL, ang_half_pi, 0.5 * offset_y);
Point2d rightPt2 = PolarPoint(PtR, ang_half_pi2, 0.5 * offset_y);
//添加到点集
Point2dCollection pts = new Point2dCollection();
pts.Add(StartPoint); //起点
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(EndPoint); //终点1
return pts;
}
页:
[1]