简体   繁体   中英

AutoCAD Get Length from BlockReference.

The Problem

im struggling to get the Length from a BlockReference in AutoCAD. I somehow being a math noob got the Widh & Height but i cannot get the Length so far of a BlockReference. Is there a way of getting the Length of a BlockReference. Ive looked through the AutoCad API but without succsess. 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? 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. 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.

You can also try looking at the properties of the Block Reference under the AutoCAD 'Properties' palette. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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