Jump to content
nicodam84

AutoLisp for compiling of block attributes from csv file

Recommended Posts

nicodam84

Hi

 

i'm looking for an Autolisp that help me to compile title block starting from a csv file. At the moment I know this Lisp

 

http://www.lee-mac.com/updatetitleblock.html

 

It is exactly what I need. The problem is that this program compile all the block title (present in "model" sheet) with the first line of the csv file.

So all the block title will be equal.

 

I need a routine that -for example- compile 5 block with attribute taking information from 5 different line of the csv file. This has to be done in "model" area.

 

Does exist some progrma that colud be ok for this ??

 

 

Tnx in advance

 

Nicola

Share this post


Link to post
Share on other sites
BIGAL

Post a dwg and csv, sounds like you need a more specific lisp that just does your task, reading a csv is easy, updating a block is easy, the only hard part here is that your titles are all in model space so you need to advise of the desired method of the order picking line 1 is this title, and so on. If you had them as layouts its much easier to match with out user input.

Share this post


Link to post
Share on other sites
BIGAL

Had a look at this need to start maybe with Lee-mac ReadCSV this will make a list that can be looked at line by line. Then its just a case of using a double repeat to update the block. It would allow manual picking of the block for the order etc. Will see if I can find time if some one else does not do first.

Share this post


Link to post
Share on other sites
BIGAL

Use Zip rather than Rar easier to open. Will try to find time.

Share this post


Link to post
Share on other sites
BIGAL

I have taken a different approach to this problem and are putting forward a fits all type of solution it will work with any block any number of attributes rather than just doing one that only works for you block only.

 

Ok do this for any one else as well this is the intended method the numbers represent each variable pulled from the csv file.

;Copy next line to command line
(setq x 0)

