Author Topic: How to know number of rows and columns of strings  (Read 2232 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
How to know number of rows and columns of strings
« on: October 18, 2012, 08:26:02 AM »
Hello everyone .

As it shows in attached image below , I have a few lines of strings .
How can I know the quantity of rows and columns I have with the use of ssget function ?

Thank you all .

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: How to know number of rows and columns of strings
« Reply #1 on: October 18, 2012, 08:57:33 AM »
As shown in image you posted, you can get boundingbox of all objects... You then get boundingbox of 4 upperleft objects 2 in horizontal and 2 in vertical direction... Then calculate delta X and delta Y of first boundingbox, then calculate delta X and delta Y of 4 upperleft objects boundingbox... Finally, you devide delta X major with delta X minor - spaces between columns => columns number = spaces + 1; then do the same for rows ( Y direction )...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to know number of rows and columns of strings
« Reply #2 on: October 18, 2012, 09:15:03 AM »
Consider the method used in the following program:

http://www.theswamp.org/index.php?topic=40483.msg457902#msg457902

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How to know number of rows and columns of strings
« Reply #3 on: October 18, 2012, 09:15:45 AM »
The way I would takle it is first I would select all of the items manually, then iterate through each item extracting the coordinates, creating a list of X coords and Y coords.
If the X or Y coord of the current item being checked doesn't exist in the list of coordinates, add that coordinate to the list. When you are done, count the items in the list.

You could use the bounding box, but I suspect that unless you used a fixed width font, the top left corner may not be uniform. If all of the items utilize the same justification location, then this should work. Of course, if the insertion points of the items are not lined up, then you will have to compare the items with a fuzz factor.

Something like this ... (quick and dirty) ... it could be made more robust by getting the font size and comparing the insertion points with a fuzz factor by percentage of height and by filtering for object type (i.e. only selecting text, mtext, blocks etc.)

The return value is a list (rows cols)

Code: [Select]
(defun countRowCol( / ent ss xcoord xlist ycoord ylist)
  (setq ss (ssget)
ndx 0)
  (repeat (sslength ss)
    (setq ent (ssname ss ndx)
  ndx (1+ ndx)
    )
    (setq xcoord (cadr (assoc 10 (entget ent)))
  ycoord (caddr(assoc 10 (entget ent)))
    )
    (if (= (member xcoord xlist) nil)
      (setq xlist (append xlist (list xcoord)))
    )
    (if (= (member ycoord ylist) nil)
      (setq ylist (append ylist (list ycoord)))
    )
  )
  (list (length ylist) (length xlist))
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

pBe

  • Bull Frog
  • Posts: 402
Re: How to know number of rows and columns of strings
« Reply #4 on: October 20, 2012, 06:44:06 AM »
Hello everyone .

As it shows in attached image below , I have a few lines of strings .
How can I know the quantity of rows and columns I have with the use of ssget function ?

Thank you all .

Is that number of items per row and column?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:RC ( / _sort counter ip objs i)
  2. (defun _sort (m1 m2 m3 m4 ls)
  3.              (vl-sort
  4.               ls
  5.               '(lambda (a b)
  6.                  (cond
  7.                    ((m1 (m2 a) (m2 b)) T)
  8.                    ((equal (m2 a) (m2 b) 0.1) (m3 (m4 a) (m4 b)))
  9.                  )
  10.                )
  11.              )
  12. )
  13. (defun counter (name n tm md / a b i)  
  14.         (while (setq a (car tm))
  15.                (setq b (cdr tm) i 1)
  16.                 (vl-some '(lambda (k)
  17.                             (if (= (md a)(md k))
  18.                               (progn
  19.                               (setq tm (vl-remove k tm))
  20.                               (setq i (1+ i)) nil) T)) b)
  21.                 (setq tm (cdr tm))
  22.                 (princ (strcat "\n" name " " (itoa (setq n (1+ n))) "\t" (itoa i)))
  23.           )(princ (strcat "\nTotal number of " name " " (itoa n)))
  24.   )
  25.   (if
  26.         (setq ip nil objs (ssget '((0 . "*TEXT"))))
  27.         (progn
  28.                 (repeat (setq i (sslength objs))
  29.                   (setq ip (cons (cdr (assoc 10 (entget (ssname objs (setq i (1- i))))))
  30.                               ip
  31.                         )
  32.                   )
  33.                 )(textscr)
  34.                 (counter "Row   " 0 (_sort > cadr < car ip) cadr )
  35.                 (print)
  36.                 (counter "Column" 0 (_sort < car > cadr ip) car)
  37.           )
  38.     )
  39.   )

Code - Auto/Visual Lisp: [Select]
  1. Row    1  3
  2. Row    2  2
  3. Row    3  3
  4. Row    4  2
  5. Row    5  3
  6.  
  7. Column 1  5
  8. Column 2  4
  9. Column 3  4
  10.  
« Last Edit: October 21, 2012, 09:43:23 AM by pBe »

Coder

  • Swamp Rat
  • Posts: 827
Re: How to know number of rows and columns of strings
« Reply #5 on: October 21, 2012, 12:21:25 AM »
Hello guys .

Lee , I could not find the related method to my request in this thread . I am sorry  :oops:
Keith , Your idea works well if all text objects are in line .
pBe , I am sorry , the result of the code is very strange .  :-(

Thank you all

pBe

  • Bull Frog
  • Posts: 402
Re: How to know number of rows and columns of strings
« Reply #6 on: October 21, 2012, 01:10:23 AM »
.....pBe , I am sorry , the result of the code is very strange .  :-(....

Tell me whats so strange about this result

Code - Auto/Visual Lisp: [Select]
  1. Row    1  3
  2. Row    2  2
  3. Row    3  3
  4. Row    4  2
  5. Row    5  3
  6.  
  7. Column 1  5
  8. Column 2  4
  9. Column 3  4
  10.  

On the first row there are 3 items
On the second row there are 2 items
and so on...

On the first column there are 5 items
On the second column there are 4 items....

Perhaps a drawing file from you will remove all the confusion [strangeness]  coder  ;-)

and yes. it works better if the text are aligned :)
« Last Edit: October 21, 2012, 01:18:37 AM by pBe »

Coder

  • Swamp Rat
  • Posts: 827
Re: How to know number of rows and columns of strings
« Reply #7 on: October 21, 2012, 02:31:58 AM »
Thanks pBe for your time and efforts  :-)

I attached a drawing .

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How to know number of rows and columns of strings
« Reply #8 on: October 21, 2012, 09:29:56 AM »
If the items are not in line, the problem is deciding what represents a column and what represents a row.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

pBe

  • Bull Frog
  • Posts: 402
Re: How to know number of rows and columns of strings
« Reply #9 on: October 21, 2012, 09:38:23 AM »
Thanks pBe for your time and efforts  :-)

I attached a drawing .

 :-o  just as i suspected. the alignment is messing up the sorting . I should come up with a more wicked sorting routine. I have not really dwell on real-life situations and still see drawings setup in a perfect world kind of way.

In that case, the link LM posted is perfect for your situation.

Oh well, back to the drawing board  (i'll start dissecting LMs code for clues on how he pulled it off.....)    :-D

EDIT: What Keith said
« Last Edit: October 21, 2012, 09:44:17 AM by pBe »