Jump to content

Have command button run lisp function using a variable


Recommended Posts

Posted

I have searched for several days and tried a dozen different ways but, so far, nothing works.

I have many command buttons that execute my custom functions.  Assuming the file is "loaded" a button's macro is simply: ^C^Cfname;

Now, I want a button that will execute the function name stored as a string in USERS1 variable.

The last code I tried is:  ^C^C(read(getvar "USERS1"));

This does, in fact, evaluate to the name of the function (with out quotes) but just sits there.  The function is not executed.

Please, how do I do this very, very simple thing?  I'm pulling out my hair and there isn't much left!

Posted

Try (command (read(getvar "USERS1"))) but note the defun must be a 😄 to work.

Posted

 

 

13 hours ago, JerryFiedler said:

I have searched for several days and tried a dozen different ways but, so far, nothing works.

I have many command buttons that execute my custom functions.  Assuming the file is "loaded" a button's macro is simply: ^C^Cfname;

Now, I want a button that will execute the function name stored as a string in USERS1 variable.

The last code I tried is:  ^C^C(read(getvar "USERS1"));

This does, in fact, evaluate to the name of the function (with out quotes) but just sits there.  The function is not executed.

Please, how do I do this very, very simple thing?  I'm pulling out my hair and there isn't much left!

 

 

hi, not sure useful - click button?

(hp:buttons 
             "Test app" ;; DCL caption - string
             
            '(  (  (getvar "USERS1") (progn (princ "\nUSERS1") (c:function1) )  )
                (  "Button2" (c:function2)  ) 
                (  "Button3" (progn (alert "NP") (startapp "notepad") )  )
                (  "Buttonx" (c:functionx)  )
              )  ;; quoted action list  '( ( <button_caption> (progn <Expression>) ) ( <capt> <expr> ) ( <etc ... >  <etc ... > )  etc... )
              
              20 ; dialog width
              2  ; height button
)

check at forum upper tab Downloads page

 

Posted

Thanks BIGAL,

I have tried your suggestion.  Yes the function is a 😄 definition. For this example the "USERS1" variable is "DLEADER".  I use "read" to remove the quotation marks in the button's macro.

^C^C(command (read (getvar "users1"))) in the button's macro field results in:

error: bad argument value: AutoCAD command: DLEADER

 

When I type DLEADER in the command line the function executes perfectly.

 

Just for laughs I set "USERS1" to "LINE" and tried the button.  As you might expect the result was:

error: bad argument value: AutoCAD command: LINE

 

Obviously this is not the way to run a function using a variable.  I am missing something.  Again today I spent hours searching the web to no avail.

I have another button that loads the file name stored in "users1" and it works.

^C^C(load (getvar "users1"))

I have many buttons to run routines.  The button's macro is:

^C^C(if (null 😄 name)(load "name"));name;

 

 

Thanks hanhphuc,

I have no idea what you wanted me to do with your answer.  But thank you for reading my post and responding it is greatly appreciated.

 

 

Posted (edited)

