CAD二次开发:修改图块名教程

最近做一个把CAD图块转换为Revit模型的功能,需要对大量的图块进行重命名的操作,AutoCAD提供的方法需要从整个块表中找到要重命名的图块,效率实在太低,就自己写了一个工具。

环境配置

吐槽:感觉CAD二次开发的环境配置要比Revit麻烦的多。先要根据CAD的版本,把需要的开发工具理清楚,安装包找全,安装好,一天就过去了。

环境:ObjectARX2014+wizards2014+AutoCAD2014+VS2012

代码

选择图块,获取图块名称

            var doc = Application.DocumentManager.MdiActiveDocument;

            PromptSelectionOptions options = new PromptSelectionOptions();
            options.SingleOnly = true;
            var result = doc.Editor.GetSelection();
            string oldName=null;
            if (result.Status == PromptStatus.OK)
            {
                var db = HostApplicationServices.WorkingDatabase;
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    ObjectId[] idArray = result.Value.GetObjectIds();

                    foreach (ObjectId blkId in idArray)
                    {
                        BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead);
                        if (blkRef != null)
                        {
                            oldName=blkRef.Name;
                        }
                    }
                    tr.Commit();
                }
            }

调出窗口,输入新的图块名称

            Window1 win = new Window1();
            Application.ShowModalWindow(win);
            newName = win.newName;
            RenameBlock(oldName, newName);
            win.Close();

修改图块名称

        public void RenameBlock(string oldName, string newName)
        {
            if (string.IsNullOrEmpty(oldName) || string.IsNullOrEmpty(newName))
            {
                return;
            }
            // get the working Database 
            var db = HostApplicationServices.WorkingDatabase;
            // start a transaction
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // open the block table 
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                // check if the block table contains the block to rename 
                if (bt.Has(oldName))
                {
                    // check if the block table already contains a block named as the new name 
                    if (bt.Has(newName))
                    {
                        Application.ShowAlertDialog("A block named " + newName + "already exits");
                    }
                    else
                    {
                        // open the block definition
                        var btr = (BlockTableRecord)tr.GetObject(bt[oldName], OpenMode.ForWrite);
                        // rename the bloc 
                        btr.Name = newName;
                    }
                }
                else
                {
                    Application.ShowAlertDialog("Block " + oldName + " not found");
                }
                tr.Commit();
            }
        }
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空