Hi,
You can use or get inspiration from these extension methods.
/// <summary>
/// Imports SymbolTableRecords whose names match the template supplied from the specified file.
/// </summary>
/// <typeparam name="T">Type of SymbolTable.</typeparam>
/// <param name="targetDb">Instance to which the method applies.</param>
/// <param name="sourceFileName">Complete path of the source file.</param>
/// <param name="pattern">WcMatch pattern.</param>
/// <param name="cloning">Input action for duplicate records.</param>
/// <returns>A dictionary containing the names and ObjectIds of the imported records.</returns>
/// <exception cref="ArgumentNullException"></exception>
public static Dictionary<string, ObjectId> ImportRecords<T>(
this Database targetDb, string sourceFileName, string pattern, DuplicateRecordCloning cloning)
where T : SymbolTable
{
if (targetDb is null)
throw new ArgumentNullException(nameof(targetDb));
Dictionary<string, ObjectId> records;
using (var sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(sourceFileName, FileOpenMode.OpenForReadAndAllShare, false, null);
using (Transaction tr = sourceDb.TransactionManager.StartOpenCloseTransaction())
{
var sourceTable = (T)tr.GetObject(sourceDb.GetTableId<T>(), OpenMode.ForRead);
records = sourceTable
.Cast<ObjectId>()
.Select(id => (SymbolTableRecord)tr.GetObject(id, OpenMode.ForRead))
.Where(r => Autodesk.AutoCAD.Internal.Utils.WcMatchEx(r.Name, pattern, true))
.ToDictionary(r => r.Name, r => r.ObjectId);
var ids = new ObjectIdCollection(records.Values.ToArray());
var mapping = new IdMapping();
sourceDb.WblockCloneObjects(ids, targetDb.GetTableId<T>(), mapping, cloning, false);
tr.Commit();
}
}
using (var tr = targetDb.TransactionManager.StartOpenCloseTransaction())
{
var table = (T)tr.GetObject(targetDb.GetTableId<T>(), OpenMode.ForRead);
return records
.Where(r => table.Has(r.Key))
.ToDictionary(r => r.Key, r => table[r.Key]);
}
}
/// <summary>
/// Gets the ObjectId of the specified SymbolTable.
/// </summary>
/// <typeparam name="T">Type of SymbolTable.</typeparam>
/// <param name="db">Instance to which the method applies.</param>
/// <returns>The ObjectId of the specified SymbolTable.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name ="db"/> is null.</exception>
/// <exception cref="NotImplementedException"></exception>
public static ObjectId GetTableId<T>(this Database db) where T : SymbolTable
{
if (db is null) throw new ArgumentNullException(nameof(db));
switch (typeof(T).Name)
{
case nameof(BlockTable): return db.BlockTableId;
case nameof(DimStyleTable): return db.DimStyleTableId;
case nameof(LayerTable): return db.LayerTableId;
case nameof(LinetypeTable): return db.LinetypeTableId;
case nameof(RegAppTable): return db.RegAppTableId;
case nameof(TextStyleTable): return db.TextStyleTableId;
case nameof(UcsTable): return db.UcsTableId;
case nameof(ViewTable): return db.ViewTableId;
case nameof(ViewportTable): return db.ViewportTableId;
default:
throw new NotImplementedException();
}
}