我的兩台電腦是win10的,(可能這個問題也存在在xp.win7.win8.......畢竟十年前我就遇到過了.......)
一台電腦的cad字體設置為"宋體",另一台電腦打開之后就變成了問號,這很奇怪....
以至於我很煩惱....
搜了一波之后,這個鏈接說的是對的 https://blog.csdn.net/tisn05/article/details/53063702
但是他只是找到了解決方案,沒有找到問題原因
- 原因
我用了e大告訴我的方法,在cmd上用 dir c:\windows\fonts\*.* 檢查字體是否相同,
因為win字體文件夾有權限,所以我用pe (u盤裝機進入的小系統)來替換字體,
第一次直接替換了沒有受影響的電腦的所有字體,然后發現OK的.....
雖然這樣可以結束了,但是我要具體分析到是什么字體導致的,免得再次出現同類情況,
然后就是排除法,每次把所有字體都分成幾份,然后如果是錯的...就再分它....重復開關電腦十來次.....
經過一晚上的排查,發現了一個字體: KT_ST.ttf 導致的宋體問號....請大家刪掉它....
(話外題,宋體比新宋體好!!原因是宋體沒有字體偏移問題,詳見我 的字體問題集合)
- 代碼的解決方案:
我的c#代碼寫得比鏈接多了點東西,主要是兩台電腦相互保存的話,要能看見才行....
主函數: 部分代碼是替換新宋體到宋體...沒刪,懶.....
AutoGo.ConfigTxtStyle ="宋體"
public class Command_ChangNewSong : IAutoGo //接口是自動運行的接口,可以看我另一篇做這個接口的文章 { public void Initialize() { ChangNewSong();//自執行一次 Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(ChangNewSong); } public void Terminate() { } public static void ChangNewSong(object sender = null, DocumentCollectionEventArgs e = null) { try { using (Application.DocumentManager.MdiActiveDocument.LockDocument())//鎖文檔 { Database db = HostApplicationServices.WorkingDatabase;//當前的數據庫 ChangNewSongEnt(db);
//直接發送命令到cad命令欄 高版本發送命令刷新不了,只能發送延后的lisp...
SendToCad.SendLisp("(command \"REGENALL\")");
} } catch { } } public static void ChangNewSongEnt(Database db) { //更改新宋體為宋體 bool songticunzai = false;//SD_宋體 存在 bool songticunzaiNew = false;//SD_新宋體 存在 string ttc = ".TTF"; using (Transaction tr = db.TransactionManager.StartTransaction()) { var st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable; foreach (ObjectId id in st) { if (id.IsOk()) { var tstr = tr.GetObject(id, OpenMode.ForRead) as TextStyleTableRecord; if (tstr.Name == "SD_" + AutoGo.ConfigTxtStyle) { songticunzai = true; } if (tstr.Name == "SD_新宋體") { songticunzaiNew = true; } if (tstr.Font.TypeFace != "")//缺失字體 { string trypFace = tstr.Font.TypeFace.ToUpper(); //if (tstr.Name == "SD_宋體") //{ // System.Windows.Forms.MessageBox.Show($"SD_宋體用了這個:{trypFace}"); //} if (trypFace == "RS_Song".ToUpper() || trypFace == "SimSun".ToUpper()) { tr.AddTextStyle(db, tstr.Name, AutoGo.ConfigTxtStyle + ttc); } } } } if (!songticunzai) //沒有就是可以改名稱 { foreach (ObjectId id in st) { if (id.IsOk()) { var tstr = tr.GetObject(id, OpenMode.ForRead) as TextStyleTableRecord; if (tstr.Font.TypeFace == "新宋體" && tstr.Name == "SD_新宋體") { tstr.UpgradeOpen(); tstr.Name = "SD_" + AutoGo.ConfigTxtStyle; tstr.DowngradeOpen(); } if (tstr.Font.TypeFace == "新宋體") { tr.AddTextStyle(db, tstr.Name, AutoGo.ConfigTxtStyle + ttc); } } } } tr.Commit(); } //防止ctrl+u回滾事務出錯,改名字之后再改內容 using (Transaction tr = db.TransactionManager.StartTransaction()) { if (songticunzai) { if (songticunzaiNew) //判斷有沒有 "SD_新宋體" { #if !HC2019 //文字樣式 var stid = tr.AddTextStyle(db, "SD_新宋體", AutoGo.ConfigTxtStyle + ttc); #else //文字樣式 var stid = tr.AddTextStyleToDatabase(db,"SD_新宋體", AutoGo.ConfigTxtStyle); #endif } } else { #if !HC2019 //文字樣式 var stid = tr.AddTextStyle(db, "SD_" + AutoGo.ConfigTxtStyle, AutoGo.ConfigTxtStyle + ttc); #else //文字樣式 var stid = tr.AddTextStyleToDatabase(db,"SD_" + AutoGo.ConfigTxtStyle, AutoGo.ConfigTxtStyle); #endif } tr.Commit(); } } }
新建字體函數

/// <summary> /// 新建文字樣式 /// </summary> /// <param name="db"></param> /// <param name="name">樣式名</param> /// <param name="smallfont">字體名</param> /// <param name="bigfont">大字體</param> /// <param name="height">高度</param> /// <param name="xscale">寬度因子</param> /// <returns></returns> public static ObjectId AddTextStyle(this Transaction tr, Database db, string name, string smallfont, string bigfont = null, double xscale = 1, double height = 0) { ObjectId ida = ObjectId.Null; //獲取已有這個名字的文字樣式表記錄 var st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable; foreach (ObjectId id in st) { if (id.IsOk()) { var tstr = tr.GetObject(id, OpenMode.ForRead) as TextStyleTableRecord; if (tstr.Name == name)//有就打開,並修改內容 { if (!string.IsNullOrEmpty(smallfont))//如果字體不設置,就只返回 { tstr.UpgradeOpen(); tstr.FileName = smallfont; if (bigfont == null) { tstr.BigFontFileName = ""; } else { tstr.BigFontFileName = bigfont; } tstr.TextSize = height; tstr.XScale = xscale; tstr.DowngradeOpen(); } ida = id; break; } } } //沒有就新建一個 if (ida == ObjectId.Null) { TextStyleTableRecord tstr = new TextStyleTableRecord { Name = name, FileName = smallfont, TextSize = height, XScale = xscale }; if (bigfont == null) { tstr.BigFontFileName = ""; } else { tstr.BigFontFileName = bigfont; } st.UpgradeOpen(); ida = st.Add(tstr); tr.AddNewlyCreatedDBObject(tstr, true); st.DowngradeOpen(); } return ida; }