Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to extract text values from an AutoCAD table to a drawing

31 REPLIES 31
SOLVED
Reply
Message 1 of 32
Anonymous
5930 Views, 31 Replies

How to extract text values from an AutoCAD table to a drawing

Hi,

 

I am looking to extract text values from an AutoCAD table with an AutoLISP routine and then use those values to create a textbox.

 

For example, I want to use cell B3 containing the value ''Test'' and then create a textbox in AutoCAD containing the value ''Test'' (and repeat those steps with all different cell values).

 

How can I do that?

 

Thanks

31 REPLIES 31
Message 21 of 32
Anonymous
in reply to: Ranjit_Singh1

It has corrected the Excel formatting issue. Thank you!

 

Is it normal for the program to give as value to var1, for example, 101.0 when it is written only "101" in the cell and that the precision is set to "0"?

 

I am looking to use those variable to put in a single line text with the function "_text"

 

When I write in AutoLISP : (command "_text" "14.5,-67.25" "4" "0" var1 "")

 

It gives me: 101.0000000000000

 

instead of what I expected, which is 101. (See image below)

 

numberissue.png

 

Is there a way to fix that?

 

Thanks again

Message 22 of 32
pbejse
in reply to: Anonymous


@Anonymous wrote:

 

 ....It gives me: 101.0000000000000

 

instead of what I expected, which is 101. (See image below) 


YES, there is, What other issues are you having stagiaireF9RWW?, you mind posting drawing sample so we can consider all the conditions.

 

FWIW: 

 

 

(defun c:demo ( / _remFormat _GetRow _acadTable r c cellType RowData RowHeader)
(defun _remFormat (Str / p) 
		(vl-string-right-trim   "}"
                     (if (setq p (vl-string-position 59 str nil t))
                      		(substr str (+ 2 p)) str ) ))
(Defun _GetRow (e n i / data)
      (repeat i
            (if (setq st (vlax-invoke e 'GetCellValue n (setq i (1- i))))
            (setq data  (cons
	            (_remFormat (vl-princ-to-string st)) data)))
            )
      )
	(if
	      (setq  _acadTable (car (entsel "\nSelect table object: ")))
	      
	      (progn
	       (setq _acadTable (vlax-ename->vla-object _acadTable)
	             r          (Vlax-get _acadTable 'Rows)
	             c          (Vlax-get _acadTable 'Columns)
	             )
		(repeat r
	              (setq cellType (Vlax-invoke _acadTable 'GetRowType (setq r (1- r))))
	              		(cond
	                              (	(= cellType 2)	)
	                              (	(= cellType 4)
	                               	(setq RowHeader (_GetRow _acadTable r c)) )
	                              ( (and
	                                      (= cellType 1)
	                               	      (Setq value (_GetRow _acadTable r c)))
	                               	 	(setq RowData (cons value RowData))
							)
	                              )
	              )
	          )
	      )
      (list RowHeader RowData)
	)

 

You can group the results using RowHeader / RowData  Your goal is unclear to us, I'm sire  we can give you better advice and suggestions if we have all the facts.

 

Cheers

 

 

 

Message 23 of 32
Anonymous
in reply to: pbejse

Hi,

 

I tried your code, but the same issue appears : 

 

Command: !var1
101.0

 

If you want me to be more specific, here I go: I will write a code which will create shapes and single line texts depending on three inputs by the user and by the values contained in a table.

 

 

(defun c:TEST  (/ c ctr dat etdata ind lst prefix r tab tblent tblobj val varname NbInputs Title NumMod iteration YValue)

  (setq snapMode (getvar "osmode"))                                                                  ;Remove Object Snap (F3)
  (setvar "osmode" 0)
  
    (setq NbInputs (getint "\nNumber of inputs: "))                                                   ;Enter Number of inputs
  (if NbInputs
    (prompt (strcat "\nYou entered: " (itoa NbInputs)))
    (prompt "\nYou didn't entered a number."))

    (setq NumMod (getint "\nModule Number (ex.: 1): "))                                           ;Enter module number
  (if NumMod
    (prompt (strcat "\nYou entered: " (itoa NumMod)))
    (prompt "\nYou didn't entered a number."))

    (setq Title (getstring "\nTitle (ex.: Title 1): "))                                          ;Enter Title

 (vl-load-com)                                                                                       ;Puts into variables the cells of the table
 (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)))))
                          val))))
   (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))

  (command "line" "0,-16" "40,-16" "")                                                               ;Draw top line

  (setq iteration 1)                                                                                 ;Draw circles
  (setq YValue -35.25)
  (while (<= iteration NbInputs)
    (command "circle" (strcat "6.25," (itoa YValue)) "6.25")
    (setq YValue (+ Yvalue -30.5))
    (setq iteration (1+ iteration)))



  (command "_text" "14.5,-37.25" "4" "0" var1 "")                                                  ;Create single line texts
  (command "_text" "14.5,-67.25" "4" "0" var2 "")
  (command "_text" "14.5,-97.25" "4" "0" var18 "")
  (command "_text" "14.5,-127.25" "4" "0" var19 "")
  (command "_text" "14.5,-157.25" "4" "0" var33 "")
  (command "_text" "14.5,-187.25" "4" "0" var34 "")
  
 )                                                                                                   ;End of program

 

 

