Author Topic: How to get the 4 corner_points' coordinates of a table cell ?  (Read 7135 times)

0 Members and 1 Guest are viewing this topic.

e2002

  • Mosquito
  • Posts: 8
How to get the 4 corner_points' coordinates of a table cell ?
« on: January 01, 2015, 03:09:23 AM »
How to get the 4 corner_points'  coordinates of a table cell ?

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #1 on: January 01, 2015, 06:06:54 AM »
Just curious , What would you do with the four corners of a  cell ?

e2002

  • Mosquito
  • Posts: 8
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #2 on: January 01, 2015, 06:36:20 AM »
i want use AutoLISP move text objects to the rectangle region of cells , then convert the text contents as the contens of cell.

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #3 on: January 01, 2015, 07:24:56 AM »
Try this function and let me know .

Code - Auto/Visual Lisp: [Select]
  1. (defun _cell:coordinates (tbl column row / w h i a b c d)
  2.   ;;;   Tharwat 01.01.2015      ;;;
  3.   ;;; Get the four coordinates  ;;;
  4.   ;;; of a specific cell        ;;;
  5.   (setq w 0.
  6.         h 0.
  7.         i -1
  8.   )
  9.   (if (and (eq (vla-get-objectname tbl) "AcDbTable")
  10.            (<= row (vla-get-rows tbl))
  11.            (<= column (vla-get-columns tbl))
  12.       )
  13.     (progn
  14.       (repeat (1- column)
  15.         (setq w (+ w (vla-getcolumnwidth tbl (setq i (1+ i)))))
  16.       )
  17.       (setq p (vlax-get tbl 'insertionpoint)
  18.             i -1
  19.       )
  20.       (repeat (1- row)
  21.         (setq h (+ h (vla-getrowheight tbl (setq i (1+ i)))))
  22.       )
  23.       (setq a (list (+ (car p) w) (- (cadr p) h) (caddr p))
  24.             b (list (+ (car a) (vla-getcolumnwidth tbl (1- column)))
  25.                     (cadr a)
  26.                     (caddr a)
  27.               )
  28.             c (list (car b)
  29.                     (- (cadr b) (vla-getrowheight tbl (1- row)))
  30.                     (caddr b)
  31.               )
  32.             d (list (car a)
  33.                     (- (cadr a) (vla-getrowheight tbl (1- row)))
  34.                     (caddr a)
  35.               )
  36.       )
  37.     )
  38.   )
  39.   (list a b c d)
  40. )
  41.  


The way to call the function .

Code - Auto/Visual Lisp: [Select]
  1. (_cell:coordinates  (vlax-ename->vla-object (car (entsel "\n Select Table :"))) 4 8)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #4 on: January 01, 2015, 09:29:07 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun cellextents ( obj row col / lst rtn )
  2.     (setq lst (vlax-invoke obj 'getcellextents row col :vlax-true))
  3.     (repeat 4 (setq rtn (cons (list (car lst) (cadr lst) (caddr lst)) rtn) lst (cdddr lst)))
  4.     rtn
  5. )

e2002

  • Mosquito
  • Posts: 8
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #5 on: January 02, 2015, 09:41:49 AM »
Cool code! Thanks for LM and Tharwat.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #6 on: January 02, 2015, 09:44:44 AM »
you may need to test if the cell is merged, not sure with com

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #7 on: January 02, 2015, 10:27:03 AM »
Cool code! Thanks for LM and Tharwat.

You're welcome!  :-)

you may need to test if the cell is merged, not sure with com

A good point - after some brief testing, it would appear that the ActiveX getcellextents method accounts for this if the queried cell is the leftmost/topmost cell of those merged. With this in mind, something like this should suffice:

Code - Auto/Visual Lisp: [Select]
  1. (defun cellextents ( obj row col / a b c d lst rtn )
  2.     (if (= :vlax-true (vla-ismergedcell obj row col 'a 'b 'c 'd))
  3.         (setq row a col c)
  4.     )
  5.     (setq lst (vlax-invoke obj 'getcellextents row col :vlax-true))
  6.     (repeat 4 (setq rtn (cons (list (car lst) (cadr lst) (caddr lst)) rtn) lst (cdddr lst)))
  7.     (reverse rtn)
  8. )
« Last Edit: January 02, 2015, 10:35:00 AM by Lee Mac »

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #8 on: January 02, 2015, 12:30:23 PM »
Cool code! Thanks for LM and Tharwat.

You are welcome and Happy that my approach worked for you .  :-)

Nice approach Lee and specially the second one for the emerged cells  :wink:

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #9 on: January 03, 2015, 08:27:10 AM »
Thank you Tharwat  :-)

kozmos

  • Newt
  • Posts: 114
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #10 on: January 04, 2015, 11:38:37 PM »
Cool code! Thanks for LM and Tharwat.

You're welcome!  :-)
Seemed that GetCellExtents only get the boundary of cell content area, so need to add the horizontal and vertical margins into the the calculation to get the exact cell corner.

you may need to test if the cell is merged, not sure with com

A good point - after some brief testing, it would appear that the ActiveX getcellextents method accounts for this if the queried cell is the leftmost/topmost cell of those merged. With this in mind, something like this should suffice:

Code - Auto/Visual Lisp: [Select]
  1. (defun cellextents ( obj row col / a b c d lst rtn )
  2.     (if (= :vlax-true (vla-ismergedcell obj row col 'a 'b 'c 'd))
  3.         (setq row a col c)
  4.     )
  5.     (setq lst (vlax-invoke obj 'getcellextents row col :vlax-true))
  6.     (repeat 4 (setq rtn (cons (list (car lst) (cadr lst) (caddr lst)) rtn) lst (cdddr lst)))
  7.     (reverse rtn)
  8. )
KozMos Inc.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #11 on: January 05, 2015, 12:44:31 PM »
Cool code! Thanks for LM and Tharwat.

You're welcome!  :-)

Seemed that GetCellExtents only get the boundary of cell content area, so need to add the horizontal and vertical margins into the the calculation to get the exact cell corner.

The function seems to perform as expected for me - the attached image shows the points returned for a cell with horizontal & vertical margins set to 2 & 3 units respectively:

kozmos

  • Newt
  • Posts: 114
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #12 on: January 08, 2015, 01:58:21 AM »
Found the reason, I called (setq lst (vlax-invoke tbl 'getcellextents row col 0)) and it returns the cell content box.
 
KozMos Inc.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #13 on: January 08, 2015, 11:10:10 AM »
Doubt I'll ever had a use for this, but it's still pretty damn cool. Well done, Lee.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: How to get the 4 corner_points' coordinates of a table cell ?
« Reply #14 on: January 08, 2015, 06:11:40 PM »
Doubt I'll ever had a use for this, but it's still pretty damn cool. Well done, Lee.

Cheers Alan  :-)