Juergen Posted May 7, 2021 Posted May 7, 2021 Hi, i have this file. How can i make a new line after each different "HANDLE"? (each line should be in a group) My secound question is how can i select a filename for the .csv export? (defun c:L2C ( / *error* ss f fName cnt e eg en typ han lyr len txt tmp pnt) ;Lines 2 Csv ;error handle (defun *error* ( msg ) (if f (close f)) (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)) );if (princ) );defun error ;ensure items selected (if (not (setq ss (ssget '((0 . "*LINE"))))) (progn (prompt "\n...invalid.") (exit)) );if ;be sure file does not exist (setq fName (getvar 'DWGNAME)) (setq fName (strcat (getvar 'DWGPREFIX) (substr fName 1 (- (strlen fName) 4)) ".csv")) (if (findfile fName) (progn (prompt "...file already exists.") (exit)) );if ;prep file/vars (vl-load-com) (setq f (open fName "w")) (write-line "HANDLE,LENGTH,LAYER,X,Y,Z" F) ;loop through ss (repeat (setq cnt (sslength ss)) ;gather basic info (setq e (ssname ss (setq cnt (1- cnt)))) (setq eg (entget e) typ (cdr (assoc 0 eg))) (setq han (cdr (assoc 5 eg)) lyr (cdr (assoc 8 eg))) (setq len (rtos (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))) (setq txt (strcat han "," len "," lyr ",")) ;get type-specific info (vertices) (cond ((eq "LWPOLYLINE" typ) (while (setq eg (member (assoc 10 eg) eg)) (setq pnt (cdr (car eg)) eg (cdr eg)) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) ",0.0")) (write-line tmp f) );while );cond LWPOLYLINE ((eq "POLYLINE" typ) (setq en (entnext e)) (while (/= "SEQEND" (cdr (assoc 0 (setq eg (entget en))))) (while (setq eg (member (assoc 10 eg) eg)) (setq pnt (cdr (car eg)) eg (cdr eg)) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) (write-line tmp f) );while (setq en (entnext en)) );while );cond POLYLINE ((eq "LINE" typ) (setq pnt (cdr (assoc 10 eg))) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) (write-line tmp f) (setq pnt (cdr (assoc 11 eg))) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) (write-line tmp f) );cond LINE ((eq "SPLINE" typ) (while (setq eg (member (assoc 11 eg) eg)) (setq pnt (cdr (car eg)) eg (cdr eg)) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) (write-line tmp f) );while );cond SPLINE (t (prompt "\n...invalid item selected. Ignored.")) );cond );repeat ;finish up / inform user (close f) (prompt "\nL2C Complete...") (princ) );defun Quote
BIGAL Posted May 7, 2021 Posted May 7, 2021 Item 1 explain more Item 2 its up to you, stuff like dwg name and location, one directory always. Enter a filename and location. Need more info. Quote
Juergen Posted May 7, 2021 Author Posted May 7, 2021 Hi Bigal, 1. 2. it should open a window where i can choose the folder and file name. thx Quote
Tharwat Posted May 7, 2021 Posted May 7, 2021 To let the program targets only the desire objects names, then just replace the filter of the ssget function as follows. Replace this: (ssget '((0 . "*LINE"))) with (ssget '((0 . "*POLYLINE,LINE,SPLINE"))) Then you to add a blank line, add the following code in the same position as follows: ) (t (prompt "\n...invalid item selected. Ignored.")) ;; so this line of codes not need anymore. ) (write-line "" f) ;; <- Add this lilne of codes to get a blank row. ) Quote
BIGAL Posted May 8, 2021 Posted May 8, 2021 If you want blank lines then you will need to write to a list, sort it, what you do then is look at next variable in the list is it 4FO 4FO 4FO when its not do a blank line. I will try to find some sample code. Its a nth x compare nth x+1, I have used this method a lot for counting common objects. Some one may have something already. Quote
hosneyalaa Posted May 8, 2021 Posted May 8, 2021 19 hours ago, Juergen said: Hi Bigal, 1. 2. it should open a window where i can choose the folder and file name. thx (defun c:TESTL2C ( / *error* A CNT E EG EN F FNAME HAN LEN LYR PNT PTLST SS TMP TXT TXTL TYP) ;;https://www.cadtutor.net/forum/topic/72935-make-new-line-when-write-a-csv-file/ ;Lines 2 Csv ;error handle (defun *error* ( msg ) (if f (close f)) (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)) );if (princ) );defun error ;ensure items selected (if (not (setq ss (ssget '((0 . "*POLYLINE,LINE,SPLINE"))))) (progn (prompt "\n...invalid.") (exit)) );if ;;; ;loop through ss (repeat (setq cnt (sslength ss)) ;gather basic info (setq e (ssname ss (setq cnt (1- cnt)))) (setq eg (entget e) typ (cdr (assoc 0 eg))) (setq han (cdr (assoc 5 eg)) lyr (cdr (assoc 8 eg))) (setq len (rtos (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))) (setq txt (strcat han "," len "," lyr ",")) ;get type-specific info (vertices) (cond ((eq "LWPOLYLINE" typ) (while (setq eg (member (assoc 10 eg) eg)) (setq pnt (cdr (car eg)) eg (cdr eg)) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) ",0.0")) ;;; (write-line tmp f) (setq txtL (CONS (LIST han tmp) txtL)) );while );cond LWPOLYLINE ((eq "POLYLINE" typ) (setq en (entnext e)) (while (/= "SEQEND" (cdr (assoc 0 (setq eg (entget en))))) (while (setq eg (member (assoc 10 eg) eg)) (setq pnt (cdr (car eg)) eg (cdr eg)) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) ;;; (write-line tmp f) (setq txtL (CONS (LIST han tmp) txtL)) );while (setq en (entnext en)) );while );cond POLYLINE ((eq "LINE" typ) (setq pnt (cdr (assoc 10 eg))) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) (write-line tmp f) (setq pnt (cdr (assoc 11 eg))) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) ;;; (write-line tmp f) (setq txtL (CONS (LIST han tmp) txtL)) );cond LINE ((eq "SPLINE" typ) (while (setq eg (member (assoc 11 eg) eg)) (setq pnt (cdr (car eg)) eg (cdr eg)) (setq tmp (strcat txt (rtos (car pnt) 2) "," (rtos (cadr pnt) 2) "," (rtos (caddr pnt) 2))) ;;; (write-line tmp f) (setq txtL (CONS (LIST han tmp) txtL)) );while );cond SPLINE (t (prompt "\n...invalid item selected. Ignored.")) );cond );repeat ;finish up / inform user (setq ptLst(vl-sort txtL '(lambda(a b)(<(car a)(car b))))) (setq fName (getfiled "Save CSV File" (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".csv") "csv" 45 ) ) ;prep file/vars (vl-load-com) (setq f (open fName "w")) (write-line "HANDLE,LENGTH,LAYER,X,Y,Z" F) (write-line (caDr (NTH cnt ptLst)) f) ;loop through ss (setq cnt 1) (repeat (- (length ptLst) 1) (setq a (car (NTH (- cnt 1) ptLst))) (IF (EQ (car (NTH cnt ptLst)) a) (PROGN (write-line (caDr (NTH cnt ptLst)) f) ) (PROGN (write-line "" f) (write-line (caDr (NTH cnt ptLst)) f) ) ) (setq cnt (+ cnt 1)) );;(repeat (close f) (prompt "\nL2C Complete...") (startapp "explorer" fName);opin excel (princ) );defun Quote
Tharwat Posted May 8, 2021 Posted May 8, 2021 @hosneyalaa Why did you gather the info into a list and sorted them by smallest X coordinates ? Is there any benefit from that ? Quote
hosneyalaa Posted May 8, 2021 Posted May 8, 2021 35 minutes ago, Tharwat said: @hosneyalaa Why did you gather the info into a list and sorted them by smallest X coordinates ? Is there any benefit from that ? @Tharwat A HE IS request is a space for each element with its coordinates Then I made an arrangement according to the hindel element Quote
Tharwat Posted May 8, 2021 Posted May 8, 2021 You are processing two times without a reasonable goal and one write-line is more than enough as I demonstrated earlier if you noticed that !. Quote
hosneyalaa Posted May 8, 2021 Posted May 8, 2021 @Tharwat Thank you I didn't know where the redundancy lay in the process A list was required to sort by item Then Put a space between each of the two items Is it possible to indicate a recurrence of operations? 9 minutes ago, Tharwat said: You are processing two times without a reasonable goal and one write-line is more than enough as I demonstrated earlier if you noticed that !. 52 minutes ago, Tharwat said: @hosneyalaa Why did you gather the info into a list and sorted them by smallest X coordinates ? Is there any benefit from that ? @Tharwat A HE IS request is a space for each element with its coordinates Then I made an arrangement according to the hindel element Quote
Tharwat Posted May 8, 2021 Posted May 8, 2021 You assumed is that the user wanted to sort the list but they did not mention that and the redundancy lays within the 1st repeat and 2nd repeat that iterates the list. Quote
hosneyalaa Posted May 8, 2021 Posted May 8, 2021 @Tharwat Yes, you are right in what you say I didn't notice that Thanks Quote
Juergen Posted May 12, 2021 Author Posted May 12, 2021 Hi Tharwat, hi hosneyalaa, thank you for your articles. Tharwat, I adapted the code as you wrote it and it work fine. hosneyalaa your suggestion with the sorted list is a good idea that I can use. I wouldn't have thought of that. Thanks a lot for help. 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.