Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lisp to draw polyline from xls, csv or txt file?

9 REPLIES 9
Reply
Message 1 of 10
papabradley
17297 Views, 9 Replies

Lisp to draw polyline from xls, csv or txt file?

Greetings!
I did a search for this, but didn't find anything that resembled what I needed.

I am looking for a lisp routine that would draw a polyline using the data from a xls, csv or txt file. Here is a sample clip from the csv or txt file:

1955373.01,113249.903
1955397.07,112794.949
1955349.88,112381.173
1955164.02,112070.843
1954880.94,111755.906
1954556.00,111473.025
1954197.84,111229.825
1953858.95,111054.971

where the first line is the starting point - the string of numbers before the comma represents "x" and the string after the comma represents "y", then the second line is the next point on the polyline, etc.

I know this is possible and should be a simple request. Problem is I am not a lsp writer.

Any of you GURU's what to give it a try?

Thanks,

Bradley Edited by: papabradley on Oct 15, 2008 8:05 AM
9 REPLIES 9
Message 2 of 10
EC-CAD
in reply to: papabradley

papabradley,
Here's a simple lisp to import your x,y values from a .csv file format....

;; poly_from_csv.lsp

(defun C:GO ( / CSV_name ifil Field x i c px py px_lst py_lst p_lst p1 p2 )
(command "_limits" "off")
;; Browse box to pick the .csv file
(setq CSV_name "")
(setq CSV_name (getfiled "Select Point List .csv file" "" "csv" 2))

;; Read-in the values to X & Y lists
(setq ifil (open CSV_name "r")); open the file
(while (setq rd-line (read-line ifil))
(setq x (strlen rd-line))
(setq i 1 Field 1 Px "" Py "")
(repeat x
(setq c (substr rd-line i 1)); read a character
(if (or (= c ",")(= c " "))
(progn
(setq Field (+ Field 1))
(setq c "")
); end progn
); end if
(if (and (/= c "")(= Field 1))
(setq Px (strcat Px c)); stack first point
); end if
(if (and (/= c "")(= Field 2))
(setq Py (strcat Py c)); stack second point
); end if
(setq i (+ i 1))
); end repeat
(if Px
(progn
(setq Px_lst (cons Px Px_lst))
(setq Py_lst (cons Py Py_lst))
); progn
); if
); while
(close ifil)

;; Draw the Polyline
(if (and (> (length Px_lst) 1)(> (length Py_lst) 1))
(progn
(setq Px_lst (reverse Px_lst))
(setq Py_lst (reverse Py_lst))
(setq C 0)
(repeat (length Px_lst)
(setq Px (atof (nth C Px_lst)))
(setq Py (atof (nth C Py_lst)))
(setq P_lst (cons (list Px Py 0.0) P_lst))
(setq C (+ C 1))
); repeat
(setq P_lst (reverse P_lst))
(setq P1 (nth 0 P_lst)); start point
(setvar "lastpoint" P1)
(setq P_lst (cdr P_lst)); strip 1st point
(setq C 1)
(repeat (length P_lst)
(setq P1 (getvar "lastpoint"))
(setq P2 (nth C P_lst))
(if (and (/= P1 nil)(/= P2 nil))
(command "_pline" P1 "_W" "0.50" "0.50" P2 ""); width as desired
); if
(setq C (+ C 1))
); repeat
); end progn
); end if
;; Exit
(princ)
); function GO
(princ); silent load

Bob

Message 3 of 10
Anonymous
in reply to: papabradley

papabradley wrote:

> I am looking for a lisp routine ...

Your TXT is a few characters from being ready. Simply put the word
PLINE on top and rename it with a SCR extension. Then use the SCRIPT
command to bring it in.

PLINE
1955373.01,113249.903
1955397.07,112794.949

> Problem is I am not a lsp writer.

But you can become a SCRipt writer!

Terry
--
Never start any job without the right tools!
AutoCAD Add-on Tools at http://www.dotsoft.com
Message 4 of 10
papabradley
in reply to: papabradley

SWEET! IT WORKS!
Could it have been written to have the polyline joined?
Thanks!
Bradley
Message 5 of 10
papabradley
in reply to: papabradley

OMG!
Duh! How simple!!!!
Thanks so much! You're a lifesaver!
Bradley
Message 6 of 10
cortbanderson
in reply to: papabradley

THAT WAS FREAKIN AMAZING!!!!!



It now takes me about 45 seconds to process and input the data what used to take me 45 minutes to input it.

I need to investigate more into this...
Message 7 of 10
lpseifert
in reply to: papabradley

another way...
Highlight the coordinates in the text file and copy to the clipboard (Ctrl+C)
Start the pline command, at the Specify start point: prompt, rt-click the command line and select Paste
~~~Civil 3D 2008~~~
Message 8 of 10
stevor
in reply to: papabradley

That is what the 'Auto' in Autocad means, or meant.
S
Message 9 of 10
kc27315
in reply to: lpseifert

GENIUS! BRILLIANT!!!!!  This is going to save a TON of time, as there are many times I've wanted to join points with consecutive point numbers. I found that this method works equally well when you add elevation to the mix. I was able to use the line command through hundreds of points, then join them to a 3-D polyline, then convert to a featureline to use in generating surfaces. I realize this was years ago, but Thank you for taking the time to share this!

Message 10 of 10
garou01
in reply to: papabradley

Hey, you can use too an excel with this format acad.png and use CONCATENATE function to assign x and y coordinates, you copy the column lines with "pl" in the first, and copy directly in command promt in autocad.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost