- 积分
- 2848
- 明经币
- 个
- 注册时间
- 2004-7-28
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2020-10-24 22:54:08
|
显示全部楼层
本帖最后由 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;
- }
|
|