could someone clarify something

Discussion in 'AutoCAD' started by spencer1971, Jun 7, 2004.

  1. spencer1971

    spencer1971 Guest

    I have written the following lsp (still practicing) and I want the 3 variables in the LSIPIN subroutine(oldorth, oldsnap and clay) to be decalred as local.
    Should I declare these in the main routine (BLINDING) as shown or in the subroutine (LISPIN)..

    (defun lispin ()
    (setq oldorth (getvar "orthomode")
    oldsnap (getvar "osmode")
    clay (getvar "clayer")
    )
    )
    ;----------------------------------------------------------
    (defun lispout ()
    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    (setvar "orthomode" oldorth)
    )
    ;----------------------------------------------------------
    (defun MyError (strErr)
    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    (setvar "orthomode" oldorth)
    )
    ;----------------------------------------------------------
    (defun dtr (a)
    (* pi (/ a 180.0))
    )
    ;----------------------------------------------------------
    (defun C:blinding (/ pt1 pt2 thk1 sc1 oldorth oldsnap clay)
    (lispin)
    (setq *ERROR* MyError)
    (setvar "orthomode" 1)
    (setvar "OSMode" 1)
    (setq pt1 (getpoint "\npick bottom left corner of base/slab: ")
    pt2 (getpoint pt1 "\npick length of base/slab: ")
    thk1 (getint "\ninput depth of screed: <>")
    sc1 (getint "\ndrawing scale: <>")
    )
    (setvar "OSMode" 0)
    (setq Pt3 (polar Pt2 (dtr -90) thk1))
    (setq Pt4 (polar Pt1 (dtr -90) thk1))
    (command "-layer" "m" "defpoints" "")
    (command "pline" pt1 pt2 pt3 pt4 "cl")
    (command "-layer" "m" "hatch" "")
    (command "-layer" "c" "RED" "" "")
    (command "Bhatch" "s" "l" "" "p" "ar-sand" (* 0.05 sc1) 0 "")
    (command "line" pt3 pt4 "")
    (lispout)
    (princ)
    )

    Im trying to get my lisps neater, Any advice?

    Many thanks

    Spencer
     
    spencer1971, Jun 7, 2004
    #1
  2. spencer1971

    David Bethel Guest

    If you declare oldsnap as local in lispin, then lispout will not know
    what oldsnap is.

    Try getting into the habit of using the "_." prefix to
    (command "_.function" ....

    It will force all undefined commands to act properly, and any forign
    lanugae release to preform properly. Same with the international prefix
    "_" with parameters

    Also, you may want to commbine all ofmthe commands

    (command "_.layer" "_m" "defpoints" ""
    "_.pline" pt1 pt2 pt3 pt4 "_cl"
    "_.layer" "_m" "hatch" "_c" "RED" "" "")
    -David
     
    David Bethel, Jun 7, 2004
    #2
  3. spencer1971

    ECCAD Guest

    Spencer,
    What you have is correct.
    IF you were to place the 3 var's in question as 'local' to lispin,
    they would not be available for use in lispout function.
    If you 'pass' along the code to someone, you will have to remember to include the support functions.

    Bob
     
    ECCAD, Jun 7, 2004
    #3
  4. spencer1971

    spencer1971 Guest

    Thank you for your replies
     
    spencer1971, Jun 7, 2004
    #4

  5. The whole idea of local variables is that no other functions can access
    them, so using them to carry data from one function to another is not
    possible.

    One way of doing this is to return the data from lispin in a list, not
    bound to variables:

    (defun lispin ()
    (list (getvar "orthomode")
    (getvar "osmode")
    (getvar "clayer")))

    (defun lispout (data)
    (setvar "orthomode" (car data))
    (setvar "OSMode" (cadr data))
    (setvar "clayer" (caddr data)))

    (defun C:blinding (/ pt1 pt2 thk1 sc1 modedata)
    (setq modedata (lispin))
    ...
    (lispout modedata)
    (princ))

    This is slightly fragile: lispin and lispout have to agree on the
    structure used.

    Another way would be to bring the code from lispin and lispout as
    inline-code to c:blinding, possibly putting its internals as a separate
    function:

    (defun C:blinding ( / oldorth oldsnap clay)
    (setq oldorth (getvar "orthomode")
    oldsnap (getvar "osmode")
    clay (getvar "clayer"))
    (blinding-internal)

    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    (setvar "orthomode" oldorth)
    (princ))

    (defun blinding-internal ( / pt1 pt2 thk1 sc1)
    (setq *ERROR* MyError)
    (setvar "orthomode" 1)
    (setvar "OSMode" 1)
    ...
    (command "line" pt3 pt4 ""))
    Get a Common Lisp styleguide on how the programs should look: typical
    AutoLisp code is pretty horrible to look at as people indent their code
    any way they please. (Hoping the newsreader didn't clobber my
    indentation...)
    Experienced Lisp programmers read the code based on indentation, not on
    the parentheses. Using the C-style hanging parentheses is considered a
    sure sign of a newbie in the CL culture, the Right Way (TM) is to close
    all the relevant parentheses at the end of the same line.

    --
     
    Martti Halminen, Jun 8, 2004
    #5
  6. spencer1971

    spencer1971 Guest

    Many thanks for your time.

    can you reccomend a Common Lisp styleguide or is there something I can download.

    Spencer
     
    spencer1971, Jun 8, 2004
    #6
  7. You could start with

    http://www.music.mcgill.ca/~ferguson/Lisp Docs/Norvig Lisp Tutorials.pdf

    Further material see for example

    http://www.lisp.org/alu/res-lisp-education

    http://cl-cookbook.sourceforge.net/

    - With all CL material you'll notice that it is a far larger language
    than AutoLisp, so only the basics are directly applicable (with a few
    exceptions even there), but the other parts might still be worth a brief
    look to get a feel on the programming style.

    --
     
    Martti Halminen, Jun 9, 2004
    #7
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.