本帖最后由 默写容颜 于 2020-10-30 09:06 编辑
以下代码来自雪山飞狐提供
只能找到CAD缺失自带的字体库,无法找到Windows底下的fons字体,全部提示miss缺失,哪个大神能帮忙改进下
[CommandMethod("t11", CommandFlags.Session)]
public static void Test11()
{
var doc = Application.DocumentManager.Open(@"D:\Downloads\fm.dwg");
var db = doc.Database;
var ed = doc.Editor;
var hostapp = HostApplicationServices.Current;
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var tstable = db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
foreach (ObjectId id in tstable)
{
var tstr = id.GetObject(OpenMode.ForRead) as TextStyleTableRecord;
ed.WriteMessage
(
"\n{0}:{1},{2}",
tstr.Name,
FindFontFile(db, tstr.FileName),
FindFontFile(db, tstr.BigFontFileName)
);
}
}
}
ed.WriteMessage("\n");
}
public static string FindFontFile(Database db, string name)
{
var hostapp = HostApplicationServices.Current;
if (name == "")
return null;
string fullname = "";
try
{
fullname =
hostapp.FindFile
(
name,
db,
FindFileHint.FontFile
);
}
catch
{
fullname = name + " Missing";
}
return fullname;
}
网友答: 本帖最后由 mkhsj928 于 2020-10-24 23:06 编辑

网友答: 同求大神给予解决,谢谢
网友答:




网友答:
非常感谢,非常感谢,非常感谢网友答:

学习了,谢谢!网友答:
刚好这个方法帮我解决了问题,非常感谢
网友答:
用teigha写了一个批量替换指定文件夹下面的字体的程序 不过针对的是我需要的三种字体网友答:
本帖最后由 默写容颜 于 2021-1-24 15:14 编辑
1、CAD目录下有ltypeshp.shx形文件,但它不是字体文件,有可能是作图的人将其他字体改为这名字,无法替换
2、系统明明有新宋体和已经安装的字体如微软雅黑,它也是给你替换宋体
3、就是有些图纸它就是明明要繁体字体,这个字体我系统也是有的,它也是按你的指定宋体强制替换了网友答: 本帖最后由 箭头_Row 于 2025-7-13 16:43 编辑
獲取下系統字體,比對下系統字體名稱,有則支持蹼過。

以下代码来自雪山飞狐提供
只能找到CAD缺失自带的字体库,无法找到Windows底下的fons字体,全部提示miss缺失,哪个大神能帮忙改进下
[CommandMethod("t11", CommandFlags.Session)]
public static void Test11()
{
var doc = Application.DocumentManager.Open(@"D:\Downloads\fm.dwg");
var db = doc.Database;
var ed = doc.Editor;
var hostapp = HostApplicationServices.Current;
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var tstable = db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
foreach (ObjectId id in tstable)
{
var tstr = id.GetObject(OpenMode.ForRead) as TextStyleTableRecord;
ed.WriteMessage
(
"\n{0}:{1},{2}",
tstr.Name,
FindFontFile(db, tstr.FileName),
FindFontFile(db, tstr.BigFontFileName)
);
}
}
}
ed.WriteMessage("\n");
}
public static string FindFontFile(Database db, string name)
{
var hostapp = HostApplicationServices.Current;
if (name == "")
return null;
string fullname = "";
try
{
fullname =
hostapp.FindFile
(
name,
db,
FindFileHint.FontFile
);
}
catch
{
fullname = name + " Missing";
}
return fullname;
}
网友答: 本帖最后由 mkhsj928 于 2020-10-24 23:06 编辑

