Jump to content

Moving bounding box to 0,0,0 using list coordinates


Recommended Posts

Posted

Hi,

I am new to this forum and I am also new to autolisp. Over the weekend I managed to use Lee Mac's Selection Set Bounding Box function and customize it to my needs. However, the way I did it is fairly clunky and not elegant at all. What I need to do is create the bounding box, then I need to erase the original selection. After that, I need to move the empty rectangle's lower left corner to 0,0,0. Finally, I need to move everything to layer 0 and purge everything else.

I managed to do it through a move all and using EXTMIN's value as my first displacement point. However, as I discovered, EXTMIN can be very unreliable because it varies whenever you zoom your drawing or change from model space to layout. So I added a zoom _e command in the program and redo my move all a second time. So far, it works well but I know Lee's code already generates a list with the coordinates of the lower left corner and upper right corner so I would like to use that list instead EXTMIN. I just don't know how to call that list because his code is a bit too complicated for my skill level at the moment. I can see that ls1 is the one with the lower left corner coordinates but I don't know how to use it. I added all the commands at the bottom. Please don't judge haha I'm very much a beginner. Any help is greatly appreciated.
 

(defun c:bb ( / box obj sel spc )
    (if (and (setq sel (ssget))
             (setq box (LM:ssboundingbox sel))
        )
        (progn
            (setq spc
                (vlax-get-property (vla-get-activedocument (vlax-get-acad-object))
                    (if (= 1 (getvar 'cvport))
                        'paperspace
                        'modelspace
                    )
                )
            )
            (if (equal 0.0 (apply '- (mapcar 'caddr box)) 1e-6)
                (progn
                    (setq obj
                        (vlax-invoke spc 'addlightweightpolyline
                            (apply 'append
                                (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) ((eval y) box)) x))
                                   '(
                                        (caar   cadar)
                                        (caadr  cadar)
                                        (caadr cadadr)
                                        (caar  cadadr)
                                    )
                                )
                            )
                        )
                    )
                    (vla-put-closed obj :vlax-true)
                    (vla-put-elevation obj (caddar box))
                )
                (apply 'vlax-invoke 
                    (vl-list* spc 'addbox
                        (apply 'mapcar (cons '(lambda ( a b ) (/ (+ a b) 2.0)) box))
                        (apply 'mapcar (cons '- (reverse box)))
                    )
                )
            )
        )
    )
