C# CAD交互界面-自定义面板集-comboBox选择图层

本文详细描述了一个在VisualStudio2022环境中使用C#编写的AutoCAD命令SelectLayer,它通过ComboBox让用户选择图层,展示了如何获取图层列表并处理用户输入。
摘要由CSDN通过智能技术生成

  运行环境Visual Studio 2022 c# cad2016

一、代码说明 

  • SelectLayer方法是一个自定义的AutoCAD命令方法,通过[CommandMethod("SelectLayer")]进行标记。
  • 方法首先获取当前活动文档,并检查是否有效。
  • 创建一个名为"SelectLayer"的PaletteSet,并设置其样式以显示自动隐藏和关闭按钮。
  • 初始化一个ComboBox,并遍历GetLayerList()方法返回的所有图层记录,将图层名添加到ComboBox的Items集合中。
  • 创建一个OK按钮,绑定Click事件处理程序,在点击时读取ComboBox中选定项的文本内容,并显示所选图层名称的警告消息。
  • 将ComboBox和OK按钮放入数组中,并将这个数组添加到PaletteSet的控件集中。
  • 最后,使PaletteSet可见。

GetLayerList()方法:

  • 该方法负责从数据库中获取所有图层表记录。
  • 使用事务安全地打开数据库,并读取LayerTable对象。
  • 遍历LayerTable中的每个ObjectId,将其转换为LayerTableRecord对象并添加到列表中。
  • 提交事务并返回包含所有图层记录的列表。

二、完整代码 

internal class layername
{
    [CommandMethod("SelectLayer")]
    public void SelectLayer()
    {
        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        if (doc != null)
        {
            PaletteSet ps = new PaletteSet("Select Layer");
            ps.Style = PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.ShowCloseButton;
            ps.Size = new System.Drawing.Size(200, 100);
            System.Windows.Forms.ComboBox comboBox = new System.Windows.Forms.ComboBox();
            foreach (LayerTableRecord layer in GetLayerList())
            {
                comboBox.Items.Add(layer.Name);
            }

            System.Windows.Forms.Button okButton = new System.Windows.Forms.Button();
            okButton.Text = "OK";
            okButton.Click += (sender, e) =>
            {
                string selectedLayer = comboBox.SelectedItem.ToString();
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Selected Layer: " + selectedLayer);
            };


            System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
            panel.Controls.Add(comboBox);
            panel.Controls.Add(okButton);
            ps.Add("Select Layer", panel);
            ps.Visible = true;
        }
    }

    private IEnumerable<LayerTableRecord> GetLayerList()
    {
        List<LayerTableRecord> layers = new List<LayerTableRecord>();

        using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
        {
            LayerTable layerTable = (LayerTable)tr.GetObject(HostApplicationServices.WorkingDatabase.LayerTableId, OpenMode.ForRead);

            foreach (ObjectId id in layerTable)
            {
                LayerTableRecord layer = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead);
                layers.Add(layer);
            }

            tr.Commit();
        }

        return layers;
    }
}

//感谢大家的点赞,收藏,转发,关注  

  • 20
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值