CAD c# 仿多段线

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.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
using Application= Autodesk.AutoCAD.ApplicationServices.Application;
namespace Demo
{
    public class Coor
    {
        [CommandMethod("xx")]//
        public void FangLine()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            // 声明一个直线的集合对象
            List<ObjectId> lineList = new List<ObjectId>();

            // 声明一个预备的对象
            Point3d pointStart = new Point3d(100, 100, 0);
            Point3d pointPre = new Point3d(100, 100, 0);

            PromptPointResult ppr = ed.GetPoint2("\n 指定第一个点:");
            if (ppr.Status == PromptStatus.Cancel) return;
            if (ppr.Status == PromptStatus.None) pointPre = pointStart;
            if (ppr.Status == PromptStatus.OK)
            {
             
                pointStart = ppr.Value;
                pointPre = pointStart;
            }
            //判断循环是否继续
            bool isC = true;

            while (isC)
            {
                if (lineList.Count == 2)
                {
                    ppr = ed.GetPoint("\n 指定一下点或 [闭合(C)/放弃(U)]:", pointPre, new string[] { "C", "U" });
                }
                else
                {
                    ppr = ed.GetPoint("\n 指定下一点或[放弃(U)]", pointPre, new string[] { "U" });
                }
                //ppr = ed.GetPoint("\n 指定下一点或 [放弃(U)]", pointPre, new string[] { "U" });
                Point3d pointNext; // 用于接收下一点坐标
                if (ppr.Status == PromptStatus.Cancel) return;
                if (ppr.Status == PromptStatus.None) return;
                if (ppr.Status == PromptStatus.OK)
                {
                    pointNext = ppr.Value;
                    lineList.Add(db.AddLineToModeSpace(pointPre, pointNext));
                    return;

                    //pointPre = pointNext;
                }
                #region
                // if (ppr.Status == PromptStatus.Keyword)
                //{
                //    switch (ppr.StringResult)
                //    {
                //        case "U":
                //            if (lineList.Count == 0) // 没有直线
                //            {
                //                pointStart = new Point3d(100, 100, 0);
                //                pointPre = new Point3d(100, 100, 0);
                //                ppr = ed.GetPoint2("\n 指定第一个点:");
                //                if (ppr.Status == PromptStatus.Cancel) return;
                //                if (ppr.Status == PromptStatus.None) pointPre = pointStart;
                //                if (ppr.Status == PromptStatus.OK)
                //                {
                //                    pointStart = ppr.Value;
                //                    pointPre = pointStart;
                //                }
                //            }
                //            else if (lineList.Count > 0)
                //            {
                //                int count = lineList.Count;
                //                ObjectId lId = lineList.ElementAt(count - 1);
                //                pointPre = this.GetLineStartPoint(lId);
                //                lineList.RemoveAt(count - 1);
                //                EraseEntity(lId);

                //            }
                //            break;
                //        case "C":
                //            lineList.Add(db.AddLineToModeSpace(pointPre, pointStart));
                //            isC = false;
                //            break;
                //    }
                //}
                #endregion
            }
        }
        private Point3d GetLineStartPoint(ObjectId lineId)
        {
            Point3d startPoint;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Line line = (Line)lineId.GetObject(OpenMode.ForRead);
                startPoint = line.StartPoint;
            }
            return startPoint;
        }
        private void EraseEntity( ObjectId entId)
        {
            // 打开事务处理
            using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
            {
                // 打开要删除的图形对象
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
                ent.Erase();
                trans.Commit();
            }
        }
       
    }
}
  
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
   public static class Tools
    {
        public static ObjectId AddEntityToModeSpace(this Database db, Entity ent)
        {
            // 声明ObjectId 用于返回
            ObjectId entId = ObjectId.Null;
            // 开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                // 添加图形到块表记录
                entId = btr.AppendEntity(ent);
                // 更新数据信息
                trans.AddNewlyCreatedDBObject(ent, true);
                // 提交事务
                trans.Commit();
            }
            return entId;
        }
        public static ObjectId AddLineToModeSpace(this Database db, Point3d startPoint, Point3d endPoint)
        {
            return db.AddEntityToModeSpace(new Line(startPoint, endPoint));
        }
        public static PromptPointResult GetPoint2(this Editor ed, string promptStr)
        {
            // 声明一个获取点的提示类
            PromptPointOptions ppo = new PromptPointOptions(promptStr);
            // 使回车和空格键有效
            ppo.AllowNone = true;
            return ed.GetPoint(ppo);
        }
        public static PromptPointResult GetPoint(this Editor ed, string promptStr, Point3d pointBase, params string[] keyWord)
        {

            // 声明一个获取点的提示类
            PromptPointOptions ppo = new PromptPointOptions(promptStr);
            // 使回车和空格键有效
            ppo.AllowNone = true;
            // 添加字符是的相应的字符按键有效
            for (int i = 0; i < keyWord.Length; i++)
            {
                ppo.Keywords.Add(keyWord[i]);
            }
            // 取消系统自动关键字显示
            ppo.AppendKeywordsToMessage = false;
            // 设置基准点
            ppo.BasePoint = pointBase;
            ppo.UseBasePoint = true;
            return ed.GetPoint(ppo);
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值