找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 3245|回复: 4

[分享] Purge Lisp Function and CommandMethod

[复制链接]

已领礼包: 1268个

财富等级: 财源广进

发表于 2015-5-16 14:32:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
源贴 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


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.GraphicsInte**ce;
  7. using Autodesk.AutoCAD.Runtime;
  8. using LispExtensionTools;

  9. namespace PurgeLisp
  10. {
  11.     public class LispFun
  12.     {
  13.         /*
  14.          * 1            Blocks
  15.          * 2            Layers
  16.          * 4            Shapes
  17.          * 8            Materials
  18.          * 16            Dimension styles
  19.          * 32            MLeader styles
  20.          * 64            Mline styles
  21.          * 128            Table style
  22.          * 256            TextStyle
  23.          * 512            Plot styles
  24.          * 1024            Visual styles
  25.          * 2048            Line types
  26.          * 4096            Registred applications
  27.          * 8192            Groups
  28.          * 16384        Scales
  29.          * 32768        Emply texts
  30.          * 65536        Zero length entities
  31.          * 131072        Raster images
  32.          * 262144        DWF underlays
  33.          * 524288        PDF underlays
  34.          * 1048576        DGN underlays
  35.          */

  36.         private string m_str;

  37.         [LispFunction("gc-purge")]
  38.         public TypedValue Purge(ResultBuffer resbuf)
  39.         {
  40.             TypedValue nil = new TypedValue((int)LispDataType.Nil);
  41.             if (resbuf == null)
  42.             {
  43.                 LispErrorMsg.TooFew();
  44.                 return nil;
  45.             }
  46.             TypedValue[] tvs = resbuf.AsArray();
  47.             if (tvs.Length == 0)
  48.             {
  49.                 LispErrorMsg.TooFew();
  50.                 return nil;
  51.             }
  52.             if (tvs.Length > 1)
  53.             {
  54.                 LispErrorMsg.TooMuch();
  55.                 return nil;
  56.             }
  57.             TypedValue tv = tvs[0];
  58.             if (tv.TypeCode != (int)LispDataType.Int16 && tv.TypeCode != (int)LispDataType.Int32)
  59.             {
  60.                 LispErrorMsg.BadType("fixnump", tv);
  61.                 return nil;
  62.             }
  63.             int flag = Convert.ToInt32(tv.Value);
  64.             try
  65.             {
  66.                 PurgeAll(HostApplicationServices.WorkingDatabase, flag);
  67.                 return new TypedValue((int)LispDataType.T_atom);
  68.             }
  69.             catch (Autodesk.AutoCAD.Runtime.Exception ex)
  70.             {
  71.                 Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  72.                     "\nApplication error: " + ex.Message + ex.StackTrace);
  73.                 return nil;
  74.             }
  75.             catch (System.Exception ex)
  76.             {
  77.                 Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  78.                     "\nSystème error: " + ex.Message + ex.StackTrace);
  79.                 return nil;
  80.             }
  81.         }

  82.         private void PurgeAll(Database db, int flag)
  83.         {
  84.             ObjectIdCollection tableIds = new ObjectIdCollection();
  85.             ObjectIdCollection dictIds = new ObjectIdCollection();
  86.             if ((flag & 1) == 1)
  87.                 tableIds.Add(db.BlockTableId);
  88.             if ((flag & 2) == 2)
  89.                 tableIds.Add(db.LayerTableId);
  90.             if ((flag & 16) == 16)
  91.                 tableIds.Add(db.DimStyleTableId);
  92.             if ((flag & 8) == 8)
  93.                 tableIds.Add(db.LinetypeTableId);
  94.             if ((flag & 256) == 256)
  95.                 tableIds.Add(db.TextStyleTableId);
  96.             if ((flag & 4096) == 4096)
  97.                 tableIds.Add(db.RegAppTableId);
  98.             if ((flag & 64) == 64)
  99.                 dictIds.Add(db.MLStyleDictionaryId);
  100.             if ((flag & 512) == 512)
  101.                 dictIds.Add(db.PlotStyleNameDictionaryId);
  102.             if ((flag & 128) == 128)
  103.                 dictIds.Add(db.TableStyleDictionaryId);
  104.             bool emptyTxt = (flag & 32768) == 32768;
  105.             bool zeroLength = (flag & 65536) == 65536;
  106.             if (emptyTxt || zeroLength)
  107.             {
  108.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  109.                 {
  110.                     List<LayerTableRecord> locked = new List<LayerTableRecord>();
  111.                     LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  112.                     foreach (ObjectId layerId in lt)
  113.                     {
  114.                         LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);
  115.                         if (ltr.IsLocked == true)
  116.                         {
  117.                             ltr.UpgradeOpen();
  118.                             ltr.IsLocked = false;
  119.                             locked.Add(ltr);
  120.                         }
  121.                     }
  122.                     BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  123.                     foreach (ObjectId btrId in bt)
  124.                     {
  125.                         BlockTableRecord btr =
  126.                             (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
  127.                         if (!btr.IsLayout)
  128.                             continue;
  129.                         foreach (ObjectId id in btr)
  130.                         {
  131.                             if (zeroLength)
  132.                             {
  133.                                 Curve crv = tr.GetObject(id, OpenMode.ForRead) as Curve;
  134.                                 if (crv != null)
  135.                                 {
  136.                                     if (crv.GetDistanceAtParameter(crv.EndParam) == 0.0)
  137.                                     {
  138.                                         crv.UpgradeOpen();
  139.                                         crv.Erase();
  140.                                     }
  141.                                     continue;
  142.                                 }
  143.                             }
  144.                             if (emptyTxt)
  145.                             {
  146.                                 DBText txt = tr.GetObject(id, OpenMode.ForRead) as DBText;
  147.                                 if (txt != null)
  148.                                 {
  149.                                     if (txt.TextString.Trim() == string.Empty)
  150.                                     {
  151.                                         txt.UpgradeOpen();
  152.                                         txt.Erase();
  153.                                     }
  154.                                     continue;
  155.                                 }
  156.                                 MText mTxt = tr.GetObject(id, OpenMode.ForRead) as MText;
  157.                                 if (mTxt != null && GetStripedMtextContents(mTxt).Trim() == string.Empty)
  158.                                 {
  159.                                     mTxt.UpgradeOpen();
  160.                                     mTxt.Erase();
  161.                                 }
  162.                             }
  163.                         }
  164.                     }
  165.                     foreach (LayerTableRecord ltr in locked)
  166.                     {
  167.                         ltr.IsLocked = true;
  168.                     }
  169.                     tr.Commit();
  170.                 }
  171.             }
  172.             while (PurgeDatabase(db, tableIds, dictIds, flag))
  173.                 continue;
  174.         }

  175.         private string GetStripedMtextContents(MText mt)
  176.         {
  177.             m_str = string.Empty;
  178.             mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
  179.             return m_str;
  180.         }

  181.         private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
  182.         {
  183.             m_str += fragment.Text;
  184.             return MTextFragmentCallbackStatus.Continue;
  185.         }

  186.         private bool PurgeDatabase(Database db, ObjectIdCollection tableIds, ObjectIdCollection dictIds, int flag)
  187.         {
  188.             ObjectIdCollection ids = new ObjectIdCollection();
  189.             using (Transaction tr = db.TransactionManager.StartTransaction())
  190.             {
  191.                 if ((flag & 8192) == 8192)
  192.                 {
  193.                     DBDictionary dict =
  194.                         (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead, false);
  195.                     foreach (DBDictionaryEntry entry in dict)
  196.                     {
  197.                         Group grp = (Group)tr.GetObject(entry.Value, OpenMode.ForRead, false);
  198.                         if (grp.NumEntities == 0)
  199.                         {
  200.                             grp.UpgradeOpen();
  201.                             grp.Erase();
  202.                         }
  203.                     }
  204.                 }
  205.                 DBDictionary NOD =
  206.                         (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
  207.                 Dictionary<int, string> dicts = new Dictionary<int, string>();
  208.                 dicts.Add(32, "ACAD_MLEADERSTYLE");
  209.                 dicts.Add(131072, "ACAD_IMAGE_DICT");
  210.                 dicts.Add(262144, "ACAD_DWFDEFINITIONS");
  211.                 dicts.Add(524288, "ACAD_PDFDEFINITIONS");
  212.                 dicts.Add(1048576, "ACAD_DGNDEFINITIONS");
  213.                 foreach (KeyValuePair<int, string> pair in dicts)
  214.                 {
  215.                     if ((flag & pair.Key) == pair.Key && NOD.Contains(pair.Value))
  216.                         dictIds.Add(NOD.GetAt(pair.Value));
  217.                 }
  218.                 foreach (ObjectId tableId in tableIds)
  219.                 {
  220.                     SymbolTable table = (SymbolTable)tr.GetObject(tableId, OpenMode.ForRead, false);
  221.                     foreach (ObjectId recordId in table)
  222.                     {
  223.                         ids.Add(recordId);
  224.                     }
  225.                 }
  226.                 foreach (ObjectId dictId in dictIds)
  227.                 {
  228.                     DBDictionary dict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForRead, false);
  229.                     foreach (DBDictionaryEntry entry in dict)
  230.                     {
  231.                         if (entry.Value.IsValid)
  232.                         {
  233.                             DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
  234.                             if (!obj.IsAProxy)
  235.                                 ids.Add(entry.Value);
  236.                         }
  237.                     }
  238.                 }
  239.                 if ((flag & 16384) == 16384 && NOD.Contains("ACAD_SCALELIST"))
  240.                 {
  241.                     DBDictionary dict = (DBDictionary)tr.GetObject(NOD.GetAt("ACAD_SCALELIST"), OpenMode.ForRead);
  242.                     foreach (DBDictionaryEntry entry in dict)
  243.                     {
  244.                         if (entry.Key != "A0")
  245.                         {
  246.                             DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
  247.                             if (!obj.IsAProxy)
  248.                                 ids.Add(entry.Value);
  249.                         }
  250.                     }
  251.                 }
  252.                 if ((flag & 8) == 8)
  253.                 {
  254.                     DBDictionary matDict =
  255.                         (DBDictionary)tr.GetObject(db.MaterialDictionaryId, OpenMode.ForRead, false);
  256.                     foreach (DBDictionaryEntry entry in matDict)
  257.                     {
  258.                         string key = entry.Key;
  259.                         if ((key != "ByBlock") && (key != "ByLayer") && (key != "Global"))
  260.                         {
  261.                             DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
  262.                             if (!obj.IsAProxy)
  263.                                 ids.Add(entry.Value);
  264.                         }
  265.                     }
  266.                 }
  267.                 if ((flag & 1024) == 1024)
  268.                 {
  269.                     DBDictionary vsDict =
  270.                         (DBDictionary)tr.GetObject(db.VisualStyleDictionaryId, OpenMode.ForRead);
  271.                     foreach (DBDictionaryEntry entry in vsDict)
  272.                     {
  273.                         DBVisualStyle vs = tr.GetObject(entry.Value, OpenMode.ForRead) as DBVisualStyle;
  274.                         if (vs.Type == VisualStyleType.Custom)
  275.                         {
  276.                             DBObject obj = (DBObject)tr.GetObject(entry.Value, OpenMode.ForRead);
  277.                             if (!obj.IsAProxy)
  278.                                 ids.Add(entry.Value);
  279.                         }
  280.                     }
  281.                 }
  282.                 if ((flag & 4) == 4)
  283.                 {
  284.                     TextStyleTable tst =
  285.                         (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
  286.                     foreach (ObjectId id in tst)
  287.                     {
  288.                         TextStyleTableRecord tstr =
  289.                             (TextStyleTableRecord)tr.GetObject(id, OpenMode.ForRead);
  290.                         if (tstr.IsShapeFile && !tstr.IsAProxy)
  291.                             ids.Add(id);
  292.                     }
  293.                 }
  294.                 db.Purge(ids);
  295.                 foreach (ObjectId id in ids)
  296.                 {
  297.                     DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForWrite);
  298.                     obj.Erase();
  299.                 }
  300.                 tr.Commit();
  301.             }
  302.             return ids.Count > 0;
  303.         }
  304.     }
  305. }
