Silent quit

Discussion in 'AutoCAD' started by TCEBob, May 12, 2004.

  1. TCEBob

    TCEBob Guest

    I was going to ask how to do it but found it. What's your reaction to
    the following, regarding embedding setq?

    ;; in reaction to a recent post
    (defun c:c2l( / sset)
    (prompt "\nSelect the target lines: ")
    (if(null(setq sset(ssget '((0 . "LINE"))))) (vl-exit-with-value nil))
    (if(null(setq item(entsel "\nSelect the object to copy:
    ")))(vl-exit-with-value nil))
    (if(null(setq spt(getpoint "\nSpecify base point:
    ")))(vl-exit-with-value nil))
    (princ))

    rs
     
    TCEBob, May 12, 2004
    #1
  2. TCEBob

    ECCAD Guest

    (if(null(setq sset(ssget '((0 . "LINE"))))) (vl-exit-with-value nil))
    ......
    I prefer..
    (if (null
    (setq sset (ssget '((0 . "LINE")))))
    (vl-exit-with-value nil)
    );
    .......... to me is a little easier to read..

    Bob
     
    ECCAD, May 12, 2004
    #2
  3. TCEBob

    Jürg Menzi Guest

    TCEBob

    How about:
    (defun c:c2l( / sset)
    (prompt "\nSelect the target lines: ")
    (cond
    ((not (setq sset (ssget '((0 . "LINE"))))))
    ((not (setq item (entsel "\nSelect the object to copy: "))))
    ((not (setq spt (getpoint "\nSpecify base point: "))))
    (T
    (DoYourStuff)
    )
    )
    (princ)
    )

    Cheers
     
    Jürg Menzi, May 12, 2004
    #3
  4. TCEBob

    TCEBob Guest

    Geez, I like it. Fewer flops.

    rs

     
    TCEBob, May 12, 2004
    #4
  5. TCEBob

    Tom Smith Guest

    I was going to ask how to do it but found it. What's your reaction to
    I don't see where you're going with this. Embedded setq? The following will
    do (I think) the same thing via an if with an and -- ends with a nil return
    if any of the three choices are nil. Otherwise it returns a list of the
    three responses. Your fragment doesn't return anything, but leaves two
    global variables out there.

    (defun decide( / sset item spt)
    (if
    (and
    (progn
    (prompt "\nSelect the target lines: ")
    (setq sset (ssget '((0 . "LINE")))))
    (setq item (entsel "\nSelect the object to copy: "))
    (setq spt (getpoint "\nSpecify base point: ")))
    (list sset item spt)))

    Asuming the purpose is to gather input before doing something, I might call
    it in something like this fashion (allowing for other conditions to be
    checked, etc.):

    (defun c:c2l (/ decide choices)
    (cond
    ((null (conditioncheck))
    nil)
    ((null (setq choices (decide)))
    nil)
    (dowhateverwith choices))
    (princ))

    This avoids the need for a quit/exit/crash method of bailing out of the
    program flow.
     
    Tom Smith, May 12, 2004
    #5
  6. TCEBob

    Jürg Menzi Guest

    And it's not only easier to read - you can apply some messages or other
    reactions:

    (defun MyStartInit ()
    (cond
    ((not (ReadCfgFile)) (alert "Config error..."))
    ((not (InitializeReactors)) (alert "Reactor error..."))
    ((not (ImportLanguageProfiles)) (alert "Language error..."))
    (T
    (DoAllNecessaryScrapOnStartUp)
    )
    )
    (princ)
    )

    Cheers
     
    Jürg Menzi, May 12, 2004
    #6
  7. TCEBob

    Tom Smith Guest

    Great minds think alike :)
    I wrote the cond version before trying it with an if instead. This is my
    favorite way of structureing a lisp. Note in my post I defined the input
    function separately, then called it from a cond much like yours. I prefer
    pulling things like this out of the main cond loop, to keep the primary
    program flow easy to understand at a glance.
     
    Tom Smith, May 12, 2004
    #7
  8. TCEBob

    TCEBob Guest

    Thanks, Tom.

    rs
     
    TCEBob, May 12, 2004
    #8
  9. TCEBob

    Jürg Menzi Guest

    Tom

    This is a snipplet from one of my application startup's to illustrate how
    easy cond checks can be read:

    <snip>
    (setq DwgNme (strcase (getvar "DWGNAME")) ;Read dwg name
    AppKey (strcat "HKEY_CURRENT_USER\\Software" ;Set registry key
    "\\MENZI ENGINEERING GmbH\\MyApp"
    )
    Gb:Lng (vl-registry-read AppKey "ApplicationLanguage") ;Application
    language,
    Gb:App (vl-registry-read AppKey "ApplicationFolder") ;Application path,
    Gb:CdN (vl-registry-read AppKey "ConstDatabase") ;read database folder
    and
    Gb:Ofv (GetOfficeVersion) ;Office version
    TmpVal (vl-registry-read ;Read common files from
    (strcat "HKEY_LOCAL_MACHINE\\Software\\Microsoft" ;registry
    "\\Windows\\CurrentVersion"
    )
    "CommonFilesDir"
    ) ;Set path to ADO-TypeLibrary
    TmpVal (if (and TmpVal (eq (substr TmpVal (strlen TmpVal)) "\\")) ;if
    backslash,
    (substr TmpVal 1 (1- (strlen TmpVal))) ;then... remove
    TmpVal
    )
    AdoTlb (cond (TmpVal (strcat TmpVal "\\System\\ADO\\msado15.dll")) (T
    nil))
    TmpVal (vl-registry-read ;read Excel-path from
    (strcat ;registry
    "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Office\\"
    Gb:Ofv ".0\\Excel\\InstallRoot"
    )
    "Path"
    ) ;Set path for Excel-TypeLibrary
    TmpVal (if (and TmpVal (= (substr TmpVal (strlen TmpVal)) "\\")) ;if
    backslash,,
    (substr TmpVal 1 (1- (strlen TmpVal))) ;then... remove
    TmpVal
    )
    ExcTlb (cond ((> (atof Gb:Ofv) 9.0) "\\Excel.exe") ((strcat "\\Excel"
    Gb:Ofv ".olb")))
    ExcTlb (cond (TmpVal (strcat TmpVal ExcTlb)) (T nil)) ;Excel
    Objektlibrary
    )
    (cond ;Do all checks...
    ((eq (vl-filename-extension DwgNme) ".DWT") ;if template,
    (alert (strcat " Template opened... " ;then... Template message
    " MyApp will not be initialized. "
    )
    )
    )
    ((eq Gb:Ofv "0") ;if no valid office version,
    (alert (strcat " Missing Office installation... " ;then... error message
    "\n Check your Office installation please. "
    )
    )
    )
    ((not (and Gb:Lng Gb:App Gb:CdN)) ;if missing registry keys,
    (alert (strcat " Missing registry items... " ;then... error message
    "\n Check your installation please. "
    )
    )
    )
    ((not (findfile Gb:CdN)) ;if database not found,
    (alert (strcat " Database " ;then... error message
    "'" Gb:CdN "' not found. "
    "\n Check your installation please. "
    )
    )
    )
    ((not (setq Gb:CdO (ConDatabase Gb:CdN Gb:CdO nil))) ;if can't connect
    database,
    (alert (strcat " Can't connect Database " ;then... error message
    "'" Gb:CdN "'"
    "\n Check your installation please. "
    )
    )
    )
    ((not (setq Gb:pmt (ReadPromptList Gb:Lng Gb:CdO))) ;if can't find language
    table,
    (alert (strcat " Language table 'tblCadLispMsg' not found. " ;then... error
    message
    "\n Check your Construction Database please. "
    )
    )
    )
    ((not (findfile "MyApp.dvb")) ;if VBA-module not found,
    (alert "Can't find 'MyApp.dvb") ;then... error message
    )
    ((not (and AdoTlb (findfile AdoTlb))) ;if ADO-TypeLibrary not found,
    (alert "Can't find ADO-Typelibrary") ;then... error message
    )
    ((not (and ExcTlb (findfile ExcTlb))) ;if Excel-TypeLibrary not found,
    (alert "Can't find Excel-Typelibrary") ;then... error message
    )
    (T ;All ok, then...
    (princ "Initializing MyApp...") ;Initialize message
    ...
    )
    )
    <snip>

    Cheers
     
    Jürg Menzi, May 12, 2004
    #9
  10. The traditional Lisp way of doing this would be something like:

    (defun c:c2l(/ sset item spt)
    (prompt "\nSelect the target lines: ")
    (and (setq sset (ssget '((0 . "LINE"))))
    (setq item (entsel "\nSelect the object to copy: "))
    (setq spt (getpoint "\nSpecify base point: "))
    (DoYourStuff))
    (princ))

    --
     
    Martti Halminen, May 14, 2004
    #10
  11. TCEBob

    Jürg Menzi Guest

    Martti

    In this specific case you're right...
    But if you need feedback to an action (see my answer later in this thread)
    you've to use 'cond'.

    Cheers
     
    Jürg Menzi, May 14, 2004
    #11
  12. You can use cond, but that is not the only way to do it:

    (and (test-1 ...)
    (test-2 ...)
    (or (test-3 ...) (feedback-3 ...))
    (or (test-4 ...) (and (feedback-4 ...) nil))
    (protected-operation ...))

    - Here feedback-3 was sure to return nil (unless it corrected the
    problem), feedback-4 wasn't, so it requires an explicit nil to guarantee
    that protected-operation won't run.

    --
     
    Martti Halminen, May 17, 2004
    #12
  13. TCEBob

    Tom Smith Guest

    You can use cond, but that is not the only way to do it:

    There are always different ways to do things. I suppose you could make any
    kind of logical test out of and, or, and not. In this case you've basically
    duplicated a cond construction, except that it's longer and (to my eye) much
    less readable. To compare, placing each distinct statement on a new line:

    (and
    (test-1)
    (test-2)
    (or
    (test-3)
    (feedback-3))
    (or
    (test-4)
    (and
    (feedback-4)
    nil))
    (protected-operation))

    (cond
    ((test-1))
    ((test-2))
    ((test-3)
    (feedback-3))
    ((test-4)
    (feedback-4))
    (protected-operation))

    The one cond does the same work as the two and's plus two or's, in 8
    statements instead of 12, and it doesn't need to be "tricked" on test4. I
    see no reason why cond would be a less desirable construction.
     
    Tom Smith, May 17, 2004
    #13
  14. Note that these are not strictly same: for the cond version you'd have
    to rewrite the tests to produce nil on success, or wrap them in (not ...).

    I'll agree that it gets hairy with complicated choices, but for the
    bare-bones no-feedback version I think

    (cond
    ((not(test-1)))
    ((not(test-2)))
    (protected-operation))

    is less readable than

    (and
    (test-1)
    (test-2)
    (protected-operation))


    Mostly this is a style preference: I'm used to using cond strictly as a
    selection construct, not a fall-through filter. Using and in this kind
    of places is a well-known idiom in Lisp programming generally, I'm not
    sure how often it is used in AutoLisp culture.

    --
     
    Martti Halminen, May 18, 2004
    #14
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.