code can run in cad2013 but not run in cad2016.

code can run in cad2013 but not run in cad2016.

swaywood
Collaborator Collaborator
1,015 Views
4 Replies
Message 1 of 5

code can run in cad2013 but not run in cad2016.

swaywood
Collaborator
Collaborator

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.

0 Likes
Accepted solutions (1)
1,016 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

This behavior has changed since 2015, read HERE 

 

You can use (foreach p ps (command "_non" p))

Message 3 of 5

Ranjit_Singh
Advisor
Advisor

I think the concern here is that the routine ends leaving the command still active? You can replace the (command-s  with (command like this

(defun c:somefunc (/)
 (command "_.pline" p0 "_w" 1 1 "_a")
 (mapcar 'command ps)
(command ""))

 

or something like this

(defun c:somefunc (/)
 (command "_.pline" p0 "_w" 1 1 "_a")
 (mapcar 'command (append ps '(""))))

 

0 Likes
Message 4 of 5

ActivistInvestor
Mentor
Mentor
Accepted solution

@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).

Message 5 of 5

swaywood
Collaborator
Collaborator

Hi, I am so sorry too later to choose your post for a fine solution, thanks again.

0 Likes