- UID
- 10108
- 积分
- 5956
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-9-17
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
源贴 http://www.theswamp.org/index.php?topic=33516.0
The gc-purge function requires an integer as argument which is the sum of the following binary codes:
1 Blocks
2 Layers
4 Shapes
8 Materials
16 Dimension styles
32 MLeader styles
64 Mline styles
128 Table style
256 TextStyle
512 Plot styles
1024 Visual styles
2048 Line types
4096 Registred applications
8192 Groups
16384 Scales
32768 Emply texts
65536 Zero length entities
131072 Raster images
262144 DWF underlays
524288 PDF underlays
1048576 DGN underlays
It returns T if success, nil if some error occur.
Using examples
(gc-purge 1) ; purges blocks
(gc-purge 19) ou (gc-purge (+ 1 2 16)) pruges blocks, layers and dimstyles
(gc-purge 2097151) purges all
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.GraphicsInte**ce;
- using Autodesk.AutoCAD.Runtime;
- using LispExtensionTools;
- namespace PurgeLisp
- {
- public class LispFun
- {
- /*
- * 1 Blocks
- * 2 Layers
- * 4 Shapes
- * 8 Materials
- * 16 Dimension styles
- * 32 MLeader styles
- * 64 Mline styles
- * 128 Table style
- * 256 TextStyle
- * 512 Plot styles
- * 1024 Visual styles
- * 2048 Line types
- * 4096 Registred applications
- * 8192 Groups
- * 16384 Scales
- * 32768 Emply texts
- * 65536 Zero length entities
- * 131072 Raster images
- * 262144 DWF underlays
- * 524288 PDF underlays
- * 1048576 DGN underlays
- */
- private string m_str;
- [LispFunction("gc-purge")]
- public TypedValue Purge(ResultBuffer resbuf)
- {
- TypedValue nil = new TypedValue((int)LispDataType.Nil);
- if (resbuf == null)
- {
- LispErrorMsg.TooFew();
- return nil;
- }
- TypedValue[] tvs = resbuf.AsArray();
- if (tvs.Length == 0)
- {
- LispErrorMsg.TooFew();
- return nil;
- }
- if (tvs.Length > 1)
- {
- LispErrorMsg.TooMuch();
- return nil;
- }
- TypedValue tv = tvs[0];
- if (tv.TypeCode != (int)LispDataType.Int16 && tv.TypeCode != (int)LispDataType.Int32)
- {
- LispErrorMsg.BadType("fixnump", tv);
- return nil;
- }
- int flag = Convert.ToInt32(tv.Value);
- try
- {
- PurgeAll(HostApplicationServices.WorkingDatabase, flag);
- return new TypedValue((int)LispDataType.T_atom);
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
- "\nApplication error: " + ex.Message + ex.StackTrace);
- return nil;
- }
- catch (System.Exception ex)
- {
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
- "\nSystème error: " + ex.Message + ex.StackTrace);
- return nil;
- }
- }
- private void PurgeAll(Database db, int flag)
- {
- ObjectIdCollection tableIds = new ObjectIdCollection();
- ObjectIdCollection dictIds = new ObjectIdCollection();
- if ((flag & 1) == 1)
- tableIds.Add(db.BlockTableId);
- if ((flag & 2) == 2)
- tableIds.Add(db.LayerTableId);
- if ((flag & 16) == 16)
- tableIds.Add(db.DimStyleTableId);
- if ((flag & 8) == 8)
- tableIds.Add(db.LinetypeTableId);
- if ((flag & 256) == 256)
- tableIds.Add(db.TextStyleTableId);
- if ((flag & 4096) == 4096)
- tableIds.Add(db.RegAppTableId);
- if ((flag & 64) == 64)
- dictIds.Add(db.MLStyleDictionaryId);
- if ((flag & 512) == 512)
- dictIds.Add(db.PlotStyleNameDictionaryId);
- if ((flag & 128) == 128)
- dictIds.Add(db.TableStyleDictionaryId);
- bool emptyTxt = (flag & 32768) == 32768;
- bool zeroLength = (flag & 65536) == 65536;
- if (emptyTxt || zeroLength)
- {
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- List<LayerTableRecord> locked = new List<LayerTableRecord>();
- LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
- foreach (ObjectId layerId in lt)
- {
- LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);
- if (ltr.IsLocked == true)
- {
- ltr.UpgradeOpen();
- ltr.IsLocked = false;
- locked.Add(ltr);
- }
- }
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- foreach (ObjectId btrId in bt)
- {
- BlockTableRecord btr =
- (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
- if (!btr.IsLayout)
- continue;
- foreach (ObjectId id in btr)
- {
- if (zeroLength)
- {
- Curve crv = tr.GetObject(id, OpenMode.ForRead) as Curve;
- if (crv != null)
- {
- if (crv.GetDistanceAtParameter(crv.EndParam) == 0.0)
- {
- crv.UpgradeOpen();
- crv.Erase();
- }
- continue;
- }
- }
- if (emptyTxt)
- {
- DBText txt = tr.GetObject(id, OpenMode.ForRead) as DBText;
- if (txt != null)
- {
- if (txt.TextString.Trim() == string.Empty)
- {
- txt.UpgradeOpen();
- txt.Erase();
- }
- continue;
- }
- MText mTxt = tr.GetObject(id, OpenMode.ForRead) as MText;
- if (mTxt != null && GetStripedMtextContents(mTxt).Trim() == string.Empty)
- {
- mTxt.UpgradeOpen();
- mTxt.Erase();
- }
- }
- }
- }
- foreach (LayerTableRecord ltr in locked)
- {
- ltr.IsLocked = true;
- }
- tr.Commit();
- }
- }
- while (PurgeDatabase(db, tableIds, dictIds, flag))
- continue;
- }
- private string GetStripedMtextContents(MText mt)
- {
- m_str = string.Empty;
- mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
- return m_str;
- }
- private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
- {
- m_str += fragment.Text;
- return MTextFragmentCallbackStatus.Continue;
- }
- private bool PurgeDatabase(Database db, ObjectIdCollection tableIds, ObjectIdCollection dictIds, int flag)
- {
- ObjectIdCollection ids = new ObjectIdCollection();
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- if ((flag & 8192) == 8192)
- {
- DBDictionary dict =
- (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead, false);
- foreach (DBDictionaryEntry entry in dict)
- {
- Group grp = (Group)tr.GetObject(entry.Value, OpenMode.ForRead, false);
- if (grp.NumEntities == 0)
- {
- grp.UpgradeOpen();
- grp.Erase();
- }
- }
- }
- DBDictionary NOD =
- (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
- Dictionary<int, string> dicts = new Dictionary<int, string>();
- dicts.Add(32, "ACAD_MLEADERSTYLE");
- dicts.Add(131072, "ACAD_IMAGE_DICT");
- dicts.Add(262144, "ACAD_DWFDEFINITIONS");
- dicts.Add(524288, "ACAD_PDFDEFINITIONS");
- dicts.Add(1048576, "ACAD_DGNDEFINITIONS");
- foreach (KeyValuePair<int, string> pair in dicts)
- {
- if ((flag & pair.Key) == pair.Key && NOD.Contains(pair.Value))
- dictIds.Add(NOD.GetAt(pair.Value));
- }
- foreach (ObjectId tableId in tableIds)
- {
- SymbolTable table = (SymbolTable)tr.GetObject(tableId, OpenMode.ForRead, false);
- foreach (ObjectId recordId in table)
- {
- ids.Add(recordId);
- }
- }
- foreach (ObjectId dictId in dictIds)
- {
- DBDictionary dict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForRead, false);
- foreach (DBDictionaryEntry entry in dict)
- {
- if (entry.Value.IsValid)
- {
- DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
- if (!obj.IsAProxy)
- ids.Add(entry.Value);
- }
- }
- }
- if ((flag & 16384) == 16384 && NOD.Contains("ACAD_SCALELIST"))
- {
- DBDictionary dict = (DBDictionary)tr.GetObject(NOD.GetAt("ACAD_SCALELIST"), OpenMode.ForRead);
- foreach (DBDictionaryEntry entry in dict)
- {
- if (entry.Key != "A0")
- {
- DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
- if (!obj.IsAProxy)
- ids.Add(entry.Value);
- }
- }
- }
- if ((flag & 8) == 8)
- {
- DBDictionary matDict =
- (DBDictionary)tr.GetObject(db.MaterialDictionaryId, OpenMode.ForRead, false);
- foreach (DBDictionaryEntry entry in matDict)
- {
- string key = entry.Key;
- if ((key != "ByBlock") && (key != "ByLayer") && (key != "Global"))
- {
- DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
- if (!obj.IsAProxy)
- ids.Add(entry.Value);
- }
- }
- }
- if ((flag & 1024) == 1024)
- {
- DBDictionary vsDict =
- (DBDictionary)tr.GetObject(db.VisualStyleDictionaryId, OpenMode.ForRead);
- foreach (DBDictionaryEntry entry in vsDict)
- {
- DBVisualStyle vs = tr.GetObject(entry.Value, OpenMode.ForRead) as DBVisualStyle;
- if (vs.Type == VisualStyleType.Custom)
- {
- DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
- if (!obj.IsAProxy)
- ids.Add(entry.Value);
- }
- }
- }
- if ((flag & 4) == 4)
- {
- TextStyleTable tst =
- (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
- foreach (ObjectId id in tst)
- {
- TextStyleTableRecord tstr =
- (TextStyleTableRecord)tr.GetObject(id, OpenMode.ForRead);
- if (tstr.IsShapeFile && !tstr.IsAProxy)
- ids.Add(id);
- }
- }
- db.Purge(ids);
- foreach (ObjectId id in ids)
- {
- DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForWrite);
- obj.Erase();
- }
- tr.Commit();
- }
- return ids.Count > 0;
- }
- }
- }
LispErrorMsg
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- namespace LispExtensionTools
- {
- public static class LispErrorMsg
- {
- static Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- internal static void TooFew()
- {
- ed.WriteMessage("\nError: too few arguments\n");
- }
- internal static void TooMuch()
- {
- ed.WriteMessage("\nError: too many arguments\n");
- }
- internal static void BadType(string T, TypedValue tV)
- {
- string msg = tV.TypeCode == (int)LispDataType.Nil ? "nil" : tV.Value.ToString();
- ed.WriteMessage("\nError: incorrect argument type: {0} {1}\n",T , msg);
- }
- internal static void Message(string msg)
- {
- ed.WriteMessage("\nError: {0}\n", msg);
- }
- }
- }
增加了面板的命令版本(Autocad 2010 - 2016),command : mypurge
|
|