Export Closed Polyline Area total to Excel by Layer

Export Closed Polyline Area total to Excel by Layer

Anonymous
Not applicable
2,705 Views
10 Replies
Message 1 of 11

Export Closed Polyline Area total to Excel by Layer

Anonymous
Not applicable

I am new to the forum and have only basic knowledge of lisp routines and have been trying to search for and or create a lisp routine to export the area of multiple closed polylines by layer.  I would like the lisp routine to export a .csv file with the first column having the name of the file, without .DWG, the second column would contain the area of each individual closed polyline, the third column would contain the layer name.  Ideally the file would be sorted/grouped by layer type and then have a total area for each  layer type.  I am not even sure if this last request is possible.  

Below is my attempt by using a routine I found and trying to edit it.  I am unsure how to create the routine to group by layer and get the sum of each layer to display correctly.  Thanks a ton for any help or insight you can give.
 

 

(defun c:EPD2 (/ ss i area layer all_data pts csv_file openfile) ; Export Polyline Data
;;            pBe Sep 2018            ;;
  (if (and
        (setq all_data nil
              ss       (ssget '((0 . "LWPOLYLINE")))
        )
            (repeat (setq i (sslength ss))
              (setq e     (ssname ss (setq i (1- i)))
                     ent   (entget e)
                    area  ( / (vlax-curve-getarea e) (* 12 12))
                     data  (mapcar '(lambda (d)(cdr (assoc d ent))) '( 8 70 5))
                    pts   (mapcar 'cdr
                                  (vl-remove-if-not
                                    '(lambda (d)
                                       (= 10 (car d))
                                     )
                                    ent
                                  )
                          )
              )
        (setq all_data
                       (cons
                         (list 
                (cond
                                      ((null (setq ssText (ssget "_CP" pts '((0 . "TEXT")))))    (getvar 'dwgprefix)
                                        (vl-filename-base (getvar 'dwgname))
                                                                )
                                       ((= (sslength ssText) 1)
                                            (cdr (assoc 1 (entget (ssname ssText 0))))
                                                                  )
                                       ((substr 
                       (apply 'strcat
                          (mapcar '(lambda (st)
                                     (strcat " | " st))
                            (vl-sort
                              (mapcar '(lambda (s)
                                         (cdr (assoc 1 (Entget s)))
                                       )
                                      (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssText)))
                              )
                              (function (lambda (a b)
                                          (< a b)
                                        )
                                  )
                                )
                                      )
                            )
                           4
                        )
                    )
                                    )                                
                               area
                               (car data)
                               (if (zerop ( logand 1 (cadr data))) "No" "Yes")
                                  (caddr data)
                         )
                         all_data
                       )
                    )
  
               all_data
               )
            (setq csv_file (getfiled "Save CSV File"
                                     (strcat
                                       (getvar 'dwgprefix)
                                       (vl-filename-base (getvar 'dwgname))
                                       ".csv"
                                     )
                                     "csv"
                                     45
                           )
                )
            )

        (progn
          (setq openfile (open csv_file "w"))
          ;(write-line
            ;"Text inside polyline,Polyline Area (sf),Layer,Closed,Handle"
            ;openfile
          ;)
          (foreach itm (vl-sort all_data
                                '(lambda (a b) (< (Cadr a) (cadr b)))
                       )
            (write-line
              (Strcat (Car itm)
                      ","
                      (strcat (rtos (Cadr itm) 2 2))
                      ","
                      (caddr itm)
                      ","
                      (cadddr itm)
              ","
                      (last itm)              
              )
              openfile
            )
          )
          (close openfile)
          (startapp "notepad" csv_file)
        )
      )
  (princ)

)

0 Likes
2,706 Views
10 Replies
Replies (10)
Message 2 of 11

dlanorh
Advisor
Advisor

Try the attached.

I am not one of the robots you're looking for

Message 3 of 11

pbejse
Mentor
Mentor

@Anonymous wrote:

 ...first column having the name of the file, without .DWG,

...the second column would contain the area of each individual closed polyline,

...the third column would contain the layer name.  

 

 

I'm not sure but are you wanting to run a  lisp program on more that one file? hence first column as filename?

 

one file	499.888		ThatAreaLayer
		4789.074	ThisAreaLayer

another file	4498.996	ThisAreaLayer
		3101.664	OtherLayer

other file	6916.215	bananaLayer
		61722.25	kakarotLayer
		4498.996	ThatAreaLayer
		3101.664	ThisAreaLayer

If so

 

(defun c:ECPA ( / Layers&Area ss n e objprop csv_file openfileforwrite drawgname)
  (if    
      	(setq Layers&Area nil
	       ss (ssget '((0 . "LWPOLYLINE,POLYLINE") (-4 . "&") (70 . 1))))
		(progn
		  	(repeat (setq n (sslength ss))
			  	(setq e (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
			  	(setq objprop (mapcar '(lambda (pr)
					   		(vlax-get e pr)) '("Layer" "Area" )))
			  	(setq Layers&Area
			  			(if (not (setq update (assoc (car objprop) Layers&Area)))
				  			(cons objprop Layers&Area)
						  	(subst (list (car objprop) (+ (cadr objprop)(cadr update)))
							       update 	Layers&Area)
						  )
					)				      
			  )
		  (if (setq csv_file (getfiled "Save CSV File"
                                     (strcat
                                       (getvar 'dwgprefix)
                                       (setq drawgname (vl-filename-base (getvar 'dwgname)))
                                       ".csv" )  "csv"
                                     45
	                           )
		                )			  
		            
		  	(progn
			  (setq openfileforwrite (open csv_file "a"))
			  (foreach data (vl-sort Layers&Area '(lambda (a b)(< (Car a)(car b))))
			    (write-line
			    	 (strcat
				   drawgname  ","
				   		(rtos (cadr data) 2 3)  ","
				   			(car data)) openfileforwrite)
			    	(if (snvalid drawgname)
				  	(setq drawgname ""))
			    )
			  (write-line "" openfileforwrite)
			  (close openfileforwrite)
			  )
			  )
	    )
	)
  (princ)  
  )

HTH

 

EDIT: oops, i read thru the post again and looked at based on dlanorh post , i missed the "individual" areas.

I'll post another one...

 

Like this maybe?

 

one file	ThatAreaLayer	499.888
				5623.12
				7845.25

		Total		13968.3

		ThisAreaLayer	499.888
				123.45
				1290.00

another file	ThisAreaLayer	4498.996
		OtherLayer	3101.664

other file	bananaLayer	6916.215
				856.26

		Total		7772.48

		kakarotLayer	61722.25

		ThatAreaLayer	61722.25
				4583.2

		Total		66305.4	

		ThatAreaLayer	3101.664

 

Thoughts?

 

 

 

0 Likes
Message 4 of 11

pbejse
Mentor
Mentor

command: ECPA

 

one file	ThatAreaLayer	499.888
				5623.12
				7845.25

		ThisAreaLayer	499.888
				123.45
				1290.00

		Total Areas by Layer
		ThatAreaLayer	13968.3
		ThisAreaLayer	1913.34
other file bananaLayer 6916.215 856.26
kakarotLayer 61722.25 ThatAreaLayer 61722.25 4583.2 Total Areas by Layer bananaLayer 7772.48 kakarotLayer 61722.25 ThatAreaLayer 3101.664

HTH

Message 5 of 11

bit_Cad2018
Advocate
Advocate

this was very use full for me.

thanks 

can you please add code for inside text export with coordinates

0 Likes
Message 6 of 11

devitg
Advisor
Advisor

Did you try DATEXTRACTION  at the command line........

No lisp needed

0 Likes
Message 7 of 11

bit_Cad2018
Advocate
Advocate

Sir i have many closed polyline and inside polyline survey number. this lisp area export i want help for which survey number area is.

 

i attach sample file please see

0 Likes
Message 8 of 11

devitg
Advisor
Advisor

Some like it??

(DEFUN LIST#2CVS  (LST) ;_01
  (APPLY 'STRCAT
         (CONS (RTOS (CAR LST))
               (MAPCAR '(LAMBDA (STR) (STRCAT "," (RTOS STR 2 2)))
                       (CDR LST)
                       ) ;_mapcar
               ) ;_cons 
         ) ;_apply
  )




(defun c:EPD2 (/ ss i area layer all_data pts csv_file openfile) ; Export Polyline Data
;;            pBe Sep 2018            ;;


   (setq poly-sample (ssname  (ssget ":S" '((0 . "LWPOLYLINE,POLYLINE") (-4 . "&") (70 . 1)))0))
           (setq poly-layer (cdr (assoc 8 (entget poly-sample))))   
    
;;;	  (setq      ss (ssget "x"(list (cons 0  "LWPOLYLINE,POLYLINE") (cons 8 poly-layer) (cons -4  "&") (cons 70  1))))




  
  (if (and
        (setq all_data nil)
          (setq      ss (ssget "x"(list (cons 0  "LWPOLYLINE,POLYLINE") (cons 8 poly-layer) (cons -4  "&") (cons 70  1))))
               ;ss       (ssget '((0 . "LWPOLYLINE")))
        )
            (repeat (setq i (sslength ss))
              (setq e     (ssname ss (setq i (1- i)))
                     ent   (entget e)
                    area  ( / (vlax-curve-getarea e) (* 1 1))
                     data  (mapcar '(lambda (d)(cdr (assoc d ent))) '( 8 70 5))
                    pts   (mapcar 'cdr
                                  (vl-remove-if-not
                                    '(lambda (d)
                                       (= 10 (car d))
                                     )
                                    ent
                                  )
                          )
              )
        (SETQ ALL_DATA
               (CONS
                 (LIST
                   (COND
                     ((NULL (SETQ SSTEXT (SSGET "_CP" PTS '((0 . "TEXT")))))
                      (GETVAR 'DWGPREFIX)
                      (VL-FILENAME-BASE (GETVAR 'DWGNAME))
                      )
                     ((= (SSLENGTH SSTEXT) 1)
                      (CDR (ASSOC 1 (ENTGET (SSNAME SSTEXT 0))))
                      )
                     ((SUBSTR
                        (APPLY 'STRCAT
                               (MAPCAR '(LAMBDA (ST)
                                          (STRCAT " | " ST))
                                       (VL-SORT
                                         (MAPCAR '(LAMBDA (S)
                                                    (CDR (ASSOC 1 (ENTGET S)))
                                                    )
                                                 (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX SSTEXT)))
                                                 )
                                         (FUNCTION (LAMBDA (A B)
                                                     (< A B)
                                                     )
                                                   )
                                         )
                                       )
                               )
                        4
                        )
                      )
                     )
                   AREA
                   (CAR DATA)
                   (IF (ZEROP (LOGAND 1 (CADR DATA)))
                     "No"
                     "Yes")
                   ;(CADDR DATA)
                   (LIST#2CVS(cdr (assoc 10 (entget (ssname SSTEXT   0)))))
                   )
                 ALL_DATA
                 )
              )

  
               all_data
               )
            (setq csv_file (getfiled "Save CSV File"
                                     (strcat
                                       (getvar 'dwgprefix)
                                       (vl-filename-base (getvar 'dwgname))
                                       ".csv"
                                     )
                                     "csv"
                                     45
                           )
                )
            )

        (progn
          (setq openfile (open csv_file "w"))
          ;(write-line
            ;"Text inside polyline,Polyline Area (sf),Layer,Closed,Handle"
            ;openfile
          ;)
          (foreach itm (vl-sort all_data
                                '(lambda (a b) (< (Cadr a) (cadr b)))
                       )
            (write-line
              (Strcat (Car itm)
                      ","
                      (strcat (rtos (Cadr itm) 2 2))
                      ","
                      (caddr itm)
                      ","
                      (cadddr itm)
              ","
                      (last itm)              
              )
              openfile
            )
          )
          (close openfile)
          (startapp "notepad" csv_file)
        )
      )
  (princ)

)
;|«Visual LISP© Format Options»
(180 2 1 0 nil "end of " 100 20 2 2 nil nil T nil T)
;*** DO NOT add text below the comment! ***|;

 

Message 9 of 11

bit_Cad2018
Advocate
Advocate

as you attach csv result is awesome but i try black csv create

 

Please see attached Clip 

 

After lisp loaded      ; error: extra right paren on input          this error show in command line

 

 

0 Likes
Message 10 of 11

devitg
Advisor
Advisor

Solved , try it 

0 Likes
Message 11 of 11

bit_Cad2018
Advocate
Advocate

ERROR not show

 

only blank excel create

0 Likes