Author Topic: Calling different Functions  (Read 1533 times)

0 Members and 1 Guest are viewing this topic.

jsr

  • Guest
Calling different Functions
« on: October 15, 2012, 02:01:47 AM »
Hi all

I have a routine which iterates through the paper space layouts 1 by 1. I want to be able to call different functions through this routine so that when a layout is activated I can perform different operations before siwtching to next layout. For example for each layout I may want to write some text in it or insert a table or scale a block etc.

For same function call I can do so by mentioning its name in the layout iteration routine. But depending on the situation I may need to call different functions (without changing the iteration lisp file every time).

Thanks.

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Calling different Functions
« Reply #1 on: October 15, 2012, 06:56:42 AM »
Pass the functions as arguments to the 'layout iteration routine'?

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Calling different Functions
« Reply #2 on: October 15, 2012, 09:12:22 AM »
You could also pass a list of integers, with each mapped to a specific function call.  Inside the body of the main function use a (cond...) call to call the appropriate function as you iterate over the list.
If you are going to fly by the seat of your pants, expect friction burns.

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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calling different Functions
« Reply #3 on: October 15, 2012, 09:15:00 AM »
My crude example:
Quote
For same function call I can do so by mentioning its name in the layout iteration routine. But depending on the situation I may need to call different functions (without changing the iteration lisp file every time).

Code: [Select]
(setq its_name "special case")
(layout_iteration_routine its_name)

(defun layout_iteration_routine (flag / )
  (if (= flag "special case")
    (do_special)
    ;; else
    (do_normal)
  )
)
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.

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Calling different Functions
« Reply #4 on: October 15, 2012, 09:20:29 AM »
I was thinking...

Code: [Select]
(defun c:hello ( )
    (alert "Hello World!")
    (princ)
)

(defun c:goodbye ( )
    (alert "Goodbye Cruel World!")
    (princ)
)

(defun layout_iteration_routine ( func )
    ;; ... < do something > ...
    (func)
    ;; ... < do something > ...
)
Code: [Select]
(layout_iteration_routine c:hello)

(layout_iteration_routine c:goodbye)


Or, for multiple functions, perhaps:

Code: [Select]
(defun layout_iteration_routine ( funcs )
    ;; ... < do something > ...
    (mapcar 'eval funcs)
    ;; ... < do something > ...
)
Code: [Select]
(layout_iteration_routine '((c:hello) (c:goodbye)))
Or even:

Code: [Select]
(defun layout_iteration_routine ( funcs )
    ;; ... < do something > ...
    (foreach func funcs ((eval func)))
    ;; ... < do something > ...
)
Code: [Select]
(layout_iteration_routine '(c:hello c:goodbye))
The possible structures / calling methods are endless...
« Last Edit: October 15, 2012, 09:27:04 AM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calling different Functions
« Reply #5 on: October 15, 2012, 09:35:06 AM »
Good explanation Lee.
My other approach would be to use a global variable to steer the routine.
Obviously you may expand the choices and use a COND ILO IF.  8-)

Code: [Select]
(setq its_name "special case")

(defun layout_iteration_routine ( )
  (if (= its_name "special case")
    (do_special)
    ;; else
    (do_normal)
  )
)
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.

jsr

  • Guest
Re: Calling different Functions
« Reply #6 on: October 15, 2012, 01:05:40 PM »
Dear Members

Thank you very much for the valuable comments.