Lisp to find length of a block

Lisp to find length of a block

smallƑish
Advocate Advocate
1,269 Views
14 Replies
Message 1 of 15

Lisp to find length of a block

smallƑish
Advocate
Advocate

Any possibility to find length of a block even, it is scaled block, Dynamic block with visibility or multiple strech,or just a normal block , or just a polyline or a line. Is there any chance to get its current state real length only ( if it's a rectangular shape, the length (bigger side) could be the result. If it's a polyline its complete length. Is that possible?

 

Update : Sample Blocks and objects attached 

0 Likes
Accepted solutions (4)
1,270 Views
14 Replies
Replies (14)
Message 2 of 15

komondormrex
Mentor
Mentor

explode a copy of a transformed block, grab the length needed, erase exploded.

Message 3 of 15

ВeekeeCZ
Consultant
Consultant

THIS is a quite simple approach, but might have some issues with blocks... try your blocks, if it work for you or not.

Message 4 of 15

Kent1Cooper
Consultant
Consultant
Accepted solution

You can use this:

 

(defun C:BB (/ minpt maxpt); Bounding Box of selected object
  (vla-getboundingbox (vlax-ename->vla-object (car (entsel))) 'minpt 'maxpt)
  (setq
    box (list (vlax-safearray->list minpt) (vlax-safearray->list maxpt))
    delta (mapcar '- (cadr box) (car box))
  ); setq
  (prompt
    (strcat
      "\nBounding Box [in 'box' variable]: " (vl-princ-to-string box) ";"
      "\nExtents: X direction " (rtos (car delta))
      "; Y direction " (rtos (cadr delta))
      "; Z direction " (rtos (caddr delta)) "."
    ); strcat
  ); prompt
  (princ)
)

 

and read [and/or put into a variable, etc., if you want] its reported X-direction extents, or Y-direction if that's what "length" might mean for a given Block.

It works with different kinds of objects, stretch-parameter line length differences, scale factors, etc.

I also have a routine to draw the XY plane of the bounding box, instead of just report on it -- the DBB command [and DBBM for the collective box around Multiple objects] in DrawBoundingBox.lsp>here<.  That was used for the illustration below.

BUT:

That's reliable only with something orthogonally oriented.  The difficulty is that when used with a Block, if it's rotated at some non-orthogonal angle, it's as though its bounding box when at zero rotation becomes part of the definition, and the resulting bounding box of the rotated Block is that of the bounding box of the non-rotated Block, when rotated to the same angle.  Here, the dotted yellow at right illustrates what the yellow box in the middle becomes as if it's present and rotated along with the Block [even though it's not part of the Block definition].  That is what the right image builds its green bounding box around, though the Block itself is all that was selected.  So the green one extends beyond the actual extents of the white Block contents:

Kent1Cooper_0-1695061588671.png

So if "length" for you means, for example, extent in the direction of its rotation angle, whatever that is, something else would be needed.

 

Kent Cooper, AIA
Message 5 of 15

daniel_cadext
Advisor
Advisor

"That's reliable only with something orthogonally oriented"

 

Blockrefs have a rotation property, it should be possible to handle this

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 6 of 15

Sea-Haven
Mentor
Mentor

If you know the block name then you know the length of the block at scale 1:1. So can work out for other scales. Yes would need a list of block lengths. But if its your project may be well worthwhile. 

 

 

Quick examples

Pline, Line or Arc get length property.

Circle get circumference property.

 

0 Likes
Message 7 of 15

daniel_cadext
Advisor
Advisor
Accepted solution

Kent was on the right track getting the bounding box, but get the bounding boxes of the entities inside the block table record * the scale.

This python seems to work on the few dynamic blocks I tried.

Maybe someone can port this to lisp

 

def PyRxCmd_doit1():
    try:
        etype = Db.BlockReference.desc()
    
        res: tuple[Ed.PromptStatus,
                   Db.ObjectId,
                   Ge.Point3d] = Ed.Editor.entSel("\nPick it: ",etype)
        
        if res[0] != Ed.PromptStatus.eOk:
            return
        
        ref = Db.BlockReference(res[1])
        btr = Db.BlockTableRecord(ref.blockTableRecord())
        
        ext = Db.Extents()
        for id in btr.objectIds():
            ent = Db.Entity(id)
            subext = Db.Extents()
            if ent.bounds(subext):
                ext.addExt(subext)
                
        length = (ext.maxPoint().x - ext.minPoint().x)* ref.scaleFactors().sx
                
    except Exception as err:
        traceback.print_exception(err)

 

length.png

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 8 of 15

komondormrex
Mentor
Mentor

depends on a block contents. if there is the only line/pline, getting needed length seems to be an easiest task.

Message 9 of 15

smallƑish
Advocate
Advocate

Yes, only line and polyline, I mean the block made by polyline only

0 Likes
Message 10 of 15

Kent1Cooper
Consultant
Consultant
Accepted solution

@smallƑish wrote:

Yes, only line and polyline 


As objects only, or always defined into Blocks [see the topic heading]?  Can you post a drawing or an image showing some examples of the objects you're talking about?  Include the range of possibilities of scale, rotation, dynamic properties, etc.

 

It can make a big difference, for example, whether or not a Polyline is defined into a Block.  In this overly simple example, the dashed red are the bounding boxes, if that should be a way to determine what you want.  Note that they are the same on the left, but how different the ones on the right are, between the white Polyline and the green Block containing only the same Polyline, when rotated at a non-orthogonal angle.  As a Block, its bounding box is quite different than it is as an independent Polyline.  The dotted yellow is not part of it, but only illustrates the non-rotated Block's bounding box becoming the basis for that of the rotated Block.

Kent1Cooper_0-1695137229618.png

Of course, the question remains:  in a situation like this, what would "length" mean?  That's why I'd like to see some real examples of the kinds of things you want to find the length of, how they might be oriented, what length would mean in relation to them, etc.

Kent Cooper, AIA
0 Likes
Message 11 of 15

smallƑish
Advocate
Advocate

I mean the blocks are built with lines and polylines. (means no attribute or hatch-kind objects are included in the block) Please accept my apology if I made the discussion getting confused. Attached  a Sample drawing including most of the possibilities. 

 

Thank you!

0 Likes
Message 12 of 15

Sea-Haven
Mentor
Mentor

You almost need to look at each block and code accordingly. The dynamic blocks can read visibility and variable values from the block. I noticed one when stretched uses a value for length but the dim result is different, has extra length again scale comes into effect. Some of the blocks it may be easier reading the relevant dim value. 

 

You may have to pay some one to do this task once you have a few done will be easier to add more blocks.

0 Likes
Message 13 of 15

smallƑish
Advocate
Advocate

Looks this method may work for this case.

0 Likes
Message 14 of 15

komondormrex
Mentor
Mentor
Accepted solution

nope, you have complex blocks in a sense of they having not one entity, so the best way to get measurement needed is to define a parameter for each measurement in a block which may be a dimension or a formula and hiding unnecessary ones. this will make a dynamic block if it's not from which you can get needed measurement with some lisp coding. see illustration below for arc length.

komondormrex_0-1695276841787.pngkomondormrex_1-1695276952538.png

 

0 Likes
Message 15 of 15

smallƑish
Advocate
Advocate

As the given blocks and objects are not a uniform structure.I understand, it's getting very complicated. And this doesn't make a huge impact in drafting. 

Wanna Say thanks to everyone who tried to solve this. 🙏

0 Likes