using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.IO;
[assembly: CommandClass(typeof(DrawingSynchronizer.Synchronizer))]
namespace DrawingSynchronizer
{
public class Synchronizer
{
[CommandMethod("syncdrawings"/*, CommandFlags.Session*/)]
public static void SynchronizeDrawings()
{
Drawing originDrawing = new Drawing();
var originDrawings = originDrawing.GetDrawingDatabases(@"E:\sync\");
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table record for read
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
IdMapping idMap = new IdMapping();
foreach (var db in originDrawings)
{
db.WblockCloneObjects(originDrawing.GetObjects(db), acBlkTbl[BlockTableRecord.ModelSpace],
idMap, DuplicateRecordCloning.Ignore, false); // !!!!
db.Dispose(); // !!!!
}
//acCurDb.Save();
acTrans.Commit();
}
}
public static bool CheckEntityLayer(Entity entity)
{
if (Utilities.BasicLayers.layersToSync.Contains(entity.Layer))
{
return true;
}
else
{
return false;
}
}
}
class Drawing
{
public List<Database> GetDrawingDatabases(string path)
{
List<Database> originDrawings = new List<Database>();
try
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] Files = d.GetFiles("*.dwg");
foreach (FileInfo file in Files)
{
var fileName = Path.GetFileName(file.FullName);
string dwgFlpath = path + fileName;
Database db = new Database(false, true);
db.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
db.CloseInput(true);
originDrawings.Add(db);
}
}
catch (System.Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
return originDrawings;
}
public ObjectIdCollection GetObjects(Database db)
{
ObjectIdCollection objectIdCollection = new ObjectIdCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false, false);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (var item in btr)
{
Entity ent = (Entity)tr.GetObject(item, OpenMode.ForRead);
if (Synchronizer.CheckEntityLayer(ent))
{
objectIdCollection.Add(ent.ObjectId);
}
}
tr.Commit();
}
return objectIdCollection;
}
}
namespace Utilities
{
public static class BasicLayers
{
public const string Titles = "-10-Titles";
public const string Equipment = "-10-Equipment";
public const string Auxiliary = "-10-Auxiliary";
public const string Adapter1C = "-10-Adapter1C";
public const string Cabins = "-10-Cabins";
public const string FrameRulers = "-10-FrameRulers";
public const string Dimensions = "-10-Dimensions";
public const string Doors = "-10-Doors";
public const string Coverings = "-10-Coverings";
public const string Openings = "-10-Openings";
public const string Hull = "-10-Hull";
public const string Ladders = "-10-Ladders";
public const string Manholes = "-10-Manholes";
public const string FrameGrid = "-10-FrameGrid";
public const string PhantomLines = "-10-PhantomLines";
public const string Projections = "-10-Projections";
public const string Railing = "-10-Railings";
public const string RemovableFloors = "-10-RemovableFloors";
public const string SpaceDesignations = "-10-SpaceDesignations";
public const string BulkheadsAndDecks = "-10-Bulkheads&Decks";
public const string BulkheadsAndDecksThin = "-10-Bulkheads&DecksThin";
public const string Stiffeners = "-10-Stiffeners";
public const string SystemTraces = "-10-SystemTraces";
public const string Text = "-10-Text";
public const string Viewports = "-10-Viewports";
public const string Windows = "-10-Windows";
public const string ServiceAreas = "-10-ServiceAreas";
public const string HVAC = "-10-HVAC";
public const string Bioshield = "-10-Bioshield";
public static List<string> layersToSync = new List<string>
{
BulkheadsAndDecks, BulkheadsAndDecksThin, SpaceDesignations
};
}
}
}