C# 以编程方式将表格(左下)添加到Autocad 2015中的(0,0,0)
这是我的程序,用于打开excel表并在所提到的点以编程方式创建。当我指定(0,0)时,它会添加左上角。但我想要左下角。代码是什么 这是我的节目C# 以编程方式将表格(左下)添加到Autocad 2015中的(0,0,0),c#,autocad-plugin,C#,Autocad Plugin,这是我的程序,用于打开excel表并在所提到的点以编程方式创建。当我指定(0,0)时,它会添加左上角。但我想要左下角。代码是什么 这是我的节目 [CommandMethod("exl")] static public void TableFromSpreadsheet() { const string dlName = "Excel to Autocad"; var doc = Application.DocumentManager.MdiActiveDocument;
[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();
}
[CommandMethod(“exl”)]
静态公共void TableFromSpreadsheet()
{
常量字符串dlName=“Excel到Autocad”;
var doc=Application.DocumentManager.MdiActiveDocument;
var db=文档数据库;
var ed=文档编辑器;
var ofd=新建OpenFileDialog(“选择要链接的Excel电子表格”,null,“xls;xlsx”,“ExcelFileToLink”,OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles);
var dr=ofd.ShowDialog();
if(dr!=System.Windows.Forms.DialogResult.OK)
返回;
ed.WriteMessage(“\n所选文件为\“{0}\”。包含以下工作表:”,ofd.Filename);
var sheetNames=GetSheetNames(ofd.Filename);
如果(sheetNames.Count==0)
{
ed.WriteMessage(“\n工作簿不包含任何工作表。”);
返回;
}
对于(int i=0;i
}您可以尝试这种扩展方法(没有更多的时间来测试它…)
//
///重新计算左下角坐标的位置。
///必须在GenerateLayout()之后调用。
///
///表对象
///左下角所需位置
public static void MoveToBottomLeft(此表待定,Point3d bottomLeft)
{
Point3d maxPoint=tbl.Bounds.Value.maxPoint;//这是左下角(最小点)
Point3d topLeft=新的Point3d(maxPoint.Y,bottomLeft.Y,bottomLeft.Z);//这应该是topLeft
Point3d newPosition=bottomLeft.TransformBy(Matrix3d.Displacement(bottomLeft.getvectoro(newPosition));//将bottomLeft移动到newPosition
tbl.Position=newPosition;//应用新位置
}
我相信这个位置是固定的,您需要在GenerateLayout之后使用BoundingBox重新计算位置。如果您能向我解释一下BoundingBox,我会很高兴的。提前感谢!!!谢谢你的时间!!再一次
/// <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
}