コード例 #1
0
        private void EraseProxies(Database db)
        {
            RXClass  zombieEntity = RXClass.GetClass(typeof(ProxyEntity));
            RXClass  zombieObject = RXClass.GetClass(typeof(ProxyObject));
            ObjectId id;

            for (long l = db.BlockTableId.Handle.Value; l < db.Handseed.Value; l++)
            {
                if (!db.TryGetObjectId(new Handle(l), out id))
                {
                    continue;
                }
                if (id.ObjectClass.IsDerivedFrom(zombieObject) && !id.IsErased)
                {
                    try
                    {
                        using (DBObject proxy = id.Open(OpenMode.ForWrite))
                        {
                            proxy.Erase();
                        }
                    }
                    catch
                    {
                        using (DBDictionary newDict = new DBDictionary())
                            using (DBObject proxy = id.Open(OpenMode.ForWrite))
                            {
                                try
                                {
                                    proxy.HandOverTo(newDict, true, true);
                                }
                                catch { }
                            }
                    }
                }
                else if (id.ObjectClass.IsDerivedFrom(zombieEntity) && !id.IsErased)
                {
                    try
                    {
                        using (DBObject proxy = id.Open(OpenMode.ForWrite))
                        {
                            proxy.Erase();
                        }
                    }
                    catch { }
                }
            }
        }
コード例 #2
0
        public void searchAndExplodeProxy()
        {
            _NODEntriesForErase = new ObjectIdCollection();
            _explodedcount      = 0;
            Database db = Application.DocumentManager.
                          MdiActiveDocument.Database;

            using (Transaction trans = db.TransactionManager.
                                       StartTransaction())
            {
                // open block table
                BlockTable bt = trans.GetObject(db.BlockTableId,
                                                OpenMode.ForRead) as BlockTable;
                // for each block table record
                // (mspace, pspace, other blocks)
                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr = trans.GetObject(btrId,
                                                           OpenMode.ForRead) as BlockTableRecord;
                    // for each entity on this block table record
                    foreach (ObjectId entId in btr)
                    {
                        Entity ent = trans.GetObject(entId,
                                                     OpenMode.ForRead) as Entity;
                        if ((ent.IsAProxy))
                        {
                            try
                            {
                                ent.UpgradeOpen();

                                // Это коллекция объектов, которая будет включать все элементы взорванной прокси
                                DBObjectCollection objs = new DBObjectCollection();

                                // Взрываем блок в нашу коллекцию объектов
                                ent.Explode(objs);

                                // Открываем текущее пространство на запись
                                btr.UpgradeOpen();

                                // Пробегаем по коллекции объектов и
                                // каждый из них добавляем к текущему пространству
                                foreach (DBObject obj in objs)
                                {
                                    //преобразуем объект к Entity
                                    Entity entExplode = (Entity)obj;
                                    //Добавляем эту Entity в пространство
                                    btr.AppendEntity(entExplode);
                                    //Добавляем к транзакции новые объекты
                                    trans.AddNewlyCreatedDBObject(entExplode, true);
                                }
                                ;
                            }
                            catch (platformExeption ex)
                            {
                                //Если что-то сломалось, то в командную строку выводится ошибка
                                ed.WriteMessage("Не могу взорвать - ошибка: " + ex.Message);
                            };

                            try
                            {
                                //и удаляем
                                ent.Erase();
                                _explodedcount++;
                                //ed.WriteMessage(string.Format("\nВзорван прокси в {0}", btr.Name));
                            }
                            catch
                            {
                                ed.WriteMessage("Не могу удалить объект: " + ent.BlockName);
                            }
                        }
                    }
                }

                // now search for NOD proxy entries
                DBDictionary nod = trans.GetObject(
                    db.NamedObjectsDictionaryId,
                    OpenMode.ForRead) as DBDictionary;
                searchSubDictionary(trans, nod);
                trans.Commit();
            }

            // the HandOverTo operation must
            // be perfomed outside transactions
            foreach (ObjectId nodID in _NODEntriesForErase)
            {
                //replace with an empty Dic Entry
                DBDictionary newNODEntry = new DBDictionary();
#pragma warning disable
                DBObject NODEntry = nodID.Open(OpenMode.ForWrite);
#pragma warning enable
                try
                {
                    NODEntry.HandOverTo(newNODEntry, true, true);
                }
                catch { };

                // if we cannot close the OLD entry,
                // then is not database resident
#pragma warning disable
                //http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings
                try { NODEntry.Close(); }
#pragma warining enable
                catch
                {
                    //ed.WriteMessage("\nErased");
                }

                //close the new one
                try { newNODEntry.Close(); }
                catch { }
            }
            ed.WriteMessage("Взорвано Proxy: " + _explodedcount);
        }