Jump to content

Recommended Posts

Posted

Hi Guys,

 

I've written this lisp to resize set tables that are output from another program, and they are displayed in the dwg attached. The lisp I wrote doesn't quite work they way I would like it to. Which is to select all or 1 table set the columns to a certain width and then resize the height of the rows to the smallest it can go and repeat for all selected tables. At the moment it will only do 1 table at a time and has to be run twice to work properly.

 

I've tried fixing it my self, now I'm just lost if anyone  has an pointer or could fix the lisp that would be great, and explain what I was missing.

 

Kind Regards,

 

 

TW TABLE.lsp setout tables.dwg

Posted (edited)

Set the width first then the height. noticed two different types of tables one with 9 columns others with 6.

Update the cond for other tables.

 

(defun c:TW (/ ss ent obj row)
  (vl-load-com)
  (if (setq ss (ssget '((0 . "ACAD_TABLE")))) ;selects only tables
    (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq obj (vlax-ename->vla-object ent))
      (cond
        ((eq (vla-get-columns obj) 6)
          (mapcar (function (lambda (a b) (vla-setcolumnwidth obj a b)))
            '(0  1  2  3  4  5)
            '(15 25 25 25 25 35)
          )
        )
        ((eq (vla-get-columns obj) 9)
          (mapcar (function (lambda (a b) (vla-setcolumnwidth obj a b)))
            '(0  1  2  3  4  5  6  7  8)
            '(15 25 25 25 25 35 35 35 35)
          )
        )
      )
      (vla-setrowheight obj 0 10)
      (setq TotalRowCount (vla-get-rows obj))
      (setq RowNumber 1)
      (while (< RowNumber TotalRowCount)
        (vla-setrowheight obj RowNumber 6)
        (setq RowNumber (+ RowNumber 1))
      )
    )
    (prompt "\nNothing Selected")
  )
  (princ)
)

 

 

Edited by mhupp
spelling
  • Like 2
Posted
2 hours ago, mhupp said:

 

Update the cond for other tables.

 

 

 

 

If the columns are always the same with for the same column number (eg, column 3 is always 25) then you could make a reference list of all these widths and refer to that, a bit more versatile maybe if that is so. 

 

Quickly something like below could work? You'd probably want to tidy it up using repeat, cdr, cadr and things like that to create your lists

 

 

(defun c:trytthis ( / columns columnwidths usercols allcolumns acount)
;;  (setq usercols (getint "Columns?"))

  (setq allcolumns (list 15 25 25 25 25 35 35 35 35 50 50 50 50 50 50 50 50 50 50)) ;A list of all column widths by column number
  (setq usercols (vla-get-columns obj) )
  (setq acount 0)
  (while (< acount usercols)
    (setq columns (append columns (list acount)))
    (setq columnwidths (append columnwidths (list (nth acount allcolumns))))
    (setq acount (+ acount 1))
  )
)

 

  • Like 2
Posted (edited)

Good thinking.

 

Cant look it up right now but must be a way to trim down a list so lenght is x.

-edit

vl-remove-if (> vl-postion (vla-get-columns obj)) maybe?

 

(defun c:TW (/ c w ss ent obj row)
  (vl-load-com)
  (if (setq ss (ssget '((0 . "ACAD_TABLE")))) ;selects only tables
    (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq c '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14))
      (setq w '(15 25 25 25 25 35 35 35 35 50 50 50 50 50 50))
      (setq obj (vlax-ename->vla-object ent))
      (repeat (- (length w) (vla-get-columns obj))
        (setq w (reverse (cdr (reverse w))))
        (setq c (reverse (cdr (reverse c))))
      )
      (mapcar (function (lambda (a b) (vla-setcolumnwidth obj a b))) c w)
      (vla-setrowheight obj 0 10)
      (setq TotalRowCount (vla-get-rows obj))
      (setq RowNumber 1)
      (while (< RowNumber TotalRowCount)
        (vla-setrowheight obj RowNumber 6)
        (setq RowNumber (+ RowNumber 1))
      )
    )
    (prompt "\nNothing Selected")
  )
  (princ)
)

 

Edited by mhupp
  • Like 2
Posted
1 hour ago, mhupp said:

Cant look it up right now but must be a way to trim down a list so lenght is x.

-edit

vl-remove-if (> vl-postion (vla-get-columns obj)) maybe?

 

 

 

 

.. I was suitably impressed with reverse-reverse, hadn't thought of that

  • Like 1
  • Thanks 1
Posted (edited)

my first thought didn't work with lists that have duplicate values.
 

(setq w (vl-remove (last w) w))
'(15 25 25 25 25 35 35 35 35 50 50 50 50 50 50)
to
'(15 25 25 25 25 35 35 35 35)

 

then I was like if their was only a way to take away the last item like cdr does with the first. 💡

Edited by mhupp
  • Like 2
Posted
7 hours ago, mhupp said:

Good thinking.

 

Cant look it up right now but must be a way to trim down a list so lenght is x.

-edit

vl-remove-if (> vl-postion (vla-get-columns obj)) maybe?

 

 

Thanks you guys this code works the treat cheers, clearly I was missing a lot to the knowledge when it comes to writing lisps. Might need to post more that I have written to see if someone can clean it up better.

Posted (edited)

Rather than setting the width of the columns to a number, you can use strlen to get the length of strings, its a bit more complicated as you need to check each column, but this can be a defun so returns you a list of max column widths, you can use a fuzz factor on the strlen to compenstate for "11111" v's "222222". But looking at table probably not a problem, say use max strlen + 4. I think I have something if I can find will post.

 

As you have done already can get No cols and No of rows, so can work out max len. Note ignore cell 0,0

 

image.png.97fad86f48872f6f395de862021041f8.png

 

It also needs set text justification to say centre.

 

String max (setq strmax (apply 'max (mapcar 'strlen lst)))

 

Edited by BIGAL
  • Thanks 1

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...