; copy next line to command line and pick blocks.
(foreach att (vlax-invoke (vlax-ename->vla-object (car (entsel))) 'getattributes)(vla-put-textstring att (setq x (+ x 1)))) 

Share this post


Link to post
Share on other sites
BIGAL

Here is a working version note the CSV is true comma seperated value file for testing you can change the "," to ";" for semicolon.

 

Note actual code is after Lee-mac routines.

Change (setq j 1) to skip 1st line

DWG,LAYOUT,LOC,SIGLA,REPARTO,CODICE_SIECAB,CODICE_DTR,CODIFICA,PALL
EX,MODEL,1,2,3,4,5,6,7
EX,MODEL,8,9,10,12,13,14
EX,MODEL,15,16,17,18,19,20,21
EX,MODEL,22,23,2,4,25,26,27,28

 

;; Read CSV  -  Lee Mac
;; Parses a CSV file into a matrix list of cell values.
;; csv - [str] filename of CSV file to read

(defun LM:readcsv ( csv / des lst sep str )
   (if (setq des (open csv "r"))
       (progn
           (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
           (while (setq str (read-line des))
               (setq lst (cons (LM:csv->lst str sep 0) lst))
           )
           (close des)
       )
   )
   (reverse lst)
)

;; CSV -> List  -  Lee Mac
;; Parses a line from a CSV file into a list of cell values.
;; str - [str] string read from CSV file
;; sep - [str] CSV separator token
;; pos - [int] initial position index (always zero)

(defun LM:csv->lst ( str sep pos / s )
   (cond
       (   (not (setq pos (vl-string-search sep str pos)))
           (if (wcmatch str "\"*\"")
               (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))
               (list str)
           )
       )
       (   (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")
               (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))
           )
           (LM:csv->lst str sep (+ pos 2))
       )
       (   (wcmatch s "\"*\"")
           (cons
               (LM:csv-replacequotes (substr str 2 (- pos 2)))
               (LM:csv->lst (substr str (+ pos 2)) sep 0)
           )
       )
       (   (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))
   )
)

(defun LM:csv-replacequotes ( str / pos )
   (setq pos 0)
   (while (setq pos (vl-string-search  "\"\"" str pos))
       (setq str (vl-string-subst "\"" "\"\"" str pos)
             pos (1+ pos)
       )
   )
   str
)

(setq csvlst (LM:readcsv "C:\\temp\\test.csv"))

(setq len (length csvlst))
(setq j 0) ;Change (setq j 1) to skip 1st line
(repeat len
(setq sublist (nth J csvlst))
(setq x -1)

(foreach att (vlax-invoke (vlax-ename->vla-object (car (entsel))) 'getattributes)
(setq x (+ x 1))
(vla-put-textstring att (nth x sublist))
)
(setq j (+ J 1))
) ; len

Share this post


Link to post
Share on other sites
nicodam84

hi Bigal

 

i can't run your lisp (present in second answer). Could you send me the file (.lsp) to load in dwg ??

 

other question: csv file must change or not ??

 

tnx a lot for your help

Share this post


Link to post
Share on other sites
nicodam84

hi Bigal

 

i can't run your lisp (present in second answer). Could you send me the file (.lsp) to load in dwg ??

 

other question: csv file must change or not ??

 

tnx a lot for your help

Share this post


Link to post
Share on other sites
BIGAL

Just open Notepad and copy and paste save as csvtoblock.lsp use Appload to run it.

 

you can change the "," to ";" for semicol
look for this in code your data has ;

Share this post


Link to post
Share on other sites
nicodam84

Hi

 

I try run the program, but when I appload it, autocad say to me

 

Comando: (LOAD "C:/Users/nicola.dambrosio/Desktop/csvtoblock.lsp") nil

 

i don't understand if is still necessary run Lee Mac before

 

tnx

Share this post


Link to post
Share on other sites
Tharwat

Try this program and keep the Excel file in the same directory where the drawing is though I can modify the program to allow the user to select the Excel file if you want to .

 

Let me know how you get on with it .

 

(defun c:Upatts
      (/ _doc _peel:string fl o st tag v lst ss i sn p att str)
;;;    Author: Tharwat Al Shoufi    ;;;
;;;    Date: 22.01.2015        ;;;
 (defun _peel:string (str del / pos lst)
   (while (setq pos (vl-string-search del str 0))
     (progn (setq lst (cons (substr str 1 pos) lst))
            (setq str (substr str (+ pos 2) (strlen str)))
     )
   )
   (if (and str (/= str ""))
     (setq lst (cons str lst))
   )
   (setq lst (reverse lst))
 )
 (if (and (setq fl
                 (findfile (strcat (getvar 'DWGPREFIX) "Cartel1.csv"))
          )
          (setq o (open fl "r"))
     )
   (progn
     (setq st (read-line o))
     (setq tag (cddr (_peel:string st ";")))
     (while (setq st (read-line o))
       (setq v   (_peel:string st ";")
             lst (cons (list (car v) (cadr v) (cddr v)) lst)
       )
     )
     (close o)
     (vla-startundomark
       (setq _doc (vla-get-ActiveDocument (vlax-get-acad-object)))
     )
     (if (setq ss
                (ssget
                  "_X"
                  (list
                    '(0 . "INSERT")
                    '(66 . 1)
                    (cons
                      2
                      (apply 'strcat
                             (mapcar '(lambda (x) (strcat x ","))
                                     (mapcar 'car lst)
                             )
                      )
                    )
                  )
                )
         )
       (repeat (setq i (sslength ss))
         (setq sn (ssname ss (setq i (1- i))))
         (if
           (vl-some
             '(lambda (x)
                (and
                  (eq (strcase (cdr (assoc 2 (entget sn))))
                      (strcase (car x))
                  )
                  (eq (strcase (cdr (assoc 410 (entget sn))))
                      (strcase (cadr x))
                  )
                  (setq att x)
                )
              )
             lst
           )
            (mapcar
              '(lambda (a)
                 (if
                   (and (setq p (vl-position (vla-get-tagstring a) tag))
                        (setq str (nth p (caddr att)))
                   )
                    (vla-put-textstring a str)
                 )
               )
              (vlax-invoke
                (vlax-ename->vla-object sn)
                'getattributes
              )
            )
         )
       )
     )
     (vla-endundomark _doc)
   )
 )
 (princ)
)(vl-load-com)
(c:Upatts)

Edited by Tharwat

Share this post


Link to post
Share on other sites
Tharwat

Open your drawing that contains the attributed blocks that listed in the excel file .csv then load the lisp with the use of appload command .

 

The command name is Upatts and it should run automatically without and user intervention .

Share this post


Link to post
Share on other sites
Tharwat

How do you expect from the program to work since that the block name is not listed ( included ) in the Excel file ?

Beside that , the second column should obtain the space name ( Model/layout )

Besides all of that , your last attached files are completely different than the one you uploaded earlier .

Share this post


Link to post
Share on other sites
nicodam84

Hi

 

How do you expect from the program to work since that the block name is not listed ( included ) in the Excel file ?

Sorry I didn't know this. in which column is necessary put thhe block name ??

 

Beside that , the second column should obtain the space name ( Model/layout )?

First I tried with csv file with Model/layout in second column but it still doesn't work, than I changed it tryng to do something different (bad idea I suppose)

I load the original csv file, but i really dont' know where I have to insert column with block name

 

https://www.dropbox.com/sh/k8cupww37ubo1z0/AAA_m3haxsPv2jZG8hTOkXtMa?dl=0

 

As you probably understood i'm a beginner with lisp; thanks for all contribution you kindly are giving me

 

ND

Share this post


Link to post
Share on other sites
Tharwat

I updated the codes above and just add the program to your acaddoc.lsp and put the following excel file in the same location where

the drawing is located .

 

Note: you can update the Excel file as per the sequence of the data .

Cartel1.csv

Share this post


Link to post
Share on other sites
nicodam84

hi

 

now it works

I knew LEE MAC lisp that works similar

 

http://www.lee-mac.com/updatetitleblock.html

 

yours is faster because automatically load csv file. I'm lokking for something that without update the Excel file as per the sequence of the data.

 

For example: csv file with 100 different line that update dwg eith 100 block

 

do you think is it possible (maybe adding some loop function in the script) ??

 

Tnx a lot

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...