1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
public static void InsertExcelSheetIntoCAD(this Document doc, string filePath, string sheetName, Point3d insertPoint, double tableWidth,out double tableHeight,string fontStyleName = "Standard", int textColorIndex = 3, int lineColorIndex = 3, string layoutName = null) { tableHeight = 0.0; Database db = doc.Database; var table = doc.InsertExcelSheet0(filePath, sheetName, insertPoint, tableWidth, layoutName); if (table == null) return; var tablebox = table.GeometricExtents; tableHeight = tablebox.MaxPoint.Y - tablebox.MinPoint.Y;
var styleId = db.GetTextStyleIdByName(fontStyleName, out double textSize, out double widthFactor); if (styleId == ObjectId.Null) return;
using (Transaction transaction = doc.TransactionManager.StartTransaction()) { DBObjectCollection objs = new DBObjectCollection(); table.Explode(objs); List<Entity> entities = new List<Entity>(); foreach (DBObject obj in objs) { if (obj is Entity) { var entity = (Entity)obj; if (entity is MText mText) { DBObjectCollection childObjs = new DBObjectCollection(); mText.Explode(childObjs);
foreach (var childObj in childObjs) { if (childObj is DBText dbtext) { dbtext.TextString = dbtext.TextString.Trim(); dbtext.TextStyleId = styleId; dbtext.ColorIndex = textColorIndex; entities.Add(dbtext); } } } else { entity.ColorIndex = lineColorIndex; entities.Add(entity); } } } DwgPainter.DeleteEntity(db, table.ObjectId); DwgPainter.AddEntities(db, entities, layoutName); transaction.Commit(); } }
|