Jump to content

Is there a way to exit a loop without escaping a command?


stlo

Recommended Posts

Hi! I would like to create a defined sequence of commands for a new collegue but not with a recorded macro because I don't like the user input feature in those, it's messy (Maybe I'm missing something). If I write a simple routine with back to back commands, am I able at some point to create a loop that would say as an example "REPEAT" the align command "UNTIL" you hit the "Q" key  and it would go automatically to next command so if I have 2 align to perform or 100 the code would loop until I hit the "Q" key without escaping the whole sequence! What I see for the user is that there wouldn't be any command typing, it would all be done automatically, the user would only have to perform the asking predefined commands and exiting some loops by pressing keys! 

Thanks for your time!

Have a good day!

Edited by stlo
Mistype
Link to comment
Share on other sites

Assuming that they're all AutoCAD commands and not LISP commands, perhaps you want something like this? (As soon as you hit escape, it's over)

 

(defun c:foo ( / cmds rep)
    (setq cmds '("MOVE" "COPY" "ALIGN"))	;; Your list of commands to rinse and repeat. Will repeat to the start at the end of the list
    (while
        (progn
            (command (car cmds))
            (while (not (zerop (getvar "cmdactive"))) (command pause))
            (initget "Repeat Next Exit")
            (setq rep (getkword (strcat "\nRepeat " (car cmds) " command or proceed to next? [Repeat/Next/Exit]: <Repeat>: ")))
            (cond 
                ((member rep '("Repeat" nil)))
                ((= rep "Next") (setq cmds (append (cdr cmds) (list (car cmds)))))
            )
        )
    )
    (princ)
)

 

  • Like 1
Link to comment
Share on other sites

LISP commands are called differently, so it wouldn't simply work if you put "YOURLISP" or "C:YOURLISP" to the above. Because they are user-defined functions, you would have to call them like you would any other sub functions.

 

The routine can of course be modified to suit. In my case below, for a LISP command, do not put them in quotations in the list, and supply the full name of your function including the "c:"

 

(defun c:foo ( / cmd cmds rep)
    (setq cmds '("MOVE" "COPY" c:lisp1 c:lisp2 "ALIGN"))	;; Your list of commands to rinse and repeat. Will repeat to the start at the end of the list
    (while
        (progn
            (cond
                (   (eq (type (setq cmd (car cmds))) 'str)
                    (command cmd)
                    (while (not (zerop (getvar "cmdactive"))) (command pause))
                )
                (   (and (eq (type cmd) 'sym) (member (type (eval cmd)) '(subr usubr)))
                    ((eval cmd))
                )
            )
            (initget "Repeat Next Exit")
            (setq rep 
                (getkword 
                    (strcat "\nRepeat " (if (eq (type cmd) 'sym) (vl-string-subst "" "C:" (vl-prin1-to-string cmd)) cmd) " command or proceed to next? [Repeat/Next/Exit]: <Repeat>: ")
                )
            )
            (cond 
                ((member rep '("Repeat" nil)))
                ((= rep "Next") (setq cmds (append (cdr cmds) (list cmd))))
            )
        )
    )
    (princ)
)

 

Edited by Jonathan Handojo
  • Like 1
Link to comment
Share on other sites

One of the simplest is did you pick an object if no then exit

 

(while (setq ent (entsel "\nPick object - Press Enter to exit"))

do your stuff

) ; while

 

  • Like 1
Link to comment
Share on other sites

Thanks for your help guys!!! BIGAL, I will try this in a routine for sure! Jonathan, this is exactly what I was looking for! I love this stuff! I will have a lot of fun trying to build the perfect sequence! Simply Wow!

Have a great evening guys! 

Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...