Need help with Lee Mac Lisp "Col2Back"

Need help with Lee Mac Lisp "Col2Back"

jk1173
Advocate Advocate
1,018 Views
2 Replies
Message 1 of 3

Need help with Lee Mac Lisp "Col2Back"

jk1173
Advocate
Advocate

This lisp sends objects to back that are set to a certain color.  It has been working but now has mysteriously stopped and now gives the error "AutoCAD.Application: Invalid input".  This code is way more advanced than my understanding so any help is appreciated.

 

Jared

 

;; Colour(s) to Back  -  Lee Mac
;; Sends all objects of specific colour(s) (object colour/layer colour) to the back of the draw order.

(defun c:col2back ( / *error* blk bll col doc exd lac lck ls1 ls2 sor )

    (setq col '(9 13 14 21 51 61)) ;; List of colour(s) to send to back
    
    (vlax-for lay (vla-get-layers (setq doc (vla-get-activedocument (vlax-get-acad-object))))
        (if (= :vlax-true (vla-get-lock lay))
            (vla-put-lock (car (setq lck (cons lay lck))) :vlax-false)
        )
        (if (member (vla-get-color lay) col)
            (setq lac (cons (vla-get-name lay) lac))
        )
    )
    (vlax-for blk (vla-get-blocks doc)
        (if (and (= :vlax-false (vla-get-isxref blk)) (= :vlax-false (vla-get-islayout blk)))
            (   (lambda ( / lst )
                    (vlax-for obj blk
                        (if (or (and (= acbylayer (vla-get-color obj)) (member (vla-get-layer obj) lac))
                                (member (vla-get-color obj) col)
                            )
                            (setq lst (cons obj lst))
                        )
                    )
                    (if lst
                        (progn
                            (setq exd (vla-getextensiondictionary blk)
                                  bll (cons (vla-get-name blk) bll)
                            )
                            (vlax-invoke
                                (cond
                                    (   (LM:catchapply 'vla-getobject (list exd "acad_sortents")))
                                    (   (vla-addobject exd "acad_sortents" "acdbsortentstable"))
                                )
                                'movetobottom lst
                            )
                        )
                    )
                )
            )
        )
    )
    (vlax-for lay (vla-get-layouts doc)
        (vlax-for obj (setq blk (vla-get-block lay))
            (cond
                (   (or (and (= acbylayer (vla-get-color obj)) (member (vla-get-layer obj) lac))
                        (member (vla-get-color obj) col)
                    )
                    (setq ls1 (cons obj ls1))
                )
                (   (and (= "AcDbBlockReference" (vla-get-objectname obj)) (member (vla-get-name obj) bll))
                    (setq ls2 (cons obj ls2))
                )
            )
        )
        (if (or ls1 ls2)
            (progn
                (setq exd (vla-getextensiondictionary blk)
                      sor
                    (cond
                        (   (LM:catchapply 'vla-getobject (list exd "acad_sortents")))
                        (   (vla-addobject exd "acad_sortents" "acdbsortentstable"))
                    )
                )
                (if ls2 (vlax-invoke sor 'movetobottom ls2))
                (if ls1 (vlax-invoke sor 'movetobottom ls1))
            )
        )
    )
    (foreach lay lck (vla-put-lock lay :vlax-true))
    (vla-regen doc acallviewports)
    (princ)
)

;; Catch Apply  -  Lee Mac
;; Applies a function to a list of parameters and catches any exceptions.
 
(defun LM:catchapply ( fun arg / rtn )
    (if (not (vl-catch-all-error-p (setq rtn (vl-catch-all-apply fun arg)))) rtn)
)

(vl-load-com) (princ)

Jared - Sr. CAD Technician

Words to live by: If it ain't broke, you're not trying hard enough.
0 Likes
Accepted solutions (1)
1,019 Views
2 Replies
Replies (2)
Message 2 of 3

e3do
Participant
Participant

Lee is top notch at writing these. I've encountered errors occasionally while using his (and other) routines.

If it just stopped working:

Normally I'll just restart AutoCAD and everything's fine. Hopefully this is one of those times (when you're like, "really"?)

0 Likes
Message 3 of 3

jk1173
Advocate
Advocate
Accepted solution

After looking more closely, it is actually working, but is still throwing up the error.  I guess as long as it is working, I won't worry about the error.


Jared - Sr. CAD Technician

Words to live by: If it ain't broke, you're not trying hard enough.
0 Likes