Author Topic: What is wrong? - Execute a function retrieved from Atoms-Family  (Read 3124 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
What is wrong? - Execute a function retrieved from Atoms-Family
« on: September 28, 2010, 12:22:07 PM »
Given a command function named c:szm:
Code: [Select]
(defun c:szm()(alert "SZM Ran"))

(setq cmd (car (atoms-family 0 "c:szm")))

Returns the atom c:szm.

How can I then call that function from within my code?

I've tried:
(cmd)
and get: ; error: bad function: C:SZM

I've tried:
(eval cmd)
and get: #<USUBR @000000002d71d408 C:SZM>
but the command isn't run.

What am I doing wrong?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #1 on: September 28, 2010, 12:23:16 PM »
(c:szm)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

mkweaver

  • Bull Frog
  • Posts: 352
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #2 on: September 28, 2010, 01:23:49 PM »
MP, Thanks for taking a look at this.

Let me give a little background on what I'm trying to do.

For years I have written all my lisp routines with a brief description of the routine on the same line as the defun, thus:
Code: [Select]
(defun c:ssr(;SHIFT OBJECT RIGHT shift:inc
 )
 (shift (list 0.0 shift:inc))
)

(defun c:ssl(;SHIFT OBJECT LEFT shift:inc
 )
 (shift (list 180.0 shift:inc))
)

(defun c:ssu(;SHIFT OBJECT UP shift:inc
 )
 (shift (list 90.0 shift:inc))
)

(defun c:ssd(;SHIFT OBJECT DOWN shift:inc
 )
 (shift (list 270.0 shift:inc))
)

Now I am writing a routine that will read a lisp file and parse the contents, returning a list of command names and their corresponding descriptions, thus:


Code: [Select]
(GetDefunsAndDescriptions (findfile "shift.lsp"))

(("shft" . "SHIFT OBJECTS")
  ("cshiftinc" . "initialize cstshift:inc")
  ("csr" . "SHIFT OBJECT RIGHT cshift:inc")
  ("csl" . "SHIFT OBJECT LEFT cshift:inc")
  ("csu" . "SHIFT OBJECT UP cshift:inc")
  ("csd" . "SHIFT OBJECT DOWN cshift:inc")
  ("shiftinc" . "initialize stshift:inc")
  ("ssr" . "SHIFT OBJECT RIGHT shift:inc")
  ("ssl" . "SHIFT OBJECT LEFT shift:inc")
  ("ssu" . "SHIFT OBJECT UP shift:inc")
  ("ssd" . "SHIFT OBJECT DOWN shift:inc")
  ("stshiftinc" . "initialize stshift:inc")
  ("stsr" . "stretch right stshift:inc")
  ("stsl" . "stretch left stshift:inc")
  ("stsu" . "stretch up stshift:inc")
  ("stsd" . "stretch down stshift:inc")
  ("stshift" . "stretch shift")
)

Now I am placing this list of command names and their descriptions in an opendcl list box.  This will allow the user to select the comamnd and run it from the opendcl form.  The Atoms-Family chunk of code is being used to verify that the command has been defined.  So, given the name of the function in a variable, how can I call that function?

Thanks for the help
Mike

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #3 on: September 28, 2010, 01:43:56 PM »
Sorry, no time for a detailed answer, you'll have to read between the lines. Crude concept code:

Code: [Select]
(defun foo ( symbol_as_string / symbol cmd terminated_normally )

    (if (wcmatch (strcase symbol_as_string) "C`:*")
        (progn
            (setq
               symbol (read symbol_as_string)
               cmd    (read (strcat (chr 40) symbol_as_string (chr 41)))
            )
            (if (member symbol (atoms-family 0))
                (progn
                    (vl-catch-all-apply
                       '(lambda ( )
                             (eval cmd)
                             (setq terminated_normally t)
                        )
                    )
                    (if (null terminated_normally)
                        (princ
                            (strcat
                                "Ackkk, something effed up when executing <"
                                symbol_as_string
                                ">."
                            )
                        )        
                    )
                )
                (princ (strcat "Command <" symbol_as_string "> not defined."))
            )
        )
        (princ (strcat "Argument <" symbol_as_string "> is not a command."))
        
    )        
    
    (princ)
    
)

Usage:

(foo "c:ssr") ...
« Last Edit: September 28, 2010, 05:34:50 PM by Se7en »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #4 on: September 28, 2010, 01:50:54 PM »
This works: ((eval cmd))

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #5 on: September 28, 2010, 02:01:54 PM »
Might be worth trying (set...) rather than (setq...).  Like others, no time for checking.  Let me know how this turns out - I've been considering loading XML fragments (commented out so they don't interfere) for similar assistance.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

mkweaver

  • Bull Frog
  • Posts: 352
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #6 on: September 28, 2010, 03:02:38 PM »
This works: ((eval cmd))

Yes, that does exactly what I want

Thanks, Roy.
And thanks to MP and dgorsman.

Mike

mkweaver

  • Bull Frog
  • Posts: 352
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #7 on: September 28, 2010, 03:22:59 PM »
Might be worth trying (set...) rather than (setq...).  Like others, no time for checking.  Let me know how this turns out - I've been considering loading XML fragments (commented out so they don't interfere) for similar assistance.

I'm doing a re-write of a routine that has been working well for several months, but had some shortcomings.  We found that the list could get quite long making it difficult to find the routine we wanted.  My thought with the re-write was to add the ability to filter based on the words found in the description.

Screenshot - this is still in the early stages:

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: What is wrong? - Execute a function retrieved from Atoms-Family
« Reply #8 on: September 29, 2010, 02:16:52 PM »
Something you might want to consider is a "floating list" that monitors what gets used.  More frequently used items float up the list.  This would require a separate tracker per-user (HKCU registry, or a file under Documents and Settings\<username>\..., perhaps), but with a little planning you could harvest those files on a regular basis to build a priority list so the overall-most-used items are always at the top.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}