Lee Roy Posted September 9, 2011 Posted September 9, 2011 I've been all over Google. It's pointed me here, TheSwamp, CADPanacea, and many more. I have a lisp to create a Table Style. It works. However, I want to add a check to the beginning in case the style already exists. Here is the code I've been using to get this functionality to work, but it's not... (defun c:test () (vl-load-com) (setq StyleName (getstring T "\nName of Table Style: ")) (setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object))) (setq Dict (vla-get-Dictionaries ActDoc)) (setq TabCol (vla-item Dict "ACAD_TABLESTYLE")) (setq TabSty (vla-Item TabCol StyleName)) (if (= nil TabSty) (prompt "does not exist") (prompt "exists") ) (princ) ) I get the error: "Automation error. Key not found" if the style doesn't exist. If it does exist, it says, "exists" as described. What is preventing me from getting the "does not exist" response? Help? Thanks! Quote
pBe Posted September 10, 2011 Posted September 10, 2011 what you need is a lazy mans error checking (defun c:test () (vl-load-com) (setq StyleName (getstring T "\nName of Table Style: ")) (setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object))) (setq Dict (vla-get-Dictionaries ActDoc)) (setq TabCol (vla-item Dict "ACAD_TABLESTYLE")) [color=sienna] (if (vl-catch-all-error-p (setq TabSty (vl-catch-all-apply 'vla-Item (list TabCol StyleName)))) (prompt "does not exist") (prompt "exists") ) [/color] (princ) ) Quote
Tharwat Posted September 10, 2011 Posted September 10, 2011 easy with Vanilla ... (setq StyleName (getstring T "\n Name of Table Style: ")) (setq dic (cdr (assoc -1 (dictsearch (namedobjdict) "ACAD_TABLESTYLE")) ) ) (if (dictsearch dic StyleName) (prompt "exists") (prompt "does not exist") ) Tharwat Quote
pBe Posted September 10, 2011 Posted September 10, 2011 Tharwat, you need to undertstand what the OP is asking for. What is preventing me from getting the "does not exist" response? Help? (vla-Item TabCol StyleName) this line vl-catch-all-error-p in conjunction with vl-catch-all-apply The value of vl-catch-all-apply is in catching errors and allowing your program to continue execution. But nice of you to provide an alternative nonetheless Quote
Tharwat Posted September 10, 2011 Posted September 10, 2011 Tharwat, you need to undertstand what the OP is asking for. (vla-Item TabCol StyleName) this line vl-catch-all-error-p in conjunction with vl-catch-all-apply The value of vl-catch-all-apply is in catching errors and allowing your program to continue execution. I am sorry , I have just taken a look at your changes to OP's codes and thought with Vanilla would be alternative besides that easier . But nice of you to provide an alternative nonetheless Thanks for your kind words Buddy . Quote
pBe Posted September 10, 2011 Posted September 10, 2011 I am sorry , I have just taken a look at your changes to OP's codes and thought with Vanilla would be alternative besides that easier . Dont get me wrong. you did a nice job there providing that snippet. no need to apoligize my friend. the OP is acutally looking for one. Cheers Quote
Lee Roy Posted September 11, 2011 Author Posted September 11, 2011 what you need is a lazy mans error checking (defun c:test () (vl-load-com) (setq stylename (getstring t "\nname of table style: ")) (setq actdoc (vla-get-activedocument (vlax-get-acad-object))) (setq dict (vla-get-dictionaries actdoc)) (setq tabcol (vla-item dict "acad_tablestyle")) [color=sienna] (if (vl-catch-all-error-p (setq tabsty (vl-catch-all-apply 'vla-item (list tabcol stylename)))) (prompt "does not exist") (prompt "exists") ) [/color] (princ) ) thank you!!! Quote
Lee Roy Posted September 13, 2011 Author Posted September 13, 2011 Thanks again. Just updating you how I implemented what you gave me. (while (/= Exists "N") (setq StyleName (getstring T "\nName of Table Style to create: ")) (if (vl-catch-all-error-p (setq TabSty (vl-catch-all-apply 'vla-item (list TabCol StyleName)))) (setq Exists "N") (progn (setq Exists "Y") (prompt (strcat "\n\"" StyleName "\" style already exists. \nPlease use it or delete it and try again.")) (alert (strcat "\"" StyleName "\" style already exists. \nPlease use it or delete it and try again.")) ) ) (princ) ) Quote
Lee Mac Posted September 13, 2011 Posted September 13, 2011 For your consideration: (defun c:test ( / _getitem tab name ) (defun _getitem ( collection item ) (if (not (vl-catch-all-error-p (setq item (vl-catch-all-apply 'vla-item (list collection item)) ) ) ) item ) ) (if (setq tab (_getitem (vla-get-dictionaries (vla-get-activedocument (vlax-get-acad-object)) ) "ACAD_TABLESTYLE" ) ) (progn (while (and (/= "" (setq name (getstring t "\nName of Table Style to Create <Exit>: "))) (_getitem tab name) ) (princ "\nStyle already exists.") ) (if (/= "" name) (princ (strcat "\nYou have selected: " name)) ) ) ) (princ) ) Quote
Lee Roy Posted September 13, 2011 Author Posted September 13, 2011 Whoa, that scares me trying to understand it. I finally did, though. Is there benefit to your method Lee, or is it just another way to skin a cat? Thanks either way; always good to know other methods. I did flip mine around using the not method, like you have yours. (while (/= Exists "N") (setq StyleName (getstring T "\nName of Table Style to create: ")) (if (not (vl-catch-all-error-p (setq TabSty (vl-catch-all-apply 'vla-item (list TabCol StyleName))))) (progn (setq Exists "Y") (prompt (strcat "\n\"" StyleName "\" style already exists. \nPlease use it or delete it and try again.")) (alert (strcat "\"" StyleName "\" style already exists. \nPlease use it or delete it and try again.")) ) (setq Exists "N") ) (princ) ) It makes more sense to me to put the looping prompts in the then rather than the else. I don't know why I didn't do it the first time around. Quote
Lee Mac Posted September 13, 2011 Posted September 13, 2011 Is there benefit to your method Lee, or is it just another way to skin a cat? Thanks either way; always good to know other methods. IMO it is cleaner to use a generic subfunction to retrieve an item from a collection, instead of using the vl-catch-error-p construct repetitively throughout the code. Also, I don't like using extra variables (such as 'Exists') when they can be avoided. Quote
Lee Roy Posted September 13, 2011 Author Posted September 13, 2011 Hmm. Ok, cool, I understand. Thanks! Quote
Morphix Posted April 9, 2013 Posted April 9, 2013 Further to detecting the table how would you then change values in the cells? Many thanks in advance. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.