CAD2016获得当前布局代码C#

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qqider/article/details/71124601

CAD2016获得当前布局代码(C#)

常规代码是这么写的:

            string curSpaceName = string.Empty;
            using (Transaction trans = curDoc.Database.TransactionManager.StartTransaction())
            {
                string layoutName = LayoutManager.Current.CurrentLayout;
                Layout curLayout = trans.GetObject(LayoutManager.Current.GetLayoutId(layoutName), OpenMode.ForRead) as Layout;
                BlockTableRecord curSpace = curLayout.BlockTableRecordId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                curSpaceName = curSpace.Name;
            }

可是发现在cad2016下LayoutManager.Current.GetLayoutId(layoutName)出来的布局id是空的!!!很是费解。
后来饶了些弯路,只能遍历出来多加了个获得布局空间的函数如下:

        public static ObjectId GetLayoutId(Database db, string name)
        {
            ObjectId layoutId = new ObjectId();
            BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
            foreach (ObjectId btrId in bt)
            {
                BlockTableRecord btr = (BlockTableRecord)btrId.GetObject(OpenMode.ForRead);
                if (btr.IsLayout)
                {
                    Layout layout = (Layout)btr.LayoutId.GetObject(OpenMode.ForRead);
                    if (layout.LayoutName.CompareTo(name) == 0)
                    {
                        layoutId = btr.LayoutId;
                        break;
                    }
                }
            }
            return layoutId;
        }
展开阅读全文

没有更多推荐了,返回首页