Jump to content

Recommended Posts

Posted

Hi I am writting a lisp to draw 2d stair. The idea is to select the fist step --> move it to layer STAIR then pick two points for the length of the stair and give the number of the steps. Then the code draw the stair with direction arrow in the middle. The code work perfect if i select the two points only from the one side of the stair. Is not all the time the left or the right side depends of the rotation. Can any one help me to update the code to work for bouth sides and don't misalign the steps. Here is the code . By the way i use ZWCAD.

(defun c:Stair2D (/ p1 p2 ent ent_data step_start step_end step_vec stair_vec num_steps i base_pt new_p1 new_p2 mid1 mid2 last_base last_p1 last_p2 arrow_p1 arrow_p2 arrow_p3)
  (vl-load-com)
  (setvar "CMDECHO" 0)
  (command "._UNDO" "_BEGIN")

  (if (not (tblsearch "LAYER" "STAIR"))
    (command "_layer" "_m" "STAIR" "_c" "90" "" "")
  )
  (setvar "CLAYER" "STAIR")

  (princ "\nSelect the first step of the staircase: ")
  (while (not (setq ent (ssget ":S" '((0 . "LINE")))))
    (princ "\nTry again!! ")
  )
  (command "._CHANGE" ent "" "_P" "_LA" "STAIR" "")
  
  (setq ent_data (entget (ssname ent 0)))
  (setq step_start (cdr (assoc 10 ent_data)))
  (setq step_end   (cdr (assoc 11 ent_data)))
  (setq step_vec   (mapcar '- step_end step_start))

  (setvar "OSMODE" 1)
  (setq p1 (getpoint "\nSelect the start of the staircase: "))
  (setq p2 (getpoint p1 "\nSelect the end of the staircase: "))
  (setvar "OSMODE" 0)

  (initget 7)
  (setq num_steps (getint "\nEnter the number of steps: "))
  (setq stair_vec (mapcar '(lambda (a b) (/ (- b a) num_steps)) p1 p2))

  (setq i 1)
  (while (<= i num_steps)
    (setq base_pt (mapcar '+ p1 (mapcar '(lambda (x) (* x i)) stair_vec)))
    (setq new_p1 base_pt)
    (setq new_p2 (mapcar '+ base_pt step_vec))
    (command "._LINE" new_p1 new_p2 "")
    (setq i (1+ i))
  )

  (setq mid1 (mapcar '(lambda (a b) (/ (+ a b) 2.0)) step_start step_end))
  (setq last_base (mapcar '+ p1 (mapcar '(lambda (x) (* x num_steps)) stair_vec)))
  (setq last_p1 last_base)
  (setq last_p2 (mapcar '+ last_base step_vec))
  (setq mid2 (mapcar '(lambda (a b) (/ (+ a b) 2.0)) last_p1 last_p2))
  (command "._line" mid1 mid2 "" "" "_N")
  (command "._CIRCLE" mid1 0.06)

  (setq mid1mid2_vec (mapcar '- mid2 mid1))
  (setq mid1mid2_length (distance mid1 mid2))
  (setq unit_vec (mapcar '(lambda (x) (/ x mid1mid2_length)) mid1mid2_vec))
  (setq perp_vec (list (- (cadr unit_vec)) (car unit_vec) 0.0))
  (setq arrow_p1 (mapcar '+ mid2 (mapcar '(lambda (x) (* x 0.10)) unit_vec)))
  (setq arrow_p2 (mapcar '+ mid2 (mapcar '(lambda (x) (* x -0.15)) perp_vec)))
  (setq arrow_p3 (mapcar '+ mid2 (mapcar '(lambda (x) (* x 0.15)) perp_vec)))
  (command "._LINE" arrow_p1 arrow_p2 "")
  (command "._LINE" arrow_p1 arrow_p3 "")

  (command "._UNDO" "_END")
  (setvar "CMDECHO" 1)
  (setvar "CLAYER" "0")
  (princ)
)

 

Thanks

Screenshot 2025-08-05 190207.png

Drawing2.dwg

Posted (edited)
16 hours ago, mhy3sx said:

Hi I am writting a lisp to draw 2d stair. The idea is to select the fist step --> move it to layer STAIR then pick two points for the length of the stair and give the number of the steps. Then the code draw the stair with direction arrow in the middle. The code work perfect if i select the two points only from the one side of the stair. Is not all the time the left or the right side depends of the rotation. Can any one help me to update the code to work for bouth sides and don't misalign the steps. Here is the code . By the way i use ZWCAD.

 

 

Does this match your expectations?

 

spacer.png

screenshot_1754468588.png.d71dd0534829a4925e3cd440f2ce4fac.png

Edited by DATVO
  • Like 1
Posted (edited)

Sorry, I placed my phone in my pocket without locking the screen. Mod @SLW210, please delete this post.

Edited by DATVO
Posted (edited)

Hi DATVO. Yes , I update my code to work only for the left side every time.How you do it?  Can you post the code?

Thanks

Edited by mhy3sx
Posted (edited)

@DATVO just hit the    ...   top right of your post to delete it.

Edited by mhupp
  • Like 1
Posted

Here is the code @mhy3sx, based on your original file. I've modified a few lines using a different approach, but the result remains the same.
If you find this helpful, feel free to visit my page where I share tools designed to improve efficiency and save time, as effortlessly as enjoying a cup of coffee: https://lispautocad.gumroad.com/

 

;; Modified by DV. Visit my page at: https://lispautocad.gumroad.com/

(defun c:Stair2D (/ p1 p2 ent start_pt end_pt mid_pt line_length circle_diameter num_steps i move_vec array_vec step_vec last_start_pt last_end_pt last_mid_pt mid1mid2_vec mid1mid2_length unit_vec perp_vec arrow_p1 arrow_p2 arrow_p3)
  (vl-load-com)
  (setvar "CMDECHO" 0)
  (command "._UNDO" "_BEGIN")

  (if (not (tblsearch "LAYER" "STAIR"))
    (command "_layer" "_m" "STAIR" "_c" "90" "" "")
  )
  (setvar "CLAYER" "STAIR")

  (princ "\nSelect the first LINE of the staircase: ")
  (while (not (setq ent (ssget ":S" '((0 . "LINE")))))
    (princ "\nTry again!! ")
  )
  (command "._CHANGE" ent "" "_P" "_LA" "STAIR" "")

  (setq ent (ssname ent 0))
  (setq ent_data (entget ent))
  (setq start_pt (cdr (assoc 10 ent_data)))
  (setq end_pt (cdr (assoc 11 ent_data)))
  (setq mid_pt (mapcar '(lambda (a b) (/ (+ a b) 2.0)) start_pt end_pt))
  (setq line_length (distance start_pt end_pt))
  (setq circle_diameter (/ line_length 10.0))

  (princ "\nSelect the start of the staircase: ")
  (setq p1 (getpoint))
  (princ "\nSelect the end of the staircase: ")
  (setq p2 (getpoint p1))

  (initget 7)
  (setq num_steps (getint "\nEnter the number of steps: "))

  (setq array_vec (mapcar '- p2 p1))
  (setq step_vec (mapcar '(lambda (x) (/ x num_steps)) array_vec))

  (setq i 1)
  (while (<= i num_steps)
    (setq move_vec (mapcar '(lambda (x) (* x i)) step_vec))
    (command "._COPY" ent "" "_non" (list 0 0 0) "_non" move_vec)
    (setq i (1+ i))
  )

  ;; Circle and arrow line
  (command "._CIRCLE" mid_pt (/ circle_diameter 2.0))

  (setq last_start_pt (mapcar '+ start_pt (mapcar '(lambda (x) (* x num_steps)) step_vec)))
  (setq last_end_pt (mapcar '+ end_pt (mapcar '(lambda (x) (* x num_steps)) step_vec)))
  (setq last_mid_pt (mapcar '(lambda (a b) (/ (+ a b) 2.0)) last_start_pt last_end_pt))

  (command "._LINE" mid_pt last_mid_pt "")

  (setq mid1mid2_vec (mapcar '- last_mid_pt mid_pt))
  (setq mid1mid2_length (distance mid_pt last_mid_pt))
  (if (> mid1mid2_length 0)
    (progn
      (setq unit_vec (mapcar '(lambda (x) (/ x mid1mid2_length)) mid1mid2_vec))
      (setq perp_vec (list (- (cadr unit_vec)) (car unit_vec) 0.0))
      (setq arrow_p1 (mapcar '+ last_mid_pt (mapcar '(lambda (x) (* x 0.10 mid1mid2_length)) unit_vec)))
      (setq arrow_p2 (mapcar '+ last_mid_pt (mapcar '(lambda (x) (* x -0.15 mid1mid2_length)) perp_vec)))
      (setq arrow_p3 (mapcar '+ last_mid_pt (mapcar '(lambda (x) (* x 0.15 mid1mid2_length)) perp_vec)))
      (command "._LINE" arrow_p1 arrow_p2 "")
      (command "._LINE" arrow_p1 arrow_p3 "")
    )
  )

  (command "._UNDO" "_END")
  (setvar "CMDECHO" 1)
  (setvar "CLAYER" "0")
  (princ)
)

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...