bills Posted February 4, 2019 Posted February 4, 2019 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. Quote
Emmanuel Delay Posted February 4, 2019 Posted February 4, 2019 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 Quote
bills Posted February 8, 2019 Author Posted February 8, 2019 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. Quote
bills Posted February 11, 2019 Author Posted February 11, 2019 Thanks Emmanuel Delay Its work fine. Actually initially i missed to change the path. Thanks a lot once again. 1 Quote
bills Posted May 5, 2019 Author Posted May 5, 2019 Hi Emmanuel Delay Based on your this routine i tried to develop a similar one to export text with its nearest line to csv file. But i didn't succeed since I don't have much knowledge on this. Can you help me out on that as well. Thanks Quote
BIGAL Posted May 5, 2019 Posted May 5, 2019 (edited) Are you talking about say text near the midpoint of a line and wanting the co-ords of the end points ? This has been posted before maybe not here I know I asked for some details like a dwg so could match the text when looking for a line. Was it you ? Easiest to do as seperate routine as leader code is very different approach. Will see if I can find or if have time will do. Found your other post on April 27 here. Will just do for lines and plines. Edited May 6, 2019 by BIGAL Quote
BIGAL Posted May 6, 2019 Posted May 6, 2019 Try this, note change name of output file and path. text-line-co-ords.lsp Quote
bills Posted May 7, 2019 Author Posted May 7, 2019 Thanks Bigal I will use this routine and let you know. 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.