@swaywood wrote:
Hi,
I have a question, the following code run fine in cad2013 but can not run in cad2016.
(command "_.pline" p0 "_w" 1 1 "_a")
(MAPCAR 'COMMAND ps)
(command-s "")
p0 is a point
ps is a list of points and i do not know the number of points.
can anyone tell me why? thanks.
Doing this:
(mapcar 'command '(a b c d e f g))
Is equivalent to this:
(foreach arg '(a b c d e f g)
(command arg)
)
Doing this:
(apply 'command '(a b c d e f g))
Is equivalent to this:
(command a b c d e f g)
IOW, you shouldn't be using (mapcar) at all in this case. You should be using (apply). The use of (mapcar) entails a separate call to (command) for each argument or list element, whereas the use of (apply) entails only a single call to command, passing all of the list elements as arguments.
When using (command-s) you should complete the entire command in a single call, which you can do using (apply) like so (using your example from above):
(apply 'command-s (append '("_.pline") (list p0) '("_w" 1 1 "_a") ps '("")))
The reason you want to avoid one call to command for each list element (the mapcar form), is because for each call to (command) or (command-s) there are many things going on internally. Every time you make a call to (command) or (command-s), AutoCAD has to create and initialize a private instance of the command processor to execute the commands in, and after doing that, it then discards that copy of the command processor before the call returns. There is significant overhead to that, which you can avoid by minimizing calls to (command) or (command-s).