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

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

Anonymous
Not applicable
8,297 Views
31 Replies
Message 1 of 32

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

Anonymous
Not applicable

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

0 Likes
Accepted solutions (1)
8,298 Views
31 Replies
Replies (31)
Message 2 of 32

pbejse
Mentor
Mentor

@Anonymous wrote:

 

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


What is it? Can you define textbox for us stagiaireF9RWW.

0 Likes
Message 3 of 32

Anonymous
Not applicable

Hi,

 

By textbox I mean the equivalent of going under the home ribbon, under annotation and clicking on Multiline Text, then selecting both corners of the textbox and entering "Test" as a value.

 

In Autolisp, the equivalent command is :

 

(command "_text" "J" "M" "0,0" "4" "0" "Test"  "")

 

To further clarify what I mean, I prepared an example.

 

Example.jpg

 

Using an AutoLISP routine, I want to be able to extract values in my table (lets say 3 and Wed) and put the extracted data in a textbox, as shown to the right of my image.

 

I hope what I mean to do and what I meant by textbox is clearer now.

 

Thanks

0 Likes
Message 4 of 32

pbejse
Mentor
Mentor

@Anonymous wrote:

 

 

I hope what I mean to do and what I meant by textbox is clearer now.

 

Thanks


Okay, but is there a limit to the number of cells selected at one time?

 

0 Likes
Message 5 of 32

Anonymous
Not applicable

For the use I intend to do, I don't need to select more than one box at a time.

 

The way I thought the program could be written is:

 

(setq ValueInCellA1 CellA1)               ;gives to a variable the value contained in a cell

(setq ValueInCellA2 CellA2)

(setq ValueInCellA3 CellA3)

(setq ValueInCellB1 CellB1)

(setq ValueInCellB2 CellB2)

(setq ValueInCellB3 CellB3)

(setq ValueInCellC1 CellC1)

(setq ValueInCellC2 CellC2)

(setq ValueInCellC3 CellC3)

 

And then simply use those variables to create a single line text.

 

So maybe the simplest way to put it is this way:

 

How can I give to a variable the value contained in a cell?

0 Likes
Message 6 of 32

pbejse
Mentor
Mentor

@Anonymous wrote:

For the use I intend to do, I don't need to select more than one box at a time.

 

 

How can I give to a variable the value contained in a cell?


 

Try this

 

