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()
{
List<string> fileNames = GetDrawingsPath(@"E:\sync\");
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
ObjectId idMSpace = ObjectId.Null;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
idMSpace = acBlkTbl[BlockTableRecord.ModelSpace];
acTrans.Commit();
}
foreach (string fileName in fileNames)
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
using (IdMapping idMap = new IdMapping())
{
db.WblockCloneObjects(GetObjects(db), idMSpace, idMap, DuplicateRecordCloning.Ignore, false); // !!!!
}
}
}
//acCurDb.Save();
}
public static List<string> GetDrawingsPath(string path)
{
List<string> paths = new List<string>();
try
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] Files = d.GetFiles("*.dwg");
foreach (FileInfo file in Files)
{
string filePath = path + Path.GetFileName(file.FullName);
paths.Add(filePath);
}
}
catch (System.Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
return paths;
}
public static 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;
}
public static bool CheckEntityLayer(Entity entity)
{
if (Utilities.BasicLayers.layersToSync.Contains(entity.Layer))
{
return true;
}
else
{
return false;
}
}
}
}
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
};
}
}