找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1942|回复: 3

[每日一码] 3D POLYLINE合并(C#)

[复制链接]

已领礼包: 20个

财富等级: 恭喜发财

发表于 2017-5-18 21:29:37 | 显示全部楼层 |阅读模式

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

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

×
UPL2.gif



[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Globalization;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace MergePlines
{
    public class Commands
    {
        bool _en;
        Document _doc;

        public Commands()
        {
            //System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            //System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            _en = CultureInfo.CurrentCulture.Name.StartsWith("en-");
            _doc = AcAp.DocumentManager.MdiActiveDocument;
        }

        class Segment
        {
            public Point2d StartPt { get; set; }
            public Point2d EndPt { get; set; }
            public double Bulge { get; set; }
        }

        private int MergePlines(ObjectIdCollection ids, bool erase)
        {
            Database db = _doc.Database;
            int result = 0;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (DBObjectCollection segments = new DBObjectCollection())
            {
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                List<Segment> segs = new List<Segment>();
                List<Polyline> plines = new List<Polyline>();
                Polyline pl;
                while (ids.Count > 0)
                {
                    pl = (Polyline)tr.GetObject(ids[0], OpenMode.ForWrite);
                    Vector3d normal = pl.Normal;
                    double elev = pl.Elevation;
                    Plane plane = new Plane(Point3d.Origin, normal);
                    plines.Add(pl);
                    ids.RemoveAt(0);
                    for (int i = 0; i < ids.Count; )
                    {
                        pl = (Polyline)tr.GetObject(ids[i], OpenMode.ForWrite);
                        if (Math.Abs(pl.Elevation - elev) < 1e-12 && pl.Normal.IsEqualTo(normal))
                        {
                            plines.Add(pl);
                            ids.RemoveAt(i);
                        }
                        else
                            i++;
                    }
                    if (plines.Count < 2)
                        continue;
                    for (int i = 0; i < plines.Count; i++)
                    {
                        pl = plines[i];
                        pl.Explode(segments);
                        if (erase) pl.Erase();
                    }
                    plines.Clear();
                    using (DBObjectCollection regions = Region.CreateFromCurves(segments))
                    {
                        Region reg = (Region)regions[0];
                        for (int i = 1; i < regions.Count; i++)
                        {
                            reg.BooleanOperation(BooleanOperationType.BoolUnite, (Region)regions[i]);
                            regions[i].Dispose();
                        }
                        foreach (DBObject o in segments) o.Dispose();
                        segments.Clear();
                        reg.Explode(segments);
                        reg.Dispose();
                        for (int i = 0; i < segments.Count; i++)
                        {
                            if (segments[i] is Region)
                            {
                                ((Region)segments[i]).Explode(segments);
                                continue;
                            }
                            Curve crv = (Curve)segments[i];
                            Point3d start = crv.StartPoint;
                            Point3d end = crv.EndPoint;
                            double bulge = 0.0;
                            if (crv is Arc)
                            {
                                Arc arc = (Arc)crv;
                                double angle = arc.Center.GetVectorTo(start).GetAngleTo(arc.Center.GetVectorTo(end), arc.Normal);
                                bulge = Math.Tan(angle / 4.0);
                            }
                            segs.Add(new Segment { StartPt = start.Convert2d(plane), EndPt = end.Convert2d(plane), Bulge = bulge });
                        }
                        foreach (DBObject o in segments) o.Dispose();
                        segments.Clear();
                        while (segs.Count > 0)
                        {
                            using (Polyline pline = new Polyline())
                            {
                                pline.AddVertexAt(0, segs[0].StartPt, segs[0].Bulge, 0.0, 0.0);
                                Point2d pt = segs[0].EndPt;
                                segs.RemoveAt(0);
                                int vtx = 1;
                                while (true)
                                {
                                    int i = segs.FindIndex(delegate(Segment s)
                                    {
                                        return s.StartPt.IsEqualTo(pt) || s.EndPt.IsEqualTo(pt);
                                    });
                                    if (i < 0) break;
                                    Segment seg = segs[i];
                                    if (seg.EndPt.IsEqualTo(pt))
                                        seg = new Segment { StartPt = seg.EndPt, EndPt = seg.StartPt, Bulge = -seg.Bulge };
                                    pline.AddVertexAt(vtx, seg.StartPt, seg.Bulge, 0.0, 0.0);
                                    pt = seg.EndPt;
                                    segs.RemoveAt(i);
                                    vtx++;
                                }
                                pline.SetDatabaseDefaults();
                                pline.Closed = true;
                                pline.Normal = normal;
                                pline.Elevation = elev;
                                btr.AppendEntity(pline);
                                tr.AddNewlyCreatedDBObject(pline, true);
                                result++;
                            }
                        }
                        segs.Clear();
                    }
                }
                tr.Commit();
            }
            return result;
        }

        [CommandMethod("UPL", CommandFlags.UsePickSet)]
        public void UnionPlines()
        {
            Editor ed = _doc.Editor;
            TypedValue[] filter = new TypedValue[3]{
                new TypedValue(0, "LWPOLYLINE"),
                new TypedValue(-4, "&"),
                new TypedValue(70, 1)};
            PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
            if (psr.Status != PromptStatus.OK)
                return;
            PromptKeywordOptions pko = new PromptKeywordOptions("\nEffacer les polylignes source ?: ");
            if (_en)
            {
                pko.Message = "\nErase source polylines ?: ";
                pko.Keywords.Add("Yes");
                pko.Keywords.Add("No");
                pko.Keywords.Default = "Yes";
            }
            else
            {
                pko.Keywords.Add("Oui");
                pko.Keywords.Add("Non");
                pko.Keywords.Default = "Oui";
            }
            pko.AllowNone = true;
            PromptResult pr = ed.GetKeywords(pko);
            if (pr.Status == PromptStatus.Cancel)
                return;
            bool erase = _en ? pr.StringResult == "Yes" : pr.StringResult == "Oui";
            try
            {
                ObjectIdCollection ids = new ObjectIdCollection(psr.Value.GetObjectIds());
                int n = MergePlines(ids, erase);
                if (_en)
                    ed.WriteMessage("\n{0} polyline{1} have been created", n, n > 1 ? "s" : "");
                else
                    ed.WriteMessage("\n{0} polyligne{1} créée{1}", n, n > 1 ? "s" : "");
            }
            catch (Autodesk.AutoCAD.Runtime.Exception e)
            {
                ed.WriteMessage("\n{0}: {1}\n{2}", _en ? "Error" : "Erreur", e.Message, e.StackTrace);
            }
        }
    }
}


请点击此处下载

查看状态:需购买或无权限

您的用户组是:游客

文件名称:MergePlines.zip 
下载次数:29  文件大小:4.47 KB 
下载权限: 不限 以上  [免费赚D豆]

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

已领礼包: 70个

财富等级: 招财进宝

发表于 2017-5-19 08:26:37 | 显示全部楼层
看着挺有意思的,可以用在什么地方呢?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 3191个

财富等级: 富可敌国

发表于 2017-5-19 08:37:53 | 显示全部楼层
学习了,谢谢,加紧练习试一试
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-12-18 21:09 , Processed in 0.418220 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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