Remember last input autolisp challenge

Remember last input autolisp challenge

DC-MWA
Collaborator Collaborator
2,579 Views
18 Replies
Message 1 of 19

Remember last input autolisp challenge

DC-MWA
Collaborator
Collaborator

Ok... I got past the dimension edit part.

I now need the program to remeber the last input and make it default until changed by user.

Something like this?

(setq userinput1 (cond ((getkword (strcat "\nSLOPE DESCRIPTION: <" userinput1 ">: ")))
(userinput1)))

 

See attached file

0 Likes
Accepted solutions (2)
2,580 Views
18 Replies
Replies (18)
Message 2 of 19

dbhunia
Advisor
Advisor

Hi

 

To get last object use (entget (entlast))

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 3 of 19

ВeekeeCZ
Consultant
Consultant

Yes, you can do that using the (getkword)... but need to use (initget) as well

 

(or *userinput1*
    (setq *userinput1* "Default description"))
(initget 128)
(setq *userinput1* (cond ((getkword (strcat "\nSLOPE DESCRIPTION: <" *userinput1* ">: ")))
		       (*userinput1*)))

*userinput1* has be a global variable.

0 Likes
Message 4 of 19

dbhunia
Advisor
Advisor

@DC-MWA wrote:

Ok... I got past the dimension edit part.

I now need the program to remeber the last input and make it default until changed by user.

 

 


Hi,

 

Try this

 

(setq userinput1
(progn
(setq val (getstring (strcat "\nSLOPE DESCRIPTION: <" userinput1 ">: ")))
(if (= val "") (setq userinput1 userinput1) (setq userinput1 val))
)
)


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 5 of 19

_gile
Consultant
Consultant

Hi,

 

If you want the default value remains in the drawing between sessions, you can store it in a dictionary instead of a global variable.

 

