繁体   English   中英

AutoCAD从BlockReference获取长度。

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM