简体   繁体   English

AutoCAD从BlockReference获取长度。

[英]AutoCAD Get Length from BlockReference.

The Problem 问题

im struggling to get the Length from a BlockReference in AutoCAD. 我很难从AutoCAD中的BlockReference获取长度。 I somehow being a math noob got the Widh & Height but i cannot get the Length so far of a BlockReference. 我以某种方式成为数学新手时得到了Widh&Height,但是到目前为止,我无法获得BlockReference的Length。 Is there a way of getting the Length of a BlockReference. 有没有一种方法可以获取BlockReference的长度。 Ive looked through the AutoCad API but without succsess. Ive浏览了AutoCad API,但没有成功。 Maybe someone can show me the Direction. 也许有人可以告诉我方向。

What ive Done 我做了什么

   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;
        }

Is your block reference a 3D object? 您的块参照了3D对象吗? If so. 如果是这样的话。 I noticed that you currently get the bounds of the object along the X axis (Width) and Y Axis (Height) but you're missing utilizing the Z Axis. 我注意到您当前沿X轴(宽度)和Y轴(高度)获得对象的边界,但缺少使用Z轴。 If the Block Reference is a 2D object, then going about the method you described won't work since that information simply isn't there. 如果“块引用”是2D对象,则继续执行您描述的方法将不起作用,因为该信息根本不存在。

You can also try looking at the properties of the Block Reference under the AutoCAD 'Properties' palette. 您也可以尝试在AutoCAD“属性”面板下查看“块参考”的属性。 Depending on how the block reference was made, there may already be values for it's dimensions that you can simply access via the API. 根据创建块引用的方式,您可能已经可以通过API简单地访问其尺寸值。

Here's a link to Kean Wamsley's blog giving some brief examples of how to utilize the API to access the block info directly - http://through-the-interface.typepad.com/through_the_interface/2009/03/accessing-the-properties-of-a-dynamic-autocad-block-using-net.html 下面是基恩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