Jump to content

Help ~ IF or COND statement??


jonathann3891

Recommended Posts

I've been tinkering with this lisp all week and I feel its almost complete. The only thing left that I want to add is to alert you if you try to run the command in paper space.

 

I'm not sure if I should use a "IF" statement or "COND" statement. I've messed around with both but couldn't get it to work right.

I was trying to use this "IF" statement

(if (= 1 (getvar 'tilemode)

 

Which is best for my situation and how do I implement it?

 

(defun c:3DS (/ sel)
  (initget "3Dsection Restore Help")
  (if (null (setq sel (getkword (strcat "\nSelect Option [Restore View] <3Dsection> :"))))
    (setq sel "3Dsection"))
  (cond
    ((= sel "3Dsection") (3Dsection))
    ((= sel "Restore") (Restore))
    )
  )

; ---------------------------------------------------------------------------------------------------------

(defun 3Dsection (/ )
  (if (= 1 (getvar 'tilemode))
  (setq ocmdecho (getvar 'cmdecho)
	oexpert (getvar 'expert)
	oregenmode (getvar 'regenmode))
  (setvar "cmdecho" 0)
  (setvar "expert" 5)
  (command "-view" "s" "temp") ;;; save view "temp"
  (command "ucs" "s" "temp")  ;;;;  saves ucs to "temp"
  (setq pt1 (getpoint "\nCutline pt1: "))
  (setq pt2 (getpoint pt1 "\nCutline pt2: "))
  (setq pt1 (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2)))
  (setq pt2 (getpoint pt1 "\nSection depth: "))

  (setq pt1 (trans pt1 1 0) pt2 (trans pt2 1 0))
  (command "ucs" "w")
  (setvar "regenmode" 1)
  (command "dview" "" "po" pt2 pt1 "cl" "f" (distance pt2 pt1) "cl" "b" 0 "")

;;;;  move ucs origin to middle of section cut line & sets to view
;;;   this causes grips to be actinve in dview box because ucs origin is in box  

  (command "ucs" "or" pt2)
  (command "ucs" "v")

;;;  by passed next line when added setting ucs to origin.
;;;  (command "ucs" "r" "tmp")   ;;;;  restores ucs to "tmp"

  (setvar 'cmdecho ocmdecho)
  (setvar 'expert oexpert)
  (setvar 'regenmode oregenmode)
  (princ)
  )

; ---------------------------------------------------------------------------------------------------------

(defun restore ()
(if (cdr (assoc 2 (tblsearch "view" "temp")))
  (progn
  (command "view" "r" "temp")
  (command "view" "d" "temp")
  (command "ucs" "d" "temp")
  )
(prompt "\nView not found!")) 
(princ)
)

; ---------------------------------------------------------------------------------------------------------

 

Link to comment
Share on other sites

If you wish to use an if statement, since you will be evaluating multiple expressions when the test expression is validated, you will need to enclose the expressions constituting your 'then' argument within a progn expression, so that this single expression may be supplied as a single argument to the if function, e.g.:

(if (= 1 (getvar 'tilemode))
    (progn
        ;; multiple expressions
    )
    (princ "\nCommand unavailable in Paperspace.")
)

Notice that progn was not required for the 'else' argument, since only a single (princ) expression is being evaluated.

 

Alternatively, since each cond condition accepts multiple arguments, a progn expression is not required, e.g.:

(cond
    (   (= 1 (getvar 'tilemode))
        ;; multiple expressions
    )
    (   (princ "\nCommand not available in Paperspace."))
)

 

Edited by Lee Mac
Link to comment
Share on other sites

2 minutes ago, jonathann3891 said:

Which is best for this particular situation?

 

Both will achieve the desired result with negligible performance difference, and so the decision could be driven by the resulting readability of the code. In this particular case I'd be inclined to suggest an if statement given that there are only two outcomes: the TILEMODE system variable is either equal to 1 or it isn't.

Edited by Lee Mac
Link to comment
Share on other sites

You could also do something like this:

(cond ((= (getvar 'cvport) 1)
       (alert "Code does not work in paperspace! Here we come modelspace! :)")
       (setvar 'tilemode 1)
      )
)

 

  • Funny 2
Link to comment
Share on other sites

1 hour ago, ronjonp said:

You could also do something like this:


(cond ((= (getvar 'cvport) 1)
       (alert "Code does not work in paperspace! Here we come modelspace! :)")
       (setvar 'tilemode 1)
      )
)

 

 

Well it made me chuckle

  • Thanks 1
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...