TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: e2002 on January 01, 2015, 03:09:23 AM

Title: How to get the 4 corner_points' coordinates of a table cell ?
Post by: e2002 on January 01, 2015, 03:09:23 AM
How to get the 4 corner_points'  coordinates of a table cell ?
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Tharwat on January 01, 2015, 06:06:54 AM
Just curious , What would you do with the four corners of a  cell ?
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: e2002 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.
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Tharwat 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)
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Lee Mac 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. )
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: e2002 on January 02, 2015, 09:41:49 AM
Cool code! Thanks for LM and Tharwat.
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: It's Alive! on January 02, 2015, 09:44:44 AM
you may need to test if the cell is merged, not sure with com
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Lee Mac 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. )
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Tharwat 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:
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Lee Mac on January 03, 2015, 08:27:10 AM
Thank you Tharwat  :-)
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: kozmos 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. )
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Lee Mac 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:
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: kozmos 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.
 
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: alanjt 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.
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: Lee Mac 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  :-)
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: roy_043 on October 16, 2015, 07:36:22 PM
Can someone explain the meaning of the 4th argument of (vla-getcellextents)? I don't understand.

bOuterCell (Boolean )
  TRUE: The specified cell is an outer cell.
  FALSE: The specified cell is an inner cell.
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: ChrisCarlson on October 19, 2015, 10:19:16 AM
Just a stab in the dark, as an easy conditional to determine if the cell is a border cell or interior cell. Potentially for borders?
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: roy_043 on October 19, 2015, 01:35:05 PM
Of course this was my first thought as well. But it doesn't make sense. The Boolean is an argument not a return value. And I already know from the column and row indices if a cell is a border cell or not.
Title: Re: How to get the 4 corner_points' coordinates of a table cell ?
Post by: It's Alive! on February 07, 2023, 10:42:38 PM
I think the outer is the actual grid whereas inner is the extents taking the margin into consideration