- <div class="blockcode"><blockquote>/// <summary>
- /// 文字样式校正
- /// </summary>
- [CommandMethod("TSTVF")]
- public static void MTextStyleVerify()
- {
- string sysFontsPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);//windows系统字体目录
- DirectoryInfo sysDirInfo = new DirectoryInfo(sysFontsPath);//Windows系统字体文件夹
- Document CurrentDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
- Database CurrentDB = CurrentDoc.Database;
- Editor CurrentEd = CurrentDoc.Editor;
- using (Transaction tr = CurrentDoc.TransactionManager.StartTransaction())
- {
- TextStyleTable tst = (TextStyleTable)tr.GetObject(CurrentDB.TextStyleTableId, OpenMode.ForRead, false);
- int n = 1;
- foreach (ObjectId tstid in tst)
- {
- using (TextStyleTableRecord tstr = (TextStyleTableRecord)tr.GetObject(tstid, OpenMode.ForWrite, false))
- {
- #region 校正windows系统字体
- if (tstr.Font.TypeFace != string.Empty)
- {
- string FontFileFullName = string.Empty;
- FileInfo[] fis = sysDirInfo.GetFiles(tstr.FileName);
- if (fis.Length > 0)
- {
- FontFileFullName = fis[0].FullName;
- }
- else
- {
- FontFileFullName = FindFontFile(CurrentDB, tstr.FileName);
- }
- if (FontFileFullName != string.Empty)
- {
- using (PrivateFontCollection fc = new PrivateFontCollection())
- {
- try
- {
- fc.AddFontFile(FontFileFullName);
- //更正文字样式的字体名
- if (fc.Families[0].Name != tstr.Font.TypeFace)
- {
- tstr.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(
- fc.Families[0].Name, tstr.Font.Bold, tstr.Font.Italic,
- tstr.Font.CharacterSet, tstr.Font.PitchAndFamily
- );
- }
- }
- catch (System.Exception e)
- {
- CurrentEd.WriteMessage($"\n***错误***:{FontFileFullName}-{e.Message}");
- }
- }
- }
- else
- {
- //字体缺失,则用宋体代替
- tstr.FileName = "SimSun.ttf";
- tstr.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("宋体", false, false, 134, 2);
- }
- }
- #endregion
- #region 校正shx字体
- else
- {
- if (!tstr.IsShapeFile &&
- FindFontFile(CurrentDB, tstr.FileName) == string.Empty)
- {
- tstr.FileName = "romans.shx";//用romans.shx代替
- }
- if (tstr.BigFontFileName != string.Empty &&
- FindFontFile(CurrentDB, tstr.BigFontFileName) == string.Empty)
- {
- tstr.BigFontFileName = "gbcbig.shx";//用gbcbig.shx代替
- }
- }
- #endregion
- }
- }
- tr.Commit();
- }
- CurrentEd.Regen();
- CurrentEd.UpdateScreen();
- }
- private static string FindFontFile(Database db, string name)
- {
- var hostapp = HostApplicationServices.Current;
- if (name == "") return string.Empty;
- string fullname = string.Empty;
- try
- {
- fullname = hostapp.FindFile(name, db, FindFileHint.FontFile);
- }
- catch { }
- return fullname;
- }
网友答: 同求大神给予解决,谢谢
网友答:





网友答:
mkhsj928 发表于 2020-10-24 22:54
非常感谢,非常感谢,非常感谢网友答:


学习了,谢谢!网友答:
mkhsj928 发表于 2020-10-24 22:54
刚好这个方法帮我解决了问题,非常感谢
网友答:
用teigha写了一个批量替换指定文件夹下面的字体的程序 不过针对的是我需要的三种字体网友答:
本帖最后由 默写容颜 于 2021-1-24 15:14 编辑 mkhsj928 发表于 2020-10-24 22:54
1、CAD目录下有ltypeshp.shx形文件,但它不是字体文件,有可能是作图的人将其他字体改为这名字,无法替换
2、系统明明有新宋体和已经安装的字体如微软雅黑,它也是给你替换宋体
3、就是有些图纸它就是明明要繁体字体,这个字体我系统也是有的,它也是按你的指定宋体强制替换了网友答: 本帖最后由 箭头_Row 于 2025-7-13 16:43 编辑
默写容颜 发表于 2021-1-24 15:10
1、CAD目录下有ltypeshp.shx形文件,但它不是字体文件,有可能是作图的人将其他字体改为这名字,无法替换 ...
獲取下系統字體,比對下系統字體名稱,有則支持蹼過。

-
- using System.Drawing.Text;
- internal static List<string> GetSystemFontNames()
- {
- using var fonts = new InstalledFontCollection();
- return [.. fonts.Families.Select(f => f.Name).Distinct().OrderBy(n => n)];
- }
- private static readonly List<string> IgnoreFonts = GetSystemFontNames();
- // 跳过系统默认字体:宋體、黑體、楷體、仿宋等
- if (IgnoreFonts.Contains(textStyle.Font.TypeFace))
- continue;