Jump to content

Recommended Posts

Posted

Ignoring the crudeness of this lisp, I need help with the IF statement. I need to test for the EXISTDWG name and I think the equal is for numbers. How to test for a string?

 

(defun C:layerexport ( / DWGN DWGBLKNAME PATHDWG EXISTDWG)

(command "-layer" "lock" "*" 
      "unlock" "C-ANNO, C-CONC, C-CONC-N" "");layer names for wblock
(command "filedia" "0")

(SETQ     
       DWGN       (GETVAR "DWGNAME")
       DWGBLKNAME (STRCAT "c:/_TEMP/" DWGN)
       PATHDWG    (strcase (strcat DWGBLKNAME));makes string uppercase
       EXISTDWG   (findfile PATHDWG);search for existing dwg or set existdwg to nil if not found
);closes setq
;|
(IF
(equal EXISTDWG nil)
(command "-wblock" EXISTDWG "" "0,0" "all" "")
(command "-wblock" EXISTDWG "y" "" "0,0" "all" "")
);close if
(command "undo" "" "-layer" "unlock" "*" "" "filedia" "1")
); CLOSE DEFUN

Posted
(if
 (setq EXISTDWG
        (findfile (strcase (strcat "c:/_TEMP/" (getvar 'dwgname))))
 )
 ;; then
 ;; else
)

Posted (edited)

It looks like Renderman is getting rid of all the otherwise unused variable names by absorbing all of the other setq actions into just the one. If you use his code as is, be sure to switch your then-else actions. If you prefer them in the order you had, just add “not” (see below).

 

“equal” is for expressions, as is “eq”. The AutoLISP Reference Guide is a good source for this. If you want to check if something is nil, you can just use “not” instead of the equal – nil combo, e.g.,

 

(if (not EXISTDWG)…

If EXISTDWG exists, that is, is non-nil, then the expression returns T and executes the next statement; otherwise it returns nil and executes the second (if there is a second). OOPS! Did I just say that? I meant, if EXISTDWG does NOT exist, that is, is nil, then the expression returns T and executes the next statement; otherwise it returns nil (because it DOES exist) and executes the second statement (if there is a second).

Edited by neophoible
OOPS! Got it reversed.
Posted

Thanks for the help, I will try it out.

 

I have to do the multiple setq actions because I have a hard time keeping track when they are in one line. Maybe some day I will get better at it.

Posted

No worries; I'm always happy to not help. LoL :beer:

Posted
No worries; I'm always happy to not help. LoL :beer:

 

I dunno, Renderman. I find it quite useful to see things like that (reductions in code), as well as exchanges regarding them, even if I was not the OP, or even involved in the discussion at all! That's one of the nice things about these forums. And you will never know how many you've actually helped!:)

Posted

And just to make sure it’s clear, Renderman’s version is slightly shorter, as well as a bit more direct. If you use his method without getting rid of the multiple setq’s, then you simply have

(if EXISTDWG
 ;;then
 ;;else

but remember to reverse your original actions.

Posted
... you will never know how many you've actually helped!:)

 

That is kind of you to say, neophoible :)... We all start somewhere, and there's more than one way to accomplish the same task (more often than not).

 

I would like to believe that rkent already knows that I will always try to be of help when time permits.

Posted

So do I need the test for nil explicitly called out, or is the nil implied?

 

(if
 (setq EXISTDWG
        (findfile (.....

 

Because neophoible wrote

if (not EXISTDWG)…

for the not test, but that doesn't match with the other code with the setq showing.

 

Very confused.

Posted
So do I need the test for nil explicitly called out, or is the nil implied?

 

 

Very confused.

 

Perhaps familiarizing yourself with FINDFILE would help.

 

Basically, so as to not re-type what the help already says in full, if Findfile locates the filename parameter (which can also be a directory path), then T is returned, other wise Nil is returned.

 

By placing the single Setq for EXISTDWG in the If statement's test expression, the value of EXISTDWG (either T, or Nil) is used to evaluate which step to take next.

 

(defun Find (path / found)
 ;; Example:
 ;; (Find "acad.pgp")
 (prompt
   (if (setq found (findfile path))
     "\nHooray, file found! "
     "\nBooooo, file NOT found. "
   )
 )
 (princ)
)

Posted

OK, thanks for the help so far.

 

So I have this (below) and if the drawing file exists, the lisp works as expected (it writes out a file over the top of the existing), if the file doesn't exist it crashes and says drawing namedoes not exist.

 

(defun C:civiltest ( / EXISTDWG)
(command "tilemode" 1)
(command "-layer" "lock" "*" 
   "unlock" "C-ANNO, C-CONC, C-CONC-N" "");layer names for wblock
(setvar "filedia" 0)
(if 
 (setq EXISTDWG
        (findfile (strcase (strcat "c:/_TEMP/" (getvar 'dwgname))))
 )
(command "-wblock" EXISTDWG "y" "" "0,0" "all" "")
(command "-wblock" EXISTDWG     "" "0,0" "all" "")
);CLOSE IF
(command "undo" "" "-layer" "unlock" "*" "" "filedia" "1")
); CLOSE DEFUN

 

If you have the time and patience to help I would appreciate it. I have tried several things but no worky.

Posted

@rkent

if the situation is to test the existence of a file with the same name than the new, and then overwrite,

may be worth changing the system variable "expert" to 2, which suppresses the preceding prompts for

wblock, block or save ...

 

(defun C:layerexport ( / old_exp)
(setq old_exp (getvar "EXPERT"))
(setvar "expert" 2)
(command "-layer" "lock" "*" 
 "unlock" "C-ANNO, C-CONC, C-CONC-N" "");layer names for wblock
(command "-wblock" (STRCAT "c:/_TEMP/" (GETVAR "DWGNAME")) "" "0,0" "all" "")
(command "undo" "" "-layer" "unlock" "*" "" "expert" old_exp)
); CLOSE DEFUN

 

hope that helps

Henrique

Posted

... if the drawing file exists, the lisp works as expected (it writes out a file over the top of the existing), if the file doesn't exist it crashes and says drawing namedoes not exist.

 

The error you're receiving results from this line:

 

(command "-wblock" EXISTDWG     "" "0,0" "all" "")

 

... As you're attempting to supply a variable with value = Nil in lieu of the valid filepath (as string).

 

Instead, consider either reporting the issue to the user (that the file could not be found), or use an alternative filepath.

Posted

Wouldn't EXISTDWG be set to "C:/_TEMP/drawingname" regardless of whether the file is found or not?

 

If not then I need to go back to what I had earlier as I want to create a path and file name based on the current drawing, test whether it exists or not, and wblock out set layers to that name.

 

If the name exists then the wblock command needs the YES for the over write question, if the name doesn't exist then the wblock doesn't need the YES as there is nothing to over write.

Posted

Untested (and obviously without error handling):

 

(defun c:LayerExport (/ dwgName filePath existDwg)
 (command "._-layer" "lock" "*" "unlock" "C-ANNO,C-CONC,C-CONC-N" "")
 (setvar 'filedia 0)
 (setq dwgName (getvar 'dwgname))
 (setq filePath (strcase (strcat "C:\\_temp\\" dwgName)))

 (if (setq existDwg (findfile filePath))
   (command "._-wblock" existDwg "" "0,0" "all" "")
   (command "._-wblock" existDwg "y" "" "0,0" "all" "")
 )
 (setvar 'filedia 1)
 (princ)
)

Posted

RenderMan - I made a change and finally got it, thanks.

 

(defun c:LayerExport (/ dwgName filePath existDwg)
 (setq tmode (getvar 'tilemode))
 (setvar 'tilemode 1)
 (command ".-layer" "lock" "*" "unlock" 
"C-ANNO,C-CONC,C-CONC-N" "");change layer names to specific needs leave double quotes "")
 (setvar 'filedia 0)
 (setq dwgName (getvar 'dwgname))
 (setq existDwg (strcase (strcat "C:\\_temp\\" dwgName))) ;change path to specific needs
   (if (findfile existDwg)
   (command ".-wblock" existDwg "y" "" "0,0" "all" "")
   (command ".-wblock" existDwg "" "0,0" "all" "")
 )
 (setvar 'filedia 1)
 (setvar 'tilemode tmode)
(command "oops" "-layer" "unlock" "*" "")
 (princ)
)

Posted
RenderMan - I made a change and finally got it, thanks.

 

Happy to help, rkent... You may want to localize all of your variables though. :wink:

Posted

I see it wasn’t over yet, but that you managed to get it with more RenderMan help.

 

Did you try hmsilva’s idea? It sounded quite sound. I’ve used it many times.

 

Another option that took me a long while to discover is that you can break up the “command” function responses; thus, as an alternative, you could code it this way:

 

(command ".-wblock" existDwg)
(if (findfile existDwg) (command "y"))
(command "" "0,0" "all" "")

Note that you cannot simply put the “if” portion inside the “command”, as it would return nil where you did not want it to.

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