public static void ChangePolylineLayers() { using (DatabaseCommands commands = new DatabaseCommands()) { #region Get Dwgs to Process var dwgs = commands.LoadandProcessPolys(); foreach (var dwg in dwgs) { DwDrawingStacks.Add(dwg); } #endregion GDwgPath = commands.GetGlobalDWGPath(); var doc = Application.DocumentManager.MdiActiveDocument; var ed = doc.Editor; foreach (var dwg in DwDrawingStacks) { string gpath = Convert.ToString(GDwgPath); if (gpath != null) { string path = Path.Combine(gpath, dwg.PolylineDwgName); if (path == null) { throw new ArgumentNullException(nameof(path)); } if (!File.Exists(path)) { DatabaseLogs.FormatLogs("File Not Found", path); return; } try { // We'll just suffix the selected filename with "-purged" // for the output location. This file will be overwritten // if the command is called multiple times //var output = // Path.GetDirectoryName(pfnr.StringResult) + "\\" + // Path.GetFileNameWithoutExtension(pfnr.StringResult) + // "-purged" + // Path.GetExtension(pfnr.StringResult); // Assume a post-R12 drawing using (var db = doc.Database) { ObjectIdCollection collection = GetIdsByTypeTypeValue("POLYLINE", "LWPOLYLINE", "POLYLINE2D", "POLYLINE3d"); //db.Save(); string date = dwg.DateStamp.Value.ToFileTime().ToString(); string output = Path.Combine(commands.GetGlobalDestinationPath(dwg.DateStamp.Value), String.Format("HOLES-{0}", date)); Pge.Common.Framework.FileUtilities.CreateDirectory(output); PLineToLayers.ProcessLayers(collection, db); output = Path.Combine(output, dwg.PolylineDwgName); doc.Database.SaveAs(output, DwgVersion.Current); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\nException: {0}", ex.Message); } } break; } } }
public static void LoadandProcessPolys() { using (DatabaseCommands commands = new DatabaseCommands()) { #region Get Dwgs to Process var dwgs = commands.LoadandProcessPolys(); foreach (var dwg in dwgs) { DwDrawingStacks.Add(dwg); } #endregion GDwgPath = commands.GetGlobalDWGPath(); var doc = Application.DocumentManager.MdiActiveDocument; var ed = doc.Editor; foreach (var dwg in DwDrawingStacks) { string gpath = Convert.ToString(GDwgPath); if (gpath != null) { string path = Path.Combine(gpath, dwg.PolylineDwgName); if (path == null) { throw new ArgumentNullException(nameof(path)); } if (!File.Exists(path)) { DatabaseLogs.FormatLogs("File Not Found", path); return; } try { // We'll just suffix the selected filename with "-purged" // for the output location. This file will be overwritten // if the command is called multiple times //var output = // Path.GetDirectoryName(pfnr.StringResult) + "\\" + // Path.GetFileNameWithoutExtension(pfnr.StringResult) + // "-purged" + // Path.GetExtension(pfnr.StringResult); // Assume a post-R12 drawing using (var db = new Autodesk.AutoCAD.DatabaseServices.Database(false, true)) { // Read the DWG file into our Database object db.ReadDwgFile( path, FileOpenMode.OpenForReadAndReadShare, false, "" ); // No graphical changes, so we can keep the preview // bitmap db.RetainOriginalThumbnailBitmap = true; // We'll store the current working database, to reset // after the purge operation var wdb = HostApplicationServices.WorkingDatabase; HostApplicationServices.WorkingDatabase = db; // Purge unused DGN linestyles from the drawing // (returns false if nothing is erased) ObjectIdCollection collection = GetIdsByTypeTypeValue("POLYLINE", "LWPOLYLINE", "POLYLINE2D", "POLYLINE3d"); CopyPolylinesBetweenDatabases(db, collection); // Still need to reset the working database HostApplicationServices.WorkingDatabase = wdb; wdb.Save(); string date = dwg.DateStamp.Value.ToFileTime().ToString(); string output = Path.Combine(commands.GetGlobalDestinationPath(dwg.DateStamp.Value), String.Format("HOLES-{0}", date)); FileUtilities.CreateDirectory(output); PLineToLayers.ProcessLayers(collection, wdb); output = Path.Combine(output, dwg.PolylineDwgName); doc.Database.SaveAs(output, DwgVersion.Current); // if (PurgeDgnLinetypesInDb(db, ed)) // { // // Check the version of the drawing to save back to // var ver = // (db.LastSavedAsVersion == DwgVersion.MC0To0 // ? DwgVersion.Current // : db.LastSavedAsVersion // ); // // Now we can save // string output = null; // db.SaveAs(output, ver); // ed.WriteMessage( // "\nSaved purged file to \"{0}\".", // output // ); // } // // Still need to reset the working database // HostApplicationServices.WorkingDatabase = wdb; } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\nException: {0}", ex.Message); } } break; } } }