Loop problem, insert dwg in a grid like fashion

Loop problem, insert dwg in a grid like fashion

Anonymous
Not applicable
448 Views
3 Replies
Message 1 of 4

Loop problem, insert dwg in a grid like fashion

Anonymous
Not applicable

I have this code which I modified from another question (thanks to @jdiala and to @hmsilvahttp://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/auto-insertion-of-multiple-dwg-into-a...

 

It is supposed to insert every dwg files from a folder in a grid like fashion. After inserting the first dwg at coord (10 , 20 , 0), it increases x by deltax. If x is smaller than a max value of 10000, the loop continues and the second dwg will be inserted at coord (460, 20, 0). After some iteration, x eventually becomes greater than 10000, and x is reset to its original value of 10, and y is increased by deltay to 340. So, the next iteration, the program should insert a dwg at coordinate (10, 340, 0), effectively starting a new row over the first one.

 

But, instead, the program crashes, the message is something like: Error: Incorrect function : 10 . So, it seems it bugs when trying to launch the insert command with a x value of 10.

 

 

 

(defun c:MyInsert ( / x y)
   (vl-load-com)
   (setq x 10)
   (setq y 20)
(setq deltax 450)
(setq deltay 320)
(setq max_x 10000)
(foreach path (vl-directory-files "C:/Users/My Folder/" "*.dwg") (command "-insert" (strcat "C:/Users/My Folder/" path) (list x y 0.0) "" "" "") (setq x (+ x deltax))

(if (> x max_x) (
(setq x 10)
(setq y (+ y deltay))
)
) ) (princ) )

 

Anything? Might be something simple actually, I just don't understand...

 

Regards

 

 

0 Likes
Accepted solutions (1)
449 Views
3 Replies
Replies (3)
Message 2 of 4

hmsilva
Mentor
Mentor
Accepted solution

Try

 

(defun c:MyInsert (/ x y)
   (vl-load-com)
   (setq x 10)
   (setq y 20)
   (setq deltax 450)
   (setq deltay 320)
   (setq max_x 10000)

   (foreach path (vl-directory-files "C:/Users/My Folder/" "*.dwg")
      (command "-insert" (strcat "C:/Users/My Folder/" path) (list x y 0.0) "" "" "")

      (setq x (+ x deltax))

      (if (> x max_x)
         (progn
            (setq x 10)
            (setq y (+ y deltay))
         )
      )
   )
   (princ)
)

 

 

Hope this helps,
Henrique

EESignature

Message 3 of 4

Anonymous
Not applicable
It works! If I understand correctly, in order to be able to evaluate more than one element inside the "if", I must use progn. Thanks a lot!
0 Likes
Message 4 of 4

hmsilva
Mentor
Mentor

@Anonymous wrote:
It works! If I understand correctly, in order to be able to evaluate more than one element inside the "if", I must use progn. Thanks a lot!

You're welcome, Doom1
Glad I could help!

 

Yes, more than one function, we need to use the 'progn' function...

In this case, we don't need to use a 'progn' I did use it to explain the reason for the error, but we are only using the 'setq' function, and the 'if' function could have been written like this:

 

(if (> x max_x)
   (setq x 10
         y (+ y deltay)
   )
)

 

Hope this helps,
Henrique

EESignature