(setq default (vlax-ldata-get "DC-MWA" "UserInput1" "Default description"))
(if (= "" (setq userinput1 (getstring (strcat "\nSLOPE DESCRIPTION <" default ">: "))))
  (setq userinput1 default)
  (vlax-ldata-put "DC-MWA" "UserInput1" userinput1)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 19

CADaSchtroumpf
Advisor
Advisor

You can also use variable USERS1 to USERS5

(if (eq (getvar "USERS1") "") (setvar "USERS1" "Default"))
(setq userinput1 (getstring (strcat "\nSLOPE DESCRIPTION <" (getvar "USERS1") ">:"))) (if (not (eq userinput1 "")) (setvar "USERS1" userinput1) (setq userinput1 (getvar "USERS1")))
0 Likes
Message 7 of 19

roland.r71
Collaborator
Collaborator

@DC-MWA wrote:

Ok... I got past the dimension edit part.

I now need the program to remeber the last input and make it default until changed by user.

Something like this?

(setq userinput1 (cond ((getkword (strcat "\nSLOPE DESCRIPTION: <" userinput1 ">: ")))
(userinput1)))

 

See attached file


(initget "R G B")
(setq *uc*
   (cond
      ((getkword (strcat "\nChoose [Red/Green/Blue] <"
         (setq *uc* (cond (*uc*) ("R"))) ">: "))
      )
(*uc*)
)
)

Working example.

This allows user to choose a color: Red, Green or Blue. (this is used for marking parts of a drawing. red needs to be dismanteled, Blue is new, Green is for comments/information.)

 

This will show either the previous user choice (stored in global *uc*) -or- the default ("R")

In case the user hits enter, it uses whatever is set (previous or default).

 

 

However, all you are asking for is a string.

So, I would suggest you drop the getkword and replace it by getString. this also makes the initget obsolete.

 

In your case that would look something like:

(setq *userinput1*
   (cond
      ((getstring (strcat "\nSLOPE DESCRIPTION <"
         (setq *userinput1* (cond (*userinput1*) ("default description"))) ">: "))
      )
(*userinput1*)
)
)

 

Note: as stated before, to remember the user input, the variable needs to be a global. Which is indicated by the asterix (before and after). (this is just a visual thing for yourself and others to see it is a global)

 

By default any variable is global, unless you declare it to be local within the function definition.

 

(setq var "\nThis is a global")

(defun c:blahblah ( / var var2)

   (setq var "\nThis is a local variable")

   (setq var1 "\nThis is  global again")

   (setq var2 "\nAnother local variable")

   (princ var)

)

(c:blahblah)

(princ var)

(princ var1)

(princ var2)

 

Results in:

This is a local var

This is a global

This is a global again

nil

 

Note: i used var twice, but they are not the same variable. One is a global, the other is bound to the function, with each its own value. (demonstrated by the nil, instead of "Another local variable" for var2)

0 Likes
Message 8 of 19

ВeekeeCZ
Consultant
Consultant

@ВeekeeCZ wrote:

Yes, you can do that using the (getkword)... but need to use (initget) as well

 

(or *userinput1*
    (setq *userinput1* "Default description"))
(initget 128)
(setq *userinput1* (cond ((getkword (strcat "\nSLOPE DESCRIPTION: <" *userinput1* ">: ")))
		       (*userinput1*)))

*userinput1* has be a global variable.


 

Well, I would say this is nice non-traditional approach... unfortunately it does not allow text with spaces at all.

And since nobody else (just a quick look) posted version that allow text of multiple words... here is one

(defun c:Test ( / tmp)
  (or *userinput1*
      (setq *userinput1* "Default description"))
  (setq *userinput1* (if (= "" (setq tmp (getstring T (strcat "\nSLOPE DESCRIPTION: <" *userinput1* ">: "))))
                       *userinput1*
                       tmp)))

 

Message 9 of 19

ВeekeeCZ
Consultant
Consultant

@roland.r71 wrote:

 

.... However, all you are asking for is a string.

So, I would suggest you drop the getkword and replace it by getString. this also makes the initget obsolete.

 

In your case that would look something like:

(setq *userinput1*
   (cond
      ((getstring (strcat "\nSLOPE DESCRIPTION <"
         (setq *userinput1* (cond (*userinput1*) ("default description"))) ">: "))
      )
(*userinput1*)
)
)

 

Note: as stated before, to remember the user input, the variable needs to be a global. Which is indicated by the asterix (before and after). (this is just a visual thing for yourself and others to see it is a global)

 ....

 

 

Unfortunately this is not working. Empty return of (getstring) is not nil, but "". I think the "cond" method will only work as used in my 1st suggestion. 

0 Likes
Message 10 of 19

roland.r71
Collaborator
Collaborator

Indeed.

& i expected getstring to allow spaces, but it doesn't. (unless you use quotes, but then you need to cut those off the string afterwards)

0 Likes
Message 11 of 19

roland.r71
Collaborator
Collaborator
Accepted solution

Got it working, with cond. 

 

(setq *userinput1*
   (cond 
      ((/= "" (setq tmp (getstring T (strcat "\nSLOPE DESCRIPTION <"
         (setq *userinput1* (cond (*userinput1*)("default description"))) ">: "))))
         tmp
      )
      (*userinput1*)
   )
)
0 Likes
Message 12 of 19

ВeekeeCZ
Consultant
Consultant

@roland.r71 wrote:

Got it working, with cond. 

 

(setq *userinput1*
   (cond 
      ((/= "" (setq tmp (getstring T (strcat "\nSLOPE DESCRIPTION <"
         (setq *userinput1* (cond (*userinput1*)("default description"))) ">: "))))
         tmp
      )
      (*userinput1*)
   )
)

 

You've got my attention but...

The whole point and beauty of the "cond" method is that whole thing can be done with SINGLE global variable. No tmp is necessary.  And as a benefit it's quite short and clean.

What you did is... pointless. No benefit of using the cond compared to if. Actually if seems to me more readable in this case. And it's sorter, less parenthesis required. Do YOU see any benefit of using cond like that?

0 Likes
Message 13 of 19

roland.r71
Collaborator
Collaborator
Accepted solution

I'm not one of them, but some say cond should always be used instead of if.

I wasn't looking for benefits, just to get it working with cond, instead of if. (& i wish i could do it without extra vars)

 

But as for seeing benefits, I do.

...but only for my own getOption function, which allows choosing between getstring, getpoint, getkword & entsel.

 

Beyond that it's a matter of preference, not of one being better as the other.

Message 14 of 19

DC-MWA
Collaborator
Collaborator

This is perfect. Thank you so very much for your assistance!!!

0 Likes
Message 15 of 19

ВeekeeCZ
Consultant
Consultant

@_gile wrote:

Hi,

 

If you want the default value remains in the drawing between sessions, you can store it in a dictionary instead of a global variable.

 

(setq default (vlax-ldata-get "DC-MWA" "UserInput1" "Default description"))
(if (= "" (setq userinput1 (getstring (strcat "\nSLOPE DESCRIPTION <" default ">: "))))
  (setq userinput1 default)
  (vlax-ldata-put "DC-MWA" "UserInput1" userinput1)
)

 

Hi @_gile,

not sure what I'm doing wrong, I tried simple 

(vlax-ldata-put "MD" "Test" "Hello")

and then in another drawing in same session, tried also different session

(vlax-ldata-get "MD" "Test")

but receiving nil. I also tried to save the source drawing... What do I have to do to make it global?

Have C3D as ACAD 2016/w7

0 Likes
Message 16 of 19

_gile
Consultant
Consultant

@ВeekeeCZ I said: "If you want the default value remains in the drawing between sessions", that means the value is registered in this drawing so that you you can get it in this same drawing in another session (i.e. after closing and re-opening the drawing or AutoCAD).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 17 of 19

DC-MWA
Collaborator
Collaborator

Thank you for your input. I may try this in future programs.

0 Likes
Message 18 of 19

Kent1Cooper
Consultant
Consultant

@CADaSchtroumpf wrote:

You can also use variable USERS1 to USERS5


 

Just so you know....  For whatever inexplicable reason, the USERS1-5 [text String] System Variables are not  saved when the drawing is closed, the way the USERR1-5 [Real-number] and USERI1-5 [Integer] ones are, so they will contain only an empty string [""] when it is re-opened.

Kent Cooper, AIA
0 Likes
Message 19 of 19

roland.r71
Collaborator
Collaborator

This code is ment for saving the value with the drawing, so to test it you need the same drawing, new session.

For what you tried (new/other drawing, same session) you should write it to the registry, or a file. (& read the registry/file first. *which requires code to run with every drawing, if it should always work)

 

For such cases i prefer using files (.ini or .csv) as i wan't to keep away from poluting the registry.

0 Likes