/// <summary>
/// Import Dimension StyleRecords, either all or nominated names.
/// </summary>
/// <param name="sourceCADFilePath">Fully qualified file name, either .DWG or .DXF</param>
/// <param name="styleNames">List of StyleNames OR null for all. </param>
/// <returns></returns>
internal static bool ImportDimensionStyleFromFile(string sourceCADFilePath, List<string> styleNames = null)
{
var doc = CadApp.DocumentManager.MdiActiveDocument;
var db = doc.Database;
if (!File.Exists(sourceCADFilePath) )
{ doc.Editor.WriteMessage($"File Exists Failure for {sourceCADFilePath}");
return false;
}
//else, proceed
using (var sourceDb
= new Database
(false,
true)) {
ObjectIdCollection idsForAction
= new ObjectIdCollection
();
string fileExtension = System.IO.Path.GetExtension(sourceCADFilePath);
string fileName = System.IO.Path.GetFileNameWithoutExtension(sourceCADFilePath);
try
{
if (fileExtension == ".dxf")
sourceDb.DxfIn(sourceCADFilePath, null);
else if (fileExtension == ".dwg")
sourceDb.ReadDwgFile(sourceCADFilePath,
FileOpenMode.OpenForReadAndReadShare,
false, null);
else
{
return false;
}
}
catch (System.Exception)
{
return false;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DimStyleTable dimstyleTable = (DimStyleTable)tr.GetObject(
db.DimStyleTableId, OpenMode.ForWrite);
using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction())
{
DimStyleTable sourceDimStyleTable = (DimStyleTable)sourceTr.GetObject(
sourceDb.DimStyleTableId, OpenMode.ForRead);
// either nominated name or all, providing it exists in source an not in target.
foreach (ObjectId id in sourceDimStyleTable)
{
DimStyleTableRecord sourceStyleRecord = (DimStyleTableRecord)sourceTr.GetObject(
id, OpenMode.ForRead);
if (!(sourceStyleRecord
is null) && (styleNames
is null || styleNames
.Contains(sourceStyleRecord
.Name)) && !(dimstyleTable.Has(sourceStyleRecord.Name)))
{
idsForAction.Add(id);
}
}
}
if (idsForAction.Count != 0)
{
IdMapping iMap
= new IdMapping
(); db.WblockCloneObjects(idsForAction,
db.DimStyleTableId,
iMap,
DuplicateRecordCloning.Ignore,
false);
}
tr.Commit();
}
}
return true;
}