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

set attribute values automatically according to plan name

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
957 Views, 2 Replies

set attribute values automatically according to plan name

Hello, people,

New question : I want to change the values of attributes according to the dwgname. For example, when dwgname is “…SAR-01..dwg”, attribute TITRE 1 to be “STRING1a”,TITRE2 to be “STRING 1b”, ECHELLE_H to be “1:100”, when dwgname is “…SAR-02..dwg”, the three values to be “STRING 2a”, “STRING 2b” and “1:20” respectively, etc.. ; the block is attached; the name is “FR_ATTR_T*”.

 

also if this routine can open each plan in a folder , carry out lisp routine, in paper space, zoom to extent, save, quit. the path could be "I:\P_11100\11175\CAD\01-Station"; attention: make sure files in subfolder wouldn't be opened, only for those in said folder.

 

many thanks,

2 REPLIES 2
Message 2 of 3
roland.r71
in reply to: Anonymous


@Anonymous wrote:

Hello, people,

New question : I want to change the values of attributes according to the dwgname. For example, when dwgname is “…SAR-01..dwg”, attribute TITRE 1 to be “STRING1a”,TITRE2 to be “STRING 1b”, ECHELLE_H to be “1:100”, when dwgname is “…SAR-02..dwg”, the three values to be “STRING 2a”, “STRING 2b” and “1:20” respectively, etc.. ; the block is attached; the name is “FR_ATTR_T*”.

 

also if this routine can open each plan in a folder , carry out lisp routine, in paper space, zoom to extent, save, quit. the path could be "I:\P_11100\11175\CAD\01-Station"; attention: make sure files in subfolder wouldn't be opened, only for those in said folder.

 

many thanks,


As for the 1st part, this will do:

 

(cond 
   ((not (= (vl-string-search "SAR-01" (getvar "DWGNAME")) nil))
      (setq TITRE1 "STRING 1a"
            TITRE2 "STRING 1b"
            ECHELLE_H "1:100")
   )
   ((not (= (vl-string-search "SAR-02" (getvar "DWGNAME")) nil))
      (setq TITRE1 "STRING 2a"
            TITRE2 "STRING 2b"
            ECHELLE_H "1:20")
   )
)
(setq ss (ssget "X" (list (cons 0 "INSERT")))
      i 0)
(if ss
   (progn
      (while (< i (sslength ss))
         (setq ent (entget (ssname ss i)))
         (while (setq ent (entnext (cdr (assoc -1 ent))))
            (setq ent (entget ent))
            (if (= (cdr (assoc 0 ent)) "ATTRIB")
               (cond
                  ((= (cdr (assoc 2 ent)) "TITRE1")
                     (setq ent (subst (cons 1 TITRE1)(assoc 1 ent) ent))
                     (entmod ent)
                  )
                  ((= (cdr (assoc 2 ent)) "TITRE2")
                     (setq ent (subst (cons 1 TITRE2)(assoc 1 ent) ent))
                     (entmod ent)
                  )
                  ((= (cdr (assoc 2 ent)) "ECHELLE_H")
                     (setq ent (subst (cons 1 ECHELLE_H)(assoc 1 ent) ent))
                     (entmod ent)
                  )
               )
            )
         )
         (setq i (+ 1 i))
      )
   )
)

For the second part you should know that's not possible, just like that. A AutoLISP file runs within a drawing and can not open another one & continue running.

 

To do something like that you need to create a batch file (*.scr) to open each drawing, load the lisp file with the above code, run it, save the drawing, & repeat...

Message 3 of 3
roland.r71
in reply to: Anonymous

Here's the smallest version i could produce, to create a script to run the lisp on a collection of files from a selected directory.

Note: It uses express tools for the directory & file selection, as well as the confirmation dialog.

 

It is best to start a new drawing, or have one (only 1) drawing open, without any pending changes, as the script would fail to 'save changes?'

The script needs SDI to run, so having more as 1 drawing open will crash the script.

Make sure all files are writeable, as read-only files will crash the script.

 

Set the right paths for locating the lisp script, inside the init section.

 

Use C:DNBV (Drawing Name Based Values) to select directory & generate the script to run the code i posted earlier (included as C:DNBV_RUN) on all dwg files there. (Not including subdirs)

 

The script file will be automatically executed at the end & can be found in c:\temp (set in init section) for reviewing.

 

After running the DNBV_RUN function, it will perform a zoom extent & QSAVE on all drawings. If you wish for more commands, you can easily add them to the code.

 

