美文网首页
CAD二次开发:修改图块名

CAD二次开发:修改图块名

作者: BIMnotes | 来源:发表于2020-08-11 15:47 被阅读0次

最近做一个把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();
            }
        }

相关文章

网友评论

      本文标题:CAD二次开发:修改图块名

      本文链接:https://www.haomeiwen.com/subject/zfpxhktx.html