2016-04-05 1 views
0

Это моя программа, чтобы открыть таблицу excel & с программным созданием в указанной точке. Когда я укажу (0,0), он добавляет верхний левый угол. Но я хочу левый нижний угол. Как будет код?Добавление таблицы (внизу слева) программно к (0,0,0) в Autocad 2015

Это моя программа ..

[CommandMethod("exl")] 
static public void TableFromSpreadsheet() 
{ 
    const string dlName = "Excel to Autocad"; 
    var doc = Application.DocumentManager.MdiActiveDocument; 
    var db = doc.Database; 
    var ed = doc.Editor; 
    var ofd = new OpenFileDialog("Select Excel Spreadsheet to Link", null, "xls; xlsx", "ExcelFileToLink", OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles); 
    var dr = ofd.ShowDialog(); 
    if (dr != System.Windows.Forms.DialogResult.OK) 
     return; 
    ed.WriteMessage("\nFile selected was \"{0}\". Contains these sheets:", ofd.Filename); 
    var sheetNames = GetSheetNames(ofd.Filename); 
    if (sheetNames.Count == 0) 
    { 
     ed.WriteMessage("\nWorkbook doesn't contain any sheets."); 
     return; 
    } 
    for (int i = 0; i < sheetNames.Count; i++) 
    { 
     var name = sheetNames[i]; 
     ed.WriteMessage("\n{0} - {1}", i + 1, name); 
    } 
    var pio = new PromptIntegerOptions("\nSelect a sheet"); 
    pio.AllowNegative = false; 
    pio.AllowZero = false; 
    pio.DefaultValue = 1; 
    pio.UseDefaultValue = true; 
    pio.LowerLimit = 1; 
    pio.UpperLimit = sheetNames.Count; 
    var pir = ed.GetInteger(pio); 
    if (pir.Status != PromptStatus.OK) 
     return; 
    var ppr = ed.GetPoint("\nEnter table insertion point"); 
    if (ppr.Status != PromptStatus.OK) 
     return; 
    var dlm = db.DataLinkManager; 
    var dlId = dlm.GetDataLink(dlName); 
    if (dlId != ObjectId.Null) 
    { 
     dlm.RemoveDataLink(dlId); 
    } 
    var dl = new DataLink(); 
    dl.DataAdapterId = "AcExcel"; 
    dl.Name = dlName; 
    dl.Description = "Excel 2 Autocad"; 
    dl.ConnectionString = ofd.Filename + "!" + sheetNames[pir.Value - 1]; 
    dl.DataLinkOption = DataLinkOption.PersistCache; 
    dl.UpdateOption |= (int)UpdateOption.AllowSourceUpdate; 
    dlId = dlm.AddDataLink(dl); 
    using (var tr = doc.TransactionManager.StartTransaction()) 
    { 
     tr.AddNewlyCreatedDBObject(dl, true); 
     var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); 
     var tb = new Table(); 
     tb.TableStyle = db.Tablestyle; 
     tb.Position = ppr.Value; 
     tb.Cells.SetDataLink(dlId, true); 
     tb.GenerateLayout(); 
     var btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 
     btr.AppendEntity(tb); 
     tr.AddNewlyCreatedDBObject(tb, true); 
     tr.Commit(); 
    } 

}

+0

Я считаю эту позицию фиксированной, и вам нужно будет пересчитать позицию с помощью BoundingBox после GenerateLayout. –

+0

Я был бы рад, если бы вы объяснили мне о BoundingBox. Заранее спасибо!!! –

ответ

0

Вы можете попробовать что-то вроде этого метода Extension (не было больше времени, чтобы проверить его ...)

/// <summary> 
/// Recalculate the position for a bottom left coordinate. 
/// Must be called after GenerateLayout(). 
/// </summary> 
/// <param name="tbl">Table object</param> 
/// <param name="bottomLeft">Bottom Left desired position</param> 
public static void MoveToBottomLeft(this Table tbl, Point3d bottomLeft) 
{ 
    Point3d maxPoint = tbl.Bounds.Value.MaxPoint; // this is the bottom left (min point) 
    Point3d topLeft = new Point3d(maxPoint.Y, bottomLeft.Y, bottomLeft.Z); // this should be topLeft 
    Point3d newPosition = bottomLeft.TransformBy(Matrix3d.Displacement(bottomLeft.GetVectorTo(newPosition))); // move bottomLeft to newPosition 
    tbl.Position = newPosition; // apply the new position 
} 
+0

Спасибо за ваше время! Еще раз. –