본문 바로가기
Study(매일매일한걸음씩)/Autocad

for 문 사용하기 (foreach) - 반복 작업 한번에

by 여유러운백수삶개발자 2022. 12. 9.
  1. 여러줄을 한번에 그리기
    1. list를 만들어서 지정된 list수량 만큼 선을 그린다. 

 

(defun c:test () 
   (setq p1 (list 0 0))
   (setq p2 (list 2000 2000))
   (command "line" p1 p2 "")
  
  (setq slist (list 1000 2000 3000 4000 5000 6000 7000))
  (foreach temp slist 
    (setq p1 (list 1000 0))
    (setq p2 (list 2000 temp))
    (command "line" p1 p2 "")
  )
)

 

2. 실행해보기(vsc에서 다시 시작 누르고 autocad 에서 "test" 입력) - 잘됨

3. commit하기(안해도됨) - 저의 기록을 위해

git add *.lsp
git commit -m "study:foreach"

 

note.

  1. git branch는 사용안함(공부용이라 불필요)
  2. foreach 익숙하지 않아서 고생
  3. foreach 사용법 알고 기존 코드 다 바꾼다고 고생
  4. 다음에는 입력 받는 것 기록하기로
  5. 전체선택하고 ctl+k, ctl+f 누르면 탭 간격 정리됨(의외로 편리함)

 

현재 작성중인 코드(foreach를 적용해도 무식해 보이는것 어쩔) - foreach 알자마자 다 바꿈(foreach 덩어리코드됨) 그래도 3000줄에서 줄어듬(뿌듯)

