[英]How to access AutoCad elements / get xyz coordinates from Autocad
[英]AutoCAD Get Length from BlockReference.
問題
我很難從AutoCAD中的BlockReference獲取長度。 我以某種方式成為數學新手時得到了Widh&Height,但是到目前為止,我無法獲得BlockReference的Length。 有沒有一種方法可以獲取BlockReference的長度。 Ive瀏覽了AutoCad API,但沒有成功。 也許有人可以告訴我方向。
我做了什么
public static double GetBlockWidthAndHeight(BlockReference blockReference) {
try {
var db = HostApplicationServices.WorkingDatabase;
var blockname = blockReference.Name;
double width = 0;
using (var tr = db.TransactionManager.StartTransaction()) {
var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(blockname)) {
return 0;
}
var btrec = (BlockTableRecord)tr.GetObject(bt[blockname], OpenMode.ForRead, false);
Extents3d? bounds;
bounds = btrec.Bounds;
if (bounds.HasValue) {
var ext = bounds.Value;
width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
}
else {
var bref = new BlockReference(Point3d.Origin, bt[blockname]);
bounds = bref.Bounds;
var ext = bounds.Value;
width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
bref.Dispose();
}
tr.Commit();
}
return width;
}
catch (Exception ex) {
Debug.WriteLine(ex.Message);
}
return 0;
}
您的塊參照了3D對象嗎? 如果是這樣的話。 我注意到您當前沿X軸(寬度)和Y軸(高度)獲得對象的邊界,但缺少使用Z軸。 如果“塊引用”是2D對象,則繼續執行您描述的方法將不起作用,因為該信息根本不存在。
您也可以嘗試在AutoCAD“屬性”面板下查看“塊參考”的屬性。 根據創建塊引用的方式,您可能已經可以通過API簡單地訪問其尺寸值。
下面是基恩Wamsley的博客鏈接給出如何利用API直接訪問該區塊信息的一些簡短的例子- http://through-the-interface.typepad.com/through_the_interface/2009/03/accessing-the-properties- -動態-autocad-block-using-net.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.