Author Topic: Automation Error. Duplicate key  (Read 3936 times)

0 Members and 1 Guest are viewing this topic.

cadman6735

  • Guest
Automation Error. Duplicate key
« on: August 31, 2012, 10:24:16 AM »
I am getting this error, I don't know what it means, the macro runs but is followed by this message:  Automation Error. Duplicate key

Code: [Select]
(defun c:test ( / layout)

  (setq layout (vla-get-Layouts (vla-get-ActiveDocument (vlax-get-acad-object))))

  (vlax-for eachLayout layout
    (if (not (eq (strcase (vla-get-Name eachLayout)) "PLOT"))
      (vla-add layout "Plot")
    )
  )

(princ)
)

thanks for any advise

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Automation Error. Duplicate key
« Reply #1 on: August 31, 2012, 10:36:45 AM »
The error is because you are trying to create the layout with the same name multiple times:
Try something like this to add the layout:

Code: [Select]
(setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
(vl-catch-all-apply 'vla-add (list layouts "foo"))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cadman6735

  • Guest
Re: Automation Error. Duplicate key
« Reply #2 on: August 31, 2012, 10:48:51 AM »
Thanks for the reply ronjonp

thanks for the code, it does what I need, but I am not sure why I am trying to add the layout multiple times.

the vlax-for looks for the layout name if there does nothing if not there adds only one layout, I don't see the logic of it trying to add the layout multiple times.  What am I missing?  What part makes it try to add multple times?

Thanks for your advise,

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Automation Error. Duplicate key
« Reply #3 on: August 31, 2012, 11:36:04 AM »
the vlax-for looks for the layout name if there does nothing if not there adds only one layout, I don't see the logic of it trying to add the layout multiple times.  What am I missing?  What part makes it try to add multple times?

The vlax-for expression in your code is iterating over every layout in the Layouts Collection (analogous to a foreach loop for a Collection), and, if the upper-case layout name doesn't equal "PLOT", then it will add a new layout called "PLOT".

Hence, if the first layout isn't "PLOT" then "PLOT" will be added, now, if the second layout doesn't equal "PLOT" then the program will again attempt to create the layout "PLOT", which already exists in the Collection.

You could test for the existence of the layout using vla-item before creating it:
Code: [Select]
(setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-item (list layouts "PLOT")))
    (vla-add layouts "PLOT")
)

However, since the use of vl-catch-all-apply is required for vla-item to prevent an exception should the layout not already exist, you may as well wrap the vla-add in a vl-catch-all-apply statement as ronjonp has demonstrated.

cadman6735

  • Guest
Re: Automation Error. Duplicate key
« Reply #4 on: August 31, 2012, 11:49:13 AM »
Quote
Hence, if the first layout isn't "PLOT" then "PLOT" will be added, now, if the second layout doesn't equal "PLOT" then the program will again attempt to create the layout "PLOT", which already exists in the Collection.

I understand now, can't believe I didn't before asking the question...  You guru's blow my mind at how well you can wright this stuff as if you are writting a letter, this is such a struggle for me.

thanks for the explanation, lee

and

thanks for the code ron

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Automation Error. Duplicate key
« Reply #5 on: August 31, 2012, 12:19:07 PM »
Glad to help out. Here is some more food for thought:

Code: [Select]
(defun _addlayout (name)
  (and (snvalid name)
       (not (vl-position (strcase name) (mapcar 'strcase (layoutlist))))
       (vla-add (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) name)
  )
)
(_addlayout "foo")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Automation Error. Duplicate key
« Reply #6 on: August 31, 2012, 03:32:06 PM »
Quote
Hence, if the first layout isn't "PLOT" then "PLOT" will be added, now, if the second layout doesn't equal "PLOT" then the program will again attempt to create the layout "PLOT", which already exists in the Collection.

I understand now, can't believe I didn't before asking the question...  You guru's blow my mind at how well you can wright this stuff as if you are writting a letter, this is such a struggle for me.

thanks for the explanation, lee

You're very welcome :-)

Don't fret about how long it takes to write a program when you are only just learning the language and programming technique, we've all been there at some point - there will come a moment when the penny will drop and it'll suddenly all make sense to you.  :-)