MajorTom Posted October 3, 2019 Posted October 3, 2019 Hi, I need to edit "test.csv" as a "test MB.csv". 4th line and multiples give scale information. I removed it from "Test MB.csv" because I didn't need it. Thanks for any help, Truly Regards, testt.lsp testt.csv testt MB.csv Quote
rlx Posted October 3, 2019 Posted October 3, 2019 best would be to save the right format to begin with but assuming you're already stuck with one : ; (chop '(1 2 3 4 5 6 7 8 9) 4) -> '((1 2 3 4) (5 6 7 8) (9)) (defun chop (l n / tl fl) (cond ((or (null l) (null n) (not (vl-consp l)) (not (numberp n)))) ((< (length l) n) l) (t (while (vl-consp l) (setq tl (cons (car l) tl))(if (= n (length tl))(setq fl (cons tl fl) tl nil)) (if (and (not (vl-consp (setq l (cdr l))))(vl-consp tl))(setq fl (cons tl fl)))))) (reverse (mapcar 'reverse fl))) ; convert (defun convert_csv_file (fn-in / fp-in fnl fn-out fp-out dat item lst) (if (and fn-in (findfile fn-in) (setq fp-in (open fn-in "r")) (setq fnl (fnsplitl fn-in)) (setq fn-out (strcat (car fnl) (cadr fnl) " MB" (last fnl))) (setq fp-out (open fn-out "w"))) (progn ; first read entire file and put it in a list (while (setq dat (read-line fp-in))(setq lst (cons dat lst))) ; now chop-up list in pieces of 6 (based on posted example) (setq lst (chop (reverse lst) 6)) ; org format posted : ; MT-AR-FC-01-TW-GN-CD-0312 + 30.09.2019 + 1/2 + 0 + FT01 TYPICAL DETAIL 311 + "" ; desired format : ; Drawing Number;Drawing Name;Date;Revision Number ; MT-AR-FC-01-TW-GN-CD-0312;FT01 TYPICAL DETAIL 311;30.09.2019;0 ; first save header (write-line "Drawing Number;Drawing Name;Date;Revision Number" fp-out) ; now write chopped up list ; we need item_0 + ";" + item_4 + ";" + item_1 + ";" + item_3 (foreach item lst (write-line (strcat (nth 0 item) ";" (nth 4 item) ";" (nth 1 item) ";" (nth 3 item)) fp-out) ) (close fp-in)(close fp-out)(gc) (startapp "notepad" fn-out) ) ) (princ) ) (defun c:t1 ( / fn) (if (setq fn (getfiled "select source csv file" "" "csv" 0)) (convert_csv_file fn)) (princ) ) Quote
rlx Posted October 3, 2019 Posted October 3, 2019 haven't tested this one (since I have no drawing to test it on) (defun c:t2 (/ *error* f o e ss i sn atl) (defun *error* (msg) (if o (close o)) (if (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*") (princ msg) (princ (strcat "\n ** Error : " msg " **")))) (if (and (setq ss (ssget "X" '((0 . "INSERT") (66 . 1) (2 . "A1 ANTET")))) (setq f (getfiled "Specify Excel file name" (getvar 'dwgprefix) "csv" 1)) (setq o (open f "w"))) (progn ; write header "Drawing Number;Drawing Name;Date;Revision Number" (write-line "Drawing Number;Drawing Name;Date;Revision Number" o) (repeat (setq i (sslength ss)) (setq atl '() sn (ssname ss (setq i (1- i)))) (while (and (setq sn (entnext sn)) (/= (cdr (assoc 0 (setq e (entget sn)))) "SEQEND")) (if (eq (cdr (assoc 0 e)) "ATTRIB")(setq atl (cons (cdr (assoc 1 e)) atl)))) ; format posted : (0) MT-AR-FC-01-TW-GN-CD-0312, (1) 30.09.2019, (2) 1/2, (3) 0, (4) FT01 TYPICAL DETAIL 311, (5)"" ; format desired : MT-AR-FC-01-TW-GN-CD-0312;FT01 TYPICAL DETAIL 311;30.09.2019;0 (if (vl-consp atl) (write-line (strcat (nth 0 atl) ";" (nth 4 atl) ";" (nth 1 atl) ";" (nth 3 atl)) o)) ) ) ) (if o (close o)) (gc) (princ) ) Quote
MajorTom Posted October 4, 2019 Author Posted October 4, 2019 (edited) 12 hours ago, rlx said: best would be to save the right format to begin with but assuming you're already stuck with one : This isn't worked Quote ; (chop '(1 2 3 4 5 6 7 8 9) 4) -> '((1 2 3 4) (5 6 7 8) (9)) (defun chop (l n / tl fl) (cond ((or (null l) (null n) (not (vl-consp l)) (not (numberp n)))) ((< (length l) n) l) (t (while (vl-consp l) (setq tl (cons (car l) tl))(if (= n (length tl))(setq fl (cons tl fl) tl nil)) (if (and (not (vl-consp (setq l (cdr l))))(vl-consp tl))(setq fl (cons tl fl)))))) (reverse (mapcar 'reverse fl))) ; convert (defun convert_csv_file (fn-in / fp-in fnl fn-out fp-out dat item lst) (if (and fn-in (findfile fn-in) (setq fp-in (open fn-in "r")) (setq fnl (fnsplitl fn-in)) (setq fn-out (strcat (car fnl) (cadr fnl) " MB" (last fnl))) (setq fp-out (open fn-out "w"))) (progn ; first read entire file and put it in a list (while (setq dat (read-line fp-in))(setq lst (cons dat lst))) ; now chop-up list in pieces of 6 (based on posted example) (setq lst (chop (reverse lst) 6)) ; org format posted : ; MT-AR-FC-01-TW-GN-CD-0312 + 30.09.2019 + 1/2 + 0 + FT01 TYPICAL DETAIL 311 + "" ; desired format : ; Drawing Number;Drawing Name;Date;Revision Number ; MT-AR-FC-01-TW-GN-CD-0312;FT01 TYPICAL DETAIL 311;30.09.2019;0 ; first save header (write-line "Drawing Number;Drawing Name;Date;Revision Number" fp-out) ; now write chopped up list ; we need item_0 + ";" + item_4 + ";" + item_1 + ";" + item_3 (foreach item lst (write-line (strcat (nth 0 item) ";" (nth 4 item) ";" (nth 1 item) ";" (nth 3 item)) fp-out) ) (close fp-in)(close fp-out)(gc) (startapp "notepad" fn-out) ) ) (princ) ) (defun c:t1 ( / fn) (if (setq fn (getfiled "select source csv file" "" "csv" 0)) (convert_csv_file fn)) (princ) ) On 10/1/2019 at 6:56 PM, Lee Mac said: Have you tried using the standard AutoCAD DATAEXTRACTION command? On 10/2/2019 at 5:01 AM, BIGAL said: Another simple is a lisp extract attributes to csv file. Just search. 12 hours ago, rlx said: best would be to save the right format to begin with but assuming you're already stuck with one : ; (chop '(1 2 3 4 5 6 7 8 9) 4) -> '((1 2 3 4) (5 6 7 8) (9)) (defun chop (l n / tl fl) (cond ((or (null l) (null n) (not (vl-consp l)) (not (numberp n)))) ((< (length l) n) l) (t (while (vl-consp l) (setq tl (cons (car l) tl))(if (= n (length tl))(setq fl (cons tl fl) tl nil)) (if (and (not (vl-consp (setq l (cdr l))))(vl-consp tl))(setq fl (cons tl fl)))))) (reverse (mapcar 'reverse fl))) ; convert (defun convert_csv_file (fn-in / fp-in fnl fn-out fp-out dat item lst) (if (and fn-in (findfile fn-in) (setq fp-in (open fn-in "r")) (setq fnl (fnsplitl fn-in)) (setq fn-out (strcat (car fnl) (cadr fnl) " MB" (last fnl))) (setq fp-out (open fn-out "w"))) (progn ; first read entire file and put it in a list (while (setq dat (read-line fp-in))(setq lst (cons dat lst))) ; now chop-up list in pieces of 6 (based on posted example) (setq lst (chop (reverse lst) 6)) ; org format posted : ; MT-AR-FC-01-TW-GN-CD-0312 + 30.09.2019 + 1/2 + 0 + FT01 TYPICAL DETAIL 311 + "" ; desired format : ; Drawing Number;Drawing Name;Date;Revision Number ; MT-AR-FC-01-TW-GN-CD-0312;FT01 TYPICAL DETAIL 311;30.09.2019;0 ; first save header (write-line "Drawing Number;Drawing Name;Date;Revision Number" fp-out) ; now write chopped up list ; we need item_0 + ";" + item_4 + ";" + item_1 + ";" + item_3 (foreach item lst (write-line (strcat (nth 0 item) ";" (nth 4 item) ";" (nth 1 item) ";" (nth 3 item)) fp-out) ) (close fp-in)(close fp-out)(gc) (startapp "notepad" fn-out) ) ) (princ) ) (defun c:t1 ( / fn) (if (setq fn (getfiled "select source csv file" "" "csv" 0)) (convert_csv_file fn)) (princ) ) 11 hours ago, rlx said: haven't tested this one (since I have no drawing to test it on) This can be helpful but onlly running in current layout I wanna same thng to all layouts (defun c:t2 (/ *error* f o e ss i sn atl) (defun *error* (msg) (if o (close o)) (if (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*") (princ msg) (princ (strcat "\n ** Error : " msg " **")))) (if (and (setq ss (ssget "X" '((0 . "INSERT") (66 . 1) (2 . "A1 ANTET")))) (setq f (getfiled "Specify Excel file name" (getvar 'dwgprefix) "csv" 1)) (setq o (open f "w"))) (progn ; write header "Drawing Number;Drawing Name;Date;Revision Number" (write-line "Drawing Number;Drawing Name;Date;Revision Number" o) (repeat (setq i (sslength ss)) (setq atl '() sn (ssname ss (setq i (1- i)))) (while (and (setq sn (entnext sn)) (/= (cdr (assoc 0 (setq e (entget sn)))) "SEQEND")) (if (eq (cdr (assoc 0 e)) "ATTRIB")(setq atl (cons (cdr (assoc 1 e)) atl)))) ; format posted : (0) MT-AR-FC-01-TW-GN-CD-0312, (1) 30.09.2019, (2) 1/2, (3) 0, (4) FT01 TYPICAL DETAIL 311, (5)"" ; format desired : MT-AR-FC-01-TW-GN-CD-0312;FT01 TYPICAL DETAIL 311;30.09.2019;0 (if (vl-consp atl) (write-line (strcat (nth 0 atl) ";" (nth 4 atl) ";" (nth 1 atl) ";" (nth 3 atl)) o)) ) ) ) (if o (close o)) (gc) (princ) ) Edited October 4, 2019 by MajorTom Quote
BIGAL Posted October 4, 2019 Posted October 4, 2019 rlx said haven't tested this one (since I have no drawing to test it on) Quote
rlx Posted October 4, 2019 Posted October 4, 2019 (edited) couldn't have said it better Bigal hahaha so SSDD (same s..t , different day) : untested (and also have other work to do) (defun c:t3 (/ *error* f o e ss i sn atl) (defun *error* (msg) (if o (close o)) (if (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*") (princ msg) (princ (strcat "\n ** Error : " msg " **")))) (cond ((not (setq f (getfiled "Specify Excel file name" (getvar 'dwgprefix) "csv" 1))) (alert "No csv file was specified")) ((not (setq o (open f "w"))) (alert "Unable to write to csv file")) (t (write-line "Drawing Number;Drawing Name;Date;Revision Number" o) (foreach lay (cons "Model" (layoutlist)) (setvar "CTAB" lay) (if (setq ss (ssget "X" (list '(0 . "INSERT") '(66 . 1) '(2 . "A1 ANTET") (cons 410 (getvar "CTAB"))))) (repeat (setq i (sslength ss)) (setq atl '() sn (ssname ss (setq i (1- i)))) (while (and (setq sn (entnext sn)) (/= (cdr (assoc 0 (setq e (entget sn)))) "SEQEND")) (if (eq (cdr (assoc 0 e)) "ATTRIB")(setq atl (cons (cdr (assoc 1 e)) atl)))) (if (vl-consp atl) (write-line (strcat (nth 0 atl) ";" (nth 4 atl) ";" (nth 1 atl) ";" (nth 3 atl)) o)) ) ) (setq ss nil) ) ) ) (if o (close o))(gc)(startapp "notepad" f) (princ) ) Edited October 4, 2019 by rlx Updated code after testing on test block from me , myself and I 1 Quote
rlx Posted October 4, 2019 Posted October 4, 2019 (edited) tested code during lunch and did a little update. Code in previous post modified. Edited October 4, 2019 by rlx 1 Quote
MajorTom Posted October 8, 2019 Author Posted October 8, 2019 Thank you rlx appreciate what you have done Quote
Recommended Posts
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.