Chuyển đến nội dung
Diễn đàn CADViet
lenhatanh

[Nhờ chỉnh sửa] lisp xuất Kết quả đo diện tích sang Excel

Các bài được khuyến nghị

Mình viết một lisp đo diện tích MCN, xuất kết quả ra màn hình CAD, xuất Kq ra Excel

nhưng khi xuất kq ra Excel thì lần thứ hai lại lặp lại từ đầu (mở lại Excel và ghi vào Book mới)

Mình muốn khi xuất kq ra Excel thì lần thứ hai, ba, bốn... kết quả vẫn ghi tiếp tục vào Book1 đầu tiên.

Nhờ mọi người chỉnh sửa lisp giúp nhé.

 

;---------Tinh Dien Tich Dao, Dap (Cho M.cat) - dientich-01
;**********************************************************
(defun Get_tle (/ g:tle)
 (set_tile "error" "")
 (setq g:tle (get_tile "tle"))
 (setq tle (atof g:tle))
)
;------------------------------------------
(defun Get_ten (/ g:ten)
 (set_tile "error" "")
 (setq g:ten (get_tile "ten"))
 (setq ten g:ten)
)
;------------------------------------------
(defun Get_cdon (/ g:cdon)
 (set_tile "error" "")
 (setq g:cdon (get_tile "cdon"))
 (setq cdon (atof g:cdon))
)
;------------------------------------------
;------------------------------------------
(defun Get_t1()
 (set_tile "error" "")
 (setq g:t1 (get_tile "t1"))
 (setq t1 g:t1)
 (cond
	((= g:t1 "1")
 	(mode_tile "e1" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p1" 0)
	)
	(T
 	(mode_tile "e1" 1)
 	(mode_tile "s1" 1)
 	(mode_tile "p1" 1)
 	(set_tile "e1" (setq g:e1 ""))
 	(set_tile "s1" (STRCAT "= " (setq g:s1 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t2()
 (set_tile "error" "")
 (setq g:t2 (get_tile "t2"))
 (setq t2 g:t2)
 (cond
	((= g:t2 "1")
 	(mode_tile "e2" 0)
 	(mode_tile "s2" 0)
 	(mode_tile "p2" 0)
	)
	(T
 	(mode_tile "e2" 1)
 	(mode_tile "s2" 1)
 	(mode_tile "p2" 1)
 	(set_tile "e2" (setq g:e2 ""))
 	(set_tile "s2" (STRCAT "= " (setq g:s2 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t3()
 (set_tile "error" "")
 (setq g:t3 (get_tile "t3"))
 (setq t3 g:t3)
 (cond
	((= g:t3 "1")
 	(mode_tile "e3" 0)
 	(mode_tile "s3" 0)
 	(mode_tile "p3" 0)
	)
	(T
 	(mode_tile "e3" 1)
 	(mode_tile "s3" 1)
 	(mode_tile "p3" 1)
 	(set_tile "e3" (setq g:e3 ""))
 	(set_tile "s3" (STRCAT "= " (setq g:s3 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t4()
 (set_tile "error" "")
 (setq g:t4 (get_tile "t4"))
 (setq t4 g:t4)
 (cond
	((= g:t4 "1")
 	(mode_tile "e4" 0)
 	(mode_tile "s4" 0)
 	(mode_tile "p4" 0)
	)
	(T
 	(mode_tile "e4" 1)
 	(mode_tile "s4" 1)
 	(mode_tile "p4" 1)
 	(set_tile "e4" (setq g:e4 ""))
 	(set_tile "s4" (STRCAT "= " (setq g:s4 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t5()
 (set_tile "error" "")
 (setq g:t5 (get_tile "t5"))
 (setq t5 g:t5)
 (cond
	((= g:t5 "1")
 	(mode_tile "e5" 0)
 	(mode_tile "s5" 0)
 	(mode_tile "p5" 0)
	)
	(T
 	(mode_tile "e5" 1)
 	(mode_tile "s5" 1)
 	(mode_tile "p5" 1)
 	(set_tile "e5" (setq g:e5 ""))
 	(set_tile "s5" (STRCAT "= " (setq g:s5 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t6()
 (set_tile "error" "")
 (setq g:t6 (get_tile "t6"))
 (setq t6 g:t6)
 (cond
	((= g:t6 "1")
 	(mode_tile "e6" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p6" 0)
	)
	(T
 	(mode_tile "e6" 1)
 	(mode_tile "s6" 1)
 	(mode_tile "p6" 1)
 	(set_tile "e6" (setq g:e6 ""))
 	(set_tile "s6" (STRCAT "= " (setq g:s6 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t7()
 (set_tile "error" "")
 (setq g:t7 (get_tile "t7"))
 (setq t7 g:t7)
 (cond
	((= g:t7 "1")
 	(mode_tile "e7" 0)
 	(mode_tile "s7" 0)
 	(mode_tile "p7" 0)
	)
	(T
 	(mode_tile "e7" 1)
 	(mode_tile "s7" 1)
 	(mode_tile "p7" 1)
 	(set_tile "e7" (setq g:e7 ""))
 	(set_tile "s7" (STRCAT "= " (setq g:s7 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t8()
 (set_tile "error" "")
 (setq g:t8 (get_tile "t8"))
 (setq t8 g:t8)
 (cond
	((= g:t8 "1")
 	(mode_tile "e8" 0)
 	(mode_tile "s8" 0)
 	(mode_tile "p8" 0)
	)
	(T
 	(mode_tile "e8" 1)
 	(mode_tile "s8" 1)
 	(mode_tile "p8" 1)
 	(set_tile "e8" (setq g:e8 ""))
 	(set_tile "s8" (STRCAT "= " (setq g:s8 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t9()
 (set_tile "error" "")
 (setq g:t9 (get_tile "t9"))
 (setq t9 g:t9)
 (cond
	((= g:t9 "1")
 	(mode_tile "e9" 0)
 	(mode_tile "s9" 0)
 	(mode_tile "p9" 0)
	)
	(T
 	(mode_tile "e9" 1)
 	(mode_tile "s9" 1)
 	(mode_tile "p9" 1)
 	(set_tile "e9" (setq g:e9 ""))
 	(set_tile "s9" (STRCAT "= " (setq g:s9 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
(defun Get_t10()
 (set_tile "error" "")
 (setq g:t10 (get_tile "t10"))
 (setq t10 g:t10)
 (cond
	((= g:t10 "1")
 	(mode_tile "e10" 0)
 	(mode_tile "s10" 0)
 	(mode_tile "p10" 0)
	)
	(T
 	(mode_tile "e10" 1)
 	(mode_tile "s10" 1)
 	(mode_tile "p10" 1)
 	(set_tile "e10" (setq g:e10 ""))
 	(set_tile "s10" (STRCAT "= " (setq g:s10 (rtos 0.0 2 3)) " m2"))
	)
 )
)
;------------------------------------------
;------------------------------------------
(defun Get_e1()
 (set_tile "error" "")
 (setq g:e1 (get_tile "e1"))
 (setq e1 g:e1)
)
;------------------------------------------
(defun Get_e2()
 (set_tile "error" "")
 (setq g:e2 (get_tile "e2"))
 (setq e2 g:e2)
)
;------------------------------------------
(defun Get_e3()
 (set_tile "error" "")
 (setq g:e3 (get_tile "e3"))
 (setq e3 g:e3)
)
;------------------------------------------
(defun Get_e4()
 (set_tile "error" "")
 (setq g:e4 (get_tile "e4"))
 (setq e4 g:e4)
)
;------------------------------------------
(defun Get_e5()
 (set_tile "error" "")
 (setq g:e5 (get_tile "e5"))
 (setq e5 g:e5)
)
;------------------------------------------
(defun Get_e6()
 (set_tile "error" "")
 (setq g:e6 (get_tile "e6"))
 (setq e6 g:e6)
)
;------------------------------------------
(defun Get_e7()
 (set_tile "error" "")
 (setq g:e7 (get_tile "e7"))
 (setq e7 g:e7)
)
;------------------------------------------
(defun Get_e8()
 (set_tile "error" "")
 (setq g:e8 (get_tile "e8"))
 (setq e8 g:e8)
)
;------------------------------------------
(defun Get_e9()
 (set_tile "error" "")
 (setq g:e9 (get_tile "e9"))
 (setq e9 g:e9)
)
;------------------------------------------
(defun Get_e10()
 (set_tile "error" "")
 (setq g:e10 (get_tile "e10"))
 (setq e10 g:e10)
)
;------------------------------------------
;------------------------------------------
(defun Get_p1()
 (set_tile "error" "")
 (dientich)
;  (alert (strcat "\n tri so g:btct = " g:btct))
)
;------------------------------------------
(defun Get_p2()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p3()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p4()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p5()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p6()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p7()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p8()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p9()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p10()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
;;;mode_tile - 0 = an (Tat), 1 = Hien (mo)
;------------------------------------------
(defun Setloc()
 (if (not tle) (setq tle 50.0))
 (set_tile "tle" (setq g:tle (rtos tle 2 0)))

 (if (not ten) (setq ten "A-A"))
 (set_tile "ten" (setq g:ten ten))

 (if (not cdon) (setq cdon 0.0))
 (set_tile "cdon" (setq g:cdon (rtos cdon 2 2)))

 (if (not e1) (setq e1 ""))
 (set_tile "e1" (setq g:e1 e1))

 (if (not e2) (setq e2 ""))
 (set_tile "e2" (setq g:e2 e2))

 (if (not e3) (setq e3 ""))
 (set_tile "e3" (setq g:e3 e3))

 (if (not e4) (setq e4 ""))
 (set_tile "e4" (setq g:e4 e4))

 (if (not e5) (setq e5 ""))
 (set_tile "e5" (setq g:e5 e5))

 (if (not e6) (setq e6 ""))
 (set_tile "e6" (setq g:e6 e6))

 (if (not e7) (setq e7 ""))
 (set_tile "e7" (setq g:e7 e7))

 (if (not e8) (setq e8 ""))
 (set_tile "e8" (setq g:e8 e8))

 (if (not e9) (setq e9 ""))
 (set_tile "e9" (setq g:e9 e9))

 (if (not e10) (setq e10 ""))
 (set_tile "e10" (setq g:e10 e10))

 (if (not dt1) (setq dt1 0.0))
 (set_tile "s1" (STRCAT "= " (setq g:s1 (rtos dt1 2 3)) " m2"))

 (if (not dt2) (setq dt2 0.0))
 (set_tile "s2" (STRCAT "= " (setq g:s2 (rtos dt2 2 3)) " m2"))

 (if (not dt3) (setq dt3 0.0))
 (set_tile "s3" (STRCAT "= " (setq g:s3 (rtos dt3 2 3)) " m2"))

 (if (not dt4) (setq dt4 0.0))
 (set_tile "s4" (STRCAT "= " (setq g:s4 (rtos dt4 2 3)) " m2"))

 (if (not dt5) (setq dt5 0.0))
 (set_tile "s5" (STRCAT "= " (setq g:s5 (rtos dt5 2 3)) " m2"))

 (if (not dt6) (setq dt6 0.0))
 (set_tile "s6" (STRCAT "= " (setq g:s6 (rtos dt6 2 3)) " m2"))

 (if (not dt7) (setq dt7 0.0))
 (set_tile "s7" (STRCAT "= " (setq g:s7 (rtos dt7 2 3)) " m2"))

 (if (not dt8) (setq dt8 0.0))
 (set_tile "s8" (STRCAT "= " (setq g:s8 (rtos dt8 2 3)) " m2"))

 (if (not dt9) (setq dt9 0.0))
 (set_tile "s9" (STRCAT "= " (setq g:s9 (rtos dt9 2 3)) " m2"))

 (if (not dt10) (setq dt10 0.0))
 (set_tile "s10" (STRCAT "= " (setq g:s10 (rtos dt10 2 3)) " m2"))
 (if (not g:t1) (setq g:t1 "1"))
 (set_tile "t1" g:t1)
 (cond
	((= g:t1 "1")
 	(mode_tile "e1" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p1" 0)
	)
	(T
 	(mode_tile "e1" 1)
 	(mode_tile "s1" 1)
 	(mode_tile "p1" 1)
	)
 )
 (if (not g:t2) (setq g:t2 "0"))
 (set_tile "t2" g:t2)
 (cond
	((= g:t2 "1")
 	(mode_tile "e2" 0)
 	(mode_tile "s2" 0)
 	(mode_tile "p2" 0)
	)
	(T
 	(mode_tile "e2" 1)
 	(mode_tile "s2" 1)
 	(mode_tile "p2" 1)
	)
 )
 (if (not g:t3) (setq g:t3 "0"))
 (set_tile "t3" g:t3)
 (cond
	((= g:t3 "1")
 	(mode_tile "e3" 0)
 	(mode_tile "s3" 0)
 	(mode_tile "p3" 0)
	)
	(T
 	(mode_tile "e3" 1)
 	(mode_tile "s3" 1)
 	(mode_tile "p3" 1)
	)
 )
 (if (not g:t4) (setq g:t4 "0"))
 (set_tile "t4" g:t4)
 (cond
	((= g:t4 "1")
 	(mode_tile "e4" 0)
 	(mode_tile "s4" 0)
 	(mode_tile "p4" 0)
	)
	(T
 	(mode_tile "e4" 1)
 	(mode_tile "s4" 1)
 	(mode_tile "p4" 1)
	)
 )
 (if (not g:t5) (setq g:t5 "0"))
 (set_tile "t5" g:t5)
 (cond
	((= g:t5 "1")
 	(mode_tile "e5" 0)
 	(mode_tile "s5" 0)
 	(mode_tile "p5" 0)
	)
	(T
 	(mode_tile "e5" 1)
 	(mode_tile "s5" 1)
 	(mode_tile "p5" 1)
	)
 )
 (if (not g:t6) (setq g:t6 "0"))
 (set_tile "t6" g:t6)
 (cond
	((= g:t6 "1")
 	(mode_tile "e6" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p6" 0)
	)
	(T
 	(mode_tile "e6" 1)
 	(mode_tile "s6" 1)
 	(mode_tile "p6" 1)
	)
 )
 (if (not g:t7) (setq g:t7 "0"))
 (set_tile "t7" g:t7)
 (cond
	((= g:t7 "1")
 	(mode_tile "e7" 0)
 	(mode_tile "s7" 0)
 	(mode_tile "p7" 0)
	)
	(T
 	(mode_tile "e7" 1)
 	(mode_tile "s7" 1)
 	(mode_tile "p7" 1)
	)
 )
 (if (not g:t8) (setq g:t8 "0"))
 (set_tile "t8" g:t8)
 (cond
	((= g:t8 "1")
 	(mode_tile "e8" 0)
 	(mode_tile "s8" 0)
 	(mode_tile "p8" 0)
	)
	(T
 	(mode_tile "e8" 1)
 	(mode_tile "s8" 1)
 	(mode_tile "p8" 1)
	)
 )
 (if (not g:t9) (setq g:t9 "0"))
 (set_tile "t9" g:t9)
 (cond
	((= g:t9 "1")
 	(mode_tile "e9" 0)
 	(mode_tile "s9" 0)
 	(mode_tile "p9" 0)
	)
	(T
 	(mode_tile "e9" 1)
 	(mode_tile "s9" 1)
 	(mode_tile "p9" 1)
	)
 )
 (if (not g:t10) (setq g:t10 "0"))
 (set_tile "t10" g:t10)
 (cond
	((= g:t10 "1")
 	(mode_tile "e10" 0)
 	(mode_tile "s10" 0)
 	(mode_tile "p10" 0)
	)
	(T
 	(mode_tile "e10" 1)
 	(mode_tile "s10" 1)
 	(mode_tile "p10" 1)
	)
 )
)
;---------------
(defun Restore()
 (command "LUPREC" 2 "color" "bylayer" "osmode" 97)
 (setq *ERROR* Olderr)
 (setvar "BLIPMODE" Oldblp) (setvar "CMDECHO" Oldech)
 (setvar "PICKBOX" 4) (setvar "DIMZIN" 8)
)
;----------------------------------------------------------------------------
(defun C:dientich-01 (/ Oldblp Oldech Olderr Dial nhim What_next ten cdon sdo
 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 s1 s2 s3 s4 s5 s6 s7 s8 s9 s10
 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 dt1 dt2 dt3 dt4 dt5 dt6 dt7 dt8 dt9 dt10
 g:e1 g:e2 g:e3 g:e4 g:e5 g:e6 g:e7 g:e8 g:e9 g:e10                    
 g:s1 g:s2 g:s3 g:s4 g:s5 g:s6 g:s7 g:s8 g:s9 g:s10)
 (setq Oldblp (getvar "BLIPMODE") Oldech (getvar "CMDECHO") Olderr *ERROR*)
 (setvar "BLIPMODE" 0) (setvar "DIMZIN" 0) (setvar "PICKBOX" 0) (setvar "CMDECHO" 0)
 (COMMAND "LUPREC" 4 "osnap" "None" "UCS" "W")
;----------------------------------------------
(defun *ERROR* (Msg)
 (princ "\nError: ") (princ Msg)
 (unload_dialog Dial)
 (Restore)
 (princ)
)
;---------------------------------------------
 (setq Dial (load_dialog "Dien_tich_01.DCL"))
 (setq What_next 2)
 (while (> What_next 1)
	(if (not (new_dialog "Dientich01" Dial))
 	(progn (Restore) (exit))
 )
(Setloc)
(action_tile "tle" "(Get_tle)")
(action_tile "ten" "(Get_ten)")
(action_tile "cdon" "(Get_cdon)")

(action_tile "t1" "(Get_t1)")
(action_tile "t2" "(Get_t2)")
(action_tile "t3" "(Get_t3)")
(action_tile "t4" "(Get_t4)")
(action_tile "t5" "(Get_t5)")
(action_tile "t6" "(Get_t6)")
(action_tile "t7" "(Get_t7)")
(action_tile "t8" "(Get_t8)")
(action_tile "t9" "(Get_t9)")
(action_tile "t10" "(Get_t10)")

(action_tile "e1" "(Get_e1)")
(action_tile "e2" "(Get_e2)")
(action_tile "e3" "(Get_e3)")
(action_tile "e4" "(Get_e4)")
(action_tile "e5" "(Get_e5)")
(action_tile "e6" "(Get_e6)")
(action_tile "e7" "(Get_e7)")
(action_tile "e8" "(Get_e8)")
(action_tile "e9" "(Get_e9)")
(action_tile "e10" "(Get_e10)")

(action_tile "p1" "(done_dialog 2)")
(action_tile "p2" "(done_dialog 3)")
(action_tile "p3" "(done_dialog 4)")
(action_tile "p4" "(done_dialog 5)")
(action_tile "p5" "(done_dialog 6)")
(action_tile "p6" "(done_dialog 7)")
(action_tile "p7" "(done_dialog 8)")
(action_tile "p8" "(done_dialog 9)")
(action_tile "p9" "(done_dialog 10)")
(action_tile "p10" "(done_dialog 11)")
(action_tile "excel" "(done_dialog 12)")
(action_tile "Accept" "(done_dialog 1)")
(action_tile "cancel" "(done_dialog 0)")
(setq What_next (start_dialog))
(cond
 	((= What_next 1) (Go))
 	((= What_next 2) (Get_p1))
 	((= What_next 3) (Get_p2))
 	((= What_next 4) (Get_p3))
 	((= What_next 5) (Get_p4))
 	((= What_next 6) (Get_p5))
 	((= What_next 7) (Get_p6))
 	((= What_next 8) (Get_p7))
 	((= What_next 9) (Get_p8))
 	((= What_next 10) (Get_p9))
 	((= What_next 11) (Get_p10))
 	((= What_next 12) (if (vlax-get-or-create-object "Excel.Application")
                       	(WriteToExcel)
                   	))
)
 )
 (unload_dialog Dial)
 (Restore)
 (princ)
)
;;;---------------------------------------------------------------
(defun dientich (/ bli cmd E Eo En do diem s name Ename oldcolor)
 (setq do nil s nil Ename (ssadd))
 (setq E "P") (initget 128 "P S")
 (setq Eo (getkword "\nSelect Entity or Pick Point (S/<P>) : "))
 (if Eo (setq E Eo))
 (cond
((= E "P")
	(command "color" 6 "linetype" "S" "hidden" "" "osmode" 0)
	(while
  	(setq diem (getpoint "\n Pick point...(<Retern>to end) :"))
  	(command "boundary" diem "" "area" "E" "L")
  	(setq s (getvar "area"))
  	(setq name (entlast))
  	(setq do (cons s do))
  	(setq Ename (ssadd name Ename))
	)
	(setq Sdo (eval (cons + do)))
	(command "erase" Ename "" "color" "Bylayer" "linetype" "S" "continuous" "")u
)
(T
	(setq En (ssget))
	(command "area" "E" En)
	(setq Sdo (getvar "area"))
)
 )
 (setq tile (/ tle 100))
 (cond
((= What_next 2) (setq dt1 (* Sdo tile tile)))
((= What_next 3) (setq dt2 (* Sdo tile tile)))
((= What_next 4) (setq dt3 (* Sdo tile tile)))
((= What_next 5) (setq dt4 (* Sdo tile tile)))
((= What_next 6) (setq dt5 (* Sdo tile tile)))
((= What_next 7) (setq dt6 (* Sdo tile tile)))
((= What_next 8) (setq dt7 (* Sdo tile tile)))
((= What_next 9) (setq dt8 (* Sdo tile tile)))
((= What_next 10) (setq dt9 (* Sdo tile tile)))
((= What_next 11) (setq dt10 (* Sdo tile tile)))
 )
)
;;;-------------------------------------------
(defun Go ()
 (SETQ P (GETPOINT "\nStart point...: "))
 (setq oblist (ssadd))
 (if (> dt1 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e1 " (m2) ="))
;    	(setq obl (entlast) oblist (ssadd obl oblist))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt1 2 3))
;    	(setq obl (entlast) oblist (ssadd obl oblist))
;    	(command "attdef" "" e1 e1 dt1 (polar p 0.0 (getvar "Textsize")) "")
;    	(setq obl (entlast) oblist (ssadd obl oblist))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt2 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e2 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt2 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt3 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e3 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt3 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt4 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e4 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt4 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt5 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e5 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt5 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt6 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e6 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt6 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt7 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e7 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt7 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt8 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e8 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt8 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt9 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e9 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt9 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt10 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e10 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt10 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
;  (command "Block" "Du" "Y" p oblist "")
 (COMMAND "LUPREC" 4 "osmode" 33 "linetype" "S" "Continuous" "" "OOPS")
)
;;;-----------------------------------------------------
(defun WriteToExcel (/ col row x xlApp xlCells)
 (setq lst_data '())
 (setq lst_data (cons ten lst_data) lst_data (cons (rtos cdon 2 2) lst_data))
 (if (> dt1 0) (setq lst_data (cons (rtos dt1 2 3) lst_data)))
 (if (> dt2 0) (setq lst_data (cons (rtos dt2 2 3) lst_data)))
 (if (> dt3 0) (setq lst_data (cons (rtos dt3 2 3) lst_data)))
 (if (> dt4 0) (setq lst_data (cons (rtos dt4 2 3) lst_data)))
 (if (> dt5 0) (setq lst_data (cons (rtos dt5 2 3) lst_data)))
 (if (> dt6 0) (setq lst_data (cons (rtos dt6 2 3) lst_data)))
 (if (> dt7 0) (setq lst_data (cons (rtos dt7 2 3) lst_data)))
 (if (> dt8 0) (setq lst_data (cons (rtos dt8 2 3) lst_data)))
 (if (> dt9 0) (setq lst_data (cons (rtos dt9 2 3) lst_data)))
 (if (> dt10 0) (setq lst_data (cons (rtos dt10 2 3) lst_data)))
 (setq lst_data (reverse lst_data))
 (vl-load-com)
 (setq xlApp (vlax-get-or-create-object "Excel.Application")
	xlCells (vlax-get-property
     	(vlax-get-property
     	(vlax-get-property
     	(vlax-invoke-method
  	(vlax-get-property xlApp "Workbooks")
                           	"Add")
                              	"Sheets")
                              	"Item" 1)
                              	"Cells"))

 (vlax-put-property xlCells 'Item 1 1 "M.cat")
 (vlax-put-property xlCells 'Item 1 2 "C.Don")
 (if (> dt1 0) (vlax-put-property xlCells 'Item 1 3 e1))
 (if (> dt2 0) (vlax-put-property xlCells 'Item 1 4 e2))
 (if (> dt3 0) (vlax-put-property xlCells 'Item 1 5 e3))
 (if (> dt4 0) (vlax-put-property xlCells 'Item 1 6 e4))
 (if (> dt5 0) (vlax-put-property xlCells 'Item 1 7 e5))
 (if (> dt6 0) (vlax-put-property xlCells 'Item 1 8 e6))
 (if (> dt7 0) (vlax-put-property xlCells 'Item 1 9 e7))
 (if (> dt8 0) (vlax-put-property xlCells 'Item 1 10 e8))
 (if (> dt9 0) (vlax-put-property xlCells 'Item 1 11 e9))
 (if (> dt10 0) (vlax-put-property xlCells 'Item 1 12 e10))

 (setq row 2 col 1)
 (foreach pt lst_data
(vlax-put-property xlCells 'Item row col pt)
(setq col (1+ col))
 )
 (vla-put-visible xlApp :vlax-true)
 (mapcar
(function (lambda (x)
 (vl-catch-all-apply (function (lambda ()(if x (vlax-release-object x))))))
)
(list xlCells xlApp)
 )
 (gc)
 (gc)
)

- File *.DCL: Dien_tich_01.dcl

Dientich01 : dialog {
 label = "  *** - TÝnh DiÖn TÝch Cho C¸c MÆt C¾t Ngang - Hec 13";
 fixed_width = center;
 :spacer { height=1; }
 : row {
fixed_width = true;
 children_alignment = centered;
 : edit_box {
  label = "Ti Le Ve MCN:";
  key = "tle";
  edit_width = 6;
  edit_height = 1;
}
 : edit_box {
  label = "Ten Mat Cat:";
  fixed_width = true;
  key = "ten";
  edit_width = 6;
  edit_height = 1;
}
: edit_box {
  label = "Khoang Cach C.Don:";
  fixed_width = true;
  key = "cdon";
  edit_width = 6;
  edit_height = 1;
}
 }
 :spacer { height=1; }
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t1";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e1";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s1";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p1" ;
  width = 26;
}
 }
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t2";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e2";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s2";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p2" ;
  width = 26;
}
 }
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t3";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e3";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s3";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p3" ;
  width = 26;
}
 } 
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t4";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e4";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s4";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p4" ;
  width = 26;
}
 } 
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t5";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e5";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s5";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p5" ;
  width = 26;
}
 }
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t6";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e6";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s6";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p6" ;
  width = 26;
}
 }
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t7";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e7";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s7";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p7" ;
  width = 26;
}
 }   
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t8";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e8";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s8";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p8" ;
  width = 26;
}
 }   
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t9";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e9";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s9";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p9" ;
  width = 26;
}
 }
//----------------------------- 
 : row {   
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t10";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e10";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s10";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p10" ;
  width = 26;
}
 }
//-----------------------------     
 : row {
fixed_height = true;
children_alignment = centered;
: spacer { height=3; }
// : spacer { width=3; }
: button {
  label = "Xuat sang Excel" ;
  is_default = true ;
  key = "excel" ;
  width = 20;
} 
: button {
  label = "OK" ;
  is_default = true ;
  key = "start" ;
  width = 20;
}
: button {
  label = "Cancel" ;
  width = 20;
  is_cancel= true ;
}
// : text_part { label = "paragraph 1"; }
// : paragraph {
//   : text_part { label = "paragraph 1"; }
//   : spacer { height=0.1; }
//   : text_part { label = "paragraph 2"; }
//   : text_part { label = "paragraph 3"; }
// }
// : paragraph {
//   : concatenation {
// 	: text_part {label = "column 1";}
// 	: text_part {label = "column 2";}
// 	: text_part {label = "column 3";}
//   }
// }
:spacer { height=3; }
 }
}

 

- Ảnh minh họa: http://www.cadviet.com/upfiles/3/88193_anh.rar

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Có 2 lý do:

1). Cứ mỗi lần chạy chương trình là bạn lại tạo một file excel mới => Bạn phải tìm cách sửa code để nếu lần đầu thì tạo file mới, còn lần 2, 3,...,n thì mở lại file cũ.

2). Số thứ tự của Row: khi đã mở được file cũ thì bạn phải sửa code để STT của row tăng dần lên, chứ bạn đang để STT row lúc nào cũng giống nhau (cố định, trong lisp STT row luôn bằng 2) thì nó sẽ ghi đè lên thôi.

Chúc bạn thành công!

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Có 2 lý do:

1). Cứ mỗi lần chạy chương trình là bạn lại tạo một file excel mới => Bạn phải tìm cách sửa code để nếu lần đầu thì tạo file mới, còn lần 2, 3,...,n thì mở lại file cũ.

2). Số thứ tự của Row: khi đã mở được file cũ thì bạn phải sửa code để STT của row tăng dần lên, chứ bạn đang để STT row lúc nào cũng giống nhau (cố định, trong lisp STT row luôn bằng 2) thì nó sẽ ghi đè lên thôi.

Cám ơn bạn đã gợi ý, nhưng mình mù tịt về liên kết CAD-Excel nên nhờ mọi người giúp đỡ theo gợi ý của bạn Hà:

1- Code lisp để lần đầu thì tạo file Excel mới, còn lần 2, 3,...,n thì mở lại file cũ.

2- Code lisp xác định số thứ tự của dòng đã ghi cuối cùng trong file Excel đang mở...

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
Cám ơn bạn đã gợi ý, nhưng mình mù tịt về liên kết CAD-Excel nên nhờ mọi người giúp đỡ theo gợi ý của bạn Hà: 1- Code lisp để lần đầu thì tạo file Excel mới, còn lần 2, 3,...,n thì mở lại file cũ. 2- Code lisp xác định số thứ tự của dòng đã ghi cuối cùng trong file Excel đang mở...

Hề hề hề,

Vấn đề của bạn là thích xài sang mà không chịu đọc kỹ hướng dẫn khi sử dụng đấy mà.

Bạn sử dụng đoạn code WriteToExcel của bác Gaibach nhưng không chịu đọc kỹ điều bác ấy nói. Đoạn code đó dùng để ghi dữ liệu vào một file mới toe chứ không phải để ghi thêm vào file.

Để làm cái việc bạn cần, bạn hãy chịu khó đọc kỹ lại các bài viết trong topic mà bạn đã lấy code WriteToexcel. Trong đó có đề cập tới việc mở một file excel có sẵn và chép dữ liệu vô file.

Tuy nhiên còn một vấn đề bạn cần phải giải quyết là tìm địa chỉ cell mà bạn cần ghi dữ liệu.Vì thế bạn có thể vẫn phải dùng tới việc đọc dữ liệu trên file bạn ạ.

bạn có thể tham khảo cái lisp của bác tnmtpc post lên để tìm cách giải quyết vấn đề của bạn.

Do mình cũng chỉ đang mày mò về vấn đề này, chưa rành lắm nên chưa thể giúp bạn được ngay. Tuy nhiên mình cũng sẽ thử xem nó ra răng. Bạn hãy làm thử rồi cùng trao đổi, có thể sẽ nhanh hơn đó.

Chúc bạn thành công.

 

Hề hề hề,

Và đây là một đoạn lisp dựa vào lisp của bác tnmtpc đã post, mình viết để cho phép người dùng mở một file xls có sẵn và chép thêm nội dung vào hàng cuối cùng của file. Bạn có thể tham khảo để ghép với lisp của bạn sao cho phù hợp nhé.

http://www.cadviet.c...4_nhapexcel.lsp

 

Chúc bạn thành công,

Chỉnh sửa theo phamthanhbinh
Bổ sung lisp thử nghiệm
  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Mình loay hoay mãi chỗ mở Excel mà không được,

các Bạn cho mình hỏi ý nghĩa của các Code sau:

1- (setq xlApp (vlax-get-or-create-object "Excel.Application"))

2- (setq xlApp (vlax-get-object "Excel.Application"))

3- (setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))

4- (vlax-invoke-method (vlax-get-property xlApp 'WorkBooks) 'Open ExcelFile$)

5- (vlax-invoke-method (vlax-get-property xlApp 'WorkBooks) 'Add)

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Các Bạn cho mình hỏi ý nghĩa của các Code sau:

1- (setq xlApp (vlax-get-or-create-object "Excel.Application"))

2- (setq xlApp (vlax-get-object "Excel.Application"))

3- (setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))

4- (vlax-invoke-method (vlax-get-property xlApp 'WorkBooks) 'Open ExcelFile$)

5- (vlax-invoke-method (vlax-get-property xlApp 'WorkBooks) 'Add)

Hề hề hề,

Ý nghĩa của tất cả các hàm vlax- ở trên đều được nêu trong help developer của Cad rồi bạn ạ. Do nó viết bằng tiếng Anh nên mình không thể đảm bảo là mình hiểu đúng. Chỉ có thể nói nôm na theo kiểu học mót mà thôi. Vì thế cách tốt nhất là bạn nên cố gắng tự đọc nó trong Help. Còn nôm na thì như sau:

1/- (setq xlApp (vlax-get-or-create-object "Excel.Application")) : Đặt biến xlApp là một đối tượng vla-object được tạo hay có sẵn từ các ứng dụng của phần mềm Excel. Nói một cách ngu ngu hơn một tí là hàm này sẽ tả về một file excel có sẵn hay tạo mới một file excel để người dùng cad có thể thao tác trên đó. Cái việc làm này được gọi tên là biến xlApp.

2- (setq xlApp (vlax-get-object "Excel.Application")): Tương tự như trên nhưng không thể tạo ra một file excel mới mà chỉ sử dụng được với các file excel đã có sẵn.

3- (setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells")): Hàm này hiểu nôm na từ trong ra ngoài như sau:

(vlax-get-property xlApp "Workbooks") : lấy thuộc tính Workbooks (tức tên file) của biến xlApp

(vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add"): Ứng dụng method Add cho file có tên vừa lấy.

(vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets"): mở sheet hiện hành.

(vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1): gán tên sheets là 1

(vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells")): Tìm tới Cells hiện hành.

và đặt tên cells hiện hành này là một vla-object với tên là xlCells

4- (vlax-invoke-method (vlax-get-property xlApp 'WorkBooks) 'Open ExcelFile$): Áp dụng method open với thuộc tính Workbooks của đối tượng xlApp tức là mở file mang tên ExelFile&

5- (vlax-invoke-method (vlax-get-property xlApp 'WorkBooks) 'Add): Áp dụng method Add với thuộc tính Workbooks của đối tượng xlApp tức là thêm một file excel mới.

 

Do mình học mót nên nếu có gì hiểu chưa đúng mong bạn đừng chấp nhé.

Chúc bạn thành công.

  • Vote tăng 2

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Mình viết đoạn code sau để mở Excel từ CAD nhưng vẫn chưa đạt theo yêu cầu:

- Nếu Excel đang mở thì thông báo tắt các Book cũ không liên quan và mở một Book mới để làm việc.

- Nếu Excel chưa mở thì mở Excel mới để làm việc.

Nhờ mọi người sửa giúp...

(defun C:OpenExcel (/ *ExcelApp% lst_data Cell& i)
 (vl-load-com)
 (setq *ExcelApp% (vlax-get-object "Excel.Application"))
 (if *ExcelApp%
(progn
 	(alert "Close all Excel spreadsheets to continue!")
)
(progn
 	(setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
 	(vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
)
 )
 (vla-put-visible *ExcelApp% :vlax-true)
)

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
(defun C:HA( / xlCells xlApp)
(while
 (and
  (setq xlApp (vlax-get-or-create-object "Excel.Application"))
  (vlax-invoke-method xlapp 'Quit)))
(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))
(vla-put-visible xlApp :vlax-true))

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Nhờ bạn Hà thêm cho mình một trường hợp nữa:

- Nếu chưa có Excel thì mở Boock mới, nếu có rồi thì thôi.

(ví dụ sử dụng hàm (if...)) thì tốt, để mình học tập.

Cám ơn trước !.

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Thêm trường hợp thứ 3 thì phải bỏ trường hợp thứ nhất chứ? Và trường hợp này chính là trường hợp thứ 2?

Lisp trên của bạn Hà chạy lần 2, 3... đều đóng Book cũ và mở Book mới - OK,

ý mình là: "Nếu chưa có Excel thì mở Boock mới, nếu có rồi thì thôi".

Như trên mình đã dùng hàm (if... mà không được, nhờ Bạn xem giúp:

(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(if xlApp
(progn
(alert "Excel spreadsheets opening")
)
(progn
(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))
)
)

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Lisp trên của bạn Hà chạy lần 2, 3... đều đóng Book cũ và mở Book mới - OK,

ý mình là: "Nếu chưa có Excel thì mở Boock mới, nếu có rồi thì thôi".

Như trên mình đã dùng hàm (if... mà không được, nhờ Bạn xem giúp:

(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(if xlApp
(progn
(alert "Excel spreadsheets opening")
)
(progn
(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))
)
)

Bạn tham khảo code của bác tnmtpc gửi lên sẽ có thể tìm ra giải pháp đó. Theo ý mình thử dùng hàm con xem sao...

Ý của bạn trong hai bài post là khác nhau nên mình chưa hiểu rõ. Trong code bạn viết chỉ có thông báo đóng file excel mà chưa có lệnh đóng. Bạn có thể dùng hàm con closeexcel của bác tnmtpc để đóng. Muốn vậy bạn phải lấy ra tên file của file Excel đang mở.

Chúc bạn thành công.

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Lisp trên của bạn Hà chạy lần 2, 3... đều đóng Book cũ và mở Book mới - OK,

Ý mình là: "Nếu chưa có Excel thì mở Boock mới, nếu có rồi thì thôi".

Lisp mở WorkBook mới nếu Excel chưa được mở.

;----- Neu chua co WorkBook nao dang mo thi mo WorkBook moi, neu co roi thi thoi.
(defun C:HA1( / xlCells xlApp)
(if
 (or
  (not (setq xlApp (vlax-get-object "Excel.Application")))
  (= (vla-get-visible xlApp) :vlax-false))
 (progn
  (setq xlApp (vlax-get-or-create-object "Excel.Application"))
  (setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))
  (vla-put-visible xlApp :vlax-true)
  (mapcar (function (lambda (x) (vl-catch-all-apply (function (lambda () (if x (vlax-release-object x))))))) (list xlCells xlApp)) ;®Ó b¾t ®Çu l¹i tõ "Bock1.xls"
  (gc) (gc))))
;----- Mo WorkBook moi va dong tat ca WorkBook cu dang mo (neu co).
(defun C:HA2( / xlCells xlApp)
(while
 (and
  (setq xlApp (vlax-get-or-create-object "Excel.Application"))
  (vlax-invoke-method xlapp 'Quit)))
(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(setq xlCells (vlax-get-property (vlax-get-property (vlax-get-property (vlax-invoke-method (vlax-get-property xlApp "Workbooks") "Add") "Sheets") "Item" 1) "Cells"))
(vla-put-visible xlApp :vlax-true))

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Mọi người dùng thử lisp "Đo diện tích, xuât kết quả ra CAD và Excel" và đóng góp ý kiến nhé.

Cám ơn sự góp ý, đóng góp và code của các bạn Hà, Bình, Ketxu, tnmtpc...

- File Dien_tich_01.dcl

Dientich01 : dialog {
 label = "  *** - TÝnh DiÖn TÝch Cho C¸c MÆt C¾t Ngang - Hec 13";
 fixed_width = center;
:  boxed_column {
 : row {
fixed_width = true;
 children_alignment = centered;
 : edit_box {
  label = "Ti Le Ve MCN:";
  key = "tle";
  edit_width = 6;
  edit_height = 1;
}
 : edit_box {
  label = "Ten Mat Cat:";
  fixed_width = true;
  key = "ten";
  edit_width = 6;
  edit_height = 1;
}
: edit_box {
  label = "Khoang Cach C.Don:";
  fixed_width = true;
  key = "cdon";
  edit_width = 6;
  edit_height = 1;
}
 }
}
 :spacer { height=0.5; }
//-----------------------------
: boxed_column {
 label = "-------------Thanh Phan - Hang Muc Tinh Dien Tich:";
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t1";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e1";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s1";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p1" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t2";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e2";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s2";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p2" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t3";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e3";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s3";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p3" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t4";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e4";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s4";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p4" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t5";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e5";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s5";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p5" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t6";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e6";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s6";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p6" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t7";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e7";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s7";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p7" ;
  width = 26;
}
 }  
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t8";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e8";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s8";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p8" ;
  width = 26;
}
 }  
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t9";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e9";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s9";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p9" ;
  width = 26;
}
 }
//-----------------------------
 : row {  
fixed_width = true;
children_alignment = Right;
: toggle {
  key = "t10";
  mnemonic = "b";
 	value = "0";
}
: edit_box {
  label = "S";
  key = "e10";
  edit_width = 30;
}
: text {
  label = "=                	";
  key = "s10";
  edit_width = 100;
}
: button {
  label = "Pick" ;
  is_default = true ;
  key = "p10" ;
  width = 26;
}
 }
}
//-----------------------------    
 : row {
fixed_height = true;
children_alignment = centered;
: spacer { height=3; }
// : spacer { width=3; }
: button {
  label = "Xuat sang Excel" ;
  is_default = true ;
  key = "excel" ;
  width = 20;
}
: button {
  label = "OK" ;
  is_default = true ;
  key = "start" ;
  width = 20;
}
: button {
  label = "Cancel" ;
  width = 20;
  is_cancel= true ;
}
// : text_part { label = "paragraph 1"; }
// : paragraph {
//   : text_part { label = "paragraph 1"; }
//   : spacer { height=0.1; }
//   : text_part { label = "paragraph 2"; }
//   : text_part { label = "paragraph 3"; }
// }
// : paragraph {
//   : concatenation {
// 	: text_part {label = "column 1";}
// 	: text_part {label = "column 2";}
// 	: text_part {label = "column 3";}
//   }
// }
:spacer { height=0.1; }
 }
}

- File lisp:

;---------Tinh Dien Tich Dao, Dap (Cho M.cat) - dientich-01
;**********************************************************
(defun Get_tle (/ g:tle)
 (set_tile "error" "")
 (setq g:tle (get_tile "tle"))
 (setq tle (atof g:tle))
)
;------------------------------------------
(defun Get_ten (/ g:ten)
 (set_tile "error" "")
 (setq g:ten (get_tile "ten"))
 (setq ten g:ten)
)
;------------------------------------------
(defun Get_cdon (/ g:cdon)
 (set_tile "error" "")
 (setq g:cdon (get_tile "cdon"))
 (setq cdon (atof g:cdon))
)
;------------------------------------------
;------------------------------------------
(defun Get_t1()
 (set_tile "error" "")
 (setq g:t1 (get_tile "t1"))
 (setq t1 g:t1)
 (cond
((= g:t1 "1")
 	(mode_tile "e1" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p1" 0)
)
(T
 	(mode_tile "e1" 1)
 	(mode_tile "s1" 1)
 	(mode_tile "p1" 1)
 	(set_tile "e1" (setq g:e1 ""))
 	(set_tile "s1" (STRCAT "= " (setq g:s1 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t2()
 (set_tile "error" "")
 (setq g:t2 (get_tile "t2"))
 (setq t2 g:t2)
 (cond
((= g:t2 "1")
 	(mode_tile "e2" 0)
 	(mode_tile "s2" 0)
 	(mode_tile "p2" 0)
)
(T
 	(mode_tile "e2" 1)
 	(mode_tile "s2" 1)
 	(mode_tile "p2" 1)
 	(set_tile "e2" (setq g:e2 ""))
 	(set_tile "s2" (STRCAT "= " (setq g:s2 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t3()
 (set_tile "error" "")
 (setq g:t3 (get_tile "t3"))
 (setq t3 g:t3)
 (cond
((= g:t3 "1")
 	(mode_tile "e3" 0)
 	(mode_tile "s3" 0)
 	(mode_tile "p3" 0)
)
(T
 	(mode_tile "e3" 1)
 	(mode_tile "s3" 1)
 	(mode_tile "p3" 1)
 	(set_tile "e3" (setq g:e3 ""))
 	(set_tile "s3" (STRCAT "= " (setq g:s3 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t4()
 (set_tile "error" "")
 (setq g:t4 (get_tile "t4"))
 (setq t4 g:t4)
 (cond
((= g:t4 "1")
 	(mode_tile "e4" 0)
 	(mode_tile "s4" 0)
 	(mode_tile "p4" 0)
)
(T
 	(mode_tile "e4" 1)
 	(mode_tile "s4" 1)
 	(mode_tile "p4" 1)
 	(set_tile "e4" (setq g:e4 ""))
 	(set_tile "s4" (STRCAT "= " (setq g:s4 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t5()
 (set_tile "error" "")
 (setq g:t5 (get_tile "t5"))
 (setq t5 g:t5)
 (cond
((= g:t5 "1")
 	(mode_tile "e5" 0)
 	(mode_tile "s5" 0)
 	(mode_tile "p5" 0)
)
(T
 	(mode_tile "e5" 1)
 	(mode_tile "s5" 1)
 	(mode_tile "p5" 1)
 	(set_tile "e5" (setq g:e5 ""))
 	(set_tile "s5" (STRCAT "= " (setq g:s5 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t6()
 (set_tile "error" "")
 (setq g:t6 (get_tile "t6"))
 (setq t6 g:t6)
 (cond
((= g:t6 "1")
 	(mode_tile "e6" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p6" 0)
)
(T
 	(mode_tile "e6" 1)
 	(mode_tile "s6" 1)
 	(mode_tile "p6" 1)
 	(set_tile "e6" (setq g:e6 ""))
 	(set_tile "s6" (STRCAT "= " (setq g:s6 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t7()
 (set_tile "error" "")
 (setq g:t7 (get_tile "t7"))
 (setq t7 g:t7)
 (cond
((= g:t7 "1")
 	(mode_tile "e7" 0)
 	(mode_tile "s7" 0)
 	(mode_tile "p7" 0)
)
(T
 	(mode_tile "e7" 1)
 	(mode_tile "s7" 1)
 	(mode_tile "p7" 1)
 	(set_tile "e7" (setq g:e7 ""))
 	(set_tile "s7" (STRCAT "= " (setq g:s7 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t8()
 (set_tile "error" "")
 (setq g:t8 (get_tile "t8"))
 (setq t8 g:t8)
 (cond
((= g:t8 "1")
 	(mode_tile "e8" 0)
 	(mode_tile "s8" 0)
 	(mode_tile "p8" 0)
)
(T
 	(mode_tile "e8" 1)
 	(mode_tile "s8" 1)
 	(mode_tile "p8" 1)
 	(set_tile "e8" (setq g:e8 ""))
 	(set_tile "s8" (STRCAT "= " (setq g:s8 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t9()
 (set_tile "error" "")
 (setq g:t9 (get_tile "t9"))
 (setq t9 g:t9)
 (cond
((= g:t9 "1")
 	(mode_tile "e9" 0)
 	(mode_tile "s9" 0)
 	(mode_tile "p9" 0)
)
(T
 	(mode_tile "e9" 1)
 	(mode_tile "s9" 1)
 	(mode_tile "p9" 1)
 	(set_tile "e9" (setq g:e9 ""))
 	(set_tile "s9" (STRCAT "= " (setq g:s9 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
(defun Get_t10()
 (set_tile "error" "")
 (setq g:t10 (get_tile "t10"))
 (setq t10 g:t10)
 (cond
((= g:t10 "1")
 	(mode_tile "e10" 0)
 	(mode_tile "s10" 0)
 	(mode_tile "p10" 0)
)
(T
 	(mode_tile "e10" 1)
 	(mode_tile "s10" 1)
 	(mode_tile "p10" 1)
 	(set_tile "e10" (setq g:e10 ""))
 	(set_tile "s10" (STRCAT "= " (setq g:s10 (rtos 0.0 2 3)) " m2"))
)
 )
)
;------------------------------------------
;------------------------------------------
(defun Get_e1()
 (set_tile "error" "")
 (setq g:e1 (get_tile "e1"))
 (setq e1 g:e1)
)
;------------------------------------------
(defun Get_e2()
 (set_tile "error" "")
 (setq g:e2 (get_tile "e2"))
 (setq e2 g:e2)
)
;------------------------------------------
(defun Get_e3()
 (set_tile "error" "")
 (setq g:e3 (get_tile "e3"))
 (setq e3 g:e3)
)
;------------------------------------------
(defun Get_e4()
 (set_tile "error" "")
 (setq g:e4 (get_tile "e4"))
 (setq e4 g:e4)
)
;------------------------------------------
(defun Get_e5()
 (set_tile "error" "")
 (setq g:e5 (get_tile "e5"))
 (setq e5 g:e5)
)
;------------------------------------------
(defun Get_e6()
 (set_tile "error" "")
 (setq g:e6 (get_tile "e6"))
 (setq e6 g:e6)
)
;------------------------------------------
(defun Get_e7()
 (set_tile "error" "")
 (setq g:e7 (get_tile "e7"))
 (setq e7 g:e7)
)
;------------------------------------------
(defun Get_e8()
 (set_tile "error" "")
 (setq g:e8 (get_tile "e8"))
 (setq e8 g:e8)
)
;------------------------------------------
(defun Get_e9()
 (set_tile "error" "")
 (setq g:e9 (get_tile "e9"))
 (setq e9 g:e9)
)
;------------------------------------------
(defun Get_e10()
 (set_tile "error" "")
 (setq g:e10 (get_tile "e10"))
 (setq e10 g:e10)
)
;------------------------------------------
;------------------------------------------
(defun Get_p1()
 (set_tile "error" "")
 (dientich)
;  (alert (strcat "\n tri so g:btct = " g:btct))
)
;------------------------------------------
(defun Get_p2()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p3()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p4()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p5()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p6()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p7()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p8()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p9()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
(defun Get_p10()
 (set_tile "error" "")
 (dientich)
)
;------------------------------------------
;;;mode_tile - 0 = an (Tat), 1 = Hien (mo)
;------------------------------------------
(defun Setloc()
 (if (not tle) (setq tle 50.0))
 (set_tile "tle" (setq g:tle (rtos tle 2 0)))

 (if (not ten) (setq ten "A-A"))
 (set_tile "ten" (setq g:ten ten))

 (if (not cdon) (setq cdon 0.0))
 (set_tile "cdon" (setq g:cdon (rtos cdon 2 2)))

 (if (not e1) (setq e1 ""))
 (set_tile "e1" (setq g:e1 e1))

 (if (not e2) (setq e2 ""))
 (set_tile "e2" (setq g:e2 e2))

 (if (not e3) (setq e3 ""))
 (set_tile "e3" (setq g:e3 e3))

 (if (not e4) (setq e4 ""))
 (set_tile "e4" (setq g:e4 e4))

 (if (not e5) (setq e5 ""))
 (set_tile "e5" (setq g:e5 e5))

 (if (not e6) (setq e6 ""))
 (set_tile "e6" (setq g:e6 e6))

 (if (not e7) (setq e7 ""))
 (set_tile "e7" (setq g:e7 e7))

 (if (not e8) (setq e8 ""))
 (set_tile "e8" (setq g:e8 e8))

 (if (not e9) (setq e9 ""))
 (set_tile "e9" (setq g:e9 e9))

 (if (not e10) (setq e10 ""))
 (set_tile "e10" (setq g:e10 e10))

 (if (not dt1) (setq dt1 0.0))
 (set_tile "s1" (STRCAT "= " (setq g:s1 (rtos dt1 2 3)) " m2"))

 (if (not dt2) (setq dt2 0.0))
 (set_tile "s2" (STRCAT "= " (setq g:s2 (rtos dt2 2 3)) " m2"))

 (if (not dt3) (setq dt3 0.0))
 (set_tile "s3" (STRCAT "= " (setq g:s3 (rtos dt3 2 3)) " m2"))

 (if (not dt4) (setq dt4 0.0))
 (set_tile "s4" (STRCAT "= " (setq g:s4 (rtos dt4 2 3)) " m2"))

 (if (not dt5) (setq dt5 0.0))
 (set_tile "s5" (STRCAT "= " (setq g:s5 (rtos dt5 2 3)) " m2"))

 (if (not dt6) (setq dt6 0.0))
 (set_tile "s6" (STRCAT "= " (setq g:s6 (rtos dt6 2 3)) " m2"))

 (if (not dt7) (setq dt7 0.0))
 (set_tile "s7" (STRCAT "= " (setq g:s7 (rtos dt7 2 3)) " m2"))

 (if (not dt8) (setq dt8 0.0))
 (set_tile "s8" (STRCAT "= " (setq g:s8 (rtos dt8 2 3)) " m2"))

 (if (not dt9) (setq dt9 0.0))
 (set_tile "s9" (STRCAT "= " (setq g:s9 (rtos dt9 2 3)) " m2"))

 (if (not dt10) (setq dt10 0.0))
 (set_tile "s10" (STRCAT "= " (setq g:s10 (rtos dt10 2 3)) " m2"))
 (if (not g:t1) (setq g:t1 "1"))
 (set_tile "t1" g:t1)
 (cond
((= g:t1 "1")
 	(mode_tile "e1" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p1" 0)
)
(T
 	(mode_tile "e1" 1)
 	(mode_tile "s1" 1)
 	(mode_tile "p1" 1)
)
 )
 (if (not g:t2) (setq g:t2 "0"))
 (set_tile "t2" g:t2)
 (cond
((= g:t2 "1")
 	(mode_tile "e2" 0)
 	(mode_tile "s2" 0)
 	(mode_tile "p2" 0)
)
(T
 	(mode_tile "e2" 1)
 	(mode_tile "s2" 1)
 	(mode_tile "p2" 1)
)
 )
 (if (not g:t3) (setq g:t3 "0"))
 (set_tile "t3" g:t3)
 (cond
((= g:t3 "1")
 	(mode_tile "e3" 0)
 	(mode_tile "s3" 0)
 	(mode_tile "p3" 0)
)
(T
 	(mode_tile "e3" 1)
 	(mode_tile "s3" 1)
 	(mode_tile "p3" 1)
)
 )
 (if (not g:t4) (setq g:t4 "0"))
 (set_tile "t4" g:t4)
 (cond
((= g:t4 "1")
 	(mode_tile "e4" 0)
 	(mode_tile "s4" 0)
 	(mode_tile "p4" 0)
)
(T
 	(mode_tile "e4" 1)
 	(mode_tile "s4" 1)
 	(mode_tile "p4" 1)
)
 )
 (if (not g:t5) (setq g:t5 "0"))
 (set_tile "t5" g:t5)
 (cond
((= g:t5 "1")
 	(mode_tile "e5" 0)
 	(mode_tile "s5" 0)
 	(mode_tile "p5" 0)
)
(T
 	(mode_tile "e5" 1)
 	(mode_tile "s5" 1)
 	(mode_tile "p5" 1)
)
 )
 (if (not g:t6) (setq g:t6 "0"))
 (set_tile "t6" g:t6)
 (cond
((= g:t6 "1")
 	(mode_tile "e6" 0)
 	(mode_tile "s1" 0)
 	(mode_tile "p6" 0)
)
(T
 	(mode_tile "e6" 1)
 	(mode_tile "s6" 1)
 	(mode_tile "p6" 1)
)
 )
 (if (not g:t7) (setq g:t7 "0"))
 (set_tile "t7" g:t7)
 (cond
((= g:t7 "1")
 	(mode_tile "e7" 0)
 	(mode_tile "s7" 0)
 	(mode_tile "p7" 0)
)
(T
 	(mode_tile "e7" 1)
 	(mode_tile "s7" 1)
 	(mode_tile "p7" 1)
)
 )
 (if (not g:t8) (setq g:t8 "0"))
 (set_tile "t8" g:t8)
 (cond
((= g:t8 "1")
 	(mode_tile "e8" 0)
 	(mode_tile "s8" 0)
 	(mode_tile "p8" 0)
)
(T
 	(mode_tile "e8" 1)
 	(mode_tile "s8" 1)
 	(mode_tile "p8" 1)
)
 )
 (if (not g:t9) (setq g:t9 "0"))
 (set_tile "t9" g:t9)
 (cond
((= g:t9 "1")
 	(mode_tile "e9" 0)
 	(mode_tile "s9" 0)
 	(mode_tile "p9" 0)
)
(T
 	(mode_tile "e9" 1)
 	(mode_tile "s9" 1)
 	(mode_tile "p9" 1)
)
 )
 (if (not g:t10) (setq g:t10 "0"))
 (set_tile "t10" g:t10)
 (cond
((= g:t10 "1")
 	(mode_tile "e10" 0)
 	(mode_tile "s10" 0)
 	(mode_tile "p10" 0)
)
(T
 	(mode_tile "e10" 1)
 	(mode_tile "s10" 1)
 	(mode_tile "p10" 1)
)
 )
)
;---------------
(defun Restore()
 (command "LUPREC" 2 "color" "bylayer" "osmode" 97)
 (setq *ERROR* Olderr)
 (setvar "BLIPMODE" Oldblp) (setvar "CMDECHO" Oldech)
 (setvar "PICKBOX" 4) (setvar "DIMZIN" 8)
)
;----------------------------------------------------------------------------
(defun C:dientich-01 (/ Oldblp Oldech Olderr Dial nhim What_next ten cdon sdo
 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 s1 s2 s3 s4 s5 s6 s7 s8 s9 s10
 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 dt1 dt2 dt3 dt4 dt5 dt6 dt7 dt8 dt9 dt10
 g:e1 g:e2 g:e3 g:e4 g:e5 g:e6 g:e7 g:e8 g:e9 g:e10                    
 g:s1 g:s2 g:s3 g:s4 g:s5 g:s6 g:s7 g:s8 g:s9 g:s10)
 (setq Oldblp (getvar "BLIPMODE") Oldech (getvar "CMDECHO") Olderr *ERROR*)
 (setvar "BLIPMODE" 0) (setvar "DIMZIN" 0) (setvar "PICKBOX" 0) (setvar "CMDECHO" 0)
 (COMMAND "LUPREC" 4 "osnap" "None" "UCS" "W")
;----------------------------------------------
(defun *ERROR* (Msg)
 (princ "\nError: ") (princ Msg)
 (unload_dialog Dial)
 (Restore)
 (princ)
)
;---------------------------------------------
 (setq Dial (load_dialog "Dien_tich_01.DCL"))
 (setq What_next 2)
 (while (> What_next 1)
(if (not (new_dialog "Dientich01" Dial))
 	(progn (Restore) (exit))
 )
(Setloc)
(action_tile "tle" "(Get_tle)")
(action_tile "ten" "(Get_ten)")
(action_tile "cdon" "(Get_cdon)")

(action_tile "t1" "(Get_t1)")
(action_tile "t2" "(Get_t2)")
(action_tile "t3" "(Get_t3)")
(action_tile "t4" "(Get_t4)")
(action_tile "t5" "(Get_t5)")
(action_tile "t6" "(Get_t6)")
(action_tile "t7" "(Get_t7)")
(action_tile "t8" "(Get_t8)")
(action_tile "t9" "(Get_t9)")
(action_tile "t10" "(Get_t10)")

(action_tile "e1" "(Get_e1)")
(action_tile "e2" "(Get_e2)")
(action_tile "e3" "(Get_e3)")
(action_tile "e4" "(Get_e4)")
(action_tile "e5" "(Get_e5)")
(action_tile "e6" "(Get_e6)")
(action_tile "e7" "(Get_e7)")
(action_tile "e8" "(Get_e8)")
(action_tile "e9" "(Get_e9)")
(action_tile "e10" "(Get_e10)")

(action_tile "p1" "(done_dialog 2)")
(action_tile "p2" "(done_dialog 3)")
(action_tile "p3" "(done_dialog 4)")
(action_tile "p4" "(done_dialog 5)")
(action_tile "p5" "(done_dialog 6)")
(action_tile "p6" "(done_dialog 7)")
(action_tile "p7" "(done_dialog 8)")
(action_tile "p8" "(done_dialog 9)")
(action_tile "p9" "(done_dialog 10)")
(action_tile "p10" "(done_dialog 11)")
(action_tile "excel" "(done_dialog 12)")
(action_tile "Accept" "(done_dialog 1)")
(action_tile "cancel" "(done_dialog 0)")
(setq What_next (start_dialog))
(cond
 	((= What_next 1) (Go))
 	((= What_next 2) (Get_p1))
 	((= What_next 3) (Get_p2))
 	((= What_next 4) (Get_p3))
 	((= What_next 5) (Get_p4))
 	((= What_next 6) (Get_p5))
 	((= What_next 7) (Get_p6))
 	((= What_next 8) (Get_p7))
 	((= What_next 9) (Get_p8))
 	((= What_next 10) (Get_p9))
 	((= What_next 11) (Get_p10))
 	((= What_next 12) (WriteToExcel))
)
 )
 (unload_dialog Dial)
 (Restore)
 (princ)
)
;;;--------------------------------------------------------------
(defun dientich (/ bli cmd E Eo En do diem s oldcolor nam Enam)
 (setq do nil s nil Ename (ssadd))
 (setq E "P") (initget 128 "P S")
 (setq Eo (getkword "\nSelect Entity or Pick Point (S/<P>) : "))
 (if Eo (setq E Eo))
 (cond
((= E "P")
(command "color" 112 "linetype" "S" "hidden" "" "osmode" 0)
(while
  	(setq diem (getpoint "\n Pick point...(<Retern> to end) :"))
  	(command "boundary" diem "" "area" "E" "L")
  	(setq s (getvar "area"))
  	(setq do (cons s do))
  	(setq name (entlast))
  	(setq Ename (ssadd name Ename))    
)
(setq Sdo (eval (cons + do)))
(command "erase" Ename "" "color" "Bylayer" "linetype" "S" "continuous" "")
)
(T
(setq En (ssget))
(command "area" "E" En)
(setq Sdo (getvar "area"))
)
 )
 (setq tile (/ tle 100))
 (cond
((= What_next 2) (setq dt1 (* Sdo tile tile)))
((= What_next 3) (setq dt2 (* Sdo tile tile)))
((= What_next 4) (setq dt3 (* Sdo tile tile)))
((= What_next 5) (setq dt4 (* Sdo tile tile)))
((= What_next 6) (setq dt5 (* Sdo tile tile)))
((= What_next 7) (setq dt6 (* Sdo tile tile)))
((= What_next 8) (setq dt7 (* Sdo tile tile)))
((= What_next 9) (setq dt8 (* Sdo tile tile)))
((= What_next 10) (setq dt9 (* Sdo tile tile)))
((= What_next 11) (setq dt10 (* Sdo tile tile)))
 )
)
;;;--------------------------------------------------------------
(defun Go (/ p Enam Ena m n)
 (SETQ P (GETPOINT "\nStart point...: "))
 (setq oblist (ssadd))
 (if (> dt1 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e1 " (m2) ="))
;    	(setq obl (entlast) oblist (ssadd obl oblist))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt1 2 3))
;    	(setq obl (entlast) oblist (ssadd obl oblist))
;    	(command "attdef" "" e1 e1 dt1 (polar p 0.0 (getvar "Textsize")) "")
;    	(setq obl (entlast) oblist (ssadd obl oblist))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt2 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e2 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt2 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt3 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e3 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt3 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt4 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e4 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt4 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt5 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e5 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt5 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt6 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e6 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt6 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt7 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e7 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt7 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt8 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e8 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt8 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt9 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e9 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt9 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
 (if (> dt10 0)
 	(progn
   	(command "text" "J" "R" p "0" (strcat "S " e10 " (m2) ="))
   	(command "text" (polar p 0.0 (getvar "Textsize")) "0" (rtos dt10 2 3))
   	(setq p (polar p (- (/ pi 2)) (* (getvar "Textsize") 2.0)))
 	)
 )
;  (command "Block" "Du" "Y" p oblist "")
 (setq Enam (ssget "X" '((0 . "LWPOLYLINE") (62 . 112) (6 . "HIDDEN"))))
 (setq m 0)
 (repeat (sslength Enam)
(setq Ena (ssname Enam m))
(command "_erase" Ena "")
(setq m (+ m 1))
 )
 (COMMAND "LUPREC" 4 "osmode" 33 "linetype" "S" "Continuous" "" "OOPS")
)
;;;-----------------------------------------------------------------------------
(defun WriteToExcel (/ *ExcelApp% lst_data cviec Cell& i)
 (setq lst_data '() cviec '())
 (setq lst_data (cons ten lst_data) lst_data (cons (rtos cdon 2 2) lst_data))
 (if (> dt1 0) (setq lst_data (cons (rtos dt1 2 3) lst_data)))
 (if (> dt2 0) (setq lst_data (cons (rtos dt2 2 3) lst_data)))
 (if (> dt3 0) (setq lst_data (cons (rtos dt3 2 3) lst_data)))
 (if (> dt4 0) (setq lst_data (cons (rtos dt4 2 3) lst_data)))
 (if (> dt5 0) (setq lst_data (cons (rtos dt5 2 3) lst_data)))
 (if (> dt6 0) (setq lst_data (cons (rtos dt6 2 3) lst_data)))
 (if (> dt7 0) (setq lst_data (cons (rtos dt7 2 3) lst_data)))
 (if (> dt8 0) (setq lst_data (cons (rtos dt8 2 3) lst_data)))
 (if (> dt9 0) (setq lst_data (cons (rtos dt9 2 3) lst_data)))
 (if (> dt10 0) (setq lst_data (cons (rtos dt10 2 3) lst_data)))
 (setq lst_data (reverse lst_data))

 (setq cviec (cons "M.Cat" cviec) cviec (cons "C.Don" cviec))
 (if (> dt1 0) (setq cviec (cons e1 cviec)))
 (if (> dt2 0) (setq cviec (cons e2 cviec)))
 (if (> dt3 0) (setq cviec (cons e3 cviec)))
 (if (> dt4 0) (setq cviec (cons e4 cviec)))
 (if (> dt5 0) (setq cviec (cons e5 cviec)))
 (if (> dt6 0) (setq cviec (cons e6 cviec)))
 (if (> dt7 0) (setq cviec (cons e7 cviec)))
 (if (> dt8 0) (setq cviec (cons e8 cviec)))
 (if (> dt9 0) (setq cviec (cons e9 cviec)))
 (if (> dt10 0) (setq cviec (cons e10 cviec)))
 (setq cviec (reverse cviec))
 (vl-load-com)
 (if
(or
 	(not (setq *ExcelApp% (vlax-get-object "Excel.Application")))
 	(= (vla-get-visible *ExcelApp%) :vlax-false)
)
(progn
 	(setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
 	(vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
 	(vla-put-visible *ExcelApp% :vlax-true)
)
 )
 (setq i 1)
 (while (/= (getcell (strcat "A" (itoa i))) "")
		(setq i (1+ i))
 )
;  (alert (strcat "So dong hien tai: " (rtos i 2 0)))
 (setq Cell& (strcat "A" (itoa i)))
 (putcell Cell& cviec)
 (setq Cell& (strcat "A" (itoa (+ i 1))))  
 (putcell Cell& lst_data)
)
;-------------------------------------------------------------------------------
; GetCell - Returns the cell value from the *ExcelData@ list
; Arguments: 1
;   Cell$ = Cell ID
; Syntax example: (GetCell "E19") = value of cell E19
;-------------------------------------------------------------------------------
(defun GetCell (Cell$ / ExcelRange^ ExcelVariant^ )
  (setq ExcelRange^ (vlax-get-property *ExcelApp% "Range" Cell$))
 	(setq ExcelVariant^ (vlax-get-property ExcelRange^ 'Value))
 	(setq ExcelValue (vlax-variant-value ExcelVariant^))
 	(setq ExcelValue
   	(cond
     	((= (type ExcelValue) 'INT) (itoa ExcelValue))
     	((= (type ExcelValue) 'REAL) (rtosr ExcelValue))
     	((= (type ExcelValue) 'STR) (vl-string-trim " " ExcelValue))
     	((/= (type ExcelValue) 'STR) "")
   	);cond
 	);setq
 ExcelValue
);defun GetCell
;-------------------------------------------------------------------------------
; PutCell - Put values into Excel cells
; Arguments: 2
;   StartCell$ = Starting Cell ID
;   Data@ = Value or list of values
; Syntax examples:
; (PutCell "A1" "PART NUMBER") = Puts PART NUMBER in cell A1
; (PutCell "B3" '("Dim" 7.5 "9.75")) = Starting with cell B3 put Dim, 7.5, and 9.75 across
;-------------------------------------------------------------------------------
(defun PutCell (StartCell$ Data@ / Cell$ Column# ExcelRange Row#)
 (if (= (type Data@) 'STR)
(setq Data@ (list Data@))
 )
 (setq ExcelRange (vlax-get-property *ExcelApp% "Cells"))
 (if (Cell-p StartCell$)
(setq Column# (car (ColumnRow StartCell$))
     	Row# (cadr (ColumnRow StartCell$))
);setq
(if (vl-catch-all-error-p
     	(setq Cell$ (vl-catch-all-apply 'vlax-get-property
       	(list (vlax-get-property *ExcelApp% "ActiveSheet") "Range" StartCell$))
     	);setq
   	);vl-catch-all-error-p
   	(alert (strcat "The cell ID \"" StartCell$ "\" is invalid."))
   	(setq Column# (vlax-get-property Cell$ "Column")
         	Row# (vlax-get-property Cell$ "Row")
   	);setq
);if
 );if
 (if (and Column# Row#)
(foreach Item Data@
 	(vlax-put-property ExcelRange "Item" Row# Column# (vl-princ-to-string Item))
 	(setq Column# (1+ Column#))
);foreach
 );if
 (princ)
);defun PutCell
;-------------------------------------------------------------------------------
(defun ColumnRow (Cell$ / Column$ Char$ Row#)
 (setq Column$ "")
 (while (< 64 (ascii (setq Char$ (strcase (substr Cell$ 1 1)))) 91)
(setq Column$ (strcat Column$ Char$)
     	Cell$ (substr Cell$ 2)
);setq
 );while
 (if (and (/= Column$ "") (numberp (setq Row# (read Cell$))))
(list (Alpha2Number Column$) Row#)
'(1 1);default to "A1" if there's a problem
 );if
);defun ColumnRow
;-------------------------------------------------------------------------------
; Cell-p - Evaluates if the argument Cell$ is a valid cell ID
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Cell$ = String of the cell ID to evaluate
; Syntax examples: (Cell-p "B12") = t, (Cell-p "BT") = nil
;-------------------------------------------------------------------------------
(defun Cell-p (Cell$)
 (and (= (type Cell$) 'STR)
(or (= (strcase Cell$) "A1")
 	(not (equal (ColumnRow Cell$) '(1 1)))
);or
 );and
);defun Cell-p
;-------------------------------------------------------------------------------
(defun rtosr (RealNum~ / DimZin# ShortReal$)
 (setq DimZin# (getvar "DIMZIN"))
 (setvar "DIMZIN" 8)
 (setq ShortReal$ (rtos RealNum~ 2 8))
 (setvar "DIMZIN" DimZin#)
 ShortReal$
);defun rtosr
;-------------------------------------------------------------------------------
; Alpha2Number - Converts Alpha string into Number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Str$ = String to convert
; Syntax example: (Alpha2Number "ABC") = 731
;-------------------------------------------------------------------------------
(defun Alpha2Number (Str$ / Num#)
 (if (= 0 (setq Num# (strlen Str$)))
0
(+ (* (- (ascii (strcase (substr Str$ 1 1))) 64) (expt 26 (1- Num#)))
  	(Alpha2Number (substr Str$ 2))
);+
 );if
);defun Alpha2Number
;-------------------------------------------------------------------------------
; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
 (if (< Num# 27)
(chr (+ 64 Num#))
(if (= 0 (setq Val# (rem Num# 26)))
 	(strcat (Number2Alpha (1- (/ Num# 26))) "Z")
 	(strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
);if
 );if
);defun Number2Alpha

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

À hiểu rồi, cảm ơn bạn ^^ Code hơi dài quá, hi vọng dần dần bạn sẽ cải thiện nó ^^

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Bạn cho mình hỏi sao mình apload về mà không dùng được? lệnh của nó là gì vậy bạn? bạn có thể hướng dẫn cách sử dụng được không?

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

-  Bạn tải hai file File "Dien_tich_01.dcl" và File "Dien_tich_01.lsp" về C:\Program Files\AutoCAD...\Support\

-  Appload "Dien_tich_01.lsp" rồi gõ: "Dien_tich-01" trên dòng nhắc command của CAD.

-  Thực hiện các bước như Video hướng dẫn đã up. 

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

http://www.cadviet.com/upfiles/3/132590_dtich_1.lsp. Mình có một file lisp tính diện tích như trên, mình muốn viết thêm một đoạn code để khi mình pick chọn các vùng tính diện tích sẽ xuất kết quả ra màn hình autocad và cả xuất ra bảng excel nữa. Mong bác Nguyên Hoanh va các cao thủ chỉ giúp!

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay

×