(Defun c:demo  (/ xxx yyy outRow outCol )
(setq string "")      
      (while
            (and (setq xxx (entsel))
                 (vla-HitTest
                       (Setq yyy (vlax-ename->vla-object
                                       (car xxx)))
                       (vlax-3d-point (cadr xxx))
                       (vlax-3d-point
                             (trans (getvar 'ViewDir) 1 0))
                       'outRow
                       'outCol
                       )
                 (setq value (vl-princ-to-string (vlax-invoke  yyy
                                        'getcellvalue  outRow  outCol)))
                 )
            	(print value)
                 (setq string (strcat string value " "))

                 )
      string
      )

 

HTH

 

 Side note: Strangest thing, the entsel function evaluates to nil when selecting an Arial font but behaves normally with RomanS.

 

@Anonymous

 

If selecting the text evaluates to nil. try selecting the Left Vertical border of the cell adjacent to the TEXT 

 

EDIT: DEMO CODE modified to concatenate strings

 

0 Likes
Message 7 of 32

Anonymous
Not applicable

Hi,

 

I am not sure how to use your program. I have loaded the program in AutoLISP.

 

When I enter "demo" in the command bar, its says: "Select Object :"

 

Unfortunately, i am not able to select anything in my table. When I click on the text or the outline of the table, the program just stops. and Select object: "" appears in the command bar.

 

Thanks

0 Likes
Message 8 of 32

pbejse
Mentor
Mentor

@Anonymous wrote:

 


When I enter "demo" in the command bar, its says: "Select Object :"

 

Thanks


As I've said in my previous post, because of the odd behavior,  Your table font style probably fall into the  category I called "strange"

 

....If selecting the text evaluates to nil. try selecting the Left Vertical border of the cell adjacent to the TEXT ...

 

I attached a short video of what I meant by the above statement, (for some reason, the screencast video is stuck in "pending" status) 

Notice if I select the vertical border slightly to the right, the value would be the one on the right side of the cell, if slightly left then the value at the left.

 

I found a code from LM and another form FIXO, if the previous code and the workaround won't cut-it for you, I will post another code based on window selection.

 

0 Likes
Message 9 of 32

Anonymous
Not applicable

Hi,

 

I see now what the routine does and it's pretty nice! Unfortunately, it is not exactly what I am looking for. Maybe I wasn't precise enough. I'll try again.

 

I am looking for a code, without any interaction with the user, that takes each cells of a selected table and gives its value to a variable.

 

The way I want to use it : I write demo in the command bar, then I select a table.

 

Next, for all the data in the table (in our case, for the nine inputs, which are a,b,c,1,2,3,Mon,Tue,Wed) I want nine variables which have the different inputs as value.

 

For example:

 

Var1=a

Var2=b

Var3=c

Var4=1

Var5=2

Var6=3

Var7=Mon

Var8=Tue

Var9=Wed

 

Thanks

0 Likes
Message 10 of 32

pbejse
Mentor
Mentor
that is actually easier than the one had in mind..
0 Likes
Message 11 of 32

Ranjit_Singh
Advisor
Advisor

Theree maybe more than one way of doing just that. Below is one example

;: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)))) (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_Cells_2_Variables.gif

 

0 Likes
Message 12 of 32

Anonymous
Not applicable

Ok great! In fact that is what I wanted to do! Thank you.

 

The only thing: I can't select the table as you did in the video. When my cursor goes over the table, it doesn't change, just as if I moved it on any empty space in my drawing. is there a way to fix that? Maybe it's a setting  that needs to be changed?

 

Thanks

0 Likes
Message 13 of 32

Ranjit_Singh
Advisor
Advisor

stagiaireF9RWW wrote:...........................

The only thing: I can't select the table as you did in the video. ..................


I hope you have not just drawn some linework (lines/polylines etc.) to create a table? If that is an AutoCAD table entity you should be able to select it. Maybe post your drawing or a screencast.

0 Likes
Message 14 of 32

Anonymous
Not applicable

No of course not! Look at the images below for my situation:

 

1st section: Proof that it is a table

2nd section: What happens when I try to select the table

3rd section: Error message that I get

 

Thanks

 

situation.png

0 Likes
Message 15 of 32

Ranjit_Singh
Advisor
Advisor

Try after adding this

(vl-load-com)

at start of the routine.

0 Likes
Message 16 of 32

pbejse
Mentor
Mentor

I'm away from any computer now i , would suggest using an association list instead of multiple variables, i'll post a code as soon as i can get my hands on cad.

 

cheers

 

0 Likes
Message 17 of 32

Anonymous
Not applicable

Yes it works! thanks a lot Ranjit!

 

And pbejse, would there be any difference in the way I could use those variables with your technique?

 

Thanks to both of you, you are great!

0 Likes
Message 18 of 32

Anonymous
Not applicable

Hi,

 

While using the routine, I have noticed that the variables' values are not exactly the way I want them to be.

 

How I use it:

 

* I copy a table in Excel

* I do a Paste Special AutoCAD Entities in AutoCAD

* I run the program

 

It works great with variables containing numbers. For example:

 

Command: !var12
112.0

 

For variables containing text, it doesn't work well. When I write in the command bar !var39, this is what AutoCAD gives me:

 

Command: !var40
"{\\fArial|b0|i0|c0;\\C7;Test"

 

I want the variable to be only "Test"

 

Is there a way to correct that?

 

Thank you

0 Likes
Message 19 of 32

Ranjit_Singh
Advisor
Advisor

@Anonymous wrote:

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

Command: !var40
"{\\fArial|b0|i0|c0;\\C7;Test"

I want the variable to be only "Test"

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


That's the formatting from your excel document. 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)))))
                          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)))
0 Likes
Message 20 of 32

pbejse
Mentor
Mentor

@Anonymous wrote:

... And pbejse, would there be any difference in the way I could use those variables with your technique?

 


Looking closely at Ranjit's code and GIF [ only this morning I was able to see those ]. The result is an associated list. It follows your requirement of setting a value to a variable name.

 

The difference with what I had in mind is lose the variable name altogether and group the values by row number.

 

So basically it's same same but different :), the question now is how or in what way are you planning to use those data.