Jump to content
bills

Leader export of excel along with text and pline length

Recommended Posts

bills

Hi All

 

I am looking for a lisp routine using which i can export the following data to excel.

1. selected leader handle & coordinates

2. nearest text to leader tail and

3. nearest pline/line length to leaders head 

 

Can any one help me in this.

Share this post


Link to post
Share on other sites
Emmanuel Delay

It gets saved as a CVS.  I save it to "C:\usertemp\leaders.csv".  You should change this to fit your needs, see the top of the code.

 

Command ELC (for Export Leaders to Cvs)

 

All leaders are selected.

 



(vl-load-com)
(setq very_big_number 1000000000)
(setq csvpath "C:\\UserTemp\\leader.csv")  ;; Adapt this to your needs

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Write CSV  -  Lee Mac
;; Writes a matrix list of cell values to a CSV file.
;; lst - [lst] list of lists, sublist is row of cell values
;; csv - [str] filename of CSV file to write
;; Returns T if successful, else nil

(defun LM:writecsv ( lst csv / des sep )
    (if (setq des (open csv "w"))
        (progn
            (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
            (foreach row lst (write-line (LM:lst->csv row sep) des))
            (close des)
            t
        )
    )
)

;; List -> CSV  -  Lee Mac
;; Concatenates a row of cell values to be written to a CSV file.
;; lst - [lst] list containing row of CSV cell values
;; sep - [str] CSV separator token

(defun LM:lst->csv ( lst sep )
    (if (cdr lst)
        (strcat (LM:csv-addquotes (car lst) sep) sep (LM:lst->csv (cdr lst) sep))
        (LM:csv-addquotes (car lst) sep)
    )
)

(defun LM:csv-addquotes ( str sep / pos )
    (cond
        (   (wcmatch str (strcat "*[`" sep "\"]*"))
            (setq pos 0)    
            (while (setq pos (vl-string-position 34 str pos))
                (setq str (vl-string-subst "\"\"" "\"" str pos)
                      pos (+ pos 2)
                )
            )
            (strcat "\"" str "\"")
        )
        (   str   )
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Converts a 3d coordinates flat list into a 3d point list
;; recursive function
(defun 3d-coord->pt-lst (lst)
  (if lst
    (cons (list (car lst) (cadr lst) (caddr lst))
      (3d-coord->pt-lst (cdddr lst))
    )
  )
)

(defun pline_length ( ent / a obj sum)
  (vla-get-length (vlax-ename->vla-object ent))
)

(defun c:elc ( / leader leaders mtexts plines mtext ip i j ind ind2 pts eol pt2 data)
  ;; get MTexts
  (setq mtexts  (ssget "_x" (list '(0 . "MTEXT"))))
  ;; get leaders
  (setq leaders (ssget "_x" (list '(0 . "LEADER"))))
  ;; Polylines
  (setq plines  (ssget "_x" (list '(0 . "LWPOLYLINE,POLYLINE,LINE"))))
 
  (setq i 0)
  (setq data (list (list "text" "length" "x head" "y head" "x point 2" "y point 2" "x tail" "y tail")))
 
  (repeat (sslength leaders)
    (setq leader (vlax-ename->vla-object (ssname leaders i)))
    (setq pts (3d-coord->pt-lst (vlax-get leader 'coordinates)))
    (setq eol (last pts))  ;; end of the leader
    
    ;; now search for the nearest Mtext
    (setq
      j 0
      ind -1  ;; index of the matching Mtext
      inf very_big_number  ;; a big number, we search for a better match
    )
    (repeat (sslength mtexts)
      (setq mtext (ssname mtexts j))
      (setq ip (cdr (assoc 10 (entget mtext))))
      (princ "\n")
      (if (< (distance eol ip) inf)
        (progn
          (setq inf (distance eol ip))
          (setq ind j)
        )
      )
      (setq j (+ j 1))
    )
    
    ;; now we do the same to find the nearest polyline
    (setq
      j 0
      ind2 -1  ;; index of the matching polyline
      inf very_big_number  ;; a big number, we search for a better match
    )
    (repeat (sslength plines)
      ;; pt2 is the closest point from the hed to the polyline
      (setq pt2 (vlax-curve-getClosestPointTo (ssname plines j) (nth 0 pts) ))  
      (if (< (distance (nth 0 pts) pt2) inf)
        (progn
          (setq inf (distance (nth 0 pts) pt2))
          (setq ind2 j)
        )
      )       
      (setq j (+ j 1))
    )
    ;; we have all the data for this row (for 1 leader)
    ;; the (rtos) is to convert numbers to string.  The csv expects string values
    (setq data (append data (list (list
      (cdr (assoc 1 (entget (ssname mtexts ind))))  ;; text
      
      (rtos (pline_length (ssname plines ind2)) 2 4)           ;; polyline length
      
      (rtos (nth 0 (nth 0 pts)) 2 4)                           ;; x coordinate head
      (rtos (nth 1 (nth 0 pts)) 2 4)                           ;; y coordinate head
       
      (rtos (nth 0 (nth 1 pts)) 2 4)                           ;; x coordinate 2nd point
      (rtos (nth 1 (nth 1 pts)) 2 4)                          ;; y coordinate 2nd point
       
      (rtos (nth 0 (last pts)) 2 4)                            ;; x coordinate tail
      (rtos (nth 1 (last pts)) 2 4)                            ;; y coordinate tail
    ))))
    
    (setq i (+ i 1))
  )

  (if
    (LM:writecsv data csvpath)
    (princ "\nSuccessful")
    (princ "\nFailed")
  )
  (princ)
)

leaders.dwg

Share this post


Link to post
Share on other sites
bills

Thanks Emmanuel Delay

 

Looking at your dwg file, it seems you had understood my requirement perfectly.

 

But I am unable to use this lsp. It gets loaded but on execution it gives error "Failed" or "Error:Incorrect type - nil"

 

Please help.

Share this post


Link to post
Share on other sites
bills

Thanks Emmanuel Delay

 

Its work fine. Actually initially i missed to change the path.

 

Thanks a lot once again.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×