Search code examples
lispcellautocadautolisp

Extract data from a table object using autolisp


I want to extract certain information from the comparison of the information stored in a drawing-table or table object, as you prefer to call it, if the comparison succeeds then store the related values into variables. I am new to Visual lisp or Auto Lisp. So please can you help me with that issue and explain me step by step please.

Example of table

So for example, if my table have in the first column D1 I want to store the information in the next three columns next to it but in the same row.

enter image description here

So in this example it would be 132156, 432 y 11 the numbers to be stored in three different variables or an array. Please help me and explain me step by step the possible solutions, I am really new to Lisp


Solution

  • first You need to get table. You can ask user to select one, for example like this:

    (setq table (vlax-ename->vla-object (car (entsel ))) )
    

    You should remember to catch error if user wouldn't like to select . Also You should check if user select table not some other enity. but now let's imagine that user select table so now You may try this

    (setq columns (vlax-get-property table 'Columns))
    (setq rows (vlax-get-property table 'rows))
    
    (setq row 1 )   ; 0 is header
    (repeat rows
        (setq vals nil)
        (setq column 0)
        (setq txtval (vlax-invoke-method table 'GetText row column ))
            ; now we have value from first cell in row.
            ; and now You can go at least two ways. 
            ; 1 check value and make Your analyse, read values from other columns or anything You need
            ; 2 build array of all values from all cells and after that analyse only array of texts (remove dependency from object table)
            ; for this sample I choose 1 way.
        (if (= txtval "D1") (progn          
            (repeat 3 ; because You "want to store the information in the next three columns"
                (setq column (1+ column))
                (setq vals ( append vals (list (vlax-invoke-method table 'GetText row column ))))
            )
        ))
        (if (not (null vals )) (progn
            (setq arrayOfVals (append arrayOfVals (list vals)))
        ))
        (setq row (1+ row ))
    )
    
    (print arrayOfVals)