- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
综合了几个片断,最后用的是 3DPolyline ,这个生成简单
- // (C) Copyright 2014 by
- using System.Collections.Generic;
- using System.Linq;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- using Exception = System.Exception;
- [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in16.MyCommands))]
- namespace AutoCAD_CSharp_plug_in16
- {
- public class MyCommands
- {
-
- private Point2d _p0;
- [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
- public void MyCommand() // This method can have any name
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- //OpenCloseTransaction tr = new OpenCloseTransaction();
- Transaction tr = db.TransactionManager.StartTransaction();
- PromptEntityOptions peo = new PromptEntityOptions("\nSelect Region");
- peo.SetRejectMessage("Not Region!");
- peo.AddAllowedClass(typeof(Region), true);
-
- PromptEntityResult per = ed.GetEntity("\nSelect Region");
- if (per.Status != PromptStatus.OK) return;
- using (tr)
- {
- try
- {
- BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite ) as BlockTableRecord;
- ObjectId id = per.ObjectId;
- Region region = id.GetObject(OpenMode.ForRead) as Region;
- DBObjectCollection cvs = new DBObjectCollection();
- region.Explode(cvs);
- Point3dCollection plpts = new Point3dCollection();
- for (int i = 0; i < cvs.Count; i++)
- {
- Curve cv = cvs[i] as Curve ;
- Curve3d ccv = cv.GetGeCurve();
- PointOnCurve3d[] pts = ccv.GetSamplePoints(ccv.GetParameterOf(ccv.StartPoint),
- ccv.GetParameterOf(ccv.EndPoint), 1e-2);
- foreach (PointOnCurve3d pointOnCurve3D in pts)
- {
- plpts.Add(pointOnCurve3D.Point );
- }
- }
- List<Point3d> nn = PntColToLst(plpts);
- List< Point3d > result = RemoveDup(nn, 1e-3);
- Point3d[] spt = result.ToArray();
- Point2d[] spt1 = ConvexHull(Point3dTo2d(spt));
- Point3dCollection cpts = PointCovertTo3d(spt1);
- Curve plCurve = new Polyline3d(Poly3dType.SimplePoly ,cpts,true );
- btr.AppendEntity((Entity) plCurve);
- tr.AddNewlyCreatedDBObject(plCurve, true);
- tr.Commit();
- }
- catch (Exception)
- {
- tr.Abort();
- }
- }
- }
- private Point3dCollection PointCovertTo3d(Point2d[] pts)
- {
- Point3dCollection npts = new Point3dCollection();
- for (int i = 0; i < pts.Length ; i++)
- {
- npts.Add(new Point3d( pts[i].X ,pts[i].Y ,0.0));
- }
- return npts;
- }
- /// <summary>
- /// 3D点集转化为2D List
- /// </summary>
- /// <param name="pts">3D点集</param>
- /// <returns></returns>
- private List< Point2d> Point3dTo2d(Point3d[] pts)
- {
- List <Point2d> npts = new List<Point2d>();
- for (int i = 0; i < pts.Length ; i++)
- {
- npts.Add( new Point2d(pts[i].X, pts[i].Y) );
- }
- return npts;
- }
- /// <summary>
- /// 三点时针方向
- /// </summary>
- /// <param name="p1"></param>
- /// <param name="p2"></param>
- /// <param name="p3"></param>
- /// <returns></returns>
- private bool Clockwise(Point2d p1, Point2d p2, Point2d p3)
- {
- return ((p2.X - p3.X )*(p2.Y - p1.Y ) - (p2.X - p1.X )*(p2.Y - p3.Y )) > -1e-6;
- }
- /// <summary>
- /// 排序条件点和基点的角度
- /// </summary>
- /// <param name="pt"></param>
- /// <returns></returns>
- private double Cosine(Point2d pt)
- {
- double d = _p0.GetDistanceTo(pt);
- return d == 0.0 ? 1.0 : (pt.Y - _p0.Y) / d;//sin
- }
- /// <summary>
- /// 2D点集凸包
- /// </summary>
- /// <param name="pts">2D点List</param>
- /// <returns>凸包上的点</returns>
- private Point2d[] ConvexHull(List<Point2d> pts)
- {
- _p0 = pts.OrderBy(p => p.X).ThenBy(p => p.Y).Last( );//最右边点
- pts = pts.OrderByDescending(p => Cosine(p)).ThenBy
- (p => _p0.GetDistanceTo(p)).ToList();//按角度及距离排序
- Stack<Point2d> outPoint2Ds = new Stack<Point2d>();
- outPoint2Ds.Push(_p0);
- outPoint2Ds.Push(pts[1]);
- bool tf = true;
- for (int i = 2; i < pts.Count ; i++)
- {
- Point2d n = pts[i];
- Point2d p = outPoint2Ds.Pop();
- Point2d q = outPoint2Ds.Peek() ;
- while (tf && Clockwise(n, p, q))
- {
- if (outPoint2Ds.Count > 1)
- {
- outPoint2Ds.Pop();
- p = q;
- q = outPoint2Ds.Peek() ;
- }
- else
- {
- tf = false;
- }
- }
- outPoint2Ds.Push(p);
- outPoint2Ds.Push(n);
- tf = true;
- }
- return outPoint2Ds.ToArray() ;
- }
- /// <summary>
- /// 3D点集合转化为List
- /// </summary>
- /// <param name="pts">3D点集合</param>
- /// <returns>List</returns>
- private List<Point3d> PntColToLst(Point3dCollection pts)
- {
- List<Point3d> npts = new List<Point3d>();
- for (int i = 0; i < pts.Count ; i++)
- {
- npts.Add(pts[i]);
- }
- return npts;
- }
- /// <summary>
- /// 判断重复点条件
- /// </summary>
- /// <param name="p1">点</param>
- /// <param name="p2">点</param>
- /// <param name="fuzz">误差</param>
- /// <returns></returns>
- private bool PntToPnt(Point3d p1, Point3d p2,double fuzz)
- {
- return p1.DistanceTo(p2) < fuzz;
- }
- /// <summary>
- /// 消除重复点
- /// </summary>
- /// <param name="pts">3D点List</param>
- /// <param name="fuzz">误差</param>
- /// <returns></returns>
- private List< Point3d > RemoveDup(List<Point3d > pts, double fuzz)
- {
- List< Point3d > npts = new List<Point3d>( );
- for (int i = 0; i < pts.Count ; i++)
- {
- Point3d tmpPts = pts[i];
- if (npts.Count > 0)
- {
- var query = from pt in npts
- where PntToPnt(tmpPts , pt ,fuzz)
- select pt;
- if (query.Count() > 0)
- {
- continue;
- }
- else
- {
- npts.Add(tmpPts);
- }
- }
- else
- {
- npts.Add(tmpPts);
- }
- }
- return npts;
- }
- }
- }
|
|