newbie zigzag lsp (be gentle)

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

  1. spencer1971

    spencer1971 Guest

    I am having a go at writing a lsp from scratch (previously I have practiced by amending existing lsp files)

    I am trying to get a lsp that will ask for a bottrom left point a depth and a length and draw a series of lines zig-zagged between the two at 45 degrees (as hardcore for anyone in the building industry)

    I can not get the loop working to make the line continue as required.

    I have a feeling that im going about it in completely the wrong way by using pt1 pt2 and pt3 then setting pt1=pt3 and looping. Any suggestions would be greatly appreciated.

    Also I know there is a command that will let me pick 2 points and return the distance between them and also the two picked points as coords. (well I think it exists)


    this is my miserable attempt so far i get no error message but it only runs as far as th e (if command I think:-

    (defun MyError (strErr)
    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    (princ)
    )
    ;----------------------------------------------------------
    (defun DTOR (degr)
    (/ (* degr pi) 180)
    )
    )
    ;----------------------------------------------------------
    (defun C:hardcore (/ oldsnap clay pt1 pt2 pt3 ht1 ht2 dist height p )
    (setq *ERROR* MyError)
    (setq oldsnap (getvar "osmode"))
    (setq clay (getvar "clayer"))
    (command "-layer" "m" "Hatch" "")
    (command "-layer" "c" "RED" "" "")
    (setq pt1 (getpoint "\npick bottom left corner of hardcore"))
    (setq dist (getdist "\npick length of hardcore"))
    (setq height (getdist "\npick depth of hardcore: "))
    (setq ht1 (* height 1.41422))
    (setq ht2 (height))
    (if (< ht2 dist)
    (progn
    (setq Pt2 (polar pt1 (dtor 45) ht1))
    (setq Pt3 (polar pt2 (dtor 315) ht1))
    (command "PLINE" PT1 PT2 PT3 "")
    )
    (setq Pt1 (pt3))
    (set1 ht2 (ht2 + height))
    )
    )

    many thanks

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

    bob.at Guest

    spencer,

    i did not test what your program does exactly, but i can show you the changes for making a continous polyline

    (defun C:hardcore (/ oldsnap clay pt1 pt2 pt3 ht1 ht2 dist height p )
    (setq *ERROR* MyError)
    (setq oldsnap (getvar "osmode"))
    (setq clay (getvar "clayer"))
    (command "-layer" "m" "Hatch" "")
    (command "-layer" "c" "RED" "" "")
    (setq pt1 (getpoint "\npick bottom left corner of hardcore"))
    (setq dist (getdist "\npick length of hardcore"))
    (setq height (getdist "\npick depth of hardcore: "))
    (setq ht1 (* height 1.41422))
    (setq ht2 (height))
    ;
    ; here my changes begin:
    (command "PLINE" pt1) ; start pline with first point
    (while (< ht2 dist) ; and not if, becaus you want it not only once but as long as it is less than
    (setq pt2 (polar pt1 (dtor 45) ht1)
    pt3 (polar pt2 (dtor 315) ht1))
    (command pt2 pt3) ; draws the next to points
    (setq pt1 pt3)
    (set1 ht2 (+ ht2 height)) ; correct + operator
    ) ; end of while
    (command "") ; now finish the polyline
    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    (princ)
    )
     
    bob.at, Jun 4, 2004
    #2
  3. spencer1971

    spencer1971 Guest

    Bob.at

    Many thanks for your quick response,

    I think I am getting closer to understanding this lsp stuff thanks to the members on this board.

    Howerver I have taken your comments aboard and now have the following routine, It seems to stop at the same place as before and does not start drawing at all. should i be using the (progn command and if so is it just a case of putting it below the while command and closing it at the point before the while command ends.?

    (defun MyError (strErr)
    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    )

    (defun DTOR (degr)
    (/ (* degr pi) 180)
    )

    (defun C:hardcore (/ oldsnap clay pt1 pt2 pt3 ht1 ht2 dist height p )
    (setq *ERROR* MyError)
    (setq oldsnap (getvar "osmode"))
    (setq clay (getvar "clayer"))
    (command "-layer" "m" "Hatch" "")
    (command "-layer" "c" "RED" "" "")
    (setq pt1 (getpoint "\npick bottom left corner of hardcore"))
    (setq dist (getdist "\npick length of hardcore"))
    (setq height (getdist "\npick depth of hardcore: "))
    (setq ht1 (* height 1.41422))
    (setq ht2 (height))
    (command "PLINE" pt1)
    (while (< ht2 dist)
    (setq pt2 (polar pt1 (dtor 45) ht1))
    (setq pt3 (polar pt2 (dtor 315) ht1))
    (command pt2 pt3)
    (setq pt1 pt3)
    (set1 ht2 (+ ht2 height))
    )
    (command "")
    (setvar "OSMode" oldsnap)
    (setvar "clayer" clay)
    (princ)
    )
     
    spencer1971, Jun 4, 2004
    #3
  4. spencer1971

    BillZ Guest

    Try this.

    ;---;
    (defun dtr (a)
    (* pi (/ a 180.0))
    )
    ;----------------------------------------------------------
    (defun c:hardcore ();(/ oldsnap clay pt1 pt2 pt3 ht1 ht2 dist height p)
    ;(setq *ERROR* MyError)
    (setq oldsnap (getvar "osmode")
    )
    (setq clay (getvar "clayer")
    )
    (command "-layer" "m" "Hatch" "")

    (command "-layer" "c" "RED" "" "")

    (setq pt1 (getpoint "\npick bottom left corner of hardcore: ")
    )
    (setq pt2 (getpoint pt1 "\npick length of hardcore: ")
    )
    (setq pt3 (getpoint pt1 "\npick depth of hardcore: ")
    )
    (setq dist1 (distance pt1 pt2) ;changed dist to dist1 so not to redefine a built in function.
    height (distance pt1 pt3) ;opposite or adjacent at 45d.
    ht1 (* height 1.41422) ;hypotenuse
    ht2 height
    ntimes (fix (/ (/ dist1 height) 2.0))
    P1 pt1
    )

    (command "PLINE")

    (repeat ntimes

    (setq Px1 (polar P1 (dtr 45) ht1)
    )
    (setq Px2 (polar Px1 (dtr 315) ht1)
    )
    (command P1 Px1 Px2)

    (setq P1 Px2
    )

    ) ;end repeat
    (command "")
    )


    Bill
     
    BillZ, Jun 4, 2004
    #4
  5. spencer1971

    bob.at Guest

    Spencer

    you dont need (progn with the while command, becaus while can have an inifinte number of arguments (indead i think its limited by 512 or 1024 or something like this): the first is used as test condition, all others are evaluated as long as test condition is true.
    In contrast, (if only can have 3 arguments: the forst is the test condition, the second is evalueted if true, the third if false (or nil). Therfore here you must group your expressions if you want to do more than one in case of true: (progn (ex1) (ex2) ... )

    In your porgram there are two false lines:
    change (setq ht2 (height)) to (setq ht2 height)
    and also (set1 ht2 (+ ht2 height)) to (setq ht2 (+ ht2 height))

    Than it works and draws a pline, but i think there is also a logical mistake in calculating the length. it looks better if you do the following:
    cnahge (set1 ht2 (+ ht2 height)) to (set1 ht2 (+ ht2 (* 2 height)))


    bob.at
     
    bob.at, Jun 4, 2004
    #5
  6. spencer1971

    bob.at Guest

    Bill

    nice posiblity to calulate the number of steps in the begin.

    If you use (command p1 px1 px2) in your code you get a pline with identical vertices at every second point, because px2 of the first call is the same as p1 of the second call.
    You should start with (command "_pline" p1) outside the repeat loop and then continue with (command px1 px2)
     
    bob.at, Jun 4, 2004
    #6
  7. spencer1971

    BillZ Guest

    Bob,

    Good point.

    Didn't think of that part.


    Bill
     
    BillZ, Jun 4, 2004
    #7
  8. spencer1971

    spencer1971 Guest

    Bill & Bob.

    Just when I thought I was getting used to it.....

    Setting ntimes by dividing the line is an excellent idea and one I would't of dreamed of.

    Many thanks to you both

    Spence
     
    spencer1971, Jun 4, 2004
    #8
  9. spencer1971

    T.Willey Guest

    (defun c:wh()

    (command "undo" "group")
    (setq pt1 (getpoint "\nSelect first point (either lower left or upper right): ")
    pt2 (getpoint pt1"\nSelect end point: ")
    th1 (getdist "\nStud size: ")
    th2 (* th1 th1)
    th3 (+ th2 th2)
    th4 (sqrt th3)
    th5 (* 2 th1)
    lng1 (distance pt1 pt2)
    lng2 (/ lng1 2)
    lng3 0
    ang1 (angle pt1 pt2)
    ang1 (+ ang1 6.28318535)
    ang2 0.7853982
    ang3 (+ ang1 ang2)
    ang4 (- ang1 ang2)
    wall (ssadd)
    pt3 (polar pt1 ang3 th4)
    pt4 (polar pt3 ang4 th4)
    os1 (getvar "osmode")
    )
    (setvar "osmode" 0)
    (command "pline" pt1 "w" 0.0 0.0 pt3 pt4 "")
    (ssadd (entlast) wall)
    (setq pt1 pt4)

    (while (< th1 lng1)
    (progn
    (setq pt3 (polar pt1 ang3 th4)
    pt4 (polar pt3 ang4 th4)
    )
    (command "pline" pt1 "w" 0.0 0.0 pt3 pt4 "")
    (ssadd (entlast) wall)
    (setq pt1 pt4)
    (setq th1 (+ th1 th5))
    )
    )
    (command "pedit" wall "j" wall "" "")
    (setvar "osmode" os1)
    (command "undo" "end")
    (princ)
    )

    Here is one I use. I don't know if it's better or the same or worse than the one posted. I just wanted to share also.

    Tim
     
    T.Willey, Jun 4, 2004
    #9
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.