C# 按色选择块 AutoCAD二次开发

这个代码段实现了一个功能,允许用户在AutoCAD环境中按颜色选择图形对象。首先,它获取当前文档和数据库,然后启动一个事务。用户被提示选择一个对象,之后所有与选中对象颜色相同的图层和对象都会被选中。最后,选中的对象数量会被显示出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  /// <summary>
        /// 按色选择
        /// </summary>
        public void SelectionColor()
        {

            // 1) Database Get, Document, Editor.
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            SetFocus(doc.Window.Handle);
            using (DocumentLock docLock = doc.LockDocument())
            {

                // 2) Using Transaction
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {


                    PromptEntityResult per = ed.GetEntity("请选择对象颜色");
                    if (per.Status == PromptStatus.OK)
                    {
                        Entity ent = trans.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
                        LayerTableRecord ltr = (LayerTableRecord)(ent.LayerId).GetObject(OpenMode.ForWrite);

                        var dbDBEntity = db.GetEntsInModelSpace();
                        var objs = (from t in dbDBEntity
                                    where (t.EntityColor.ColorIndex.Equals(ent.EntityColor.ColorIndex)) && (((LayerTableRecord)(t.LayerId).GetObject(OpenMode.ForWrite)).Color.ColorIndex.Equals(ltr.Color.ColorIndex))
                                    select t);

                        //System.Windows.Forms.MessageBox.Show(ent.Layer + "\n" + objs.Count().ToString());
                        
 
                        //设置图层的颜色

                        //ed.WriteMessage("\n\n块表记录颜色" + ltr.Color.ColorIndex+"\n");



                        if (objs != null)
                        {
                            ed.SetImpliedSelection(objs.Select(en => en.ObjectId).ToArray());

                        }
                        ed.WriteMessage("\n\n" + ent.EntityColor.ColorIndex + " -----> 共计" + objs.Count().ToString() + "个\n");
                        
                    }

                    trans.Commit();
                }

            }

            //MainUI.Instance.Visibility = System.Windows.Visibility.Visible;

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值