Extracting data from a table

Extracting data from a table

Anonymous
Not applicable
2,609 Views
6 Replies
Message 1 of 7

Extracting data from a table

Anonymous
Not applicable

Hi everybody I'm a new intern in a company and I need to work with Autolisp. Problem is I've never worked with it so I'm not very good. I've been searching for a solution but didn't found one that would do what I want.

I would like to have a script that would make me select a table in the AutoCad. This one for example.

TableAutoCad.png

 

Then I would like it to save the data from the table in variables such as : var1, var2, var#... If the data is a number I'd like it to be saved as a number, and if it's text (not shown on the picture) I'd like it to be saved as text, so I could use the text command to write it somewhere else.

My ultimate goal is to be able to be able to sort row depending on the value of the cell in their first column.

 

Thank you very much!

 

0 Likes
Accepted solutions (1)
2,610 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Just found a way to save them as I wanted to, but only the first 9 works... Here's my code

(defun c:midz  (/ c ctr dat etdata ind lst prefix r tab tblent tblobj val varname)

  (setq snapMode (getvar "osmode"))                                                                 
  (setvar "osmode" 0)

  (vl-load-com)                                                                             
  (setq etdata (entget (setq tblent (car (entsel "\nSelect table object: "))))                
       ind    (cdr (assoc 91 etdata))                                                           
       tab    (cdr (assoc 92 etdata))                                                                
       r      0
       c      0
       ctr    1
       prefix "var"
       tblobj (vlax-ename->vla-object tblent))
 (while (< c tab)
  (while (< r ind)
   (set (setq varname (read (strcat prefix (itoa ctr))))
        (progn (setq val (vlax-invoke tblobj 'getcellvalue r c)
                     val (if (= 'str (type val))
                          (substr val (- (1+ (strlen val)) (vl-position (ascii ";") (reverse (vl-string->list val)))))
                          (rtos val 2 0)))))
   (setq lst (cons (setq dat (cons varname val)) lst))
   (setq ctr (if (listp (cdr dat))
              ctr
              (1+ ctr))
         r   (1+ r)))
  (setq r 0
        c (1+ c)))
 (vl-remove-if '(lambda (x) (listp (cdr x))) (reverse lst))

 (princ))  

0 Likes
Message 3 of 7

Ranjit_Singh
Advisor
Advisor
Accepted solution

See some adjustment to the code. Last time we ran into issues with the table data having some excel formatting carried over. It will be better if you post a dwg file with the table and data.

;:Ranjit Singh
;;09/26/17
(defun c:somefunc  (/ c ctr dat etdata ind lst prefix r tab tblent tblobj val varname)
 (setq etdata (entget (setq tblent (car (entsel "\nSelect table object: "))))
       ind    (cdr (assoc 91 etdata))
       tab    (cdr (assoc 92 etdata))
       r      0
       c      0
       ctr    1
       prefix "var"
       tblobj (vlax-ename->vla-object tblent))
 (while (< c tab)
  (while (< r ind)
   (set (setq varname (read (strcat prefix (itoa ctr))))
        (setq val (vlax-invoke tblobj 'getcellvalue r c)))
   (setq lst (cons (setq dat (cons varname val)) lst))
   (setq ctr (if (listp (cdr dat))
              ctr
              (1+ ctr))
         r   (1+ r)))
  (setq r 0
        c (1+ c)))
 (vl-remove-if '(lambda (x) (listp (cdr x))) (reverse lst)))

table_data.gif

 

0 Likes
Message 4 of 7

Anonymous
Not applicable

Yes, that's exactly it!! I was wondering though, how would I be able to call them for a Cond function, I tried:

  (setq iteration 1)
(setq maximum 2)

(while (<= iteration maximum)
                    (cond
                             ((= (eval (read (strcat "var" (itoa iteration)))) 1) ((command "line" "0,0" "10,0") (setq iteration (1+ iteration))))
                     
                             ((= (eval (read (strcat "var" (itoa iteration)))) 2) ((command "line" "10,0" "20,0") (setq iteration (1+ iteration))))

                             ((= (eval (read (strcat "var" (itoa iteration)))) 3) ((command "line" "20,0" "30,0") (setq iteration (1+ iteration)))) 
 
))




  ---> meaning (if var1 = 1), the program will do the line from 0 to 10 in the X axis and add 1 to iteration so the next loop will test the same for var2       

---> meaning (if var1 = 2), the program will do the line from 30 to 20 in the X axis and add 1 to iteration so the next loop will test the same for var2

---> meaning (if var1 = 3), the program will do the line from 20 to 30 in the X axis and add 1 to iteration so the next loop will test the same for var2

 

 

But unfortunately it doesn't work.

I know the action to do if the cond is respected doesn't make sens. What I want to do is to write the the var# somewhere else on the Cad Drawing but it would be too long to write right now.

Basically, I want the program to do : Is var1=1? Yes? ok then write the value of var6 (on the same row as var1) at "20,0" but I'll see to that later. I need to go steps by steps.
                                     

0 Likes
Message 5 of 7

DannyNL
Advisor
Advisor

Maybe it's best if you post an example of what your table looks like (start) and what the result (end) needs to be.

That way it will be easier to advice on how to approach your problem and to look for direct solutions Smiley Happy

0 Likes
Message 6 of 7

Anonymous
Not applicable

I'd like it to do something like this:

1: I enter the command
2: I select the table

Img1.png

3: The said table disappear and it's content is written somewhere else depending on the content of the first element of each row. In this case, if its a 1, it the second element of the row (Left 1, Left 2, Left 3) goes to the left and if it's a 2, it goes to the right. Something like this (I'll work out the coordinate afterwards)

 

Img2.png

0 Likes
Message 7 of 7

Anonymous
Not applicable

So far here's my code:

(defun c:midz  (/ c ctr dat etdata ind lst prefix r tab tblent tblobj val varname)

  (setq snapMode (getvar "osmode"))                                                                  ;Enlève le Object Snap (F3)
  (setvar "osmode" 0)

  (setq maximum (getint "\n Nombre de lignes: "))

 (setq etdata (entget (setq tblent (car (entsel "\nSelect table object: "))))
       ind    (cdr (assoc 91 etdata))
       tab    (cdr (assoc 92 etdata))
       r      0
       c      0
       ctr    1
       prefix "var"
       tblobj (vlax-ename->vla-object tblent))
 (while (< c tab)
  (while (< r ind)
   (set (setq varname (read (strcat prefix (itoa ctr))))
        (setq val (vlax-invoke tblobj 'getcellvalue r c)))
   (setq lst (cons (setq dat (cons varname val)) lst))
   (setq ctr (if (listp (cdr dat))
              ctr
              (1+ ctr))
         r   (1+ r)))
  (setq r 0
        c (1+ c)))
 (vl-remove-if '(lambda (x) (listp (cdr x))) (reverse lst))
 
  (setq iteration 1)
  (setq yvalue1 27.75)
  (setq yvalue2 27.75)
  (while (<= iteration maximum)
    (if (= (eval (strcat "var" (itoa iteration))) 1
       (command "_text" "J" "M" (strcat "-53.5," (rtos yvalue1)) "4" "0" (eval (read (strcat "var" (itoa (+ iteration maximum))))))
       (setq yvalue1 (+ yvalue1 -30.5)  iteration (1+ iteration)))
        (= (eval (strcat "var" (itoa iteration))) 2
       (command "_text" "J" "M" (strcat "53.5," (rtos yvalue2)) "4" "0" (eval (read (strcat "var" (itoa (+ iteration maximum))))))
             (setq yvalue2 (+ yvalue2 -30.5)  iteration (1+ iteration)))))

 (princ))


It kinda does what I want but they all go to the left (see picture and the fact that it stays on "Command:" unless I do cancel)

 

Img4.png

0 Likes