Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
kpennell
Posts: 521
Registered: ‎04-14-2004
Message 1 of 8 (624 Views)
Accepted Solution

Adjust column width to length of text

624 Views, 7 Replies
04-27-2012 08:33 AM

So, my previous post titled "Modify Table Column Widths" was deemed successful, so I decided to start a new thread.

 

Several times, we generate a drawing, and the text widths in the columns vary greatly.  Some have text that is ten characters long, and others have thirty-plus characters.

 

Is there a way (does anyone think) to be able to select the table (or perform the command to all tables in the current layout) and have each of the columns expand to the appropriate width, that depends on the longest string of text in that column, for each column.  The problem I see, is that some tables have two columns, some have six, and of course other with any number in between.  The same is also true for the amount of rows.

 

How would one organize the code?

 

Is this a tall order?

 

Thanks

KP

 

 

 

Distinguished Contributor
phanaem
Posts: 149
Registered: ‎02-06-2007
Message 2 of 8 (611 Views)

Re: Adjust column width to length of text

04-27-2012 10:40 AM in reply to: kpennell

Alot of things still need to be considered but here is something to start

 

(vl-load-com)
(defun C:TEST ( / ss tb e col wid row val h lst)
  (if (setq ss (ssget '((0 . "ACAD_TABLE"))))
    (repeat (sslength ss)
      (setq tb (vlax-ename->vla-object (setq e (ssname ss 0))))
      (setq col 0)
      (repeat (vla-get-columns tb)
        (setq wid 0
              row 0)
        (repeat (vla-get-rows tb)
          (if (setq val (vlax-invoke tb 'GetCellValue row col))
            (setq
              wid (max wid
                       (+ (setq h (vlax-invoke tb 'GetCellTextHeight row col))
                          (vlax-invoke tb 'GetMargin row col acCellMarginLeft)
                          (vlax-invoke tb 'GetMargin row col acCellMarginRight)
                          (- (caadr
                               (setq lst
                                      (textbox
                                        (list
                                          (cons 1 (if (numberp val) (rtos val) val))
                                          (cons 40 h)
                                          (cons 7 (vlax-invoke tb 'GetCellTextStyle row col))
                                        )
                                      )
                               )
                             )
                             (caar lst)
                          )
                       )
                  )
            )
          )
          (setq row (1+ row))
        )
        (vla-SetColumnWidth tb col wid)
        (setq col (1+ col))
      )
      (ssdel e ss)
    )
  )
  (princ)
)

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 8 (606 Views)

Re: Adjust column width to length of text

04-27-2012 10:45 AM in reply to: phanaem

Awesome,

Regards,

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Distinguished Contributor
kpennell
Posts: 521
Registered: ‎04-14-2004
Message 4 of 8 (601 Views)

Re: Adjust column width to length of text

04-27-2012 11:07 AM in reply to: kpennell

Almost there.

 

My first column has a zero (0) assigned to the "col" variable, and it seems "1 1/2" is always assigned to the "wid" variable.  In my example (screenshot posted), it should be way less than "1 1/2".  Looking at the code, i don't know where 1.5 is coming from.  I've check the fields and there are no weird characters that I can see, spaces, or anything of that nature.

 

All the rest of the columns are perfect.

 

Thanks

KP

Distinguished Contributor
phanaem
Posts: 149
Registered: ‎02-06-2007
Message 5 of 8 (595 Views)

Re: Adjust column width to length of text

04-27-2012 11:14 AM in reply to: kpennell

2 minor changes in red


(setq wid 0

          row 1)

(repeat (1- (vla-get-rows tb))

 

Distinguished Contributor
kpennell
Posts: 521
Registered: ‎04-14-2004
Message 6 of 8 (593 Views)

Re: Adjust column width to length of text

04-27-2012 11:20 AM in reply to: kpennell

That was the fix.

 

So in my case, it was getting the initial value from what we have laid out to be the "header" and doing it's thing from there?

 

Huge thanks

KP

Distinguished Contributor
phanaem
Posts: 149
Registered: ‎02-06-2007
Message 7 of 8 (585 Views)

Re: Adjust column width to length of text

04-27-2012 11:55 AM in reply to: kpennell

I'm glad I could help you...
But I have to warn you, there are situations when my cod is not working properly.
I can think of two at this moment:
- multiple text-lines in the same cell
- Long text in the Title cell, longer than the sum of all columns

Distinguished Contributor
kpennell
Posts: 521
Registered: ‎04-14-2004
Message 8 of 8 (583 Views)

Re: Adjust column width to length of text

04-27-2012 12:00 PM in reply to: kpennell

When the text is wrapped, it works.  I haven't seen a case in our templates, where the text is forced to be multiline.  But thanks for the heads up.  If something goes awry, I'll do the work-around.

 

This is a huge time-saver

 

Thanks again

KP

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.