Jump to content

IF and COND - I need another statement...?


mrharris78

Recommended Posts


(if (getvar 'dwgname) "C101")


(progn


(c:tb1)


(if (getvar 'dwgname) "C102")


(progn


(c:tb2)

 

There is something missing above; I am trying to achieve to the following...

 

IF the open drawing has name is "C101" then run this C:TB1....Else......

 

next

 

IF the open drawing has name is "C102" then do this C:TB2....Else......

 

next

 

IF the open drawing has name is "C103" then do this C:TB3....Else......

 

 

Do I need a LOOP???

 

Any hints/advice? Cheers, H

Link to comment
Share on other sites

(cond ((= (getvar "dwgname") "c101")(c:TB1))
     ((= (getvar "dwgname") "c102")(c:TB2))
     ((= (getvar "dwgname") "c103")(c:TB3))
)

 

I'd use "cond" for this.

Link to comment
Share on other sites

(cond ((= (getvar "dwgname") "c101")(c:TB1))
     ((= (getvar "dwgname") "c102")(c:TB2))
     ((= (getvar "dwgname") "c103")(c:TB3))
)

I'd use "cond" for this.

 

While I agree that COND is well suited for this task, I'd simplify further:

 

(vl-load-com)

(defun c:FOO (/ dwgName)
 (cond
   ((vl-string-search "C101" (setq dwgName (strcase (getvar 'dwgname))))
    (c:TB1)
   )
   ((vl-string-search "C102" dwgName )
    (c:TB2)
   )
   ((vl-string-search "C103" dwgName )
    (c:TB3)
   )
   (T (prompt "\n** Drawing name does not match ** "))
 )
 (princ)
)

** Note - It's more efficient to store the DwgName to a variable the first time, than to query the system variable multiple times.

Edited by BlackBox
Changed = to vl-string-search
Link to comment
Share on other sites

It seems to require the .dwg extension in the dwgname but that aside all good.

 

If you didn't want to include the file extension in the test expressions for the cond function:

 

(setq dwg (strcase (cadr (fnsplitl (getvar 'dwgname)))))
(cond
   (   (= dwg "C101") (c:tb1))
   (   (= dwg "C102") (c:tb2))
   (   (= dwg "C103") (c:tb3))
)

Though, since "C101", "C102" etc are string literals, it would be more efficient to include the extension to avoid manipulation of the drawing filename.

 

To demonstrate another method:

 

(eval
   (cdr
       (assoc (strcase (cadr (fnsplitl (getvar 'dwgname))))
          '(
               ("C101" c:tb1)
               ("C102" c:tb2)
               ("C103" c:tb3)
           )
       )
   )
)

Link to comment
Share on other sites

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