LispErrorMsg


  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;

  5. namespace LispExtensionTools
  6. {
  7.     public static class LispErrorMsg
  8.     {
  9.         static Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  10.         internal static void TooFew()
  11.         {
  12.             ed.WriteMessage("\nError: too few arguments\n");
  13.         }

  14.         internal static void TooMuch()
  15.         {
  16.             ed.WriteMessage("\nError: too many arguments\n");
  17.         }

  18.         internal static void BadType(string T, TypedValue tV)
  19.         {
  20.             string msg = tV.TypeCode == (int)LispDataType.Nil ? "nil" : tV.Value.ToString();
  21.             ed.WriteMessage("\nError: incorrect argument type: {0} {1}\n",T , msg);
  22.         }

  23.         internal static void Message(string msg)
  24.         {
  25.             ed.WriteMessage("\nError: {0}\n", msg);
  26.         }
  27.     }
  28. }
增加了面板的命令版本(Autocad 2010 - 2016),command : mypurge

20150516142721.png

Release.rar

18.29 KB, 下载次数: 35, 下载积分: D豆 -1 , 活跃度 1

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 3198个

财富等级: 富可敌国

发表于 2015-5-16 15:44:55 | 显示全部楼层
支持大师!:handshake
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

已领礼包: 604个

财富等级: 财运亨通

发表于 2015-5-18 08:29:39 | 显示全部楼层
Purge.18.dll怎么用,我还认为是lsp文件呢。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 1268个

财富等级: 财源广进

 楼主| 发表于 2015-5-20 10:22:31 | 显示全部楼层
批量清理 Batch_purge For  2010 - 2012
batchpurge.jpg

BatchPurge.18.rar

10.55 KB, 下载次数: 20, 下载积分: D豆 -1 , 活跃度 1

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 112个

财富等级: 日进斗金

发表于 2015-11-24 18:06:56 | 显示全部楼层
未能找到类型或命名空间名称“LispFunction”(是否缺少 using 指令或程序集引用?)
请问在2016下还需要引用什么?程序在2008上能正常运行,但在引用2016的相应文件后,就出现以上提示,请问如何解决?
2015-11-24_180325.png


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-25 22:12 , Processed in 0.289798 second(s), 40 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表