Jump to content

Search for Table Style?


Lee Roy

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :thumbsup:

Link to comment
Share on other sites

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 . :oops:

 

But nice of you to provide an alternative nonetheless :thumbsup:

 

Thanks for your kind words Buddy .:)

Link to comment
Share on other sites

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 . :oops:

 

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 :)

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 year later...

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...