(vl-load-com)
(defun c:dnbv_run ( / TITRE1 TITRE2 ECHELLE_H ss i ent)
   (cond 
      ((not (= (vl-string-search "SAR-01" (getvar "DWGNAME")) nil))
         (setq TITRE1 "STRING 1a"
               TITRE2 "STRING 1b"
               ECHELLE_H "1:100")
      )
      ((not (= (vl-string-search "SAR-02" (getvar "DWGNAME")) nil))
         (setq TITRE1 "STRING 2a"
               TITRE2 "STRING 2b"
               ECHELLE_H "1:20")
      )
   )
   (setq ss (ssget "X" (list (cons 0 "INSERT")))
         i 0)
   (if ss
      (progn
         (while (< i (sslength ss))
            (setq ent (entget (ssname ss i)))
            (while (setq ent (entnext (cdr (assoc -1 ent))))
               (setq ent (entget ent))
               (if (= (cdr (assoc 0 ent)) "ATTRIB")
                  (cond
                     ((= (cdr (assoc 2 ent)) "TITRE1")
                        (setq ent (subst (cons 1 TITRE1)(assoc 1 ent) ent))
                        (entmod ent)
                     )
                     ((= (cdr (assoc 2 ent)) "TITRE2")
                        (setq ent (subst (cons 1 TITRE2)(assoc 1 ent) ent))
                        (entmod ent)
                     )
                     ((= (cdr (assoc 2 ent)) "ECHELLE_H")
                        (setq ent (subst (cons 1 ECHELLE_H)(assoc 1 ent) ent))
                        (entmod ent)
                     )
                  )
               )
            )
            (setq i (1+ i))
         )
      )
   )
   (princ)
)

(defun c:dnbv ( / dir_path scr_path path lsp_file files_list abort msg confirm)
; Drawing Name Based Value's

; --- INIT
   (setq dir_path "C:\\My Documents")              ; starting path for file selection
   (setq scr_path "c:\\temp")                      ; path for writing script
   (setq lsp_file "C:/MyLisp/dnbv.lsp")            ; (Path to) This file

; --- Functions
   (defun get_files ( dr / dt tl fp i l filelist)
      (princ "\nCollecting files: ")
      (setq filelist nil)
      (setq tl (vl-directory-files dr "*.dwg"))                      
      (setq i 0)
      (while (< i (length tl))
         (setq fp (strcat (parse_path dr) "/" (nth i tl)))
         (setq filelist (append filelist (list fp)))
         (setq i (1+ i))
      )
      (princ (itoa (length filelist)))
      filelist
   )

   (defun parse_path (path / temp i)
      (setq i 1 temp "")
      (repeat (strlen path)
         (if (= (substr path i 1) "\\")
            (setq temp (strcat temp "/"))
            (setq temp (strcat temp (substr path i 1)))
         )
         (setq i (1+ i))
      )
      temp
   )

   (defun scrGen ( scrdir f_list lspfile / file_w i f_name iSDI iFILEDIA)
      (setq file_w   (open (strcat scrdir "/dnbv.scr") "w")
            iSDI     (getvar "SDI")
            iFILEDIA (getvar "FILEDIA")
            i 0
      )
      (if (= iSDI 0)(write-line "(setvar \"SDI\" 1)" file_w))
      (if (= iFILEDIA 1)(write-line "(setvar \"FILEDIA\" 0)" file_w))
      (while (< i (length f_list))
         (setq f_name (nth i f_list))
         (write-line (strcat "(command \"_.fileopen\" \"" f_name "\")") file_w)
         (write-line (strcat "(load \"" lspfile "\")") file_w)
         (write-line "(C:dnbv_run)" file_w)
         (write-line "(command \"_.zoom\" \"e\")" file_w)
         (write-line "(command \"_.qsave\")" file_w)
         (setq i (1+ i))
      )
      (if (= iSDI 0)(write-line "(setvar \"SDI\" 0)" file_w))
      (if (= iFILEDIA 1)(write-line "(setvar \"FILEDIA\" 1)" file_w))
      (close file_w)
   )

; *** Main ***   

   (setq path (acet-ui-pickdir "Select directory:" dir_path))                        
   (if path 
      (setq files_list (get_files path))
      (princ "\n*Cancel*")
   )

   (setq abort T)
   (if files_list
      (progn
         (setq msg (strcat "Ok to start processing " (itoa (length files_list)) " file(s) ?"))
         (setq confirm (acet-ui-message msg "Confirmation" 36))
         (if (= confirm 6)
            (progn
               (scrGen scr_path files_list lsp_file)
               (setq abort nil)
            )
            (princ "\n*Cancel*")
         )
      )
   )

   (if (= abort nil)
      (command "_.script" (strcat scr_path "/dnbv"))
   )
   (princ)
)

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

Post to forums  

Forma Design Contest


AutoCAD Beta