AutoCAD二次开发(.Net)之动态块

时间:2024-04-07 07:20:50

1、进入块编辑页面,添加可见性到块附近

AutoCAD二次开发(.Net)之动态块

2、点击可见性状态,添加可见性状态,如下图

AutoCAD二次开发(.Net)之动态块

3、根据定义的可见性,选择要显示的图形,右击-->对象可见性-->在当前状态中显示,更改当前的可见性状态(红色框选部分),对应显示隐藏图形即可。

AutoCAD二次开发(.Net)之动态块

4、动态块的读取

        public static void InitialZwBlockReference()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            string fileName =  "动态块.dwg";
            ObjectIdCollection oids = new ObjectIdCollection();
            ObjectIdCollection newoids = new ObjectIdCollection();
            IdMapping idMap = new IdMapping();
            //将动态块拷贝到当前document,生成块定义
            using (Database srcDb = new Database(false, false))
            {
                srcDb.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
                srcDb.CloseInput(true);
                using (Transaction tr = srcDb.TransactionManager.StartTransaction())
                {
                    BlockTable blockTbl = tr.GetObject(srcDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace1 = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId oid in modelSpace1)
                    {
                        oids.Add(oid);
                    }
                }
                srcDb.WblockCloneObjects(oids, db.CurrentSpaceId, idMap, DuplicateRecordCloning.Ignore, false);
                foreach (ObjectId oid in oids)
                {
                    try
                    {
                        if (!newoids.Contains(idMap[oid].Value))
                        {
                            newoids.Add(idMap[oid].Value);
                        }
                    }
                    catch { }

                }
            }
            //设置显示的值
            using (DocumentLock dl = doc.LockDocument())
            {
                using (Transaction trans = doc.TransactionManager.StartTransaction())
                {
                    Entity ent = trans.GetObject(newoids[0], OpenMode.ForWrite) as Entity;
                    if (ent is BlockReference)
                    {
                        CheckBlockPropertyValue((BlockReference)ent,"圆");
                    }
                    trans.Commit();
                }
            }
        }
        



        //根据想要显示的值去显示
        public static void CheckBlockPropertyValue(BlockReference br, string blockName)
        {
            DynamicBlockReferencePropertyCollection properties = br.DynamicBlockReferencePropertyCollection;
            for (int i = 0; i < properties.Count; i++)
            {
                DynamicBlockReferenceProperty property = properties[i];
                foreach (object item in property.GetAllowedValues())
                {
                    if (item.ToString() == blockName)
                    {
                        property.Value = item;
                    }
                }
            }
        }