Jump to content

The never ending story: Polyline export to file


elfert

Recommended Posts

Hello Lisp forum!

 

Can you help i found this thread:

 

http://www.cadtutor.net/forum/showthread.php?9628-Export-Points

 

I need a lisp routine thats could export polylines to a file called infile and it should look the same as the attached one. the file should be placed in the folder C:\user\ and if the routines can't find the folder it should create it.

 

Many many many thx in advance.

infile.txt

Link to comment
Share on other sites

  • Replies 48
  • Created
  • Last Reply

Top Posters In This Topic

  • pBe

    15

  • elfert

    11

  • Kapanther

    8

  • SolarPVDesigner

    4

Hello Lisp forum!

 

the file should be placed in the folder C:\user\ and if the routines can't find the folder it should create it.

 

Just a hint, sorry

I had similar problem earlier on my machine

Make sure you have user permissions to

this folder "C:\user\" before

Link to comment
Share on other sites

Just a hint, sorry

I had similar problem earlier on my machine

Make sure you have user permissions to

this folder "C:\user\" before

 

Okay but i am not sure where to start changing the lisp routine ? I found this made by mfuccaro, thx mfuccaro. But i need it to make the file in C:\user\ and it should make the file similar to the one attached with the same filename. This about logname, logfilename ect. i am not sure how to use that? The problem is that we today use a VBA to do it but we change to 64 bit OS and AC and in the future VBA is not supported by AC. So thats why i need a lisp routine to be in a language that ac whould understand in the future. thx in advance.

 

