private static void ConvertInAutoMode(DocumentModifier docMdf) { var texts = SelectTexts(docMdf); if (texts == null || texts.Length == 0) { return; } // 将选择的文字按Y坐标排序 var sortedTexts = new SortedDictionary <double, Entity>(); double maxWidth = 0; foreach (var txtId in texts) { double width = 0; var txt = txtId.GetObject(OpenMode.ForRead) as Entity; if (txt is DBText) { var dt = txt as DBText; if (!sortedTexts.ContainsKey(dt.Position.Y)) { // width = dt.TextString.Length * dt.Height * dt.WidthFactor * 1.05; // 1.1 为放大系数 maxWidth = Math.Max(maxWidth, width); sortedTexts.Add(dt.Position.Y, dt); } } else if (txt is MText) { var mt = txt as MText; if (!sortedTexts.ContainsKey(mt.Location.Y)) { width = mt.ActualWidth; maxWidth = Math.Max(maxWidth, width); sortedTexts.Add(mt.Location.Y, mt); } } } var sb = new StringBuilder(); var textsUd = sortedTexts.Reverse().ToArray(); // 第一个元素的Y坐标值最大,表示在最上方 foreach (var v in textsUd) { var txt = v.Value; if (txt is DBText) { sb.Append(TextUtils.ConvertDbTextSpecialSymbols((txt as DBText).TextString) + @"\P"); } else if (txt is MText) { sb.Append((txt as MText).Contents + @"\P"); } } // var txtHeight = 0.0; var location = new Point3d(); Entity topText = textsUd[0].Value; if (topText is DBText) { var dt = (topText as DBText); txtHeight = dt.Height; location = new Point3d(dt.Position.X, dt.Position.Y + dt.Height, dt.Position.Z); } else if (topText is MText) { txtHeight = (topText as MText).TextHeight; location = (topText as MText).Location; } // 以只读方式打开块表 Open the Block table for read var acBlkTbl = docMdf.acTransaction.GetObject(docMdf.acDataBase.BlockTableId, OpenMode.ForRead) as BlockTable; // 以写方式打开模型空间块表记录 Open the Block table record Model space for write var btr = docMdf.acTransaction.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; var mTxt = new MText() { Location = location, Width = maxWidth, TextHeight = txtHeight, LineSpacingFactor = 0.85, Contents = sb.ToString(), }; // 刷格式 mTxt.SetPropertiesFrom(topText); // btr.AppendEntity(mTxt); docMdf.acTransaction.AddNewlyCreatedDBObject(mTxt, true); // 删除原来的文字 foreach (var ent in sortedTexts.Values) { ent.UpgradeOpen(); ent.Erase(true); } }