Author Topic: Default Lisp to open multiple DWG's and execute a routine in each one  (Read 1413 times)

0 Members and 1 Guest are viewing this topic.

andr3flaviano

  • Guest
Hello, guys!
I'm creating a lisp to open multiple DWG's in a directory and alterate a number at a especific position. I have a function (called txt) to write the number at a position and I have another function (called change_all) to open every DWG in the directory and execute the function "txt".
 I want that the "Change_all" asks me the number and then execute the "txt" to write that number in all DWG's. The functions are already working, but i couldn't call the "txt" with the parameter number previously defined

Here are the code:


(defun c:txt (tval) ; < ---- Value to be writen at a point

     (setq num0 (ssget "_C" '(347.06 18.12) '(350.90 18.12)))  ;<---
     (command ".erase" num0 "" );                               <--- Erase the actual value at the point

     (setq pt1 '(346.5 15.84)) ; <----- The coordinates of the point
 
    (if (not (tblsearch "Layer" "TEXT"))
        (command "-layer" "m" "TEXT" "")
    ) ;_  end if
    (entmake (list '(0 . "TEXT")
               '(8 . "TEXT5")
               (cons 10 pt1)
               (cons 40 4.5)
               (cons 1 tval)
               '(50 . 0.0)
               '(7 . "ROMANS")
               '(71 . 0)
               '(62 . 4)
               '(72 . 0)
               '(73 . 0)
         ) ;_  end list
    ) ;_  end entmake
    (*error* "")
    (princ)
) ;_  end defun




(defun c: change_all (/ dwglist dwgname dwgpre file openfile uhoh len)
 (setq num (getstring t "\nType the numer: ")) ; <-- Asks the value to be written
  (vl-load-com)
  (if (and (setq dwgpre (getvar 'dwgprefix))
      (setq dwglist (vl-sort (vl-remove (strcat dwgpre (getvar 'dwgname))
                    (mapcar '(lambda (dwgname) (strcat dwgpre dwgname))
                       (vl-directory-files dwgpre "*.dwg")
                    )
              )
              '<
          )
      )
      (setq uhoh
        "Readonly drawings will not be processed!"
      )
      (setq len (strlen uhoh))
      )
    (progn (setq openfile (open (setq file (strcat dwgpre "myscript.scr")) "w"))
      (progn (foreach f dwglist
          (if   (and
           (not (Is_ReadOnly f))
           (/= (checkAttFile f) 1)
         )
            (progn
         (write-line (strcat "_.open \"" f "\"") openfile)
         (write-line "(C: (txt num))" openfile)   ;<-- Should open multiple DWG’s and execute the function “txt” with num as tval. but it don't work...
         (write-line "_.qsave _.close" openfile)
            )
            (setq uhoh (strcat uhoh "\n" f))
          )
        )
        (close openfile)
        (command "_.script" file)
        (if (= (strlen uhoh) len)
          (princ "All drawings successfully processed...")
          (alert uhoh)
        )
      )
    )
  )
  (princ)
) ; End Defun

Could someone help me, please?
« Last Edit: October 06, 2016, 01:30:37 PM by andr3flaviano »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Default Lisp to open multiple DWG's and execute a routine in each one
« Reply #1 on: October 06, 2016, 03:57:07 PM »
Why do you post the same problem 3(!) times (see: here and here)?

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Default Lisp to open multiple DWG's and execute a routine in each one
« Reply #2 on: October 06, 2016, 05:02:35 PM »
Here are some my revisions (untested) :

Code: [Select]
(defun txt ( tval / num0 pt1 ) ; < ---- Value to be writen at a point

  (setq num0 (ssget "_C" '(347.06 18.12) '(350.90 18.12)))  ;<---
  (command "_.erase" num0 "");                               <--- Erase the actual value at the point

  (setq pt1 '(346.5 15.84)) ; <----- The coordinates of the point
 
  (if (not (tblsearch "Layer" "TEXT"))
    (command "_.-layer" "_m" "TEXT" "")
  ) ;_  end if
  (entmake (list '(0 . "TEXT")
                 '(8 . "TEXT") ; <------ "TEXT5" was probably mistake - according to previous statements Layer "TEXT" should be specified
                 (cons 10 pt1)
                 (cons 40 4.5)
                 (cons 1 tval)
                 '(50 . 0.0)
                 '(7 . "Standard") ; <----- Put "ROMANS" back - for testing purposes switched to "Standard"
                 '(71 . 0)
                 '(62 . 4)
                 '(72 . 0)
                 '(73 . 0)
           ) ;_  end list
  ) ;_  end entmake
  (princ)
) ;_  end defun

(defun c:change_all ( / num dwglist dwgname dwgpre file openfile uhoh len )
  (setq num (getstring t "\nType the numer: ")) ; <-- Asks the value to be written
  (vl-load-com)
  (if (and (setq dwgpre (getvar 'dwgprefix))
      (setq dwglist (vl-sort (vl-remove (strcat dwgpre (getvar 'dwgname))
                    (mapcar '(lambda (dwgname) (strcat dwgpre dwgname))
                       (vl-directory-files dwgpre "*.dwg")
                    )
              )
              '<
          )
      )
      ;| ------------------------------------ Excluded for testing purposes
      (setq uhoh
        "Readonly drawings will not be processed!"
      )
      (setq len (strlen uhoh))
      ------------------------------------- Excluded for testing purposes |;
      )
    (progn (setq openfile (open (setq file (strcat dwgpre "myscript.scr")) "w"))
      (progn (foreach f dwglist
          ;| --------------------------------- Excluded for testing purposes
          (if (and
              (not (Is_ReadOnly f))
              (/= (checkAttFile f) 1)
              )
           --------------------------------- Excluded for testing purposes |;
           (progn
             (write-line (strcat "_.open \"" f "\"") openfile)
             (write-line "(defun txt ( tval / num0 pt1 )" openfile)
             (write-line "(setq num0 (ssget \"_C\" '(347.06 18.12) '(350.90 18.12)))" openfile)
             (write-line "(command \"_.erase\" num0 \"\")" openfile)
             (write-line "(setq pt1 '(346.5 15.84))" openfile)
             (write-line "(if (not (tblsearch \"Layer\" \"TEXT\")) (command \"_.-layer\" \"_m\" \"TEXT\" \"\") )" openfile)
             (write-line "(entmake (list '(0 . \"TEXT\") '(8 . \"TEXT\") (cons 10 pt1) (cons 40 4.5) (cons 1 tval) '(50 . 0.0) '(7 . "Standard") ;| Put \"ROMANS\" instead of \"Standard\" which is used for testing purposes |; '(71 . 0) '(62 . 4) '(72 . 0) '(73 . 0)))" openfile)
             (write-line "(princ))" openfile)
             (prin1 (cons 'txt (list num)) openfile)   ;<-- Should open multiple DWG’s and execute the function “txt” with num as tval. but it don't work...
             (princ "\n" openfile)
             (write-line "_.qsave _.close" openfile)
           )
            ;;;(setq uhoh (strcat uhoh "\n" f)) Commented line for testing puposes
          ;;;) Commented bracket
        )
        (close openfile)
        (command "_.script" file)
        ;| ---------------------------------- Excluded for testing purposes
        (if (= (strlen uhoh) len)
          (princ "All drawings successfully processed...")
          (alert uhoh)
        )
        ----------------------------------- Excluded for testing purposes |;
      )
    )
  )
  (princ)
) ; End Defun
« Last Edit: October 08, 2016, 08:08:48 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube