Author Topic: execute a function from within another  (Read 3424 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
execute a function from within another
« on: November 30, 2004, 11:26:44 AM »
Let's say I have a function loaded called 'hello' defined as;
Code: [Select]

(defun c:hello () (print "hello")(princ))

and I want to execute this function from another function (run-it), only the argument to the 'run-it' function is a string. i.e. "c:hello". How can I execute 'hello' from 'run-it'?

Example:
Code: [Select]

(defun run-it (cmd_str)
   ((read cmd_str)); does not work
   (command (read smd_str)); does not work
   )

 (run-it "c:hello")
TheSwamp.org  (serving the CAD community since 2003)

David Bethel

  • Swamp Rat
  • Posts: 656
execute a function from within another
« Reply #1 on: November 30, 2004, 11:40:29 AM »
Code: [Select]

(eval (list (read "c:hello")))


Should work.  -David
R12 Dos - A2K

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
execute a function from within another
« Reply #2 on: November 30, 2004, 11:52:06 AM »
Thank you sir. Works like a champ!
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
execute a function from within another
« Reply #3 on: November 30, 2004, 12:55:46 PM »
Code: [Select]
(eval (substr cmd_str 3))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
execute a function from within another
« Reply #4 on: November 30, 2004, 01:23:31 PM »
wait a min. Why wont "(c:hello)" work?

...i dont get it.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
execute a function from within another
« Reply #5 on: November 30, 2004, 01:46:00 PM »
S7

(command ...) won't evaluate a string. The paranethisis in the string are taken literally.  -David
R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
execute a function from within another
« Reply #6 on: November 30, 2004, 02:01:42 PM »
David, (sorry man) now im even more lost. :lol:

AutoCAD menu utilities loaded.
Command: (defun c:hello () (print "hello")(princ)) C:HELLO

Command: (defun tester (str)
(_> (cond ((eq str "c:hello")
(((_> (c:hello))
((_> )
(_> )
TESTER

Command: (tester "c:hello")

"hello"

Command: (tester "c:hell")
nil
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
execute a function from within another
« Reply #7 on: November 30, 2004, 02:05:13 PM »
...Just slap me if im being to "simple minded" about all of this. I just dont see why a quick conditional wouldnt do just fine.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
execute a function from within another
« Reply #8 on: November 30, 2004, 03:13:38 PM »
Se7en,

I was under the impression that Mark was looking for a generic loader based a function's string name:

Code: [Select]

(defun run (p)
   (eval (list (read p))))

(defun my_line ()
   (entmake '((0 . "LINE")(10 0 0 0)(11 1 1 0))))

(defun my_circle ()
   (entmake '((0 . "CIRCLE")(10 0 0 0)(40 . 1))))


Code: [Select]

  (initget 1 "Line Circle")
  (setq func (getkword "\nMake Line/Circle (L/C):   "))
  (run (strcat "my_" func))



Using a conditional test would require a statement for each program.

You could add some parameter testing to (run), but I haven't found a sure fired way to determtine if the parameter is a valid AutoLisp call

Code: [Select]

(and (= (type p) 'STR)
       (= (type (eval (list (read p))) 'LIST)
       (member p (atoms-family 1))
;.....
       (eval (list (read p))))

)



-David
R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
execute a function from within another
« Reply #9 on: November 30, 2004, 03:17:45 PM »
Ahhh! I get cha now!

Well in that case: (eval (list (read "c:hello")))

:lol:
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org