i am working on a lisp to attach an image with
(command "-image" "attach" path "0,0" "1" "0") where 'path' is the prefix and image name set from a dialog box.
how can i get the program to error if the image does not exist- i.e., the name was incorrectly entered? i have tried
(if (= str "*Image not found*")
(alert ":: ERROR :: Image not found")
)
and
(if (/= str "*Image not found*")
(command "-image" "attach" path "0,0" "1" "0")
(alert ":: ERROR :: Image not found")
)
the alert comes up regardless of whether or not the image exists.
Solved! Go to Solution.
Solved by balisteor. Go to Solution.
First, I don't see anywhere that your declaring the value "str"
(= str "*Image not found*");<-- this would be asking if str is equal to the text "*Image not found*" which is not quite what you want.
What you want to try is checking your path variable with (findfile path) and if that returns nil there is no image at the specified path.
Try this.
(if (findfile path) (command "-image" "attach" path "0,0" "1" "0") ; path exists. (alert ":: ERROR :: Image not found"); path not found )
Also known as the "else" in an If-Then-Else
(if (this
(then
(else
);if
if (then or (else requires more than one function or test then you have to start the (then or (else with (progn) and close the progn where you want to proceede to the next level of the if statement (then or else)
What dialog box are you using?
My reason for asking, is that if using a file selection dialog such as GETFILED, filtering for the desired file extensions, then you've already confirmed both the file name, and that it exists:
(defun c:ImageInsert (/ image) (if (setq image (getfiled "Select image" (getvar 'dwgprefix) "gif;jpg;png;tif" 8) ) (command "._undo" "_end" "._undo" "_begin" "._-image" "_attach" image '(0 0 0) 1.0 0.0 "._undo" "_end" ) ) (princ) )
HTH
"How we think determines what we do, and what we do determines what we get."
i am not familiar with getfiled, but will look it up. can this be used in the dcl?
the dialog box has a popup list for the user to select the drawing size (1-5) and two edit boxes: one for the drawing number and one for the number of sheets.
@Anonymous.deleurere wrote:i am not familiar with getfiled, but will look it up. can this be used in the dcl?
the dialog box has a popup list for the user to select the drawing size (1-5) and two edit boxes: one for the drawing number and one for the number of sheets.
... More on GETFILED.
Okay, that's what i was missing, that you're using DCL, which is fine. You might consider using a "..."
(browse) button instead, which employs GETFILED within a sub-function, which returns a valid file name as string to populate your DCL dialog (and to be used on OK for your dialog, etc.):
(defun _BrowseForImage (/ image) (getfiled "Select image" (getvar 'dwgprefix) "gif;jpg;png;tif" 8) )
... Again, this will return a valid string for a selected image file, or Nil.
HTH
"How we think determines what we do, and what we do determines what we get."
findfile works too...
(defun c:InsertImage( / imgName bFound)
(setq imgName "D:\\IMAGE.JPG")
(setq bFound (findfile imgName))
(if bFound
(command "-image" "attach" imgName "0,0" "1" "0")
(alert (strcat "\nCould not find file " imgName ". " ))
)
(princ)
)