Search code examples
lispautocadautolisp

AutoLISP for for Ferris Wheel drawing only draws main circle


I've have this code to draw a ferris wheel with AutoLISP and I can't seem to figure out why it doesn't work.

(defun c:ferriswheel ()
  (setq radius 50)
  (setq cart-size 5)
  (setq num-carts 8)

  (setq center-point '(0 0 ))

  (command "circle" center-point radius)

  (setq angle (/ (* 2 pi) num-carts))
  (setq current-angle 0)

  (repeat num-carts
    (setq x1 (* radius (cos current-angle)))
    (setq y1 (* radius (sin current-angle)))

    (setq x2 (* (+ radius cart-size) (cos current-angle)))
    (setq y2 (* (+ radius cart-size) (sin current-angle)))

    (command "line" (list x1 y1) (list x2 y2))
    (setq current-angle (+ current-angle angle))

   )
)

Not only does it only draw the circle, and not the carts, but it also presents me a window that says

Assignment to protected symbol:
ANGLE
Enter break loop?
Yes / No

After looking online, they say to ignore it and just click no, which then results in the code cancelling itself and leaving only the circle drawn. Please help!


Solution

  • That is because command LINE not ends after second point. It is asking for the next point to continue drawing. You should end drawing by using "" at the end of (command "line" (list x1 y1) (list x2 y2) )

    So You should use it in this way:

    (command "line" (list x1 y1) (list x2 y2) "" )

    By the way: it's bad idea to call variable angle which is the name of function. If You try to use angle as function You will get ERROR: undefined function.

    So the full code would be like this:

    (defun c:ferriswheel ()
      (setq radius 50)
      (setq cart-size 5)
      (setq num-carts 8)
    
      (setq center-point '(0 0 ))
    
      (command "circle" center-point radius)
    
      (setq a (/ (* 2 pi) num-carts))
      (setq current-angle 0)
    
      (repeat num-carts
        (setq x1 (* radius (cos current-angle)))
        (setq y1 (* radius (sin current-angle)))
        (setq x2 x1)
        (setq y2 (- y1 cart-size))
        
        (command "line" (list x1 y1) (list x2 y2) "")   
        (setq cart-size2 (* cart-size 0.5))
        (setq p1 (list (- x2 cart-size2) y2))
        (setq p2 (list (+ x2 cart-size2) y2))
        (setq p3 (list (+ x2 cart-size2) (- y2 cart-size)))
        (setq p4 (list (- x2 cart-size2) (- y2 cart-size)))
        
        (command "_PLINE" p1 p2 p3 p4 p1 "")
        (setq current-angle (+ current-angle a))
       )
    )