先发几个简单的例子,抛砖引玉,希望大家也贴上自己的得意之作:)
直线打断,模拟Break命令
[CommandMethod("myb")]
public static void MyBreakLine()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//选择直线
PromptEntityOptions opt1 = new PromptEntityOptions("\nselect a line:");
opt1.SetRejectMessage("\nerror!");
opt1.AddAllowedClass(typeof(Line), true);
PromptEntityResult res1 = ed.GetEntity(opt1);
if (res1.Status == PromptStatus.OK)
{
//选择第二打断点
PromptPointOptions opt2 = new PromptPointOptions("\nselect second point:");
opt2.AllowNone = true;
PromptPointResult res2 = ed.GetPoint(opt2);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Line oldline = (Line)tr.GetObject(res1.ObjectId, OpenMode.ForRead);
List pars = new List();
Point3d pt1 = oldline.GetClosestPointTo(res1.PickedPoint, false);
Point3d pt2 = new Point3d();
pars.Add(oldline.GetParameterAtPoint(pt1));
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite,
false);
DBObjectCollection objs;
//两种情况
if (res2.Status == PromptStatus.OK)
{
//如果选择了第二点,获取直线上两点的param值,并排序
pt2 = oldline.GetClosestPointTo(res2.Value, false);
pars.Add(oldline.GetParameterAtPoint(pt2));
pars.Sort();
//按param值打断曲线
objs = oldline.GetSplitCurves(new DoubleCollection(pars.ToArray()));
foreach (Line newline in objs)
{
//如果生成的直线起点或终点不是选择的打断点,把它加入数据库
if ((newline.StartPoint != pt1 && newline.StartPoint != pt2) ^ (newline.EndPoint != pt1 && newline.EndPoint != pt2))
{
btr.AppendEntity(newline);
tr.AddNewlyCreatedDBObject(newline, true);
}
}
}
else
{
//如果没有选择第二点,就按第一点打断
objs = oldline.GetSplitCurves(new DoubleCollection(pars.ToArray()));
foreach (Line newline in objs)
{
btr.AppendEntity(newline);
tr.AddNewlyCreatedDBObject(newline, true);
}
}
oldline.UpgradeOpen();
oldline.Erase();
tr.Commit();
}
}
}
所有曲线打断于点
[CommandMethod("BAC")]
public static void BreakAllCurve()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//选择曲线
PromptSelectionResult res = ed.GetSelection(new PromptSelectionOptions(), new SelectionFilter(new TypedValue[] { new TypedValue(0, "*Line,Arc,Circle,Ellipse") }));
ObjectId[] ids = res.Value.GetObjectIds();
ObjectIdCollection oldids = new ObjectIdCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite,
false);
//遍历选择集
foreach (ObjectId i in ids)
{
List pars = new List();
Curve iCurve = (Curve)tr.GetObject(i, OpenMode.ForRead);
//获取曲线与其他曲线的交点处的param值集合,按该集合打断曲线
foreach (ObjectId j in ids)
{
if (i != j)
{
Curve jCurve = (Curve)tr.GetObject(j, OpenMode.ForRead);
Point3dCollection iwpnts = new Point3dCollection();
iCurve.IntersectWith(jCurve, Intersect.OnBothOperands, iwpnts, 0, 0);
foreach (Point3d p in iwpnts)
{
pars.Add(iCurve.GetParameterAtPoint(p));
}
}
}
//如果有交点,按param值排序并打断
if (pars.Count > 0)
{
pars.Sort();
try
{
//将子曲线加入数据库,原曲线加入oldids集合
foreach (Curve c in iCurve.GetSplitCurves(new DoubleCollection(pars.ToArray())))
{
btr.AppendEntity(c);
tr.AddNewlyCreatedDBObject(c, true);
}
oldids.Add(i);
}
catch
{ }
}
}
foreach (ObjectId id in oldids)
{
tr.GetObject(id, OpenMode.ForWrite).Erase();
}
tr.Commit();
}
}
简单的直线倒角
[CommandMethod("dj")]
public void daojiao()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptDoubleResult resgetdist = ed.GetDistance("\n请输入倒角距离");
if (resgetdist.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
double dist = resgetdist.Value;
PromptEntityOptions optgetent = new PromptEntityOptions("\n请选择第一条直线:");
optgetent.SetRejectMessage("\n错误的选择");
optgetent.AddAllowedClass(typeof(Line), true);
PromptEntityResult resgetent = ed.GetEntity(optgetent);
if (resgetent.Status == PromptStatus.OK)
{
ObjectId id1 = resgetent.ObjectId;
Line line1 = (Line)tr.GetObject(id1, OpenMode.ForWrite);
line1.Highlight();
Point3d pt1 = resgetent.PickedPoint;
optgetent.Message = "\n请选择第二条直线:";
resgetent = ed.GetEntity(optgetent);
if (resgetent.Status == PromptStatus.OK)
{
ObjectId id2 = resgetent.ObjectId;
Point3d pt2 = resgetent.PickedPoint;
Line line2 = (Line)tr.GetObject(id2, OpenMode.ForWrite);
pt1 = line1.GetClosestPointTo(pt1, false);
pt2 = line2.GetClosestPointTo(pt2, false);
//获取两直线交点
Point3dCollection pts = new Point3dCollection();
line1.IntersectWith(line2, Intersect.ExtendBoth, pts, 0, 0);
//如果有交点
if (pts.Count == 1)
{
Point3d pt = pts[0];
Plane plane = new Plane();
//判断点选在直线的哪一侧(是否靠近起点)
Vector3d v1,v2;
v1 = line1.StartPoint - pt;
v2 = line1.EndPoint - pt;
bool atstart1 = false;
if (v1.Length != 0)
{
atstart1 = Tolerance.Equals(v1.AngleOnPlane(plane), (pt1 - pt).AngleOnPlane(plane));
if (Tolerance.Equals(v1.AngleOnPlane(plane), v2.AngleOnPlane(plane)))
{
atstart1 = v1.Length > v2.Length;
}
}
v1 = line2.StartPoint - pt;
v2 = line2.EndPoint - pt;
bool atstart2 = false;
if (v1.Length != 0)
{
atstart2 = Tolerance.Equals(v1.AngleOnPlane(plane), (pt2 - pt).AngleOnPlane(plane));
if (Tolerance.Equals(v1.AngleOnPlane(plane), v2.AngleOnPlane(plane)))
{
atstart2 = v1.Length > v2.Length;
}
}
// 判断被选择段是否可以倒角
Point3d pt3 = atstart1 ? line1.StartPoint : line1.EndPoint;
Point3d pt4 = atstart2 ? line2.StartPoint : line2.EndPoint;
Vector3d vec1 = pt3 - pt;
Vector3d vec2 = pt4 - pt;
if (vec1.Length >= dist && vec2.Length >= dist)
{
//计算倒角点
vec1 = vec1.GetNormal() * dist;
vec2 = vec2.GetNormal() * dist;
pt3 = pt + vec1;
pt4 = pt + vec2;
//按点选的位置改变原直线
if (atstart1)
{
line1.EndPoint = pt3;
}
else
{
line1.StartPoint = pt3;
}
if (line1.Length == 0)
{
line1.Erase();
}
if (atstart2)
{
line2.EndPoint = pt4;
}
else
{
line2.StartPoint = pt4;
}
if (line2.Length == 0)
{
line2.Erase();
}
//生成倒角线
Line line = new Line(pt3, pt4);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite,
false);
btr.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
}
else
{
ed.WriteMessage("\n距离太大\n*无效");
}
}
else