Jump to content

control lisp flow based on command output prompts


samifox

Recommended Posts

Hello everyone,

I need to run a built-in command and decide what to do based on its output.

For instance, keep trying the "Join" command until it shows "0 Joined."

(defun c:JoinUntilZero ()
  (while (/= (getvar 'cmdactive) 0) ; Check if a command is active
    (setq ss (ssget)) ; Prompt the user to select objects
    (if ss ; If a selection set is created
      (progn
        (command "_.JOIN" ss "") ; Execute the "Join" command
        (setq numJoined (getvar 'cmdresult))
        (princ (strcat "\n" numJoined " objects joined.")) ; Display the result
        (if (= numJoined "0") ; Check if no objects were joined
          (progn
            (princ "\nAll objects joined. Exiting.") ; If so, exit the loop
            (setq ss nil) ; Clear the selection set to exit the loop
          )
        )
      )
      (princ "\nNo objects selected. Exiting.") ; If no selection set, exit
    )
  )
  (princ)
)

(c:JoinUntilZero) ; Call the function

 

Link to comment
Share on other sites

I have this that repeats the command 3 times because one purge usually doesn't do it.

 

;;----------------------------------------------------------------------------;;
;; Purge 3 Times
(defun C:PUALL ()
  (repeat 3 (command "_.Purge" "A" "*" "N"))
)

 

it might work with brute force?

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

@samifox There is no system variable called CMDRESULT that I know of in AutoCAD - so your code wont work in that respect. I don't know how else you would do this.

Link to comment
Share on other sites

Maybe LASTPROMPT?

 

-Edit

Never mind that's the last thing the user inputted

Edited by mhupp
Link to comment
Share on other sites

19 minutes ago, mhupp said:

Maybe LASTPROMPT?

 

-Edit

Never mind that's the last thing the user inputted

LOL - I thought about that too but it doesn't work. 😀

Link to comment
Share on other sites

@samifox Honestly I don't understand why you would need this. The JOIN command itself will join multiple lines and Arc sequences together and outputs how many things it joined together into how many polylines. It's not like the old PEDIT command, where you had to "brute force" it as mhupp was saying.

  • Agree 1
Link to comment
Share on other sites

As above, what is your desired result (in words not code)?

 

CAD is in the office tonight, CBAd packing it all up to bring home, but, and without checking these are just ideas:

 

(while (/= (getvar 'cmdactive) 0) ; Check if a command is active
    (setq ss (ssget)) ; Prompt the user to select objects

 

could be

 

(while (setq ss (ssget)) ; Prompt the user to select objects

(if the user selects something continue in the loop. Can then get rid of the next 'if' command

 

(setq numJoined (getvar 'cmdresult))

I'll need the work laptop to confirm what the output from join is - can't remember if it is just a command line prompt or if you can grab it in LISP

 

Edited by Steven P
  • Agree 1
Link to comment
Share on other sites

An update, in the office again....

 

If you want to count how many lines were joined, numJoined, I don't think you can do it with a simple 1 line command. Perhaps some reordering of the code is needed, as far as the user sees, they are only asked to select the line to join TO as an extra thing:

 

To count joining polylines, select the line to join TO,  then the selection set. Get the joining line coordinates (from the selection set) (use mAssoc, a search will get you this) - these are lists. After joining get the (modified) original line coordinates.... which should now contain the coordinates of any joined line. Now compare each list, if the coordinates are in both add a counter for joined lines. If counter = length of selection set then all lines were joined. If counter = 0 then no lines joined. Wonder if that makes sense.. the 'kers want me to do work now, so just a quick brain dump.

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