;;; Calculate Area by picking corners send it to excel file and import it to drawing as table
;;; Combined from existing routines and modified by Igal Averbuh 2016

(defun c:act (/ room outlist fh)
(while (/= "" (setq room (getstring 1 "\nEnter Name or Number of Digitizing Area ")))
(princ "\nDigitize Area by picking corners...\n")
(command "._AREA")
(while (not (zerop (getvar "CMDACTIVE"))) (command pause))
(setq outlist (cons (list room (rtos (getvar "AREA"))) outlist))

(setq fl (getfiled "\nSpeficy csv file name to write area data:" "" "csv" 1))

(setq fh (open fl "w"))

(write-line "No Area m²" fh)
(mapcar '(lambda (out)
(write-line (apply 'strcat (mapcar '(lambda (a) (strcat a ",")) out)) fh)
(reverse outlist)
(close fh)


;;; This routine can read a comma seperated variable (CSV) file and import
;;; them to AutoCAD drafting screen as a table.
;;; Copyright 1997 Yuqun Lian, SimpleCAD http://www.simplecad.com

; Set the global variables, you can freely edit them
(setq ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
x_distance 10
y_distance 2
text_height 1
text_style "arial.ttf"
text_align "ml"
) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun c:csvtable(/ txt txt_lst x y)
(setvar "cmdecho" 0)
(setvar "blipmode" 0)
(command "style" "txt_style" text_style text_height "1.0" "" "" "" "")
(setq data_file (getfiled "Select created Area data file" "" "CSV" 0))
(setq start_pt (getpoint "\nPick the left-upper corner for the table: "))
(setq y (cadr start_pt))
(setq fp (open data_file "r"))
;; read data
(while (setq txt (read-line fp))
(setq txt_lst (parse txt))
(setq x (car start_pt))
(print_lst txt_lst)
(setq y (- y y_distance))
(close fp)

(defun parse(txt / n count word lst in_quart)
(setq n (strlen txt) count 1 word "" in_quart nil lst nil)
(while (<= count n)
(setq char (substr txt count 1))

(if (= char "\"")
(if in_quart
(setq in_quart nil)
(setq in_quart T)

(if (and (= char ",")(= in_quart nil))
(setq lst (append lst (list word)))
(setq word "")
(if (/= char "\"")
(setq word (strcat word char))
(if (= count n)
(setq lst (append lst (list word)))
(setq count (1+ count))
(setq lst lst)

(defun print_lst (lst / txt txt_pt)
(foreach txt lst
(setq txt_pt (list x y))
(command ".text" "s" "txt_style" "j" text_align txt_pt "0" txt )
(setq x (+ x x_distance))


(defun c:car() ;Main function