Author Topic: Creating a User Saved Option within the Command  (Read 1343 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Creating a User Saved Option within the Command
« on: May 05, 2022, 01:53:48 PM »
I hope this makes some sense what I am trying do. I have two routines. Both routines do the same thing however, One is a more simplistic approach to the command, walking the user through what the command does.

I am trying to figure out a method, where once the command is activated, there would be a option or popup check box that can enable an advanced mode. Once the user selects it, then from there out, it remains on that mode.

Thank you for any pointers on this one.

Code: [Select]
(defun c:Erase_All_Lines_Simple ()

(Alert "This command will Automatically Erase All Lines within the drawing.")

(ssget "x" '((0 . "LINE")))

(initcommandversion 2) 
 
(command "erase" "p" "")

(Alert "All lines are now Erased from this drawing.")

(princ))







(defun c:Erase_All_Lines_Advanced ()

(ssget "x" '((0 . "LINE")))

(initcommandversion 2) 
 
(command "erase" "p" "")

(princ))




Civil3D 2020

mhupp

  • Bull Frog
  • Posts: 250
Re: Creating a User Saved Option within the Command
« Reply #1 on: May 05, 2022, 07:00:59 PM »
The CMD-mode lisp saves variables to the computers registry. This means it will persist between drawings.
then if you run the other command depending what mode your in it will display alerts or not.
you could also use ldata instead of setenv/getenv but this will be localized to the drawing.


Code - Auto/Visual Lisp: [Select]
  1. (defun C:CMD-Mode ()
  2.   (initget "Advance Basic")
  3.   (if (setq rep (getkword "\nUser Command Mode [Basic/Advance]<Basic>:"))
  4.     (Progn
  5.       (setenv "Mode" rep)
  6.       (prompt (strcat "User Mode set to: " rep))
  7.     )
  8.     (progn
  9.       (setenv "Mode" "Basic")
  10.       (prompt "\nUser Mode set to: Basic")
  11.     )
  12.   )
  13.   (princ)
  14. )
  15.  
  16. (defun c:Erase_All_Lines ()
  17.   (or (setq rep (getenv "Mode")) (setq rep "Basic")) ;this will default to Basic if CMD-Mode hasn't been run yet.
  18.   (if (eq rep "Basic")
  19.     (Alert "This command will Automatically Erase All Lines within the drawing.")
  20.   )
  21.   (if (setq ss (ssget "X" '((0 . "LINE") (410 . "Model"))))
  22.     (foreach line (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
  23.       (entdel line)
  24.     )
  25.   )  
  26.   (if (eq rep "Basic")
  27.     (Alert "All lines are now Erased from this drawing.
  28.           \nUse \"CMD-Mode\" to disable these prompts")
  29.   )
  30.   (princ)
  31. )
« Last Edit: May 05, 2022, 10:53:56 PM by mhupp »

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Creating a User Saved Option within the Command
« Reply #2 on: May 06, 2022, 11:16:02 AM »
Very interesting. Thank you for this direction. I had no idea you could do something like this.

Would there be a way to have the prompt at the command line show and remember the option that was shown?

Code: [Select]
Command: Erase_All_Lines
User Command Mode [Basic/Advance]<Basic>:A
User Mode set to: Advance
Command:
Command: Erase_All_Lines
User Command Mode [Basic/Advance]<Basic>:*Cancel*  <<--- This would change to Advanced instead of <Basic>?
; error: Function cancelled
Command:


I am also just playing around with it. This is a very good idea.

Code: [Select]
(defun C:Erase_All_Lines ()
  (initget "Advance Basic")
  (if (setq rep (getkword "\nUser Command Mode [Basic/Advance]<Basic>:"))
    (Progn
      (setenv "Mode" rep)
      (prompt (strcat "User Mode set to: " rep))
    )
    (progn
      (setenv "Mode" "Basic")
      (prompt "\nUser Mode set to: Basic")
    )
  )
   (c:Erase_All_Lines_Main_Program)
  (princ)
)
 


(defun c:Erase_All_Lines_Main_Program ()
  (or (setq rep (getenv "Mode")) (setq rep "Basic")) ;this will default to Basic if CMD-Mode hasn't been run yet.
  (if (eq rep "Basic")
    (Alert "This command will Automatically Erase All Lines within the drawing.")
  )
  (if (setq ss (ssget "X" '((0 . "LINE") (410 . "Model"))))
    (foreach line (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (entdel line)
    )
  ) 
  (if (eq rep "Basic")
    (Alert "All lines are now Erased from this drawing.
           \nUse \"Advanced\" to disable these prompts")
  )
  (princ)
)



Civil3D 2020

mhupp

  • Bull Frog
  • Posts: 250
Re: Creating a User Saved Option within the Command
« Reply #3 on: May 06, 2022, 12:55:32 PM »
Would there be a way to have the prompt at the command line show and remember the option that was shown?

You have to use two variables. unless someone knows a better way see code below

User Command Mode [Basic/Advance]<Basic>:*Cancel*  <<--- This would change to Advanced instead of <Basic>?

This would cancel the lisp and the mode wouldn't change and the lines wouldn't be erased.

Also I like your approach more. Tho one suggestion about calling/running another lisp from a lisp.
remove the C: from the second lisp. This will prevent the user from typing that command and skipping the first one.

Code - Auto/Visual Lisp: [Select]
  1. (defun C:Erase_All_Lines (/ *rep rep)
  2.   (or (setq *rep (getenv "Mode")) (setq *rep "Basic"))
  3.   (initget "Advance Basic")
  4.   (if (setq rep (getkword (strcat "\nUser Command Mode [Basic/Advance]<" *rep ">:")))
  5.     (progn
  6.       (setenv "Mode" rep)
  7.       (prompt (strcat "\nUser Mode set to: " rep))
  8.     )
  9.   )
  10.   (Erase_All_Lines_Main_Program)
  11.   (princ)
  12. )
  13.  
  14. (defun Erase_All_Lines_Main_Program (/ ss)
  15.   (or (setq rep (getenv "Mode")) (setq rep "Basic")) ;this will default to Basic if CMD-Mode hasn't been run yet.
  16.   (if (eq rep "Basic")
  17.     (Alert "This command will Automatically Erase All Lines within the drawing.")
  18.   )
  19.   (if (setq ss (ssget "X" '((0 . "LINE") (410 . "Model"))))
  20.     (foreach line (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
  21.       (entdel line)
  22.     )
  23.   )
  24.   (if (eq rep "Basic")
  25.     (Alert "All lines are now Erased from this drawing.
  26.           \nUse \"Advanced\" to disable these prompts")
  27.   )
  28.   (princ)
  29. )

: ERASE_ALL_LINES
User Command Mode [Basic/Advance]<Basic>:a
User Mode set to: Advance
: ERASE_ALL_LINES
User Command Mode [Basic/Advance]<Advance>: *hit enter
*no prompt runs command
*will only prompt if mode changes


also both commands will show up if user has autocomplete on if you leave in the C:
« Last Edit: May 06, 2022, 01:24:51 PM by mhupp »

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Creating a User Saved Option within the Command
« Reply #4 on: May 06, 2022, 01:39:25 PM »
Totally thinking your on the right path. Seems that the options are working! Maybe its me, but i cannot seem to get the lines to erase for the little demo.

So the theory I have with this, is to use this with other custom commands. I want to have a basic mode which for new people can prompt them thru the steps and then the advanced would be without for the other users.
Civil3D 2020

mhupp

  • Bull Frog
  • Posts: 250
Re: Creating a User Saved Option within the Command
« Reply #5 on: May 06, 2022, 02:33:47 PM »
..but i cannot seem to get the lines to erase for the little demo.

My ssget only selects lines in model space. might be polylines ?
Code - Auto/Visual Lisp: [Select]
  1. (setq ss (ssget "X" '((0 . "LINE") (410 . "Model"))))

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Creating a User Saved Option within the Command
« Reply #6 on: May 07, 2022, 03:09:34 AM »
"Tho one suggestion about calling/running another lisp from a lisp."

What about a (load "lisp2") or just use a defun.

(if (not AH:BUTTS)(load "Multigetvals"))

A lisp could be for this task  lisp1 make a choice expert or a beginner, call lisp2 or lisp3 depending on answer.

A man who never made a mistake never made anything

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Creating a User Saved Option within the Command
« Reply #7 on: May 09, 2022, 08:13:45 AM »
Thanks mhupp! Worked! sorry it was a long week again.

BigAl, Interesting to have a popup like that. Just curious but how would this work with storing the variable like mhupp's approach?
Civil3D 2020

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Creating a User Saved Option within the Command
« Reply #8 on: May 09, 2022, 07:19:41 PM »
In the multi radio buttons.lsp whilst the code returns ans behind the scenes the variable BUT holds what button was picked in this case its 1 or 2. So next time will be as per previous pick.

Code: [Select]
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (= but nil)(setq but 1))
(setq ans (ah:butts but "h"  '("Advanced or Basic " "Basic" "advanced"))) ; ans holds the button picked value
« Last Edit: May 09, 2022, 07:22:46 PM by BIGAL »
A man who never made a mistake never made anything