Author Topic: i need a script to run a command in a foreach loop, doesn't work  (Read 3871 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Guest
Hello there,


I'm getting a bit desperate here with my short comings.

i need to look into a big problem we have here in  a few hundred drawings.  I need to convert them to  a lower version of a vertical we work with. to do that i thought i will step through the objects one by one, read out whah they are (a duct, an elbow or whatever ) and let the older version of the vertical reproduce the object. i wanted to do that by calling the commend of that vertical. That is written in lisp ( compiled) and asks for user input, so i thought i feed it with a script like that:
Code: [Select]
ROHO
M
200
-325,0,0
325,0,0

and i wanted to make the script on the fly and just overwrite it over and over again. It doesn't work, it does stop after the first script and says it can't open the stream. Then I thought i add a counter and make a different script for every object (a mess, but just to test) it does no not stop anymore but not execute the scripts either, it just runs trough the foreach.

is there any setvar to be done to make script run in vlisp? or what do i do wrong here.








ronjonp

  • Needs a day job
  • Posts: 7529
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #1 on: May 16, 2013, 12:49:37 PM »
Can you post your code that has the "foreach" loop?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Amsterdammed

  • Guest
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #2 on: May 16, 2013, 12:55:20 PM »
Sure, i hope it makes sense to you

Code: [Select]
(defun c:rs()
  (setvar 'expert 2)
(SETQ FLT '((-4 . "<AND")
                 
                  (0 . "insert")
             
                 
(8 . "*577*")

                  (-4 . "AND>")
                  )
            )


(setq ss (ssget "x" flt))
(setq cnt 0)
(foreach x (ss->list ss)
(setq what (get_xdata_tl x "SubType.Descr")
      shape(get_xdata_tl x "Shape.Descr")
      sys (get_xdata_tl x "PipeDef.Descr"))



  (cond( (and (= what  "Luchtkanaal")(= shape  "Rond"))
(setq l (get_xdata_tl x "Length")
      dia(get_xdata_tl x "SizeDescr"))
      (cond ((= sys "Lucht toevoer" )
            ( setq slw "T"))
            ((= sys "Lucht retour" )
            ( setq slw "R")))
(make_script (strcat "ROHO_M_" dia "_" (rtos (- 0 (/ l 2))2 0)",0,0_"(rtos (/ l 2)2 0)",0,0 "))

(command "ucs" "ob" x "")

       
              (command "script" script)
(+cnt)

        ))

  ))

 this makes the script
Code: [Select]
(defun make_script(string)

(setq path (vl-filename-directory
                   (findfile "integraal_ontwerpen.odcl")
                   ) ;_ end of vl-filename-directory
            )             
         
(vl-file-delete script)
      (setq script (strcat path "\\script"(itoa cnt) ".scr"))
      (setq scr (open script "W"))

(setq strings (split string "_"))
      (foreach y strings
      (write-line y scr)
      )
      (close scr)

     



      )

ronjonp

  • Needs a day job
  • Posts: 7529
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #3 on: May 16, 2013, 01:08:18 PM »
Does this "ROHO" command have to be run as a script? It seems that you could just have the script open up a directory of drawings and run RS ... shouldn't RS do all the work? I'm a little lost by the script call.

Quote
_.open "c:\drawing0.dwg"
rs
_.qsave _.close
_.open "c:\drawing1.dwg"
rs
_.qsave _.close
_.open "c:\drawing2.dwg"
rs
_.qsave _.close