I only tried it on my osnap defun and it worked. (defun c:47 but I think it depends on your defun if they are a string or like this a number.

 

(setvar 'users1 "line")

(vl-cmdf (getvar 'users1))

 

(setvar 'osmode 0)

(setvar 'users1 "47")

(vl-cmdf (getvar 'users1))

osmode is now 47

Edited by BIGAL
Posted (edited)
3 hours ago, JerryFiedler said:

Thanks BIGAL,

I have tried your suggestion.  Yes the function is a 😄 definition. For this example the "USERS1" variable is "DLEADER".  I use "read" to remove the quotation marks in the button's macro.

 

Thanks hanhphuc,

I have no idea what you wanted me to do with your answer.  But thank you for reading my post and responding it is greatly appreciated.

 

 

 

no worries.. i just offer an alternative dcl buttons (> 15~20++) , as sysvar only can store up to 5 variables (USERS1~5)

it seems you have customized toolbars, so you can just ignore it.

 

so if you wanna assign LISP expressions to toolbar macro, you may refer to DIESEL language

 

eg: "DLEADER" stored in variable "users1", for command

^C^C^P (command (getvar 'users1)) ^P  

 

or you have a function name (c:test)

but you don't want "c:test" but just "test",

so in expression can prefix "c:"

^C^C^P (eval(read(strcat "(C:" (getvar 'users2)")" ))) ^P  

 

 

p/s: i'm not familiar in DIESEL, maybe @steven-g can help u more

 

 

Edited by hanhphuc
Posted

@hanhphuc thanks for the call-out, not sure if I can help this looks to me to be purely a macro to run a Lisp routine and I'm Lispless (LT). But having said that I  see that the OP tried starting the 'line' command using a stored string in the users1 variable, again something that is not available in LT (only userr1-5 and useri1-5), however this Diesel sequence does work to start the line command using a string stored in an environmental variable (test) which may or may not help. PS don't try this in full AutoCAD because I don't think full AutoCAD has access to the "GETENV" command outside of Lisp

^C^C^C$M=$(eval,$(getenv,test))

So I would imagine that this should also work in full AutoCAD using

^C^C^C$M=$(eval,$(getvar,users1))

Unfortunately I can't test it.

  • Thanks 1
Posted

Solved!  Thank you all for an education thread.  The macro below provided by hanhphuc did the trick!!

^C^C^P (eval(read(strcat "(C:" (getvar 'users2)")" ))) ^P

I have not yet tried the other macros suggested by steve-g and BIGAL but intend to do so as a means of increasing my knowledge.

 

Posted

A better way if you just want a certain number of commands to always be displayed then why not make a custom toolbar !

 

I took CIV3D pulled choices out of 3 menus and made 1 new  that I run in my Drafting workspace saving having to change workspaces.

 

I am happy to start you off its simple to do just start CUI and notepad  you copy the details from the cui into notepad making your menu. I know you can do it all in the cui but I find it easier having a custom mnu that's easy to edit.

 

This is a custom toolbar that I made, you can see the commands in it. You just use menuload to add it.

***MENUGROUP=ALANSTOOLBAR
***TOOLBARS
**ALANSTOOLS
ID_ALAN_0    [_Toolbar("Alans1", _Right, _Show, 0, 0, 1)]
AECC_ShowTS  [_Button("Show Toolspace", RCDATA_16_IMAGE, RCDATA_16_IMAGE)]^C^C^C^P_ShowTS
ID_Matchprop [_Button("Match Properties", RCDATA_16_MATCH, RCDATA_16_MATCH)]^C^C_matchprop
ID_Erase     [_Button("Erase", RCDATA_16_ERASE, RCDATA_32_ERASE)]^C^C_erase
ID_Copy      [_Button("Copy", RCDATA_16_COPYOB, RCDATA_32_COPYOB)]$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP),_copy,^C^C_copy)
ID_Mirror    [_Button("Mirror", RCDATA_16_MIRROR, RCDATA_32_MIRROR)]$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP),_mirror,^C^C_mirror)
ID_Offset    [_Button("Offset", RCDATA_16_OFFSET, RCDATA_32_OFFSET)]^C^C_offset
ID_Array     [_Button("Array...", RCDATA_16_ARRREC, RCDATA_32_ARRREC)]^C^C_array
ID_Move      [_Button("Move", RCDATA_16_MOVE, RCDATA_32_MOVE)]$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP),_move,^C^C_move)
ID_Rotate    [_Button("Rotate", RCDATA_16_ROTATE, RCDATA_32_ROTATE)]$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP),_rotate,^C^C_rotate)
ID_Scale     [_Button("Scale", RCDATA_16_SCALE, RCDATA_32_SCALE)]$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP),_scale,^C^C_scale)
ID_Stretch   [_Button("Stretch", RCDATA_16_STRETC, RCDATA_32_STRETC)]$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP),_stretch,^C^C_stretch)
ID_Trim      [_Button("Trim", RCDATA_16_TRIM, RCDATA_32_TRIM)]^C^C_trim
ID_Extend    [_Button("Extend", RCDATA_16_EXTEND, RCDATA_32_EXTEND)]^C^C_extend
ID_BreakSele [_Button("Break at Point", RCDATA_16_BREAKATPT, RCDATA_32_BREAKATPT)]^C^C_break \_f \@
ID_Break     [_Button("Break", RCDATA_16_BRE2PT, RCDATA_32_BRE2PT)]^C^C_break
ID_Join      [_Button("Join", RCDATA_16_JOIN, RCDATA_32_JOIN)]^C^C_join
ID_Chamfer   [_Button("Chamfer", RCDATA_16_CHAMFE, RCDATA_32_CHAMFE)]^C^C_chamfer
ID_Fillet    [_Button("Fillet", RCDATA_16_FILLET, RCDATA_32_FILLET)]^C^C_fillet
ID_Explode   [_Button("Explode", RCDATA_16_EXPLODE, RCDATA_32_EXPLODE)]^C^C_explode
ID_LAYFRZ    [_Button("Layer, Layer Freeze", RCDATA_16_FRZLAY, RCDATA_32_FRZLAY)]^C^C_layfrz
ID_LAYOFF    [_Button("Layer Off", RCDATA_16_OFFLAY, RCDATA_32_OFFLAY)]^C^C_layoff
ID_LAYISO    [_Button("Layer Isolate", RCDATA_16_LAYISO,RCDATA_32_LAYISO)]^C^C_layiso
MM_LAYUNISO  [_Button("Layer Unisolate", RCDATA_16_LAYUNISO, RCDATA_32_LAYUNISO)]^C^C_Layuniso
ID_LAYDEL    [_Button("Layer, Layer Delete", RCDATA_16_LAYER_DELETE,RCDATA_32_LAYER_DELETE)]^C^C_laydel
ID_Line      [_Button("Line", RCDATA_16_LINE,RCDATA_32_LINE)]^C^C_line
MM_1608      [_Button("Circle", RCDATA_16_CIRRAD,RCDATA_32_CIRRAD)]^C^C_circle
MM_1607      [_Button("Arc", RCDATA_16_ARC3PT,RCDATA_32_ARC3PT)]^C^C_arc
ID_Pline     [_Button("Polyline", RCDATA_16_PLINE,RCDATA_32_PLINE)]^C^C_pline
ID_Pedit     [_Button("Polyline Edit", RCDATA_16_PEDIT, RCDATA_32_PEDIT)]^C^C_pedit
ID_DrawordeB [_Button("Draw Order, Send to Back", RCDATA_16_SN2BCK,RCDATA_32_SN2BCK)]^C^C^P_ai_draworder _Back ^P
ID_Appload   [_Button("Load Application...", RCDATA_16_LOAD_APPLICATIONS,RCDATA_16_LOAD_APPLICATIONS)]^C^C_appload

 

 

  • Like 1
Posted
On 4/15/2020 at 4:09 PM, steven-g said:

@hanhphuc thanks for the call-out, not sure if I can help this looks to me to be purely a macro to run a Lisp routine and I'm Lispless (LT). But having said that I  see that the OP tried starting the 'line' command using a stored string in the users1 variable, again something that is not available in LT (only userr1-5 and useri1-5), however this Diesel sequence does work to start the line command using a string stored in an environmental variable (test) which may or may not help. PS don't try this in full AutoCAD because I don't think full AutoCAD has access to the "GETENV" command outside of Lisp


^C^C^C$M=$(eval,$(getenv,test))

So I would imagine that this should also work in full AutoCAD using


^C^C^C$M=$(eval,$(getvar,users1))

Unfortunately I can't test it.

 

@steven-g  Thanks for the lesson, we understand you can't test due to many of us not working at this hard time :)

 

DIESEL only has less than 30 functions 99% are available in LISP,  most useful is edtime with LISP menucmd

macro my $0.02 not too difficult? maybe get used to like ";;;" "^P" "\" " _" "+" "^C^Ctext" 

 

p/s: getenv is valid 

 

 

Posted

The setenv / getenv commands are only available to Lisp in full Autocad not at the command line, in LT they are available at the command line which is probably why the 5 variables users1-5 are not available in LT. This means that when creating macro's like the above examples they may require different code to work. A diesel macro in full Autocad can use Lisp but this won't work in LT. A diesel macro created in LT should work in full Autocad except when it uses the format in the code I posted above 

^C^C^C$M=$(eval,$(getenv,test))

This is a command line call to the getenv command and not a Lisp call, so I would have thought it would fail in full Autocad, are you telling me it does work?

Posted
42 minutes ago, steven-g said:

The setenv / getenv commands are only available to Lisp in full Autocad not at the command line, in LT they are available at the command line which is probably why the 5 variables users1-5 are not available in LT. This means that when creating macro's like the above examples they may require different code to work. A diesel macro in full Autocad can use Lisp but this won't work in LT. A diesel macro created in LT should work in full Autocad except when it uses the format in the code I posted above 


^C^C^C$M=$(eval,$(getenv,test))

This is a command line call to the getenv command and not a Lisp call, so I would have thought it would fail in full Autocad, are you telling me it does work?

 

Thank you for clarification. i never know LT they are available at the command line!

The difference in LISP that we can getenv from setenv. but DIESEL does not have setenv?

so i think above code does not work in full version, i just came out simply ^Princ   ^C^C^P   <LISP_Expression>  ^P

 

 

p/s: as Bricscad newbie, i haven't tried customization, ribbon is something new to me (ac2007)

 

 

Posted
On 4/16/2020 at 6:52 AM, BIGAL said:

A better way if you just want a certain number of commands to always be displayed then why not make a custom toolbar !

 

I took CIV3D pulled choices out of 3 menus and made 1 new  that I run in my Drafting workspace saving having to change workspaces.

 

I am happy to start you off its simple to do just start CUI and notepad  you copy the details from the cui into notepad making your menu. I know you can do it all in the cui but I find it easier having a custom mnu that's easy to edit.

 

This is a custom toolbar that I made, you can see the commands in it. You just use menuload to add it.


***MENUGROUP=ALANSTOOLBAR
***TOOLBARS
**ALANSTOOLS

$M=$(if,$(eq,$(substr,$(getvar,cmdnames),1,4),GRIP)

etc.. 

 

few good marco for reference, this old fashion customize still useful :)

if you have multiple 'commands' in 1 routine, my $0.02 the best way is customize toolbar,

or alternatively perhaps using dcl eg: like @marko_ribar 's routine, you don't need to memorize too many 'commands'

 

 

mr.png.c1097d347562ab9ed2dd3d5d26a1c32f.png

 

 

 

Posted

hanhphuc I agree like the look of Marco's dcl similar to Multi radio buttons with nicer buttons. Its a library routine so limited lisp knowledge required to make it work.

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