(defun c1_drum_block() 
  (command "insert" "C-1 DRUM LEFT" (list 0 height) "" "" "")
  (command "insert" "C-1 DRUM RIGHT" (list width height) "" "" "")
  (command "insert" "C-1 DRUM SIDE" (list side_distance height) "" "" "")
)
(defun c2_drum_block () 
  (command "insert" "c2drumleft" (list 0 height) "" "" "")
  (command "insert" "c2drumright" (list width height) "" "" "")
  (command "insert" "c2side" (list side_distance height) "" "" "")
)
(defun dim_line () 

  ; 단면도 글씨 추가  20220819
  (setq text_y (- 600))
  (setq text_x (- 3200))
  (command "insert" "text side" (list text_x text_y) "" "" "")

  ; 평면도 글씨 추가  20220819
  (setq text_y (- 600))
  (setq text_x (/ width 2))
  (setq text_x (- text_x 671))
  (command "insert" "text front" (list text_x text_y) "" "" "")

  ; 입면도 글씨 추가  20220819
  (setq text_y (+ height 1580))
  (setq text_x (/ width 2))
  (setq text_x (- text_x 671))
  (command "insert" "text front" (list text_x text_y) "" "" "")

  ; 지시선 A,B,C 추가 2022-10-13
  (command "insert" "arrow_A" (list -2555 875) "" "" "")
  (setq text_y (+ width 117))
  (command "insert" "arrow_B" (list text_y 228) "" "" "")
  (setq text_y2 (+ height 2400))
  (command "insert" "arrow_C" (list text_y text_y2) "" "" "")

  ; controller 추가 2022-10-13
  (command "insert" "controller" (list -500 1200) "" "" "")

  ; motor 지시선 추가 2022-10-13
  (setq text_y (+ height 151))
  (command "insert" "지시선_motor" (list -2650 text_y) "" "" "")

  ; sheet 지시선 추가 2022-10-13
  (setq text_y (- width 260))
  (command "insert" "지시선_sheet" (list text_y 670) "" "" "")

  ; photo sensor 추가 2022-10-13
  (command "insert" "photo_sensor" (list 0 600) "" "" "")
  (setq text_y (+ width 14))
  (command "insert" "photo_sensor" (list text_y 600) "" "" "")

  ; photo sensor 지시선 2022-10-13
  (command "insert" "지시선_photosensor" (list -20 590) "" "" "")

  ; 한점스위치 추가(with 지시선)
  (command "insert" "1point" (list -60 1200) "" "" "")

  ; 치수선 추가(120mm 140mm)
  (command "insert" "치수선_120_140" (list 0 0) "" "" "")



  ; 치수선
  (command "DIMLINEAR" (list 0 0) (list width 0) (list 2000 -207.52))
  (command "DIMLINEAR" 
           (list -2646.23 0)
           (list -2646.23 height)
           (list -3000 -207.52)
  )

  ; 측면도 아래선 그리기
  (setq KKD1 (- height 500))
  (setq KKD2 side_distance)
  (setq KKD3 141.71)
  (setq KKD2 (- KKD2 KKD3))
  (setq p1 (list KKD2 0))
  (setq p2 (list side_distance 0))
  (command "line" p2 p1 "")
)
(defun c1_base () 
  ; C-1 틀 추가
  (command "insert" "C-1 BASE" (list -5500 -4500) "" "" "")
  ; 치수선 좌우 공간 관련추가(350mm 150mm)
  (command "insert" "치수선_motor_space" (list 0 height) "" "" "")
  (command "insert" "치수선_bearing_space" (list width height) "" "" "")
)
(defun c2_base () 
  ; C-2 틀 추가
  (command "insert" "C-2 BASE" (list -5500 -4500) "" "" "")
  ; 치수선 좌우 공간 관련추가(200mm 150mm)
  (command "insert" "치수선_bearing_space_c2" (list 0 height) "" "" "")
  (command "insert" "치수선_motor_space_c2" (list width height) "" "" "")
) 
(defun c3_base () 
  ; C-3 틀 추가
  (command "insert" "c-3 base" (list -5500 -4500) "" "" "")
  ; 치수선 좌우 공간 관련추가(200mm 150mm)
  ; (command "insert" "치수선_bearing_space_c2" (list 0 height) "" "" "")
  ; (command "insert" "치수선_motor_space_c2" (list width height) "" "" "")
) 
(defun c1_drum_line_draw () 
  ; 드럼 parts
  (c1_drum_block)
  (setq slist (list 319.65 221.46 218.96 83.96 81.46 0))
  (foreach kim slist 
    (setq KKD1 (+ height kim))
    (setq KKD2 (- width 500))
    (setq p1 (list 500 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
  ;  평면도 추가
  (setq KKD1 (+ height up_distance))
  (command "insert" "C-1 UP LEFT" (list 0 KKD1) "" "" "")
  (command "insert" "C-1 UP RIGHT" (list width KKD1) "" "" "")
  (setq slist (list 0 82 2.5 135 2.5 99.6))
  (foreach kim slist 
    (setq KKD3 500)
    (setq KKD1 (- KKD1 kim))
    (setq KKD2 (- width KKD3))
    (setq p1 (list KKD3 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
)
(defun c2_drum_line_draw () 
  ; 드럼 parts
  (c2_drum_block)
  (setq slist (list 382.88 296.98 289.98 188.98 87.98 80.98 0))
  (foreach kim slist 
    (setq KKD1 (+ height kim))
    (setq KKD2 (- width 500))
    (setq p1 (list 500 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
  ;  평면도 추가
  (setq KKD1 (+ height up_distance))
  (command "insert" "C-2 UP LEFT" (list 0 KKD1) "" "" "")
  (command "insert" "C-2 UP RIGHT" (list width KKD1) "" "" "")
  (setq slist (list 0 1.6 65.4 7 101 101 7 71.9))
  (foreach kim slist 
    (setq KKD3 500)

    (setq KKD1 (- KKD1 kim))
    (setq KKD2 (- width KKD3))
    (setq p1 (list KKD3 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
)
(defun c1_frame_bottom_base () 
  (setq temp (+ side_distance -59.2))
  (command "insert" "bottom c-1 side2" (list temp 0) "" "" "")
  ; 왼쪽 프레임 그리기
  (setq slist2 (list 0 -121.8044 -121.6844 -1.6844 -5.1544 -5.4744 -26.644 -27.4844 
                     -28.6644 -30.0144 -30.5244 -59.3444 -60.1544 -60.8644 -62.4644 
                     -63.1744 -63.9844 -93.1344 -93.6244 -94.9744 -96.1544
               )
  )
  (foreach kim2 slist2 
    (setq KKD1 (- height 500))
    (setq KKD2 kim2)
    (setq p1 (list KKD2 0))
    (setq p2 (list KKD2 KKD1))
    (command "line" p2 p1 "")
  )

  ;  오른쪽 프레임
  (setq slist3 (list 0 1.68 5.15 5.47 26.6 27.48 28.66 30.01 30.52 59.33 60.15 60.86 
                     62.46 63.17 63.98 93.13 93.62 94.97 96.15 97.03 121.68 121.8
               )
  )
  (foreach kim3 slist3 
    (setq KKD1 (- height 500))
    (setq KKD2 kim3)
    (setq KKD2 (+ width KKD2))
    (setq p1 (list KKD2 0))
    (setq p2 (list KKD2 KKD1))
    (command "line" p2 p1 "")
  )

  ; 아래선 그리기
  (setq KKD1 (+ width 121.8))
  (setq KKD2 -121.8)
  (setq p1 (list KKD2 0))
  (setq p2 (list KKD1 0))
  (command "line" p2 p1 "")

  ; 측면도 그리기

  (setq slist4 (list 0 59.2 69.71 70.21 94.21 105.23 141.71 94.71))
  (foreach kim4 slist4 
    (setq KKD1 (- height 500))
    (setq KKD2 side_distance)
    (setq KKD3 kim4)
    (setq KKD2 (- KKD2 KKD3))
    (setq p1 (list KKD2 0))
    (setq p2 (list KKD2 KKD1))
    (command "line" p2 p1 "")
  )
  ; 정면도 그리기(bottom bar)
  (setq KKD1 bottombar_height)
  (setq slist5 (list 0 bottom_sheet_height windbar_height color_tie_height))
  (foreach kim5 slist5 
    (setq KKD1 (+ KKD1 kim5))
    (setq KKD2 width)
    (setq p1 (list 0 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
  (setq KKD1 (+ KKD1 nocolor_height))
  (setq temp (+ side_distance -59.2))
  (command "insert" "winbar2" (list temp KKD1) "" "" "")

  (setq slist5 (list 0 color_tie_height windbar_height))
  (foreach kim5 slist5 
    (setq KKD1 (+ KKD1 kim5))
    (setq KKD2 width)
    (setq p1 (list 0 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
)
(defun c2_frame_bottom_base () 
  (setq temp (+ side_distance -59.2))
  (command "insert" "bottom c-1 side2" (list temp 0) "" "" "")
  ; 왼쪽 프레임 그리기
  (setq slist2 (list 0 -121.8044 -121.6844 -1.6844 -5.1544 -5.4744 -26.644 -27.4844 
                     -28.6644 -30.0144 -30.5244 -59.3444 -60.1544 -60.8644 -62.4644 
                     -63.1744 -63.9844 -93.1344 -93.6244 -94.9744 -96.1544
               )
  )
  (foreach kim2 slist2 
    (setq KKD1 (- height 500))
    (setq KKD2 kim2)
    (setq p1 (list KKD2 0))
    (setq p2 (list KKD2 KKD1))
    (command "line" p2 p1 "")
  )

  ;  오른쪽 프레임
  (setq slist3 (list 0 1.68 5.15 5.47 26.6 27.48 28.66 30.01 30.52 59.33 60.15 60.86 
                     62.46 63.17 63.98 93.13 93.62 94.97 96.15 97.03 121.68 121.8
               )
  )
  (foreach kim3 slist3 
    (setq KKD1 (- height 500))
    (setq KKD2 kim3)
    (setq KKD2 (+ width KKD2))
    (setq p1 (list KKD2 0))
    (setq p2 (list KKD2 KKD1))
    (command "line" p2 p1 "")
  )

  ; 아래선 그리기
  (setq KKD1 (+ width 121.8))
  (setq KKD2 -121.8)
  (setq p1 (list KKD2 0))
  (setq p2 (list KKD1 0))
  (command "line" p2 p1 "")

  ; 측면도 그리기

  (setq slist4 (list 0 59.2 69.71 70.21 94.21 105.23 141.71 94.71))
  (foreach kim4 slist4 
    (setq KKD1 (- height 500))
    (setq KKD2 side_distance)
    (setq KKD3 kim4)
    (setq KKD2 (- KKD2 KKD3))
    (setq p1 (list KKD2 0))
    (setq p2 (list KKD2 KKD1))
    (command "line" p2 p1 "")
  )
  ; 정면도 그리기(bottom bar)
  (setq KKD1 bottombar_height)
  (setq slist5 (list 0 bottom_sheet_height windbar_height color_tie_height))
  (foreach kim5 slist5 
    (setq KKD1 (+ KKD1 kim5))
    (setq KKD2 width)
    (setq p1 (list 0 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
  (setq KKD1 (+ KKD1 c2_nocolor_height))
  (setq temp (+ side_distance -59.2))
  (command "insert" "winbar2" (list temp KKD1) "" "" "")

  (setq slist5 (list 0 color_tie_height windbar_height))
  (foreach kim5 slist5 
    (setq KKD1 (+ KKD1 kim5))
    (setq KKD2 width)
    (setq p1 (list 0 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
)
(defun defintion () 
  (setq side_distance (- 2500))
  (setq up_distance 2500)
  (setq bottombar_height 148.67)
  (setq bottom_sheet_height 700)
  (setq windbar_height 52.49)
  (setq color_tie_height 30)
  ; 투명창 높이
  (setq nocolor_height 600)
  ; c2 투명창 높이
  (setq c2_nocolor_height 550)

  (setq sheet_1 (- 850 130))
  (setq sheet_2 (- 750 130))
  (setq sheet_3 (- 800 130))

  (setq c2_sheet_1 (- 680 130))
  (setq c2_sheet_2 (- 600 130))
  (setq c2_sheet_3 (- 700 130))

  
  (setq base_width_wind 1643.66)  
  (setq c2_base_width_wind 1593.66)  
)
(defun draw (sheet) 
  (setq KKD1 (+ c2_base_width_wind sheet))
  (command "insert" "winbar2" (list temp KKD1) "" "" "")

  (setq slist5 (list 30 windbar_height))
  (foreach kim5 slist5 
    (setq KKD1 (+ KKD1 kim5))
    (setq KKD2 width)
    (setq p1 (list 0 KKD1))
    (setq p2 (list KKD2 KKD1))
    (command "line" p1 p2 "")
  )
)
(defun c1_draw_windbar () 
  (if (< height 2400) 
    (progn 
      
    )
  )
  (if (AND (>= height 2400) (< height 3200)) 
    (progn 
      (draw sheet_1)
    )
  )
  (if (AND (>= height 3200) (< height 4000)) 
    (progn 
      (draw sheet_2)
      (setq sheet (+ sheet_1 sheet_2))
      (draw (+ sheet 112.5))
    )
  )
  (if (AND (>= height 4000) (< height 5500)) 
    (progn 
      (draw sheet_3)
      (setq sheet (+ sheet_3 sheet_2))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
      (setq sheet (+ sheet sheet_1))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
    )
  )
  (if (>= height 5500) 
    (progn 
      (draw sheet_1)
      (setq sheet (+ sheet_1 sheet_2))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
      (setq sheet (+ sheet sheet_1))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
      (setq sheet (+ sheet sheet_1))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
    )
  )
)
(defun c2_draw_windbar () 
  (if (< height 2400) 
    (progn 
      
    )
  )
  (if (AND (>= height 2400) (< height 3200)) 
    (progn 
      (draw c2_sheet_1)
    )
  )
  (if (AND (>= height 3200) (< height 3700)) 
    (progn 
      (draw c2_sheet_2)
      (setq sheet (+ c2_sheet_1 c2_sheet_2))
      (draw (+ sheet 112.5))
    )
  )
  (if (AND (>= height 3700) (< height 4500)) 
    (progn 
      (draw c2_sheet_3)
      (setq sheet (+ c2_sheet_3 c2_sheet_2))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
      (setq sheet (+ sheet c2_sheet_1))
      (setq sheet (+ sheet 112.5))
      (draw sheet)
    )
  )
  (if (>= height 4500) 
    (progn 
 
    )
  )
)

(defun c:speed () 
  ; input
  (setq model (getstring "Enter Door's model ? c-1,c-2,c-3 is WIP: "))
  (setq width (getreal "Enter Door's Width ?  : "))
  (setq height (getreal "Enter Door's Height ?  : "))

  (defintion)

  (cond 
    ((= model "c-1")
     (c1_drum_line_draw)
     (c1_base)
     (c1_frame_bottom_base)
     (c1_draw_windbar)
     (dim_line)
    )
    ((= model "c-2")
     (c2_drum_line_draw)
     (c2_base)
     (c2_frame_bottom_base)
     (c2_draw_windbar)
     (dim_line)
    )
    ((= model "c-3")
;    (c2_drum_line_draw)
     (c3_base)
    ;  (c2_frame_bottom_base)
    ;  (c2_draw_windbar)
    ;  (dim_line)
    )
  )
  (princ)
)

댓글