//获取文字样式,有则返回文字样式ObjectId,没有则创建并返回文字样式ObjectId
public static ObjectId GetTextStyleTableId(string tbname)
{
ObjectId txtstyle = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTable tst = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
//foreach (ObjectId item in tst)
//{
// TextStyleTableRecord tstr = (TextStyleTableRecord)tr.GetObject(item, OpenMode.ForWrite);
// if (tbname.Equals(tstr.Name)) txtstyle = item;
//}
if (tst.Has(tbname)) txtstyle = tst[tbname];
else
{
TextStyleTableRecord tstr = new TextStyleTableRecord
{
Name = tbname,
TextSize = 3,
PriorSize = 3,
XScale = 0.7,
Font = new FontDescriptor("宋体", false, false, 134, 2)
};
tst.UpgradeOpen();//切换的文字样式表的状态为写以添加新的的文字样式
tst.Add(tstr);//将新建的文字样式表记录的信息添加到文字样式表中
//把的文字样式表记录添加到事务处理中
tr.AddNewlyCreatedDBObject(tstr, true);
tst.DowngradeOpen();//为了安全,将文字样式表的状态切换为读
tr.Commit();
txtstyle = tstr.ObjectId;
}
}
return txtstyle;
}
//CAD 数据库包含以下9个符号表及符号表记录:
// BlockTable(块表)-BlockTableRecord(块表记录)
// LayerTable(图层表)-LayerTableRecord(图层表记录)
// TextStyleTable(文字样式表)-TextStyleTableRecord(文字样式表记录)
// DimStyleTable(尺寸样式表)-DimStyleTableRecord(尺寸样式表记录)
// LinetypeTable(线型表)-LinetypeTableRecord(线型表记录)
// UcsTable(用户坐标系表)-UcsTableRecord用户坐标系表记录)
// ViewTable(视图表)-ViewTableRecord(视图表记录)
// ViewportTable(视口表)-ViewportTableRecord(视口表记录)
// RegAppTable(应用程序注册表)-RegAppTableRecord(应用程序注册表记录)
/// <summary>
/// 获取符合过滤条件的符号表记录名字集合
/// </summary>
/// <typeparam name="TTable">符号表</typeparam>
/// <typeparam name="TRecord">符号表记录</typeparam>
/// <param name="tableid"></param>
/// <returns>记录的名字集合</returns>
public static List<string> GetRecordNames<TTable, TRecord>(ObjectId tableid) where TTable : SymbolTable where TRecord : SymbolTableRecord
{
List<string> lst = new List<string>();
bool filter(TRecord tstr)
{
return !string.IsNullOrEmpty(tstr.Name) && !Regex.IsMatch(tstr.Name, "(\\*.*|.*\\|.*)") && (!(tstr is LayerTableRecord tstrl) || !tstrl.IsLocked);
}
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
TTable tst = (TTable)tr.GetObject(tableid, OpenMode.ForWrite);
lst.AddRange(from ObjectId id in tst
let record = (TRecord)tr.GetObject(id, OpenMode.ForWrite)
where filter(record)
select record.Name);
}
return lst;
}
=======================================================================================================================================================
/// <summary>
/// 获取符合过滤条件的符号表记录名字集合
/// </summary>
/// <typeparam name="TTable">符号表</typeparam>
/// <typeparam name="TRecord">符号表记录</typeparam>
/// <param name="tableid"></param>
/// <returns>记录的名字集合</returns>
public static IEnumerable<string> GetRecordNames<TTable, TRecord>(ObjectId tableid) where TTable : SymbolTable where TRecord : SymbolTableRecord
{
bool filter(TRecord tstr)
{
return !string.IsNullOrEmpty(tstr.Name) && !Regex.IsMatch(tstr.Name, "(\\*.*|.*\\|.*)") && (!(tstr is LayerTableRecord tstrl) || !tstrl.IsLocked);
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TTable tst = (TTable)tr.GetObject(tableid, OpenMode.ForWrite);
foreach (ObjectId id in tst)
{
TRecord record = (TRecord)tr.GetObject(id, OpenMode.ForWrite);
if (filter(record))
{
yield return record.Name;
}
}
}
}