using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using IFoxCAD.Cad;
namespace DYH.Action
{
public class 字体替代
{
[CommandMethod("DYH_ZTTD")]
public void ZTTD()
{
using (var tr = new DBTrans())//建事务
{
var host = HostApplicationServices.Current;//获取当前host
var itst = tr.TextStyleTable.GetRecords();//拿到当前图形所有的文字样式表记录的集合
foreach (var tstr in itst)//遍历每一个文字样式
{
if (tstr.Font.TypeFace == "")//判断文字样式没有使用windows字体
{
using (tstr.ForWrite())//只读转为可写模式
{
try
{
//利用host的支持文件搜索路径查找本地是否存在该文字样式使用的大字体,如果找不到会报错进入catch
host.FindFile(tstr.BigFontFileName, Env.Database, FindFileHint.CompiledShapeFile);
}
catch
{
//找不到的话将它替换为任意一个CAD自带的大字体,我这里使用GBHZFS
Env.Editor.WriteMessage("\n文字样式 <" + tstr.Name + "> 未找到大字体—— <" + tstr.BigFontFileName + "> 替换为GBHZFS");//命令栏输出提示语
tstr.BigFontFileName = "GBHZFS";
}
try//shx字体同理,只是BigFontFileName换成FileName,字体使用SIMPLEX
{
host.FindFile(tstr.FileName, Env.Database, FindFileHint.CompiledShapeFile);
}
catch
{
Env.Editor.WriteMessage("\n文字样式 <" + tstr.Name + "> 未找到shx字体—— <" + tstr.FileName + "> 替换为SIMPLEX");//命令栏输出提示语
tstr.FileName = "SIMPLEX";
}
}
}
}
Env.Editor.Regen();//最后要刷新一下图纸
}
}
}
}