- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
A generic extension method to import SymbolTableRecord objects from a dwg (or dwt) file into a Database.
The ImportSymbolTableRecords() method extends the Database type, that's to say it can be called with the dot(.) operator from a Database instance.
It is generic because it can target any SymbolTable using a Type parameter.
The requiered arguments are the source file full path and ParamArray of strings (one or more SymbolTableRecord names).
The function returns an ObjectIdCollection containing the ObjectId of the cloned SymbolTableRecords (null/Nothing if none have been imported).
C#
Extension methods have to be defined within a static class, they have to be static and their first argument must use the 'this' keyword and be the same type as the extended one.
public static class Extension
{
public static ObjectIdCollection ImportSymbolTableRecords<T>(
this Database targetDb,
string sourceFile,
params string[] recordNames)
where T : SymbolTable
{
using (Database sourceDb = new Database())
{
sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, false, "");
ObjectId sourceTableId, targetTableId;
switch (typeof(T).Name)
{
case "BlockTable":
sourceTableId = sourceDb.BlockTableId;
targetTableId = targetDb.BlockTableId;
break;
case "DimStyleTable":
sourceTableId = sourceDb.DimStyleTableId;
targetTableId = targetDb.DimStyleTableId;
break;
case "LayerTable":
sourceTableId = sourceDb.LayerTableId;
targetTableId = targetDb.LayerTableId;
break;
case "LinetypeTable":
sourceTableId = sourceDb.LinetypeTableId;
targetTableId = targetDb.LinetypeTableId;
break;
case "RegAppTable":
sourceTableId = sourceDb.RegAppTableId;
targetTableId = targetDb.RegAppTableId;
break;
case "TextStyleTable":
sourceTableId = sourceDb.TextStyleTableId;
targetTableId = targetDb.TextStyleTableId;
break;
case "UcsTable":
sourceTableId = sourceDb.UcsTableId;
targetTableId = targetDb.UcsTableId;
break;
case "ViewTable":
sourceTableId = sourceDb.ViewportTableId;
targetTableId = targetDb.ViewportTableId;
break;
case "ViewportTable":
sourceTableId = sourceDb.ViewportTableId;
targetTableId = targetDb.ViewportTableId;
break;
default:
throw new ArgumentException("Requires a concrete type derived from SymbolTable");
}
using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
{
T sourceTable = (T)tr.GetObject(sourceTableId, OpenMode.ForRead);
ObjectIdCollection idCol = new ObjectIdCollection();
foreach (string name in recordNames)
{
if (sourceTable.Has(name))
{
idCol.Add(sourceTable[name]);
}
}
if (idCol.Count == 0)
return null;
IdMapping idMap = new IdMapping();
sourceDb.WblockCloneObjects(
idCol, targetTableId, idMap, DuplicateRecordCloning.Replace, false);
tr.Commit();
ObjectIdCollection retVal = new ObjectIdCollection();
foreach (ObjectId id in idCol)
{
if (idMap[id].IsCloned)
{
retVal.Add(idMap[id].Value);
}
}
return retVal.Count == 0 ? null : retVal;
}
}
}
} |
|