public static class Synchronizer
{
[CommandMethod("syncdrawings")]
public static void SynchronizeDrawings()
{
List<string> fileNames = Drawing.SelectDrawings();
if (fileNames.Count == 0)
{
return;
}
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database destDb = acDoc.Database;
ObjectId idMSpace = ObjectId.Null;
var layerResourcePath = PluginConfigurator.GetResourcePath("layer");
List<string> layersToSync = EntityProcessor.GetLayersToSync(layerResourcePath);
var blockListPath = PluginConfigurator.GetResourcePath("block");
List<string> blockNames = EntityProcessor.GetBlockNames(blockListPath);
using (Transaction acTrans = destDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl = acTrans.GetObject(destDb.BlockTableId, OpenMode.ForRead) as BlockTable;
idMSpace = acBlkTbl[BlockTableRecord.ModelSpace];
var startTime = System.Diagnostics.Stopwatch.StartNew();
foreach (string fileName in fileNames)
{
using (Database sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
string areaNumber = null;
try
{
areaNumber = sourceDb.GetBuildingAreaNumber();
}
catch (NullReferenceException)
{
// Application.ShowAlertDialog("Set construction area number in source drawing by Drawing properties => subject");
}
DeletePreviousEntities(areaNumber, destDb);
using (IdMapping idMap = new IdMapping())
{
ObjectIdCollection iDrObjects = Drawing.GetObjects(sourceDb, layersToSync);
sourceDb.WblockCloneObjects(iDrObjects, idMSpace, idMap, DuplicateRecordCloning.Ignore, false);
ObjectIdCollection iDrObjects2 = new ObjectIdCollection();
foreach (ObjectId item in iDrObjects)
{
IdPair idpair = idMap.Lookup(item);
iDrObjects2.Add(idpair.Value);
}
Marker.AttachXDataToSelectionSetObjects(iDrObjects2, destDb, areaNumber);
}
}
startTime.Stop();
var resultTime = startTime.Elapsed;
string elapsedTime = String.Format("{0:00}:m{1:00}:s{2:00}.{3:000}",
resultTime.Hours,
resultTime.Minutes,
resultTime.Seconds,
resultTime.Milliseconds);
Logger logger = new Logger();
logger.CreateSynchronizationLog(fileName, elapsedTime);
startTime.Restart();
}
EntityProcessor.SetDynamicBlockProperty(destDb, blockNames);
acTrans.Commit();
}
}
public static void DeletePreviousEntities(string label, Database db)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
ObjectId btrId = bt[BlockTableRecord.ModelSpace];
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
foreach (ObjectId entId in btr)
{
Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;
if ((ent != null) && ent.GetXData2().Contains(label))
{
ent.UpgradeOpen();
ent.Erase();
ent.DowngradeOpen();
}
}
}
tr.Commit();
}
}
public static List<string> GetXData2(this DBObject item)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Transaction tr = doc.TransactionManager.StartTransaction();
List<string> xData = new List<string>();
using (tr)
{
DBObject obj = tr.GetObject(item.ObjectId, OpenMode.ForRead);
ResultBuffer rb = obj.XData;
if (rb != null)
{
foreach (TypedValue tv in rb)
{
xData.Add(tv.Value.ToString());
}
rb.Dispose();
}
}
return xData;
}
}