2016-04-05 12 views
0

Dies ist mein Programm zum Öffnen einer Excel-Tabelle & programmgesteuert an einem angegebenen Punkt erstellen. Wenn ich (0,0) spezifiziere, fügt es die obere linke Ecke hinzu. Aber ich möchte die linke untere Ecke. Wie wäre der Code?Programmatisch hinzufügen (0,0,0) in Autocad 2015

Das ist mein Programm ..

[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

Ich glaube, dass diese Position fest ist und Sie die Position neu berechnen müssen, indem Sie die BoundingBox nach GenerateLayout verwenden. –

+0

Ich würde mich freuen, wenn Sie mich über BoundingBox erklären. Danke im Voraus!!! –

Antwort

0

Sie so etwas wie diese Erweiterung Methode versuchen kann (nicht mehr Zeit, es zu testen ...)

/// <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

Danke für Ihre Zeit !! Noch einmal. –

Verwandte Themen