- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 作者 于 2009-6-20 16:04:26 编辑
先发几个简单的例子,抛砖引玉,希望大家也贴上自己的得意之作:)
直线打断,模拟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<double> pars = new List<double>();
- 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<double> pars = new List<double>();
- 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
- {
- ed.WriteMessage("\n直线平行\n*无效");
- }
- }
- }
- tr.Commit();
- }
- }
- }
找回hatch的边界,
填充边界是一组Ge曲线,所以需要转换为Db曲线
圆弧和椭圆的处理不太好,Ge库实体的方法有点晕哈,期待高人操刀- [CommandMethod("ht")]
- public static void HatchLoop()
- {
- PromptSelectionResult res = CadHelper.Editor.GetSelection(
- new PromptSelectionOptions(),
- new SelectionFilter(new TypedValue[] { new TypedValue(0, "Hatch") }));
- if (res.Status != PromptStatus.OK)
- return;
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTableRecord btr =
- (BlockTableRecord)tr.GetObject(
- db.CurrentSpaceId,
- OpenMode.ForWrite,
- false);
- foreach (ObjectId id in res.Value.GetObjectIds())
- {
- //获取Hatch对象的Ocs
- Hatch h = (Hatch)tr.GetObject(id, OpenMode.ForRead);
- Matrix3d mat = Matrix3d.PlaneToWorld(h.GetPlane());
- //遍历边界集合,通常边界有两种形式:多义线 或 曲线集合
- for (int i = 0; i < h.NumberOfLoops; i++)
- {
- HatchLoop loop = h.GetLoopAt(i);
- //如果是多义线,转换为Db库的多义线
- if (loop.IsPolyline)
- {
- BulgeVertexCollection bvs = loop.Polyline;
- Polyline pl = new Polyline();
- for (int j = 0; j < bvs.Count; j++)
- {
- BulgeVertex bv = bvs[j];
- pl.AddVertexAt(j, bv.Vertex, bv.Bulge, 0, 0);
- }
- pl.TransformBy(mat);
- btr.AppendEntity(pl);
- tr.AddNewlyCreatedDBObject(pl, true);
- }
- //否则,遍历曲线集合,依次转化为Db库的曲线
- else
- {
- foreach (Curve2d curve in loop.Curves)
- {
- Curve c = CadHelper.ConvertCurve2d(curve, mat);
- btr.AppendEntity(c);
- tr.AddNewlyCreatedDBObject(c, true);
- }
- }
- }
- }
- tr.Commit();
- }
- }
CadHelper类- using System;
- using System.Collections.Generic;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- namespace TlsCad
- {
-
- public static class CadHelper
- {
- #region Curve
- //Ge2d曲线按Ocs转化为Db曲线
- public static Curve ConvertCurve2d(Curve2d curve, Matrix3d mat)
- {
- //直线
- if (curve is LineSegment2d)
- {
- return ConvertLineSegment2d((LineSegment2d)curve, mat);
- }
- //样条曲线
- else if (curve is NurbCurve2d)
- {
- return ConvertNurbCurve2d((NurbCurve2d)curve, mat);
- }
- //椭圆
- else if (curve is EllipticalArc2d)
- {
- return ConvertEllipticalArc2d((EllipticalArc2d)curve, mat);
- }
- //圆弧
- else if (curve is CircularArc2d)
- {
- return ConvertCircularArc2d((CircularArc2d)curve, mat);
- }
- else
- {
- //待续
- return null;
- }
- }
- #region ConvertCurve2d
- //圆弧
- public static Curve ConvertCircularArc2d(CircularArc2d ca2d, Matrix3d mat)
- {
- Curve c = ConvertCircularArc2d(ca2d);
- c.TransformBy(mat);
- return c;
- }
- public static Curve ConvertCircularArc2d(CircularArc2d ca2d)
- {
- if (ca2d.IsClosed())
- {
- return ConvertCircular2d(ca2d);
- }
- else
- {
- return ConvertArc2d(ca2d);
- }
- }
- public static Circle ConvertCircular2d(CircularArc2d c2d)
- {
- return
- new Circle(
- new Point3d(new Plane(), c2d.Center),
- new Vector3d(0, 0, 1),
- c2d.Radius);
- }
- public static Arc ConvertArc2d(CircularArc2d a2d)
- {
- double startangle, endangle;
- if (a2d.IsClockWise)
- {
- startangle = (a2d.EndPoint - a2d.Center).Angle;
- endangle = (a2d.StartPoint - a2d.Center).Angle;
- }
- else
- {
- startangle = (a2d.StartPoint - a2d.Center).Angle;
- endangle = (a2d.EndPoint - a2d.Center).Angle;
- }
- return
- new Arc(
- new Point3d(new Plane(), a2d.Center),
- new Vector3d(0, 0, 1),
- a2d.Radius,
- startangle,
- endangle);
- }
- //椭圆弧
- public static Ellipse ConvertEllipticalArc2d(EllipticalArc2d ea2d, Matrix3d mat)
- {
- Ellipse e = ConvertEllipticalArc2d(ea2d);
- e.TransformBy(mat);
- return e;
- }
- public static Ellipse ConvertEllipticalArc2d(EllipticalArc2d ea2d)
- {
- double startangle, endangle;
- if (ea2d.IsCircular())
- {
- startangle = 0;
- endangle = Math.PI * 2;
- }
- else
- {
- double majorangle = ea2d.MajorAxis.Angle;
- if (ea2d.IsClockWise)
- {
- startangle = (ea2d.EndPoint - ea2d.Center).Angle - majorangle;
- endangle = (ea2d.StartPoint - ea2d.Center).Angle - majorangle;
- }
- else
- {
- startangle = (ea2d.StartPoint - ea2d.Center).Angle - majorangle;
- endangle = (ea2d.EndPoint - ea2d.Center).Angle - majorangle;
- }
- }
- return
- new Ellipse(
- new Point3d(new Plane(), ea2d.Center),
- new Vector3d(0, 0, 1),
- new Vector3d(new Plane(), ea2d.MajorAxis) * ea2d.MajorRadius,
- ea2d.MinorRadius / ea2d.MajorRadius,
- startangle,
- endangle);
- }
- //直线
- public static Line ConvertLineSegment2d(LineSegment2d ls2d, Matrix3d mat)
- {
- Line l = ConvertLineSegment2d(ls2d);
- l.TransformBy(mat);
- return l;
- }
- public static Line ConvertLineSegment2d(LineSegment2d ls2d)
- {
- Plane plane = new Plane();
- return
- new Line(
- new Point3d(plane, ls2d.StartPoint),
- new Point3d(plane, ls2d.EndPoint));
- }
- //样条曲线
- public static Spline ConvertNurbCurve2d(NurbCurve2d nc2d, Matrix3d mat)
- {
- Spline spl = ConvertNurbCurve2d(nc2d);
- spl.TransformBy(mat);
- return spl;
- }
- public static Spline ConvertNurbCurve2d(NurbCurve2d nc2d)
- {
- int i;
- Plane plane = new Plane();
- Point3dCollection ctlpnts = new Point3dCollection();
- for (i = 0; i < nc2d.NumControlPoints; i++)
- {
- ctlpnts.Add(new Point3d(plane, nc2d.GetControlPointAt(i)));
- }
- DoubleCollection knots = new DoubleCollection();
- foreach (double knot in nc2d.Knots)
- {
- knots.Add(knot);
- }
- DoubleCollection weights = new DoubleCollection();
- for (i = 0; i < nc2d.NumWeights; i++)
- {
- weights.Add(nc2d.GetWeightAt(i));
- }
- return
- new Spline(
- nc2d.Degree,
- false,
- false,
- false,
- ctlpnts,
- knots,
- weights,
- 0,
- nc2d.Knots.Tolerance);
- }
- #endregion
- #endregion
- }
- }
|
|