Lisp for CSV to Polyline

Lisp for CSV to Polyline

kajanthangavel
Advocate Advocate
2,792 Views
11 Replies
Message 1 of 12

Lisp for CSV to Polyline

kajanthangavel
Advocate
Advocate

I modified  a lisp for CSV to Polyline from Original Lisp 

But it working for Points

If I change _point to _pline not make polyline.

(command "_point" pl)

screen.PNG

 

 

(defun c:CSV2POLY (/ _delimiter _coordinates pt pl theCSVSoure allTheDataNeeded  openedCSVFile a b)

(setq
    svnames '(osmode cmdecho blipmode plinewid)
    svvals (mapcar 'getvar svnames)
  ); setq

(mapcar 'setvar svnames '(0 0 0 0))


	
	(defun _delimiter (str md / d l str)
			    (if	(listp str)
					(apply 'strcat
						(cons (car str)
							(mapcar '(lambda (r) (strcat (chr md) r))
								(cdr str)
							)
						)
					)
					(progn
						(while (setq d (vl-string-position md str nil T))
							(setq	l   (cons (substr str (+ 2 d)) l)
									str (substr str 1 d)
							)
						)
						(cons str l)	
					)
			    )
	)

	(defun _coordinates  (lst indx)
		(mapcar '(lambda (l) (distof (nth l lst)))
			(list (1+ indx) indx)))
	(setq o 0)				  
	(if (setq allTheDataNeeded nil
			theCSVSoure (getfiled "Select CSV file" (getvar 'dwgprefix) "csv" 16))
	    	(progn
				(setq openedCSVFile (open theCSVSoure "r"))
	            (while 
					(setq a (read-line openedCSVFile))
	                (setq b (_delimiter a 44))
					(setq allTheDataNeeded (cons b allTheDataNeeded))
	            ); while
				(close openedCSVFile)
	            
				(foreach data allTheDataNeeded
					(foreach pt  (list (_coordinates data 1))
						(if (= (car pt) nil)
							(setq o (+ o 50)) ; if possible
							(setq pl (list (+ (car pt) o) (cadr pt))); if not possible
						);  if - make 50m offset for every sections 
						(command "_point" pl)
					)
				);foreach
	        )
	); if
	
(mapcar 'setvar svnames svvals)
(princ)
)
(vl-load-com)

here is that lisp

Please someone help me and edit this lisp.

I am a beginner for lisp

0 Likes
Accepted solutions (1)
2,793 Views
11 Replies
Replies (11)
Message 2 of 12

hak_vz
Advisor
Advisor

You should feed command PLINE with the point data.

 

(command "pline")
(foreach pt ptlist
     (command pt)
)
(command "")

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 12

kajanthangavel
Advocate
Advocate

I already try .

result show,

Unknown command "CSV2POLY".  Press F1 for help.

no draw any polyline..

0 Likes
Message 4 of 12

hak_vz
Advisor
Advisor

You can do also this way

 

(setq ptlist '((0 0)(10 5)(20 0)))
(command "pline")(mapcar 'command ptlist)(command "")

 

 

Rearrange your CSV file according to what it reads. Check delimiter on start and number of data in each row

 

This is not hard task to do . Try to write your own code.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 12

hosneyalaa
Advisor
Advisor
Accepted solution
(defun c:testCSV2POLY (/ _delimiter _coordinates pt pl theCSVSoure allTheDataNeeded  openedCSVFile a b)

(setq
    svnames '(osmode cmdecho blipmode plinewid)
    svvals (mapcar 'getvar svnames)
  ); setq

(mapcar 'setvar svnames '(0 0 0 0))


	
	(defun _delimiter (str md / d l str)
			    (if	(listp str)
					(apply 'strcat
						(cons (car str)
							(mapcar '(lambda (r) (strcat (chr md) r))
								(cdr str)
							)
						)
					)
					(progn
						(while (setq d (vl-string-position md str nil T))
							(setq	l   (cons (substr str (+ 2 d)) l)
									str (substr str 1 d)
							)
						)
						(cons str l)	
					)
			    )
	)

	(defun _coordinates  (lst indx)
		(mapcar '(lambda (l) (distof (nth l lst)))
			(list (1+ indx) indx)))
	(setq o 0)				  
	(if (setq allTheDataNeeded nil
			theCSVSoure (getfiled "Select CSV file" (getvar 'dwgprefix) "csv" 16))
	    	(progn
				(setq openedCSVFile (open theCSVSoure "r"))
	            (while 
					(setq a (read-line openedCSVFile))
	                (setq b (_delimiter a 44))
					(setq allTheDataNeeded (cons b allTheDataNeeded))
	            ); while
				(close openedCSVFile)
	            
				(foreach data allTheDataNeeded
					(foreach pt  (list (_coordinates data 1))
						(if (= (car pt) nil)
							(setq o (+ o 50)) ; if possible
							(setq pl (list (+ (car pt) o) (cadr pt))); if not possible
						);  if - make 50m offset for every sections 
						(command "_point" pl)
					  (setq allpoint (cons pl allpoint))
					)
				);foreach
(setq ptLst (reverse allpoint))
		  
		  (setq lst(vl-sort
	   ptLst
                '(lambda(a b)(<(car a)(car b)))))
		  
		 (entmakex
  (apply 'append
         (list
           (list
             '(0 . "LWPOLYLINE")
             '(100 . "AcDbEntity")
             '(62 . 1)
             '(100 . "AcDbPolyline")
             (cons 90 (length lst))
             (cons 38 (caddr (trans '(0 0 0) 1 (trans '(0. 0. 1.) 1 0))))
             '(70 . 0)
           ) ;_  list
           (mapcar '(lambda (x) (list 10 (car x) (cadr x)))
                   (mapcar '(lambda (x) (trans x 1 (trans '(0. 0. 1.) 1 0))) lst)
           ) ;_  mapcar
           (list (cons 210 (trans '(0. 0. 1.) 1 0)))
         ) ;_  list
  ) ;_  apply
) 
	        )
	); if
	
(mapcar 'setvar svnames svvals)
(princ)
)
(vl-load-com)

 

 

0 Likes
Message 6 of 12

kajanthangavel
Advocate
Advocate

Thank you @hosneyalaa 

it is work well.

Message 7 of 12

hak_vz
Advisor
Advisor

I see you are creating sections. Why bother with CSV files.

Here is my solution for sections that I use for my work. It presumes that you have a map with all elements raised in 3d. First run command TERRAIN. It creates a layer terrain_model with all elements broken to line segments.

In that layer you can add helper lines for part of a section that is not covered.

Command TERRAIN_SECTION creates sections along a line. When you create terrain model put them in locked layer.

 

 

(defun c:terrain (/ p1 p2 ss_terrain elast ent i)
    (princ "\nSelect all terrain elements without section lines or anything that dont defines terrain >")
    (setq p1 (getpoint "\nSelect lower left corner >") p2 (getcorner p1 "\nSelect upper right corner >")  ss_terrain (ssget "W"  p1 p2
            (list (cons -4 "<OR")
                  (cons 0 "LINE")
                  (cons 0 "POLYLINE")
                  (cons 0 "LWPOLYLINE")
                  (cons -4 "OR>")
            )
        )
    )
	(setvar 'cmdecho 0)
	(setq elast (entlast))
	(if (not (tblsearch "layer" "terrain_model"))  (command "_.layer" "_M" "terrain_model"  "_Lt" "Continuous" "terrain_model" ""))
	(command "_.copy" ss_terrain "" '(0 0 0) '(0 0 0))
	(while (setq elast (entnext elast))
		(setq ent (entget elast))
		(setq ent (subst (cons 8 "terrain_model") (assoc 8 ent) ent))
		(setq ent (entmod ent))
	)
	(setq ss_terrain (ssget "X" '((8 . "terrain_model"))))
	(setq i 0)
	
	(while (< i (sslength ss_terrain))
	(if (not (= (cdr (assoc 0 (entget (ssname ss_terrain i)))) "LINE"))(command "explode" (ssname ss_terrain i)))
	(setq i (+ i 1))
	)
	(setvar 'cmdecho 1)
	(princ "\nTerrain model created in layer terrain_model")
    (princ)     
)

(defun c:terrain_section ( / *error* lt point2d e section_obj start end zero_level start end zero_level i co pi spt section_points); 
	(defun *error* ()
		(setvar 'cmdecho 1)
		(command "_.ucs" w)
		(princ)
	)
	(defun lt (pt) (trans pt 1 0))
	(defun point2d (pt) (list (car pt) (cadr pt)))
	(command "_.ucs" "w")
    (setq e (car (entsel "\nSelect section line >"))  section_obj (vlax-ename->vla-object e))
    (if (= (vlax-get section_obj 'ObjectName) "AcDbLine")
        (setq
            start (vlax-get section_obj 'StartPoint)
            end (vlax-get section_obj 'EndPoint)
        )
    )
    (if (= (vlax-get section_obj 'ObjectName) "AcDbPolyline")
        (setq
            start (vlax-curve-getstartpoint section_obj)
            end (vlax-curve-getendpoint section_obj)
        )
    )     
    (setq
		ss_terrain (ssget "F" (list start end) '((-4 . "<AND")(8 . "terrain_model")(-4 . "AND>")))
		zero_level (getreal "\nHeight of base level >")
        i 0
	)  
	(if (ssmemb e ss_terrain) (setq ss_terrain (ssdel e ss_terrain)))
	(command "_.ucs" "_ob" (list e start)) 
	(cond 
		((and ss_terrain)
			(repeat (sslength ss_terrain)
				(setq
					co (vlax-ename->vla-object (ssname ss_terrain i))
					pi (inters (point2d (vlax-get co 'StartPoint)) (point2d (vlax-get co 'EndPoint)) (point2d start) (point2d end)  T)
					spt (inters (append pi (list 0)) (append pi (list 10)) (vlax-get co 'StartPoint) (vlax-get co 'EndPoint) nil)
					section_points (cons (list (distance (point2d start) pi) (-(last spt) zero_level)) section_points)
					i (+ i 1)
				)
			)
			(setq section_points (mapcar 'lt (vl-sort section_points (function (lambda (a b)(< (car a)(car b)))))))
			(cond 
				((and section_points)
					(entmakex
						(apply 'append
							(cons
							  (list
								'(0 . "LWPOLYLINE")
								'(100 . "AcDbEntity")
								'(100 . "AcDbPolyline")
								'(410 . "Model")
								'(8 . "0")
								'(38 . 0)
								'(62 . 3)
								'(67 . 0)
								'(70 . 0)
								(cons 90 (length section_points))
							  )
							  (mapcar 'list (mapcar '(lambda (a) (cons 10 a)) section_points))
							) 
						)
					)
				)
			) 
		)
	)
	(princ)
)

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 8 of 12

kajanthangavel
Advocate
Advocate

I have field note only. no any 3d levels. so i want to draw cross section of Road.

like this

Screenshot (25).png

thank you.

0 Likes
Message 9 of 12

hosneyalaa
Advisor
Advisor

Create a surface from points
And create a profile for the road and do what is necessary
Use a program AutoCAD_Civil3D

 

تنزيل.jpg

 

0 Likes
Message 10 of 12

john.uhden
Mentor
Mentor

OR...

(mapcar 'command ptlist)

John F. Uhden

Message 11 of 12

hak_vz
Advisor
Advisor

What he needs is easy to create in autolisp, and he doesn't need CIVIL 3d for that. This post is published in autolisp forum and it asks for a solution written in it. Some users don't have access to CIV 3d.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 12 of 12

hak_vz
Advisor
Advisor

Yes it works, and it's another way to create foreach loop

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes