TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jsr on October 15, 2012, 02:01:47 AM

Title: Calling different Functions
Post by: jsr 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.
Title: Re: Calling different Functions
Post by: Lee Mac on October 15, 2012, 06:56:42 AM
Pass the functions as arguments to the 'layout iteration routine'?
Title: Re: Calling different Functions
Post by: dgorsman 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.
Title: Re: Calling different Functions
Post by: CAB 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)
  )
)
Title: Re: Calling different Functions
Post by: Lee Mac 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...
Title: Re: Calling different Functions
Post by: CAB 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)
  )
)
Title: Re: Calling different Functions
Post by: jsr on October 15, 2012, 01:05:40 PM
Dear Members

Thank you very much for the valuable comments.