;export old style polyline vertex coords to a text file
;	mfuccaro@hotmail.com
(defun c:pl2txt ( / en ask i a file)
 (while (not ask)
   (setq en (car (entsel)))
   (if en (setq ask (= "LWPOLYLINE" (cdr (assoc 0 (entget en))))))
   )
 (setq file (open (getfiled "Output file"
		     (strcat (getvar "dwgprefix")
		     (substr (getvar "DWGNAME") 1 (- (strlen (getvar "dwgname")) 4)))
		     "txt"     ;file type
		     1) "w"))
 (setq i 0 sep ";")	;sep=separator
 (while (or (zerop i) a)
   (setq a (mapcar 'rtos (cdr (assoc 10 (entget (setq en (entnext en)))))))
   (if a (write-line (strcat (car a) sep (cadr a) sep (caddr a)) file))
   (setq i (1+ i))
   )
 (close file)
 (princ (strcat "\n" (itoa (1- i)) " points exported"))
 (princ)
 )

Link to comment
Share on other sites

This about logname, logfilename ect. i am not sure how to use that?

[/code]

 

Dont worry about it effert, i'm just showing you the lazy way of exporting data. ;) I'm in that mode now :lol:

 

In any case .. Oleg got you covered.

Link to comment
Share on other sites

I found the code at afralisp and i want to retrive the z-coordinates but i can't make it do it correct can some body help , Please? Is it possible to make the file go in to the C:\user\ folder as it is now i always end up in the 'My document'. I the folder dosn't exist the will be okay report back to the user.

 

http://www.afralisp.net/visual-lisp/tutorials/polylines-part-1.php

 

(prompt "\nType \"vlplexport\" to run........")
(defun c:vlplexport ( / theobj thelist n xval yval zval fname fn)
;load the visual lisp extensions
(vl-load-com)
;get the entity and entity name
(setq theobj (car (entsel "\nSelect a Polyline: ")))
;convert to vl object
(setq theobj (vlax-ename->vla-object theobj))
;check if it's a polyline
(if (= (vlax-get-property theobj 'ObjectName) "AcDbPolyline")
;if it is, do the following
(progn
;retrieve the coordinates
(setq thelist (vlax-get-property theobj 'coordinates))
;convert to a list
(setq thelist (vlax-safearray->list  (variant-value thelist)))
;zero the counter
(setq n 0)
;create a text file
(setq fname "infile")
;open it to write
(setq fn (open fname "w"))
;write the header
(write-line "PolyLine X, Y and Z Coordinates" fn)
;underline the header
(write-line "*****************************************" fn)
;start the loop
(repeat (/ (length thelist) 3)
;get the x coordinate
(setq xval (rtos (nth n thelist)))
;increase the counter
(setq n (1+ n))
;get the y coordinate
(setq yval (rtos (nth n thelist)))
;increase the counter
(setq n (1+ n))
;get the z coordinate
(setq zval (rtos (nth n thelist)))
;write the x coordinate to the file
(write-line  (strcat "X-Value : " xval)  fn)
;write the y coordinate to the file
(write-line  (strcat "Y-Value : " yval)  fn)
;write the z coordinate to the file
(write-line  (strcat "z-Value : " zval)  fn)
;add a seperator
(write-line "-----------------------------" fn)
;increase the counter
(setq n (1+ n))
);repeat
;close the file
(close fn)
);progn
;it's not a polyline, inform the user
(alert "This is not a Polyline! - Please try again.")
);if
(princ)
);defun
;------------------------
;clean loading
(princ)
;---------------------------
;End of VL-POLY.LSP
;---------------------------

Many txh in advance.

Link to comment
Share on other sites

See these functions in the Help file:

;check if folder exist
(if (not (vl-file-directory-p "c:/User"))
 (progn
   (alert "Does not exist. Create one...")
   (vl-mkdir "c:/User")
 ;check again, who knows?
   (if (vl-file-directory-p "c:/User")
     (progn
(alert "Now it was created. Go to write your data")
;;write data here
(progn
  (alert "Exist. You can write your data")
  ;;write data here
)
     )
   )
 )
)

 

 

~'J'~

Link to comment
Share on other sites

See these functions in the Help file:

;check if folder exist
(if (not (vl-file-directory-p "c:/User"))
 (progn
   (alert "Does not exist. Create one...")
   (vl-mkdir "c:/User")
 ;check again, who knows?
   (if (vl-file-directory-p "c:/User")
     (progn
(alert "Now it was created. Go to write your data")
;;write data here
(progn
  (alert "Exist. You can write your data")
  ;;write data here
)
     )
   )
 )
)

~'J'~

 

Thx. Fixo for that

Link to comment
Share on other sites

Try this code and select a Polyline and after that select 3dpoly ..

 

(defun c:TesT (/ ss lst)
 (if (setq ss (car (entsel "\n Select Polyline :")))
   (progn
     (vl-remove-if-not
       (function
         (lambda (x)
           (if (eq (car x) 10)
             (setq lst (cons (cons (cadr x) (caddr x)) lst))
           )
         )
       )
       (entget ss)
     )
     (princ lst)
   )
 )
 (princ)
)

Link to comment
Share on other sites

I am starting to getting there but i can't make it take out the same x, y values as the same as list do, please help:

 

(defun c:vlplexport ( / theobj n thelist xval yval fname fn)
;load the visual lisp extensions
(vl-load-com)
;get the entity and entity name
(setq theobj (car (entsel "\nSelect a Polyline: ")))
;convert to vl object
(setq theobj (vlax-ename->vla-object theobj))
;check if it's a polyline
(if (= (vlax-get-property theobj 'ObjectName) "AcDbPolyline")
;if it is, do the following
(progn
;retrieve the coordinates
(setq thelist (vlax-get-property theobj 'coordinates))
;convert to a list
(setq thelist (vlax-safearray->list  (variant-value thelist)))
;Check if folder exits
(if (not (vl-file-directory-p "c:/User"))
 (progn
   (alert "Does not exist. Creating one...")
   (vl-mkdir "c:/User")
;check again, who knows?
   (if (vl-file-directory-p "c:/User")
     (progn
(alert "Now it was created. I am Going to write your data")
     )
   )
 )
)
;zero the counter
(setq n 0)
;open it to write
(setq fn (open "c:/user/infile" "w"))
;start the loop
(repeat (/ (length thelist) 4)
;get the x coordinate
(setq xval (rtos (nth n thelist)))
;increase the counter
(setq n (1+ n))
;get the y coordinate
(setq yval (rtos (nth n thelist)))
;increase the counter
(setq n (1+ n))
;get the z coordinate
;(setq zval (rtos (nth n thelist)))
;write the x coordinate to the file
(write-line  (strcat "          at point  X= " xval "  Y= " yval "  Z= 0.0000")  fn)
;increase the counter
(setq n (1+ n))
);repeat
;close the file
(close fn)
);progn
;it's not a polyline, inform the user
(alert "This is not a Polyline! - Please try again.")
);if
(princ)
);defun
;------------------------
;clean loading
(princ)
;---------------------------
;End of VL-POLY.LSP
;---------------------------

thx in advance. I think it has something to do with the repeat command but i am not sure?

Link to comment
Share on other sites

The way is see it.

- you can prompt for file name

- use getfiled function

- hardcode the filename.

- use DWGNAME variable as filename

 

(defun c:vlplexport  (/ AT:GetVertices strnum [b][color=blue]_dxf[/color][/b] theobj fn thelist [color=blue][b]pref f[/b][/color])
     (defun AT:GetVertices  (e / p l)
           ;; Return point at each vertex of curve
           ;; e - curve to evaluate (Arc, Line, *Polyline, Spline)
           ;; Alan J. Thompson, 09.30.10
           (if e
                 (if (eq (setq p (vlax-curve-getEndParam e))
                         (fix p))
                       (repeat (setq p (1+ (fix p)))
                             (setq l    (cons (vlax-curve-getPointAtParam
                                                    e
                                                    (setq p    (1- p)))
                                              l))
                             )
                       (list (vlax-curve-getStartPoint e)
                             (vlax-curve-getEndPoint e))
                       )
                 )
           )
     (defun strnum  (str val / p)
           (setq p " ")
           (repeat (- val (strlen str))
                 (setq p (strcat " " p)))
           (strcat str p))
     [color=blue][b](defun _dxf (ent dx)(cdr (assoc dx (entget ent))))
[/b][/color]      (vl-mkdir "c:/User")
    [color=blue][b] (setq fn (open (setq f "c:/user/infile.txt") "w"))
[/b][/color]      (prompt "\nSelect a Polyline: ")
     (while (and fn (setq theobj (ssget ":S:E" '((0 . "*LINE")))))  
           (setq thelist [color=blue][b](if (eq (_dxf (setq e (ssname theobj 0)) 0) "LINE")
                              (progn (setq pref '("S" "E")) (list (_dxf e 10)(_dxf e 11)))
[/b][/color]    (AT:GetVertices e))[b][color=blue])[/color][/b]                       
           (redraw e 3)
           (foreach 
                  itm  thelist
                 (write-line
                       (strcat [color=blue][b](if pref
                                   (strcat "                 " (car pref) "  X =")
                                   "          at point  X =")
[/b][/color]                                (strnum (rtos (Car itm) 2 4) 10)
                               "Y= "
                               (strnum (rtos (Cadr itm) 2 4) 10)
                               "Z= "
                               (strnum (rtos (last itm) 2 4) 10))
                       fn)
                 (setq pref (cdr pref))
                 )
           )
     
     [color=blue][b](close fn)
[/b][/color]      [b][color=blue](startapp "notepad" f)
[/color][/b]      (vla-regen (vla-get-ActiveDocument (vlax-get-acad-object)) acActiveViewport)
           (princ)
     )

 

*kudos to Alanjt*

 

HTH

 

NOTE:

DIMZIN 3 or 0

open will "append" ("a") the file rather than overwriting ("w") the data

 

 

CODE UPDATED:

Unique prefix for LINE entity, "S" for start point "E" for Endpoint.

Opens infile.txt file with notepad at end of program.

Replace "a" with "w" and relocate open/close function for loop.

Edited by pBe
Update Code
Link to comment
Share on other sites

Polyline has only X and Y , and only the 3dpoly has the Z coordinates .
LWpolyline always has zero value and it can not be others than zero because it's a 2d coordinates as far as I know .

 

Constant "Z" value but not always zero

(Elevation = "Z")

Link to comment
Share on other sites

The way is see it.

- you can prompt for file name

- use getfiled function

- hardcode the filename.

- use DWGNAME variable as filename

 

 

(defun c:vlplexport  (/ AT:GetVertices strnum theobj fn)
     (defun AT:GetVertices  (e / p l)
           ;; Return point at each vertex of curve
           ;; e - curve to evaluate (Arc, Line, *Polyline, Spline)
           ;; Alan J. Thompson, 09.30.10
           (if e
                 (if (eq (setq p (vlax-curve-getEndParam e))
                         (fix p))
                       (repeat (setq p (1+ (fix p)))
                             (setq l    (cons (vlax-curve-getPointAtParam
                                                    e
                                                    (setq p    (1- p)))
                                              l))
                             )
                       (list (vlax-curve-getStartPoint e)
                             (vlax-curve-getEndPoint e))
                       )
                 )
           )
     (defun strnum  (str val / p)
           (setq p " ")
           (repeat (- val (strlen str))
                 (setq p (strcat " " p)))
           (strcat str p))
     (vl-mkdir "c:/User")
     (prompt "\nSelect a Polyline: ")
     (while (setq theobj (ssget ":S:E" '((0 . "*LINE"))))
           (setq thelist (AT:GetVertices (setq e (ssname theobj 0))))
           (setq fn (open "c:/user/infile.txt" "a"))
           (redraw e 3)
           (foreach
                  itm  thelist
                 (write-line
                       (strcat "          at point  X ="
                               (strnum (rtos (Car itm) 2 4) 10)
                               "Y= "
                               (strnum (rtos (Cadr itm) 2 4) 10)
                               "Z= "
                               (strnum (rtos (last itm) 2 4) 10))
                       fn)
                 )
           (close fn)
           )(vla-regen (vla-get-ActiveDocument (vlax-get-acad-object)) acActiveViewport)
     (princ)
     )

*kudos to Alanjt*

 

HTH

 

NOTE:

DIMZIN 3 or 0

open will "append" ("a") the file rather than overwriting ("w") the data

 

 

Thx very much can do a ;; around in the lisp routine i need to lean what it is doing! Thx in advance.

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...