CAD二次开发仿系统直线命令

该代码段展示了一个在CAD环境中使用C#实现的直线命令,允许用户交互地绘制直线并支持闭合或放弃操作。用户首先指定起点,然后连续点击或输入关键字‘C’闭合或‘U’放弃线条。程序会动态更新直线的集合对象lineList。
摘要由CSDN通过智能技术生成

 [CommandMethod("Line1")]
        public void Line1()
        {
            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;
}
LineJig lineJig;
PromptResult pr;

判断循环是否继续

bool isContinue = true;
 while (isContinue)
            {
                //只要有两条直线就闭合
                if (lineList.Count > 1)
                {
                 
                    lineJig = new LineJig(pointPre, "\n指定下一点或 [闭合(C)/放弃(U)]:",new string[] { "C","U"});
                    pr = ed.Drag(lineJig);

                }
                else
                {
                
                    lineJig = new LineJig(pointPre, "\n指定下一点或 [放弃(U)]:", new string[] { "U" });
                    pr = ed.Drag(lineJig);
                }

                //Point3d pointNext;
                if (pr.Status == PromptStatus.Cancel)
                {
                    return;
                }
                if (pr.Status == PromptStatus.None)
                {
                    return;
                }
                if (pr.Status == PromptStatus.OK)
                {
                 
                    Line line = (Line)lineJig.GetEntity();
                    pointPre = line.EndPoint;
                    ObjectId lId = db.AddEntityToModelSpace(line);
                    lineList.Add(lId);
                }
                if (pr.Status == PromptStatus.Keyword)
                {
                    switch (pr.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);
                                lId.EraseEntity();
                            }
                            break;
                        case "C":
                            lineList.Add(db.AddLineToModelSpace(pointPre, pointStart));
                            isContinue = false;
                            //return;
                            break;
                        case " ":
                            isContinue = false;
                            break;
                             
                        default:
                            break;
                    }
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

周杰伦fans

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

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

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

打赏作者

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

抵扣说明:

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

余额充值