原贴搬运清理dgn
//http://www.theswamp.org/index.php?topic=45030.msg502441#msg502441
-
- using System;
- using System.Runtime.InteropServices;
- //using Autodesk..ApplicationServices.Core;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using System.Collections.ObjectModel;
-
- namespace DgnPurger
- {
- public class Commands
- {
- const string dgnLsDefName = "DGNLSDEF";
- const string dgnLsDictName = "ACAD_DGNLINESTYLECOMP";
-
- public struct ads_name
- {
- public IntPtr a;
- public IntPtr b;
- };
-
- //[DllImport("acdb19.dll",
- [DllImport("acdb18.dll",
- CharSet = CharSet.Unicode,
- CallingConvention = CallingConvention.Cdecl,
- EntryPoint = "acdbHandEnt")]
- public static extern int acdbHandEnt(string h, ref ads_name n);
-
- [CommandMethod("DGNPURGE")]
- public void PurgeDgnLinetypes()
- {
- var doc =
- Application.DocumentManager.MdiActiveDocument;
- var db = doc.Database;
- var ed = doc.Editor;
-
- using (var tr = doc.TransactionManager.StartTransaction())
- {
- // Start by getting all the "complex" DGN linetypes
- // from the linetype table
-
- var linetypes = CollectComplexLinetypeIds(db, tr);
-
- // Store a count before we start removing the ones
- // that are referenced
-
- var ltcnt = linetypes.Count;
-
- // Remove any from the "to remove" list that need to be
- // kept (as they have references from objects other
- // than anonymous blocks)
-
- var ltsToKeep =
- PurgeLinetypesReferencedNotByAnonBlocks(db, tr, linetypes);
-
- // Now we collect the DGN stroke entries from the NOD
-
- var strokes = CollectStrokeIds(db, tr);
-
- // Store a count before we start removing the ones
- // that are referenced
-
- var strkcnt = strokes.Count;
-
- // Open up each of the "keeper" linetypes, and go through
- // their data, removing any NOD entries from the "to
- // remove" list that are referenced
-
- PurgeStrokesReferencedByLinetypes(tr, ltsToKeep, strokes);
-
- // Erase each of the NOD entries that are safe to remove
-
- int erasedStrokes = 0;
-
- foreach (ObjectId id in strokes)
- {
- try
- {
- var obj = tr.GetObject(id, OpenMode.ForWrite);
- obj.Erase();
- if (
- obj.GetRXClass().Name.Equals("AcDbLSSymbolComponent")
- )
- {
- EraseReferencedAnonBlocks(tr, obj);
- }
- erasedStrokes++;
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(
- "\nUnable to erase stroke ({0}): {1}",
- id.ObjectClass.Name,
- ex.Message
- );
- }
- }
-
- // And the same for the complex linetypes
-
- int erasedLinetypes = 0;
-
- foreach (ObjectId id in linetypes)
- {
- try
- {
- var obj = tr.GetObject(id, OpenMode.ForWrite);
- obj.Erase();
- erasedLinetypes++;
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(
- "\nUnable to erase linetype ({0}): {1}",
- id.ObjectClass.Name,
- ex.Message
|