Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP for Alternating Background Fill in Table

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
nrz13
9302 Views, 10 Replies

LISP for Alternating Background Fill in Table

Does anyone have or know of a LISP routine that I can run that will make alternating rows of color in a table?

I can setup a table with the TABLE command and setup the rows to alternate gray and white, but if I add or delete a row, the alternating background fill pattern gets messed up (see attached example).  Manually redoing the pattern isn't bad for a few rows, but for 30 or more it's a real pain.

If I could run a command that would activate a routine that would start with gray at the top and then alternate white and gray for every row for the rest of the table, that would be awesome.

Thanks


Work:  AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-8700K, 32GB RAM, Samsung 960 Pro SSD, AMD Radeon Pro WX 5100, 3 Dell Monitors (3840x2160)
Home: AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-11700, 64GB RAM, Samsung 980 Pro SSD, NVIDIA Quadro P2200, Dell Monitor (3840x2160)
10 REPLIES 10
Message 2 of 11
Lee_Mac
in reply to: nrz13

Please try the following program, you may alter the background colour where noted as required:

 

;; Alternating Table Background Colour  -  Lee Mac

(defun c:altback ( / *error* acc col fnc idx obj row sel tmp )

    (defun *error* ( msg )
        (if (and (= 'vla-object (type acc)) (not (vlax-object-released-p acc)))
            (vlax-release-object acc)
        )
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (setq col '(215 215 215)) ;; True Colour (r g b) for cell background
    
    (if (setq sel (ssget "_:L" '((0 . "ACAD_TABLE"))))
        (if (setq acc (vla-getinterfaceobject (vlax-get-acad-object) (strcat "autocad.accmcolor." (substr (getvar 'acadver) 1 2))))
            (progn
                (apply 'vla-setrgb (cons acc col))
                (repeat (setq idx (sslength sel))
                    (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))
                          col (vla-get-columns obj)
                    )
                    (repeat (setq row (vla-get-rows obj))
                        (if (zerop (rem (setq row (1- row)) 2))
                            (setq fnc (lambda ( a b ) (vla-setcellbackgroundcolor obj a b acc)))
                            (setq fnc (lambda ( a b ) (vla-setcellbackgroundcolornone obj a b :vlax-true)))
                        )
                        (repeat (setq tmp col) (fnc row (setq tmp (1- tmp))))
                    )
                )
                (vlax-release-object acc)
            )
            (princ "\nUnable to interface with AcCmColor object.")
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

I hope this helps!

 

Lee

Message 3 of 11
nrz13
in reply to: Lee_Mac

Lee:

That is perfect!  Thanks for your help!


Work:  AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-8700K, 32GB RAM, Samsung 960 Pro SSD, AMD Radeon Pro WX 5100, 3 Dell Monitors (3840x2160)
Home: AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-11700, 64GB RAM, Samsung 980 Pro SSD, NVIDIA Quadro P2200, Dell Monitor (3840x2160)
Message 4 of 11
Lee_Mac
in reply to: nrz13

Message 5 of 11
Anonymous
in reply to: Lee_Mac

This lisp doesn't seem to be working in 2018.  When I run it, AutoCAD either locks up or sits there with the spinning wheel indefinitely.

Any chance you can update this routine?

Message 6 of 11
nrz13
in reply to: Anonymous

It's working for me in 2018.1.1.  Are you patched with the latest version?

How big is your table.  I've been using it just fine, but I also just tested in a table with 50 rows and 9 columns and it took about 15 seconds to run through the command.  Don't do anything else in the program while it's running through that command, though.


Work:  AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-8700K, 32GB RAM, Samsung 960 Pro SSD, AMD Radeon Pro WX 5100, 3 Dell Monitors (3840x2160)
Home: AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-11700, 64GB RAM, Samsung 980 Pro SSD, NVIDIA Quadro P2200, Dell Monitor (3840x2160)
Message 7 of 11
Anonymous
in reply to: nrz13

Table is a linked excel datalink file (door schedule) with 19x134 cells.  I am running 2018.1.1 as well.

Perhaps this table is just too big...

Any ideas?

 

Message 8 of 11
nrz13
in reply to: Anonymous

My table isn't linked to Excel (all AutoCAD native TABLE command), so that could be the source of the problem you're experiencing.


Work:  AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-8700K, 32GB RAM, Samsung 960 Pro SSD, AMD Radeon Pro WX 5100, 3 Dell Monitors (3840x2160)
Home: AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-11700, 64GB RAM, Samsung 980 Pro SSD, NVIDIA Quadro P2200, Dell Monitor (3840x2160)
Message 9 of 11
nrz13
in reply to: nrz13

I just tried to run it on a 19 x 134 table in a 3.1MB drawing with a lot of block and text in it and let it run for over 5 minutes with no completion and about the 4 minute mark it minimized itself and won't restore, so it's probably dead.

So yes, it may just be too large a table for AutoCAD 2018 to deal with.  Note my system specs in my signature... nothing underpowered.

A workaround might be to break your rows off into 5 sections and adjust the borders for the new sections so they seamlessly snap into your first table visually.  Then AutoCAD might be able to handle it better doing it a section at a time.  Not sure if that would be more or less hassle than manually setting the backgrounds.


Work:  AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-8700K, 32GB RAM, Samsung 960 Pro SSD, AMD Radeon Pro WX 5100, 3 Dell Monitors (3840x2160)
Home: AutoCAD 2022.1.3, Windows 10 Pro v22H2 64-bit, Intel Core i7-11700, 64GB RAM, Samsung 980 Pro SSD, NVIDIA Quadro P2200, Dell Monitor (3840x2160)
Message 10 of 11
Anonymous
in reply to: nrz13

Thanks for trying that!
For now, I am just manually formatting background color, for the headings
only, rather than alternating. When I have more time later, I might try it
by breaking it out into sections and snapping them together like you
mentioned.
Message 11 of 11
Anonymous
in reply to: Lee_Mac

Hi Lee, this LISP is very useful.  Thank you!  Is there a way to make it start working after the title and header row, or to specify at which row you want it to begin? Thanks again! Great work! 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost