Jump to content

Polyline Area Total


cpw006

Recommended Posts

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)

)
 

Link to comment
Share on other sites

Try this

 

 (Strcat (Car itm)

 (Strcat  (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) (Car itm)

 

To get just a layer,tot area as totals you need to change the method of writing the answer out you need to do the vl-sort alldata which is a list of all the plines if I read the code right, you need to take this list and make a new list looking at each line adding the areas of the same layer name and only saving the total to the new list so the output is layer based not item based. Its not a hard thing to do I will try to find code for you. You just look at current item in a list and compare to the next one if same layer add area, once different write answer to file, reset total to 0.0

 

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...