Right now I am having issues with the while loop for the circles (see above) and with the zeros following numbers (see picture one or two post above).

 

I think maybe the problem with the zeros comes because of some kind of Excel formatting. I'll try to change formatting and see what it does.

 

Thanks

Message 24 of 32
Ranjit_Singh1
in reply to: Anonymous


@Anonymous wrote:

...........

 

It gives me: 101.0000000000000

 

instead of what I expected, which is 101. (See image below)

 

 

..............


If you want to infact use it as a text then you might as well convert it to a string. Try below

;:Ranjit Singh
;;8/3/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))))
        (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)))

Extract_Table_Cells_2.gif

 

EDIT: Or if you want to not convert real to string but integers then replace

(rtos val 2 0)

in above code to 

(fix val)

Extract_Table_Cells_3.gif 

Message 25 of 32
Anonymous
in reply to: Ranjit_Singh1

Thanks a lot Ranjit! That is exactly was I was looking for. 🙂

 

If you don't mind could you help me find what is wrong with my while loop for drawing circles above?

 

Thanks 🙂 

Message 26 of 32
Anonymous
in reply to: Ranjit_Singh1

Thanks a lot Ranjit! This is exactly what I was looking for. 🙂 

 

If you don't mind, could you help me with the while loop drawing circles in my code above?

 

It doesn't work and i don't understand why.

 

Thanks

Message 27 of 32
Ranjit_Singh1
in reply to: Anonymous

I do not know the workflow or understand all the prompts in there but giving some random prompts it gives me a fixnump error in the circle command

try changing the command syntax to

(command "circle" (strcat "6.25," (itoa (fix YValue))) "6.25")
Message 28 of 32
Anonymous
in reply to: Ranjit_Singh1

Ranjit, you are simply amazing!

 

Thank you so much for your help, it is priceless to me!

 

Have a wonderful day my friend 🙂 

Message 29 of 32
Anonymous
in reply to: Ranjit_Singh1

You did it again! Thanks a lot Ranjit, you are amazing!

 

Your help is priceless to me!

 

Have a wonderful day my friend 🙂

Message 30 of 32
Ranjit_Singh1
in reply to: Anonymous

You are welcome. HTH.

Message 31 of 32
pbejse
in reply to: Anonymous


@Anonymous wrote:

Hi,

 

I tried your code, but the same issue appears : 

 

 


If you are referring to the code I posted, you are correct. I did not consider THAT issue when I posted the code, I'm only trying to show you how I would approach the other issues and I'm eagerly waiting for you to post an example or show us the intent so we can come up with a generic solution in one go. 

 

 

Anyways, everything works out for you in the end. 

 

Cheers

Message 32 of 32
Anonymous
in reply to: Ranjit_Singh1

this really does work great. but i'm wondering if rather then having to select a table, is there any way to make it always read a predesignated table? also if you could add some comments to the code i would really appreciate it. i'm new to lisp and i'm trying to figure out how this thing works but i'm a little lost.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta