天天看點

AutoCAD二次開發&多段線分割、添加頂點

今天我們來學習關于多段線中添加頂點方面知識,這裡使用的是autocad2016版,開發環境使用的是visual studio2012,很自然使用的.net4.5,這樣做的主要的目的是,在使用polyline時,需要使用到JoinEntities方法,在低版本中,例如AutoCAD2010是沒有的,好了,這些都是廢話了。這裡通過使用選擇操作,選擇了一條多段線,然後點選多段線上面的點,通過分割曲線函數,将點添加到多段線上。具體操作如gif錄制動畫所示。

AutoCAD二次開發&多段線分割、添加頂點

實作的源代碼如下圖所示。

添加頂點類:

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AddVerticesToPolyline
{
    public static class MyExtensions
    {
        public static Polyline InsertVertices(this Polyline pline, params Point3d[] points)
        {
            if (points == null || points.Length == 0)  
              throw new ArgumentException("點不能空、");
            var array = points.Select(p => pline.GetParameterAtPoint(pline.GetClosestPointTo(p, false))).ToArray();
            Array.Sort(array);

            using (var items = pline.GetSplitCurves(new DoubleCollection(array)))
            {
                 if(items.Count < 2)
                 throw new InvalidOperationException("分割線失敗");
                 var plines = items.Cast<Polyline>();
                 Polyline result = plines.First();
                 Polyline[] rest = null;

                 try
                 {
                      rest = plines.Skip(1).ToArray();
                      result.JoinEntities(rest);
                      return result;
                 }
                 catch (Exception)
                 {
                     
                      if(result != null)
                      result.Dispose();
                      throw;
                 }finally{
                     if (rest != null)
                     {
                         foreach (var item in rest)
                             item.Dispose();
                     }
                 }
            }
        }
    }
}
           

測試操作類:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AddVerticesToPolyline
{
    public static class Class1
    {
        [CommandMethod("ipt")]
        public static void InsertVertices() 
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\n選擇多段線: ");
            peo.SetRejectMessage("\nRequires a Polyline,");
            peo.AddAllowedClass(typeof(Polyline), false);
            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)
                return;
            var ppo = new PromptPointOptions("\nFirst point to insert vertex at: ");
            ppo.AllowNone = true;
            List<Point3d> points = new List<Point3d>();

            while (true)
            {
                var ppr = ed.GetPoint(ppo);
                if (ppr.Status == PromptStatus.None)
                    break;
                else if (ppr.Status == PromptStatus.Cancel)
                    return;
                points.Add(ppr.Value);
                ppo.Message = "\nNext point to insert vertex at: ";

            }

            if (points.Count == 0)
                return;

            using (Transaction tr = new OpenCloseTransaction())
            {
                Polyline pline = (Polyline) tr.GetObject(per.ObjectId, OpenMode.ForRead);
                var newPoly = pline.InsertVertices(points.ToArray());
                pline.UpgradeOpen();
                pline.HandOverTo(newPoly, true, true);
                tr.AddNewlyCreatedDBObject(newPoly, true);
                tr.AddNewlyCreatedDBObject(pline, false);
                tr.Commit();

                ed.WriteMessage("\nInserted {0} vertice(s)", points.Count);
            }
        }
    }
}
           

                                                                       更多内容,請關注公衆号

AutoCAD二次開發&amp;多段線分割、添加頂點

繼續閱讀