找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2659|回复: 4

[原创] 练习 面域转封闭多线

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-30 18:55:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
综合了几个片断,最后用的是 3DPolyline ,这个生成简单

  1. // (C) Copyright 2014 by  

  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.Geometry;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Exception = System.Exception;

  10. [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in16.MyCommands))]

  11. namespace AutoCAD_CSharp_plug_in16
  12. {

  13.     public class MyCommands
  14.     {

  15.         private Point2d _p0;

  16.         [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  17.         public void MyCommand() // This method can have any name
  18.         {
  19.             Document doc = Application.DocumentManager.MdiActiveDocument;
  20.             Editor ed = doc.Editor;
  21.             Database db = doc.Database;
  22.             //OpenCloseTransaction tr = new OpenCloseTransaction();
  23.             Transaction tr = db.TransactionManager.StartTransaction();

  24.             PromptEntityOptions peo = new PromptEntityOptions("\nSelect Region");
  25.             peo.SetRejectMessage("Not Region!");
  26.             peo.AddAllowedClass(typeof(Region), true);
  27.       
  28.             PromptEntityResult per = ed.GetEntity("\nSelect Region");

  29.             if (per.Status != PromptStatus.OK) return;

  30.             using (tr)
  31.             {
  32.                 try
  33.                 {
  34.                     BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite  ) as BlockTableRecord;
  35.                     ObjectId id = per.ObjectId;
  36.                     Region region = id.GetObject(OpenMode.ForRead) as Region;

  37.                     DBObjectCollection cvs = new DBObjectCollection();
  38.                     region.Explode(cvs);
  39.                     Point3dCollection plpts = new Point3dCollection();
  40.                     for (int i = 0; i < cvs.Count; i++)
  41.                     {
  42.                         Curve cv = cvs[i] as Curve ;
  43.                         Curve3d ccv = cv.GetGeCurve();
  44.                         PointOnCurve3d[] pts = ccv.GetSamplePoints(ccv.GetParameterOf(ccv.StartPoint),
  45.                             ccv.GetParameterOf(ccv.EndPoint), 1e-2);
  46.                         foreach (PointOnCurve3d  pointOnCurve3D in pts)
  47.                         {
  48.                             plpts.Add(pointOnCurve3D.Point );
  49.                         }
  50.                     }
  51.                     List<Point3d> nn = PntColToLst(plpts);
  52.                     List< Point3d > result = RemoveDup(nn, 1e-3);
  53.                     Point3d[] spt = result.ToArray();
  54.                     Point2d[] spt1 = ConvexHull(Point3dTo2d(spt));
  55.                     Point3dCollection cpts = PointCovertTo3d(spt1);
  56.                     Curve plCurve = new Polyline3d(Poly3dType.SimplePoly ,cpts,true );
  57.                     btr.AppendEntity((Entity) plCurve);
  58.                     tr.AddNewlyCreatedDBObject(plCurve, true);
  59.                     tr.Commit();
  60.                 }
  61.                 catch (Exception)
  62.                 {
  63.                     tr.Abort();
  64.                 }
  65.             }
  66.         }

  67.         private Point3dCollection PointCovertTo3d(Point2d[] pts)
  68.         {
  69.             Point3dCollection npts = new Point3dCollection();
  70.             for (int i = 0; i < pts.Length ; i++)
  71.             {
  72.                 npts.Add(new Point3d( pts[i].X ,pts[i].Y ,0.0));
  73.             }
  74.             return npts;
  75.         }
  76.         /// <summary>
  77.         /// 3D点集转化为2D List
  78.         /// </summary>
  79.         /// <param name="pts">3D点集</param>
  80.         /// <returns></returns>
  81.         private List< Point2d>  Point3dTo2d(Point3d[] pts)
  82.         {
  83.             List <Point2d> npts = new List<Point2d>();
  84.             for (int i = 0; i < pts.Length ; i++)
  85.             {
  86.                 npts.Add( new Point2d(pts[i].X, pts[i].Y) );
  87.             }
  88.             return npts;
  89.         }
  90.         /// <summary>
  91.         /// 三点时针方向
  92.         /// </summary>
  93.         /// <param name="p1"></param>
  94.         /// <param name="p2"></param>
  95.         /// <param name="p3"></param>
  96.         /// <returns></returns>
  97.         private bool Clockwise(Point2d p1, Point2d p2, Point2d p3)
  98.         {
  99.             return ((p2.X  - p3.X  )*(p2.Y   - p1.Y  ) - (p2.X  - p1.X )*(p2.Y   - p3.Y  )) > -1e-6;
  100.         }
  101.         /// <summary>
  102.         /// 排序条件点和基点的角度
  103.         /// </summary>
  104.         /// <param name="pt"></param>
  105.         /// <returns></returns>

  106.         private double Cosine(Point2d pt)
  107.         {
  108.             double d = _p0.GetDistanceTo(pt);
  109.             return d == 0.0 ? 1.0 : (pt.Y - _p0.Y) / d;//sin
  110.         }
  111.         /// <summary>
  112.         /// 2D点集凸包
  113.         /// </summary>
  114.         /// <param name="pts">2D点List</param>
  115.         /// <returns>凸包上的点</returns>
  116.         private Point2d[] ConvexHull(List<Point2d> pts)
  117.         {
  118.             _p0 = pts.OrderBy(p => p.X).ThenBy(p => p.Y).Last( );//最右边点
  119.             pts = pts.OrderByDescending(p => Cosine(p)).ThenBy
  120.                 (p => _p0.GetDistanceTo(p)).ToList();//按角度及距离排序
  121.             Stack<Point2d> outPoint2Ds = new Stack<Point2d>();
  122.             outPoint2Ds.Push(_p0);
  123.             outPoint2Ds.Push(pts[1]);
  124.             bool tf = true;
  125.             for (int i = 2; i < pts.Count ; i++)
  126.             {
  127.                 Point2d n = pts[i];
  128.                 Point2d p = outPoint2Ds.Pop();
  129.                 Point2d q = outPoint2Ds.Peek()  ;
  130.                 while (tf && Clockwise(n, p, q))
  131.                 {
  132.                     if (outPoint2Ds.Count > 1)
  133.                     {
  134.                         outPoint2Ds.Pop();
  135.                         p = q;
  136.                         q = outPoint2Ds.Peek() ;
  137.                     }
  138.                     else
  139.                     {
  140.                         tf = false;
  141.                     }
  142.                 }
  143.                 outPoint2Ds.Push(p);
  144.                 outPoint2Ds.Push(n);
  145.                 tf = true;
  146.             }
  147.             return outPoint2Ds.ToArray() ;
  148.         }
  149.         /// <summary>
  150.         /// 3D点集合转化为List
  151.         /// </summary>
  152.         /// <param name="pts">3D点集合</param>
  153.         /// <returns>List</returns>
  154.         private List<Point3d> PntColToLst(Point3dCollection pts)
  155.         {
  156.             List<Point3d> npts = new List<Point3d>();
  157.             for (int i = 0; i < pts.Count ; i++)
  158.             {
  159.                 npts.Add(pts[i]);
  160.             }
  161.             return npts;
  162.         }
  163.         /// <summary>
  164.         /// 判断重复点条件
  165.         /// </summary>
  166.         /// <param name="p1">点</param>
  167.         /// <param name="p2">点</param>
  168.         /// <param name="fuzz">误差</param>
  169.         /// <returns></returns>
  170.         private bool  PntToPnt(Point3d p1, Point3d p2,double fuzz)
  171.         {
  172.             return p1.DistanceTo(p2) < fuzz;
  173.         }
  174.         /// <summary>
  175.         /// 消除重复点
  176.         /// </summary>
  177.         /// <param name="pts">3D点List</param>
  178.         /// <param name="fuzz">误差</param>
  179.         /// <returns></returns>
  180.         private List< Point3d > RemoveDup(List<Point3d > pts, double fuzz)
  181.         {
  182.             List< Point3d > npts = new List<Point3d>( );
  183.             for (int i = 0; i < pts.Count ; i++)
  184.             {
  185.                 Point3d tmpPts = pts[i];
  186.                 if (npts.Count > 0)
  187.                 {
  188.                     var query = from pt in npts
  189.                         where PntToPnt(tmpPts , pt ,fuzz)
  190.                         select pt;
  191.                     if (query.Count() > 0)
  192.                     {
  193.                         continue;
  194.                     }
  195.                     else
  196.                     {
  197.                         npts.Add(tmpPts);
  198.                     }
  199.                 }
  200.                 else
  201.                 {
  202.                     npts.Add(tmpPts);
  203.                 }
  204.             }
  205.             return npts;
  206.         }
  207.     }

  208. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-30 19:03:52 | 显示全部楼层
本帖最后由 csharp 于 2014-6-1 07:25 编辑

只是最外圈(凸形),不包括内环

太罗嗦了也不够好,学习中

备用

http://through-the-interface.typepad.com/through_the_interface/2008/08/creating-a-seri.html
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2014-6-1 12:24:59 | 显示全部楼层
再用xd工具箱,把3dpolyline转为polyline不就可以了。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2014-6-17 14:37:48 | 显示全部楼层
可以把圆弧也考虑进去吗?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-17 20:07:42 来自手机 | 显示全部楼层
这个问题很多,暂时还无能力完善,学习中
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-22 05:41 , Processed in 0.278803 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表