(command "_.erase" sel "")
(command "move" "all" "" (getvar 'extmin) "0,0,0")
(command "_.zoom" "_e")
(command "move" "all" "" (getvar 'extmin) "0,0,0")
(command "_.-laymch" "All" "" "N" "0")
(command "_.-purge" "A" "*" "N")
    (princ)
)
(vl-load-com) (princ)


The function's code for reference:

;; Selection Set Bounding Box  -  Lee Mac
;; Returns a list of the lower-left and upper-right WCS coordinates of a
;; rectangular frame bounding all objects in a supplied selection set.
;; sel - [sel] Selection set for which to return bounding box

(defun LM:ssboundingbox ( sel / idx llp ls1 ls2 obj urp )
    (repeat (setq idx (sslength sel))
        (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
        (if (and (vlax-method-applicable-p obj 'getboundingbox)
                 (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'llp 'urp))))
            )
            (setq ls1 (cons (vlax-safearray->list llp) ls1)
                  ls2 (cons (vlax-safearray->list urp) ls2)
            )
        )
    )
    (if (and ls1 ls2)
        (mapcar '(lambda ( a b ) (apply 'mapcar (cons a b))) '(min max) (list ls1 ls2))
    )
)

 

Posted

Give this a try. What is the need for doing this?

(defun c:bb (/ lm:ssboundingbox box obj sel spc pl)
  ;; Selection Set Bounding Box  -  Lee Mac
  ;; Returns a list of the lower-left and upper-right WCS coordinates of a
  ;; rectangular frame bounding all objects in a supplied selection set.
  ;; sel - [sel] Selection set for which to return bounding box
  (defun lm:ssboundingbox (sel / idx llp ls1 ls2 obj urp)
    (repeat (setq idx (sslength sel))
      (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
      (if
	(and
	  (vlax-method-applicable-p obj 'getboundingbox)
	  (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'llp 'urp))))
	)
	 (setq ls1 (cons (vlax-safearray->list llp) ls1)
	       ls2 (cons (vlax-safearray->list urp) ls2)
	 )
      )
    )
    (if	(and ls1 ls2)
      (mapcar '(lambda (a b) (apply 'mapcar (cons a b))) '(min max) (list ls1 ls2))
    )
  )
  (if (and (setq sel (ssget)) (setq box (lm:ssboundingbox sel)))
    (progn
      (setq spc	(vlax-get-property
		  (vla-get-activedocument (vlax-get-acad-object))
		  (if (= 1 (getvar 'cvport))
		    'paperspace
		    'modelspace
		  )
		)
      )
      (if (equal 0.0 (apply '- (mapcar 'caddr box)) 1e-6)
	(progn (setq
		 obj (vlax-invoke
		       spc
		       'addlightweightpolyline
		       (apply 'append
			      ;; RJP added 'pl' variable which hold the BB coordinates
			      (setq pl (mapcar '(lambda (x) (mapcar '(lambda (y) ((eval y) box)) x))
					       '((caar cadar) (caadr cadar) (caadr cadadr) (caar cadadr))
				       )
			      )
		       )
		     )
	       )
	       (vla-put-closed obj :vlax-true)
	       (vla-put-elevation obj (caddar box))
	)
	(apply 'vlax-invoke
	       (vl-list* spc
			 'addbox
			 (apply 'mapcar (cons '(lambda (a b) (/ (+ a b) 2.0)) box))
			 (apply 'mapcar (cons '- (reverse box)))
	       )
	)
      )
    )
  )
  (command "_.erase" sel "")
  (command "_.move" "_all" "" (car pl) '(0 0 0))
  (command "_.zoom" "_e")
  ;; Move twice ?
  ;; (command "_.move" "_all" "" (getvar 'extmin) "0,0,0")
  (command "_.-laymch" "_All" "" "N" "0")
  (command "_.-purge" "_All" "*" "N")
  (princ)
)
(vl-load-com)
(princ)

 

Posted

Thank you Ron for your reply.

I tried your line of code and it seems to be working.

(command "_.move" "_all" "" (car pl) '(0 0 0))


However, I fail to understand what "car pl" does. I understand what car does, but what's pl?

As for the double move, as I said in my original post, I use EXTMIN's value to move to 0,0,0 but extmin is very unreliable and is tied to your zoom extent. Often, it would be off of 0,0,0 by a significant chunk. The way to resolve this is to zoom your drawing after a first, unprecise move and then move it again. After a second move it seems to be always spot on. But like I said, it's super clunky so that's precisely what I wanted to remove from my code. Not having to move my drawing twice or to zoom in on it.

Thank you very much for your help
 

Posted
19 hours ago, samysnes said:

Thank you Ron for your reply.

I tried your line of code and it seems to be working.


(command "_.move" "_all" "" (car pl) '(0 0 0))


However, I fail to understand what "car pl" does. I understand what car does, but what's pl?

.....

Thank you very much for your help
 

 

I commented in the code about that :)

image.png.2daddfc915f0b9dbaa550c35f2d797dd.png

Posted

Oh I see. I did not notice your comment. It all makes sense now. I really didn't know what that part of the code was for. I could see there were some X Y coordinates being set but the whole caar cadar stuff I really can't follow. Knowing what it is now, I understand your modification to the code. I guess I incorrectly assumed that you could use variables or lists used in the function so I was fixated on that.

Thank you for your time, it is much appreciated.

Posted
3 hours ago, samysnes said:

...

Thank you for your time, it is much appreciated.

Glad to help! 🍻

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...