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

Adjust column width to length of text

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
kpennell
5559 Views, 12 Replies

Adjust column width to length of text

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

 

 

 

12 REPLIES 12
Message 2 of 13
phanaem
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)
)

 

Message 3 of 13
Hallex
in reply to: phanaem

Awesome,

Regards,

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 13
kpennell
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

Message 5 of 13
phanaem
in reply to: kpennell

2 minor changes in red


(setq wid 0

          row 1)

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

 

Message 6 of 13
kpennell
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

Message 7 of 13
phanaem
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

Message 8 of 13
kpennell
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

Message 9 of 13
grobnik
in reply to: phanaem

Hi,

I read your message and I would like to apply same routine in VBA instead Lisp. Please could you explain in few word how do you fixed the column width to cell contents ?

 

Of course some time it's passed since this post, but probably, and I hope you are able to help me.

 

Thank you

Message 10 of 13
phanaem
in reply to: grobnik


@grobnik wrote:

Hi,

I read your message and I would like to apply same routine in VBA instead Lisp. Please could you explain in few word how do you fixed the column width to cell contents ?

 

Of course some time it's passed since this post, but probably, and I hope you are able to help me.

 

Thank you


Hi grobnik

For the selected table, for each column read the content of the each cell, calculate the text length (in autocad units), add the left and right margins. The maximum value of this sum should be the column width. For safety and a good aspect, you can add a small distance to this number, like one time the text's height.

Now, to calculate the text length in lisp, i used TEXTBOX function. I hope you have something similar in VBA. This function "returns the diagonal coordinates of a box that encloses the text" and the text length is the difference of the X coordinates.

 

HTH

 

Message 11 of 13
grobnik
in reply to: phanaem

Thank you very much for your support,

I'll try to apply your suggestion inside my routine.

I'll keep you informed.

Thank you again.


Message 12 of 13
grobnik
in reply to: phanaem

Hi Phanaem,

I'm trying to insert a mtext into the table cells but with-out results.

 

I'll try to explain my issue:

I have inside the drawing a lot of blocks with at least 3 attributes, one on this, shall be the component (block) description, all extracted from drawing and  inserted in the table cells'.

 

Of course it shall be higher or shorter than the column width established when you define the table, and I would like to avoid to have a table line with different heights due to the cell contents automatically use a carriage return see image below. So I would like to fix the columns width to cell contents. Could you help me ?

Carriage return inside the Table cells'Carriage return inside the Table cells'

 

Message 13 of 13
bhuta_n
in reply to: grobnik

Hi grobnik,

 

Did you make anything in vba? i too need that. If yes Please post it here.

 

Thank you.

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

Post to forums  

Autodesk Design & Make Report

”Boost