本帖最后由 默写容颜 于 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 编辑
  1. <div class="blockcode"><blockquote>/// <summary>
  2.         /// 文字样式校正
  3.         /// </summary>
  4.         [CommandMethod("TSTVF")]
  5.         public static void MTextStyleVerify()
  6.         {
  7.             string sysFontsPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);//windows系统字体目录
  8.             DirectoryInfo sysDirInfo = new DirectoryInfo(sysFontsPath);//Windows系统字体文件夹

  9.             Document CurrentDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
  10.             Database CurrentDB = CurrentDoc.Database;
  11.             Editor CurrentEd = CurrentDoc.Editor;

  12.             using (Transaction tr = CurrentDoc.TransactionManager.StartTransaction())
  13.             {
  14.                 TextStyleTable tst = (TextStyleTable)tr.GetObject(CurrentDB.TextStyleTableId, OpenMode.ForRead, false);
  15.                 int n = 1;
  16.                 foreach (ObjectId tstid in tst)
  17.                 {
  18.                     using (TextStyleTableRecord tstr = (TextStyleTableRecord)tr.GetObject(tstid, OpenMode.ForWrite, false))
  19.                     {
  20.                         #region 校正windows系统字体
  21.                         if (tstr.Font.TypeFace != string.Empty)
  22.                         {
  23.                             string FontFileFullName = string.Empty;

  24.                             FileInfo[] fis = sysDirInfo.GetFiles(tstr.FileName);
  25.                             if (fis.Length > 0)
  26.                             {
  27.                                 FontFileFullName = fis[0].FullName;
  28.                             }
  29.                             else
  30.                             {
  31.                                 FontFileFullName = FindFontFile(CurrentDB, tstr.FileName);
  32.                             }

  33.                             if (FontFileFullName != string.Empty)
  34.                             {
  35.                                 using (PrivateFontCollection fc = new PrivateFontCollection())
  36.                                 {
  37.                                     try
  38.                                     {
  39.                                         fc.AddFontFile(FontFileFullName);

  40.                                         //更正文字样式的字体名
  41.                                         if (fc.Families[0].Name != tstr.Font.TypeFace)
  42.                                         {
  43.                                             tstr.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(
  44.                                                 fc.Families[0].Name, tstr.Font.Bold, tstr.Font.Italic,
  45.                                                 tstr.Font.CharacterSet, tstr.Font.PitchAndFamily
  46.                                                 );
  47.                                         }
  48.                                     }
  49.                                     catch (System.Exception e)
  50.                                     {
  51.                                         CurrentEd.WriteMessage($"\n***错误***:{FontFileFullName}-{e.Message}");
  52.                                     }
  53.                                 }
  54.                             }
  55.                             else
  56.                             {
  57.                                 //字体缺失,则用宋体代替
  58.                                 tstr.FileName = "SimSun.ttf";
  59.                                 tstr.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("宋体", false, false, 134, 2);
  60.                             }
  61.                         }
  62.                         #endregion
  63.                         #region 校正shx字体
  64.                         else
  65.                         {
  66.                             if (!tstr.IsShapeFile &&
  67.                                 FindFontFile(CurrentDB, tstr.FileName) == string.Empty)
  68.                             {
  69.                                 tstr.FileName = "romans.shx";//用romans.shx代替
  70.                             }

  71.                             if (tstr.BigFontFileName != string.Empty &&
  72.                                 FindFontFile(CurrentDB, tstr.BigFontFileName) == string.Empty)
  73.                             {
  74.                                 tstr.BigFontFileName = "gbcbig.shx";//用gbcbig.shx代替
  75.                             }
  76.                         }
  77.                         #endregion
  78.                     }
  79.                 }

  80.                 tr.Commit();
  81.             }

  82.             CurrentEd.Regen();
  83.             CurrentEd.UpdateScreen();
  84.         }

  85.         private static string FindFontFile(Database db, string name)
  86.         {
  87.             var hostapp = HostApplicationServices.Current;

  88.             if (name == "") return string.Empty;

  89.             string fullname = string.Empty;
  90.             try
  91.             {
  92.                 fullname = hostapp.FindFile(name, db, FindFileHint.FontFile);
  93.             }
  94.             catch { }

  95.             return fullname;
  96.         }



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


网友答:

网友答:
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形文件,但它不是字体文件,有可能是作图的人将其他字体改为这名字,无法替换 ...

獲取下系統字體,比對下系統字體名稱,有則支持蹼過。




  1. using System.Drawing.Text;

  2. internal static List<string> GetSystemFontNames()
  3.     {
  4.         using var fonts = new InstalledFontCollection();
  5.         return [.. fonts.Families.Select(f => f.Name).Distinct().OrderBy(n => n)];
  6.     }



  7. private static readonly List<string> IgnoreFonts = GetSystemFontNames();

  8.    // 跳过系统默认字体:宋體、黑體、楷體、仿宋等
  9.    if (IgnoreFonts.Contains(textStyle.Font.TypeFace))
  10.        continue;

  • 上一篇:插入带特殊属性的块的问题
  • 下一篇:没有了