Update Custom Property - Duplicate Key Error

Update Custom Property - Duplicate Key Error

jlaidle1
Advocate Advocate
1,046 Views
2 Replies
Message 1 of 3

Update Custom Property - Duplicate Key Error

jlaidle1
Advocate
Advocate

I have the below LISP, but gives a duplicate key error if the property already exists.

How can I delete this property before updating the value again?  (DWG_Units is created by another LISP routine)

 

(vl-load-com)
(setq acadObject (vlax-get-acad-object))
(setq acadDocument (vla-get-ActiveDocument acadObject))
(setq dProps (vlax-get-Property acadDocument 'SummaryInfo))
(vla-delete dProps "DWG Units")
(vla-addcustominfo dProps "DWG Units" DWG_UNITS)

 

John Laidler
ITO - Application Management


Please use "Accept as Solution" & give "Kudos" if this response helped you.

0 Likes
Accepted solutions (1)
1,047 Views
2 Replies
Replies (2)
Message 2 of 3

CodeDing
Mentor
Mentor

@jlaidle1 ,

 

Why delete the property then create it again? Just set its value.

Here's the 2 functions I use to access my custom dwg props...

I created them so they're similar to the "getpropertyvalue" and "setpropertyvalue" functions..

(defun GetCustomDwgProp (key / docProps return num keyVal propVal)
  ;key - string, representing key value of custom dwgprop to search for
  ;returns string of dwgprop value if found, or nil
  (setq docProps (vla-get-SummaryInfo (vla-get-ActiveDocument (vlax-get-acad-object))))
  (setq return nil num 0)
  (if (and (>= (setq total (vla-NumCustomInfo docProps)) 1)
	   (eq 'STR (type key)))
    (while (and (< num total) (not return))
      (vla-GetCustomByIndex docProps num 'keyVal 'propVal)
      (if (eq (strcase key) (strcase keyVal)) (setq return propVal))
      (setq num (1+ num))
    );while
  );if
  return
);defun

(defun SetCustomDwgProp (key val / docProps return num keyVal propVal)
  ;key - string, representing key value of custom dwgprop to search for
  ;val - string, representing custom value to be used for the "key" custom dwgprop
  ;returns t if successful, or nil
  (setq docProps (vla-get-SummaryInfo (vla-get-ActiveDocument (vlax-get-acad-object))))
  (setq return nil num 0)
  (if (and (>= (setq total (vla-NumCustomInfo docProps)) 1)
	   (eq 'STR (type key))
	   (eq 'STR (type val)))
    (while (and (< num total) (not return))
      (vla-GetCustomByIndex docProps num 'keyVal 'propVal)
      (if (eq (strcase key) (strcase keyVal))
        (progn
	  (vla-SetCustomByIndex docProps num keyVal val)
	  (setq return t)
        );progn
      );if
      (setq num (1+ num))
    );while
  );if
  return
);defun

Hope they help.

Best,

~DD

Message 3 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You do not need to delete the entry.

Check if it already exists, if so, update the value; else, add a new entry.

(setq acadObject (vlax-get-acad-object))
(setq acadDocument (vla-get-ActiveDocument acadObject))
(setq dProps (vla-get-SummaryInfo acadDocument))
(if (vl-catch-all-error-p
      (vl-catch-all-apply 'vla-GetCustomByKey (list dProps "DWG Units" 'value))
    )
  (vla-AddCustomInfo dProps "DWG Units" DWG_UNITS)
  (if (/= DWG_UNITS value)
    (vla-SetCustomByKey dProps "DWG Units" DWG_UNITS)
  )
)

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub