Problem with bounding box for blocks

Problem with bounding box for blocks

roland.r71
Collaborator Collaborator
1,461 Views
6 Replies
Message 1 of 7

Problem with bounding box for blocks

roland.r71
Collaborator
Collaborator

I'm creating a function for marking elements in a dwg.

So far it works fine, but somehow it has some strange behavior with blocks.

 

Clicking the same block insert (same entity) in different places will, sometimes, get me a different (incorrect) bounding box (?)

Even clicking inserts of the same block (different entity) in the same place can result in an incorrect bounding box, at occasions.

 

...and I can't find why...

 

Any thoughts / help would be very welcome.

 

Here's an extraction of the code used/needed to create the box, for INSERTs.

(the actual routine is much bigger and does a few things more, like setting color & transparency etc.)

 

(defun C:test ( / gw dist ue ed et ll ul ur lr ne)
   (vl-load-com)

   (setq gw "1")    ; Global Width for polylines
   (setq dist 1.5)  ; bounding box Border Distance
   
; ue = user selected entity
; ed = entity data
; et = entity type
; ll = lower left
; ur = upper right
; ul = upper left
; lr = lower right
; ne = new entity

   (princ "\nSelect object to mark:")
   (while (setq ue (entsel))
      (setq ed (entget (car ue)))
      (setq et (cdr (assoc 0 ed)))
      (cond
         ((= et "INSERT") ; Block
            (setq obj (vlax-ename->vla-object (car ue)))
            (setq box (vl-catch-all-apply 'vla-getboundingbox (list obj 'minpt 'maxpt)))
            (setq ll (vlax-safearray->list minpt))
            (setq ur (vlax-safearray->list maxpt))
            (setq ul (list (- (car ll) dist)(+ (cadr ur) dist)))
            (setq lr (list (+ (car ur) dist)(- (cadr ll) dist)))
            (command "_.rectangle" ul lr)
            (setq ne (entget (entlast)))
            (setq ne (subst (cons 43 (atof gw))(assoc 43 ne) ne))
            (entmod ne)
         )
      )
   )
)

 

 

0 Likes
Accepted solutions (1)
1,462 Views
6 Replies
Replies (6)
Message 2 of 7

ВeekeeCZ
Consultant
Consultant
Accepted solution
Probably useless to note... are OSNAPs off?

(command "_.rectangle" "_none" ul "_none" lr)
0 Likes
Message 3 of 7

SeeMSixty7
Advisor
Advisor

@ВeekeeCZ brings up a very common issue. I never use running OSNAPS, never have, I always type in every OSNAP I want. That is not a debate topic, just a fact of how I have always worked. Most users utilize running OSNAPS though. As a developer, I often forget this little setting and run into this issue on a  number of occasions. "It works fine for me!" is often the response when someone runs into an issue. "Oh turn off OSNAPS and see if that works, did that work? [Yes]." DOH, I'll add that to the code then. LOL. I have so much code I have written over the years, where I left that little adjustment out. So for those of you starting your development efforts, take note, OSNAPs will get you, so write your code to account for them, when dealing with points on the screen.

Good Luck,

0 Likes
Message 4 of 7

phanaem
Collaborator
Collaborator

Hi SeeM

 

You can avoid any OSNAP problem by setting OSNAPCOORD to 1. This sysvar controls the keyboard entry priority.

See this sample. I have 2 points in 1,1 and 49,49 and the OSNAP is set to "NODE" (osmode = 8)

 

(progn
  (print (getvar 'osmode))
  (foreach a '(0 1 2)
    (setvar 'osnapcoord a)
    (command "_line" '(0 0) '(50 50) "")
    (setq e (entlast))
    (print (vl-remove-if '(lambda (x) (not (member (car x) '(10 11)))) (entget e)))
    (entdel e)
  )
  (princ)
)

The output is:

8
((10 1.0 1.0 0.0) (11 49.0 49.0 0.0))
((10 0.0 0.0 0.0) (11 50.0 50.0 0.0))
((10 1.0 1.0 0.0) (11 49.0 49.0 0.0))

 

As you see, for OSNAPCOORD = 1 the OSNAP mode is ignored.

I guess it depends on the user's habit, but it works for me, I never needed to change it back to a different value. It is a one-time setting I always made for each new AutoCAD version.

0 Likes
Message 5 of 7

SeeMSixty7
Advisor
Advisor

phanaem,

 

Thanks for the info. I am familiar with OSNAPCOORD and think it is a great variable for that kind of control. I will have to pass that onto my users as another option. Typically they just know or I remind them to toggle OSNAPS. When accounting for it In my code, I usually just do the same by toggling OSNAPS Off and then restore the setting back to the previous setting. It is almost tempting just to set OSNAPCOORD to 1 as you suggested by default when anyone uses my code. It does function quite cleanly, but I typically don't like to set my users environment for them.

 

I may use this in the future for a blog topic to remind users of the variable.

 

Thanks again!

0 Likes
Message 6 of 7

roland.r71
Collaborator
Collaborator

Yep. That seems to solve it. Totally forgot about checking that one.

Thanks!

0 Likes
Message 7 of 7

martti.halminen
Collaborator
Collaborator

 

Another way to get odd bounding boxes is if the block has been rotated on insertion.

 

-- 

 

0 Likes