lisp routine will not work

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

  1. bobbyjoe

    bobbyjoe Guest

    I am learning auto lisp. I located this routine in a book. I cannot get it to work. Any suggestions? Thank you! Please see below.

    (defun c:makeslot (/ np1 np2 cp1 cp2 angl anw crad lp1 lp2 op1 tp1 tp2)
    (setvar "OSMODE" 512)
    (setq NP1 (getpoint "\nPick a point on the first circle: ")
    NP2 (getpoint "\nPick a point on the second circle: ")
    )
    (setvar "OSMODE" 0)
    (setq CP1 (osnap NP1 "cen")
    cp2 (osnap NP2 "cen")
    ANGL (angle CP1 CP2)
    ANGW (+ (* 0.5 pi) ANGL)
    CRAD (distance CP1 (osnap CP1 "per"))
    LP1 (polar CP1 ANGW CRAD)
    LP2 (polar LP1 ANGL (distance CP1 CP2))
    )
    (command ".line" LP1 LP2 ""
    ".select" "l" ""
    )
    (setq OP1 (osnap LP1 "mid"))
    (command ".offset" (* 2 CRAD) OP1 CP1 "")
    (setq TP1 (polar CP1 ANGL CRAD)
    TP2 (polar CP2 (+ pi ANGL) CRAD)
    )
    (command ".trim" "p" "l" "" TP1 TP2 "")
    (prompt "\ndone.\n")
    )
     
    bobbyjoe, Jun 7, 2004
    #1
  2. bobbyjoe

    Paul Turvill Guest

    Change the line
    CRAD (distance CP1 (osnap CP1 "per"))

    to
    CRAD (distance CP1 (osnap NP1 "per"))
    ___

    to work. Any suggestions? Thank you! Please see below.
     
    Paul Turvill, Jun 7, 2004
    #2
  3. bobbyjoe

    bobbyjoe Guest

    Paul,
    Thank you. I went over this to see if all was okay several times. Obviously, I did not see that error. Again, thank you!
     
    bobbyjoe, Jun 7, 2004
    #3
  4. bobbyjoe

    Adesu Guest

    Hi bobbyjoe , I try added to enjoy it
    Best regards

    ; ms is stand for makeslot
    ; Desin by bobbyjoe <>
    ; Edit & added by Ade Suharna
    ; 8 June 2004
    (defun c:ms ()
    (vl-load-com)
    (setvar "blipmode" 1)
    (setq loc1 (getpoint "\nClick location for first circle: "))
    (setq loc2 (getpoint "\nClick location for second circle: "))
    (setq rad (getreal "\nEnter radius: "))
    (command ".circle" loc1 rad)
    (command ".circle" loc2 rad)
    (setvar "OSMODE" 512)
    (setq NP1 (getpoint "\nPick a point on the first circle: "))
    (setq NP2 (getpoint "\nPick a point on the second circle: "))
    (setvar "OSMODE" 0)
    (setq CP1 (osnap NP1 "cen"))
    (setq CP2 (osnap NP2 "cen"))
    (setq ANGL (angle CP1 CP2))
    (setq ANGW (+ (* 0.5 pi) ANGL))
    (setq CRAD (distance CP1 (osnap NP1 "per")))
    (setq LP1 (polar CP1 ANGW CRAD))
    (setq LP2 (polar LP1 ANGL (distance CP1 CP2)))
    (command ".line" LP1 LP2 "" ".select" "l" "" )
    (setq OP1 (osnap LP1 "mid"))
    (command ".offset" (* 2 CRAD) OP1 CP1 "")
    (setq TP1 (polar CP1 ANGL CRAD))
    (setq TP2 (polar CP2 (+ pi ANGL) CRAD))
    (command ".trim" "p" "l" "" TP1 TP2 "")
    (command "redraw")
    (prompt "\ndone.\n")
    )
    Obviously, I did not see that error. Again, thank you!
     
    Adesu, Jun 8, 2004
    #4
  5. bobbyjoe

    Paul Turvill Guest

    One suggestion, Adesu: since you're using the routine to draw the circles in
    the first place, it shouldn't be necessary to prompt the user to select
    them. You should be able to devise a way to skip that unnecessary step and
    still get the same result.
    ___
     
    Paul Turvill, Jun 8, 2004
    #5
  6. bobbyjoe

    Paul Turvill Guest

    Here's an example of what I mean:

    (defun c:ms (/ bm om cp1 cp2 crad angl angw lp1 lp2 op1 tp1 tp2)
    (setq bm (getvar "blipmode")
    om (getvar "osmode")
    )
    (setvar "blipmode" 1)
    (setvar "osmode" 0)
    (setq cp1 (getpoint "\nClick location for first circle: ")
    cp2 (getpoint "\nClick location for second circle: ")
    crad (getdist cp2 "\nEnter radius: ")
    )
    (command ".circle" cp1 crad
    ".circle" cp2 crad)
    (setq ANGL (angle CP1 CP2)
    ANGW (+ (* 0.5 pi) ANGL)
    LP1 (polar CP1 ANGW CRAD)
    LP2 (polar LP1 ANGL (distance CP1 CP2)))
    (command ".line" LP1 LP2 "" ".select" "l" "" )
    (setq OP1 (osnap LP1 "mid"))
    (command ".offset" (* 2 CRAD) OP1 CP1 "")
    (setq TP1 (polar CP1 ANGL CRAD)
    TP2 (polar CP2 (+ pi ANGL) CRAD))
    (command ".trim" "p" "l" "" TP1 TP2 "")
    (command "redraw")
    (setvar "blipmode" bm)
    (setvar "osmode" om)
    (princ)
    )

    I've also localized variables, and added code to restore the system
    variables modified by the system. The routine could be further improved by
    avoiding most or all instances of the (command ...) function.
    ___
     
    Paul Turvill, Jun 8, 2004
    #6
  7. bobbyjoe

    Adesu Guest

    Hi Paul, why do you write this,what your argument to tell me alot

    (setq bm (getvar "blipmode")
    om (getvar "osmode"))
    (setvar "blipmode" 1)
    (setvar "osmode" 0)

    Generally someone's write like this

    (setvar "blipmode" 1)
    (setvar "osmode" 0)
    it's more simple and nice to look,then easy to learn for beginner lisper
    Best regards
    Ade Suharna
     
    Adesu, Jun 8, 2004
    #7
  8. bobbyjoe

    Paul Turvill Guest

    By saving the *current* settings as variables, you can then restore them at
    the end of the routine. It's just good programming practice to return the
    system state to its original settings when closing a routine. See the
    (setvar ...) statements near the end of the routine I posted.
    ___
     
    Paul Turvill, Jun 8, 2004
    #8
  9. bobbyjoe

    Paul Turvill Guest

    .... and here's a version that not only avoids the (command ...) function,
    but draws the slot as a single closed polyline; much neater and less error
    prone:

    (defun C:SLOT (/ bm cp1 cp2 wid ang ang1 lp1 lp2 lp3 lp4)
    (setq bm (getvar "blipmode"))
    (setvar "blipmode" 1)
    (setq cp1 (getpoint "\nFirst end center point: ")
    cp2 (getpoint "\nSecond end center point: ")
    wid (getdist cp2 "\nSlot width: ")
    ang (angle cp1 cp2)
    ang1 (- ang (* 0.5 pi))
    lp1 (polar cp1 ang1 (/ wid 2.0))
    lp2 (polar lp1 ang (distance cp1 cp2))
    lp3 (polar lp2 (+ pi ang1) wid)
    lp4 (polar lp1 (+ pi ang1) wid)
    );setq
    (entmake
    (list
    '(0 . "LWPOLYLINE")
    '(100 . "AcDbEntity")
    '(100 . "AcDbPolyline")
    '(90 . 4)
    '(70 . 129)
    (cons 10 lp1)
    (cons 10 lp2)
    '(42 . 1.0)
    (cons 10 lp3)
    (cons 10 lp4)
    '(42 . 1.0)
    );list
    );entmake
    (redraw)
    (setvar "blipmode" bm)
    (princ)
    )

    Settings for BLIPMODE (as well as the (redraw)) could also be eliminated,
    but I find it helpful to be able to see the endpoint picks when inputting
    the required data. I changed the command name to SLOT because MS conflicts
    for the shorthand ModelSpace command.
    ___
     
    Paul Turvill, Jun 8, 2004
    #9
  10. bobbyjoe

    David Kozina Guest

    I bet using mlines (with a semicircular endcap style) for this would be
    neater still... :)
    YMMV

    Best regards,
    David Kozina
     
    David Kozina, Jun 8, 2004
    #10
  11. bobbyjoe

    David Doane Guest

    It probably would if all one wanted was a plain slot. :) However, if one
    needed to specify the machining of the slot by the center points, it would
    require another operation to indicate them.

    I started to post this earlier but refrained because I didn't want to
    interrupt Ade's learning. Since Paul brought up the polyline routine it
    might be appropriate. I wrote it several years ago while I was still
    designing machinery. I realize that it is archaic (but so am I) by today's
    standards and methods, and it does use "command" to draw the polyline. As
    this program contains curved slots as well as straight slots, it is rather
    cumbersome. It has only been tested up through R2002. However, it might be
    helpful to someone.
    --
    Dave D


    (defun SLTERR (msg) ; If an error (such as CTRL-C) occurs
    (if (not (member msg '("Function cancelled" "console break")))
    (princ (strcat "\nOops: " msg))
    (princ (strcat " \n"func" terminated! "))
    )
    (sltres)
    )
    ; ********
    (defun C:DRWSLT () (c:slot))
    (defun C:SLOT (/ clin hapi sdir shwd swid slen sscp secp
    sslp ssup selp seup ssls ssle sels sele)
    (sltvar)
    (setq func "STD SLOT")
    (setvar "cmdecho" 0)
    (setvar "blipmode" 0)
    (if (setq sscp (getpoint "\n \nStart center point: \n"))
    (progn
    (setq secp (getpoint sscp "\n \n(Return for slot length) End center
    point: \n"))
    (if (= secp nil)
    (setq slen (getdist sscp "\n \nSlot length: \n")
    sdir (getangle sscp "\n \nSlot direction: \n")
    secp (if (and sdir slen) (polar sscp sdir slen) nil) ) ;end
    setq
    (setq sdir (angle sscp secp)
    slen (distance sscp secp) ) ) ;end setq, if
    (setq swid (getdist secp "\n \n(Return for halfwidth) Slot width:
    \n"))
    (setq shwd (if (= swid nil) (getdist sscp "\n \nSlot halfwidth: \n")
    (* swid 0.5)))
    (if (and slen sdir shwd)
    (progn
    (setq hapi (* pi 0.5)
    sslp (polar sscp (+ sdir hapi) shwd)
    ssup (polar sscp (- sdir hapi) shwd)
    selp (polar secp (+ sdir hapi) shwd)
    seup (polar secp (- sdir hapi) shwd) ) ;end setq
    (command ".pline" sslp selp "a" seup "l" ssup "a" "cl")
    (initget "No Yes")
    (setq clin (getkword "\n \nDraw centerlines No/<Yes>: \n"))
    (if (or (= clin nil) (= clin "Yes")) (progn
    (setq ssls (polar sscp sdir (/ shwd 4))
    ssle (polar sscp (+ sdir pi) (/ shwd 4))
    sels (polar secp sdir (/ shwd 4))
    sele (polar secp (+ sdir pi) (/ shwd 4)) ) ;end setq
    (command ".line" ssls ssle ""
    ".array" (entlast) "" "p" sscp "2" "90" ""
    ".line" sels sele ""
    ".array" (entlast) "" "p" secp "2" "90" "") ) ;end
    command, progn
    ) ;end if centerlines
    ) ;end progn slen sdir shwd
    (prompt "\nMissing Slot information... ") ) ;end if slen sdir shwd
    ) ;end progn, if start point
    (prompt "\nMissing Slot start point... ") ) ;end if startpoint
    (prompt (strcat " "func" ended. << ol' greybeard dunnit >> \n"))
    (sltres)
    ) ;end C:DRWSLT, SLOT
    ; ********
    (defun C:DRWASL () (c:arcslot))
    (defun C:ARCSLOT (/ adir arad arot arsa area axis axls axle clin sang sdir
    smcp
    sida sidb swid shwd sscp secp ssip ssop seip seop ssls ssle sels
    sele)
    (sltvar)
    (setq func "ARC SLOT")
    (setvar "cmdecho" 0)
    (setvar "blipmode" 0)
    (setq axis (getpoint "\n \n(Return for 2-point/Radius) Arc axis point:
    \n"))
    (if (= axis nil)
    (progn
    (setq sscp (getpoint (strcat "\nPick points CCW around axis, but"
    "\nnot over 180 deg around axis. "
    "\n............. Start center point: ")) )
    (setq secp (if sscp (getpoint sscp "\n \nEnd center point CCW around
    Axis: \n") nil) ) ;end if, setq
    (setq arad (if secp (getdist secp "\n \nArc radius: \n") nil) )
    (if arad
    (setq sida (* (distance sscp secp) 0.5)
    sang (angle sscp secp)
    smcp (polar sscp sang sida)
    sidb (sqrt (- (* arad arad) (* sida sida)))
    axis (polar smcp (+ sang (* pi 0.5)) sidb)
    arsa (angle axis sscp)
    area (angle axis secp) ) ;end setq
    ) ;end if arad
    ) ;end progn nil
    (progn
    (setq sscp (getpoint axis "\n \n(Return for Angle/Radius) Start
    center point: \n"))
    (if (= sscp nil)
    (setq arsa (getangle axis "\n \nStart angle: \n")
    arad (getdist axis "\n \nArc radius: \n")
    sscp (if (and arsa arad) (polar axis arsa arad) nil) ) ;end
    if, setq
    (setq arsa (angle axis sscp)) ) ;end if
    (setq area (getangle axis "\n \n(Return for Arc Rotation) End Angle:
    \n"))
    (if (= area nil)
    (setq arot (getangle "\n \n(Use -Neg Angle for CW) Arc Rotation:
    \n")
    secp (if arot (polar axis (+ (angle axis sscp) arot) (distance
    axis sscp)) nil)
    area (if secp (angle axis secp) nil ) ) ;end if, setq
    (setq secp (polar axis area (distance axis sscp)))
    ) ) ) ;end if, progn T, if
    (if (and sscp secp)
    (progn
    (setq swid (getdist secp "\n \n(Return for halfwidth) Slot width:
    \n"))
    (setq shwd (if (= swid nil) (getdist secp "\n \nSlot halfwidth: \n")
    (* swid 0.5)))
    (if (and area arsa shwd) (progn
    (setq ssip (polar sscp (+ arsa pi) shwd)
    ssop (polar sscp arsa shwd)
    seip (polar secp (+ area pi) shwd)
    seop (polar secp area shwd) ) ;end setq
    (setq adir (if (< arsa area) "CCw" "CW")) ;In direction picked, or
    reverse
    (initget "CCw CW") ;if slot crosses 0
    degrees
    (setq sdir (getkword (strcat "\n \nDirection - CCw/CW <"adir">:
    \n")))
    (setq sdir (if (= sdir nil) adir sdir))
    (if (= sdir "CCw")
    (command ".pline" ssip "a" "ce" axis seip seop ssop "cl")
    (command ".pline" seip "a" "ce" axis ssip ssop seop "cl") ) ;end
    if
    (initget "No Yes")
    (setq clin (getkword "\n \nDraw centerlines No/<Yes>: \n"))
    (if (or (= clin nil) (= clin "Yes")) (progn
    (setq axls (polar axis 0 (/ shwd 2))
    axle (polar axis pi (/ shwd 2))
    ssls (polar sscp arsa (/ shwd 4))
    ssle (polar sscp (+ arsa pi) (/ shwd 4))
    sels (polar secp area (/ shwd 4))
    sele (polar secp (+ area pi) (/ shwd 4)) ) ;end setq
    (command ".line" ssls ssle ""
    ".array" (entlast) "" "p" sscp "2" "90" ""
    ".line" sels sele ""
    ".array" (entlast) "" "p" secp "2" "90" ""
    ".line" axls axle ""
    ".array" (entlast) "" "p" axis "2" "90" "") ;end
    command
    (prompt "\n ")
    ) ) ;end progn, if centerlines
    (prompt "\n ")
    ) ;end progn, if area arsa shwd
    (prompt "\nMissing Width information... ") ) ;end if area arsa shwd
    ) ;end progn, if centerpoints
    (prompt "\nMissing Slot information... ") ) ;end if centerpoints
    (prompt (strcat " "func" ended. << ol' greybeard dunnit >> \n"))
    (sltres)
    ) ;end C:DRWASL, ARCSLOT
    ; ********
    (defun SLTVAR ()
    (setq coth (getvar "orthomode")
    csty (getvar "textstyle")
    cucf (getvar "ucsfollow")
    cfrd (getvar "filletrad")
    cblp (getvar "blipmode")
    cplw (getvar "plinewid")
    ctsz (getvar "textsize")
    capr (getvar "aperture")
    canb (getvar "angbase")
    casz (* (getvar "dimasz") (getvar "dimscale"))
    cclr (getvar "cecolor")
    cmde (getvar "cmdecho")
    csna (getvar "snapang")
    cucs (getvar "ucsname")
    clay (getvar "clayer")
    csnp (getvar "osmode")
    )
    (command ".undo" "m" "undo" "g")
    (if *error* (setq olderr *error* *error* slterr))
    ; (setvar "orthomode" 1)
    (setvar "ucsfollow" 0)
    (setvar "blipmode" 0)
    (setvar "angbase" 0)
    (setvar "cmdecho" 0)
    (setvar "osmode" 0)
    ) ; end SLTVAR
    ; ********
    (defun SLTRES ()
    (if (or (= cucs "") (= cucs "WORLD"))
    (command ".ucs" "world")
    (command ".ucs" "r" cucs))
    (setvar "orthomode" coth)
    (setvar "textstyle" csty)
    (setvar "ucsfollow" cucf)
    (setvar "filletrad" cfrd)
    (setvar "blipmode" cblp)
    (setvar "plinewid" cplw)
    (setvar "textsize" ctsz)
    (setvar "aperture" capr)
    (setvar "angbase" canb)
    (setvar "cecolor" cclr)
    (setvar "cmdecho" cmde)
    (setvar "snapang" csna)
    (setvar "clayer" clay)
    (setvar "osmode" csnp)
    (if olderr (setq *error* olderr olderr nil))
    (setq func nil)
    (setq set1 nil)
    (setq set2 nil)
    (princ)
    ) ;end SLTRES
    (princ)
     
    David Doane, Jun 8, 2004
    #11
  12. bobbyjoe

    David Kozina Guest

    Hey David D!,

    Nice to hear from you.

    I thought such an mline would have grip points AT the centers...
    (and maintain parallelness on the sides when editing)
    Point objects could be grouped with the mline at said centers, if one needs
    a node to snap to.
    (would center snap work?, I dunno)...

    Just some thoughts to simplify things, depending on the situation.

    MLine scales can be modified via entmod. <bleah> -

    Autodesk, would you PLEASE expose a few more ActiveX properties for these
    things?

    Get/Put Scale would be nice
    Get/Put Justification would be nice
    Get/Put MLineStyle would be EXTRA nice

    C'mon, folks - haven't you kept these things alienated long enough?

    Sheesh,
    David Kozina
     
    David Kozina, Jun 8, 2004
    #12
  13. bobbyjoe

    speritti Guest

    Just wanted to add my 2 cents. I was interested about a slot routine as I do use them from time to time. For what its worth, here's my attempt at it.

    Speritti

    (defun c:slot1 (/ BUL1 CENANGDEG CENANGDEG1
    CENANGDEG2 CENANGRAD CENANGRAD1 CENANGRAD2
    CENPT1 CENPT2 ELST PPOINT1
    PPOINT2 PPOINT3 PPOINT4 RADDIST
    RADPOINT
    )
    (setq cenpt1 (getpoint "\nSpecify First Radius Point : ")
    cenpt2 (getpoint cenpt1 "\nSpecify Second Radius Point : ")
    )
    (grdraw cenpt1 cenpt2 3 1)
    (setq radpoint (getpoint cenpt2 "\nSpecify or Pick Point on Radius : ")
    raddist (distance cenpt2 radpoint)
    cenangrad (angle cenpt1 cenpt2)
    cenangdeg (* 180.0 (/ cenangrad pi))
    cenangdeg1 (+ cenangdeg 90.0)
    cenangdeg2 (- cenangdeg 90.0)
    cenangrad1 (* pi (/ cenangdeg1 180.0))
    cenangrad2 (* pi (/ cenangdeg2 180.0))
    )
    (setq ppoint1 (polar cenpt1 cenangrad1 raddist)
    ppoint2 (polar cenpt1 cenangrad2 raddist)
    ppoint3 (polar cenpt2 cenangrad2 raddist)
    ppoint4 (polar cenpt2 cenangrad1 raddist)
    )
    (setq bul1 1.0)
    (setq elst (list (cons 0 "LWPOLYLINE")
    (cons 100 "AcDbEntity")
    (cons 100 "AcDbPolyline")
    (cons 90 4)
    (cons 70 1)
    (cons 43 0)
    ;(cons 8 "0")
    (cons 10 ppoint1)
    (cons 42 bul1)
    (cons 10 ppoint2)
    (cons 10 ppoint3)
    (cons 42 bul1)
    (cons 10 ppoint4)
    )
    )
    (entmake elst)
    (grdraw cenpt1 cenpt2 0 1)
    )
    (princ)
     
    speritti, Jun 9, 2004
    #13
  14. bobbyjoe

    Paul Turvill Guest

    Why bother with specifying all of your angles in degrees, *then* converting
    them to radians? IMO it's much simpler to just work in radians to start
    with. Other than that complication, plus your insertion of the (grdraw ...)
    stuff and longer variable names, your code differs very little from what I
    posted earlier.
    ___
     
    Paul Turvill, Jun 9, 2004
    #14
  15. bobbyjoe

    speritti Guest

    Hi Paul. A valid point about the conversion from rads to degrees, however I'm still figuring out how the radians work. Still fairly new to this stuff. I needed to add and subtract 90 degrees from the original angle and havn't looked at doing it in rads only. The (grdraw) is my personal preference as I use it in construction routines where it is most useful.
    And no... I didn't even look at your routine before I posted. I just saw the original code with the dreaded (command) functions.

    Steven
     
    speritti, Jun 9, 2004
    #15
  16. bobbyjoe

    Paul Turvill Guest

    pi radians = 180 deg.

    90 deg = pi / 2.
    ___
     
    Paul Turvill, Jun 9, 2004
    #16
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.