Stab in the dark:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:rs (/ cnt dia l script shape slw ss sys what)
  2.   (setvar 'expert 2)
  3.   (setq ss (ssget "x" '((-4 . "<AND") (0 . "insert") (8 . "*577*") (-4 . "AND>"))))
  4.   (setq cnt 0)
  5.   (foreach x (ss->list ss)
  6.     (setq what  (get_xdata_tl x "SubType.Descr")
  7.           shape (get_xdata_tl x "Shape.Descr")
  8.           sys   (get_xdata_tl x "PipeDef.Descr")
  9.     )
  10.     (cond ((and (= what "Luchtkanaal") (= shape "Rond"))
  11.            (setq l   (get_xdata_tl x "Length")
  12.                  dia (get_xdata_tl x "SizeDescr")
  13.            )
  14.            ;; What is this doing? SYS is set but SLW not used anywhere?
  15.            (cond ((= sys "Lucht toevoer") (setq slw "T"))
  16.                  ((= sys "Lucht retour") (setq slw "R"))
  17.            )
  18.            (command "ROHO_M_" dia "_" (rtos (- 0 (/ l 2)) 2 0) ",0,0_" (rtos (/ l 2) 2 0) ",0,0 ")
  19.            (command "ucs" "ob" x "")
  20.            ;; (command "script" script)
  21.            (+cnt)
  22.           )
  23.     )
  24.   )
  25. )
« Last Edit: May 16, 2013, 01:13:02 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Amsterdammed

  • Guest
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #4 on: May 16, 2013, 01:14:12 PM »
that roho stuff is the problem, it asks for user input so i try to get this done by the script.

their newer version is done in .net en then you can run it as something like 
Code: [Select]
(command "roho" "m" (list 0 0 0) (list 500 0 0) "") but the code of their old version is in lisp and needs the input prompt during the command. but even if i replace i try for the sake of testing to draw a line with the script it won't execute, it runs through the ss, does the ucs stuff but no line is drawn.

Amsterdammed

  • Guest
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #5 on: May 16, 2013, 01:20:26 PM »
didn't see your code, the slw is a var set by the vertical for the layer indication, this is a shitty program we have to deal with, so i need to script that roho part or it won't fly. if i do the commond approach like you suggest (was my idea as well) it gives me "unknown command "roho" it needs to run as (c:roho) and if i try (c:roho "m" and the points) it gives me "too many arguments"

ronjonp

  • Needs a day job
  • Posts: 7529
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #6 on: May 16, 2013, 01:50:23 PM »
I think I may have an idea .. off to lunch right now so will post back in a bit.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Amsterdammed

  • Guest
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #7 on: May 16, 2013, 02:13:24 PM »
I'm in Austria in the moment, so i might be out for the day when you are back,. will look at it tomorrow first thing in the morning!


ronjonp

  • Needs a day job
  • Posts: 7529
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #8 on: May 16, 2013, 02:49:15 PM »
Maybe this?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:rs (/ dia file fo l shape slw ss sys what)
  2.   (if (setq ss (ssget "x" '((-4 . "<AND") (0 . "insert") (8 . "*577*") (-4 . "AND>"))))
  3.     (progn (setq file (strcat (getvar 'dwgprefix) "temp.scr"))
  4.            (setvar 'filedia 0)
  5.            (foreach x (ss->list ss)
  6.              (setq what  (get_xdata_tl x "SubType.Descr")
  7.                    shape (get_xdata_tl x "Shape.Descr")
  8.                    sys   (get_xdata_tl x "PipeDef.Descr")
  9.              )
  10.              (cond ((and (= what "Luchtkanaal") (= shape "Rond"))
  11.                     (setq l   (get_xdata_tl x "Length")
  12.                           dia (get_xdata_tl x "SizeDescr")
  13.                     )
  14.                     (cond ((= sys "Lucht toevoer") (setq slw "T"))
  15.                           ((= sys "Lucht retour") (setq slw "R"))
  16.                     )
  17.                     (setq fo (open file "W"))
  18.                     (write-line "ROHO" fo)
  19.                     (write-line "M" fo)
  20.                     (write-line (vl-princ-to-string dia) fo)
  21.                     (write-line (strcat (rtos (- 0 (/ l 2.)) 2 0) ",0,0") fo)
  22.                     (write-line (strcat (rtos (/ l 2.) 2 0) ",0,0") fo)
  23.                     (close fo)
  24.                     (command "_.ucs" "ob" x "")
  25.                     (command "_.-script" file)
  26.                    )
  27.              )
  28.            )
  29.            (setvar 'filedia 1)
  30.            (and (findfile file) (vl-file-delete file))
  31.     )
  32.   )
  33.   (princ)
  34. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Amsterdammed

  • Guest
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #9 on: May 17, 2013, 06:59:30 AM »
Ron,

same thing, same result unfortunately. Now you write the script in the same function, but it stops after the first script. look s like the script is still running while the lisp already wants to rewrite the script (remember, you use the same name.) This is why i tried to make different scripts (wit ha counter) but the no script gets executed. it seems  to me tha tyou can't run multiple script in a lisp, because the cmdactive stays at 4 (script) i tried everything i put in a command after the script, a while with cmdactive 4 (that loop never ends). so i don't l know. i attached the code change to make it draw a line (because yo udon't have the roho command) and the drawing, so you give it a test if you feel like. maybe it is something on my computer, setting or so.

Thanks Bernd

Code: [Select]
(defun c:rs()(if (setq ss (ssget "x" '((-4 . "<AND") (0 . "insert") (8 . "*577*") (-4 . "AND>"))))

    (progn (setq file (strcat (getvar 'dwgprefix) "temp.scr"))

   (setvar 'filedia 0)

   (foreach x (ss->list ss)

     (setq what (get_xdata_tl x "SubType.Descr")

   shape (get_xdata_tl x "Shape.Descr")

   sys (get_xdata_tl x "PipeDef.Descr")

     )

     (cond ((and (= what "Luchtkanaal") (= shape "Rond"))

    (setq l   (get_xdata_tl x "Length")

  dia (get_xdata_tl x "SizeDescr")

    )

    (cond ((= sys "Lucht toevoer") (setq slw "T"))

  ((= sys "Lucht retour") (setq slw "R"))

    )

    (setq fo (open file "W"))

    (write-line "_.line" fo)
;;;
;;;     (write-line "M" fo)
;;;
;;;     (write-line (vl-princ-to-string dia) fo)

    (write-line (strcat (rtos (- 0 (/ l 2.)) 2 0) ",0,0") fo)

    (write-line (strcat (rtos (/ l 2.) 2 0) ",0,0 ") fo)


                   
    (close fo)

    (command "_.ucs" "ob" x "")

    (command "_.script" file)
                 
   )

     )

   )

   (setvar 'filedia 1)

   (and (findfile file) (vl-file-delete file))

    )

  )

  (princ)

  )

ronjonp

  • Needs a day job
  • Posts: 7529
Re: i need a script to run a command in a foreach loop, doesn't work
« Reply #10 on: May 17, 2013, 09:04:15 AM »
Ron,

same thing, same result unfortunately. Now you write the script in the same function, but it stops after the first script. look s like the script is still running while the lisp already wants to rewrite the script (remember, you use the same name.) This is why i tried to make different scripts (wit ha counter) but the no script gets executed. it seems  to me tha tyou can't run multiple script in a lisp, because the cmdactive stays at 4 (script) i tried everything i put in a command after the script, a while with cmdactive 4 (that loop never ends). so i don't l know. i attached the code change to make it draw a line (because yo udon't have the roho command) and the drawing, so you give it a test if you feel like. maybe it is something on my computer, setting or so.

Thanks Bernd

Code: [Select]
(defun c:rs()(if (setq ss (ssget "x" '((-4 . "<AND") (0 . "insert") (8 . "*577*") (-4 . "AND>"))))

    (progn (setq file (strcat (getvar 'dwgprefix) "temp.scr"))

   (setvar 'filedia 0)

   (foreach x (ss->list ss)

     (setq what (get_xdata_tl x "SubType.Descr")

   shape (get_xdata_tl x "Shape.Descr")

   sys (get_xdata_tl x "PipeDef.Descr")

     )

     (cond ((and (= what "Luchtkanaal") (= shape "Rond"))

    (setq l   (get_xdata_tl x "Length")

  dia (get_xdata_tl x "SizeDescr")

    )

    (cond ((= sys "Lucht toevoer") (setq slw "T"))

  ((= sys "Lucht retour") (setq slw "R"))

    )

    (setq fo (open file "W"))

    (write-line "_.line" fo)
;;;
;;;     (write-line "M" fo)
;;;
;;;     (write-line (vl-princ-to-string dia) fo)

    (write-line (strcat (rtos (- 0 (/ l 2.)) 2 0) ",0,0") fo)

    (write-line (strcat (rtos (/ l 2.) 2 0) ",0,0 ") fo)


                   
    (close fo)

    (command "_.ucs" "ob" x "")

    (command "_.script" file)
                 
   )

     )

   )

   (setvar 'filedia 1)

   (and (findfile file) (vl-file-delete file))

    )

  )

  (princ)

  )

I need the get_xdata_tl function as well as "roho" to trouble shoot this any further.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC