Exporting multiple polylines coordinates, with an ID for the exported polylines

Exporting multiple polylines coordinates, with an ID for the exported polylines

djurk_haas
Advocate Advocate
8,258 Views
34 Replies
Message 1 of 35

Exporting multiple polylines coordinates, with an ID for the exported polylines

djurk_haas
Advocate
Advocate

Hello,

 I wonder if it's possible to modify the lisp routine which is from this forum: "Exporting multiple polylines coordinates, with text break".

 This lisp with the solution with the text break is almost perfect for me but I am wondering  if it's possible to get in the output the "ID" of the polylines?

I have given the polylines an "ID" by filling in the hyperlink field at the properties. Is it possible to adjust this lisp or the one that's below* so that you can see which coordinates belong to which polyline?

 *I have found another lisp routine (see below) which exports coordinates of polylines to a csv file (works great) but it also exports one long list of coordinates when you select more than one polyline.

 

For example this is the current output in the csv:

85790.80773261,-617.56967007,0.00000000

7907.16139717,-1476.60439300,0.00000000

86419.30065153,-823.03783397,0.00000000

87612.15468495,-027.65058909,0.00000000

89728.50834951,-886.68531202,0.00000000

88240.64760388,-233.11875299,0.00000000

 

And this is the wanted output in the csv:

HL-01

85790.80773261,-617.56967007,0.00000000

7907.16139717,-1476.60439300,0.00000000

86419.30065153,-823.03783397,0.00000000

HL-02

87612.15468495,-027.65058909,0.00000000

89728.50834951,-886.68531202,0.00000000

88240.64760388,-233.11875299,0.00000000

 

Lisp routine from dotsoft:

----------------------------------------------------------------------

;             (Export LWPOLYLINE Vertices & Points to File)

;           Copyright (C) 2000 DotSoft, All Rights Reserved

;                   Website: http://www.dotsoft.com

; ----------------------------------------------------------------------

; DISCLAIMER: DotSoft Disclaims any and all liability for any damages

; arising out of the use or operation, or inability to use the software.

; FURTHERMORE, User agrees to hold DotSoft harmless from such claims.

; DotSoft makes no warranty, either expressed or implied, as to the

; fitness of this product for a particular purpose. All materials are

; to be considered ‘as-is’, and use of this software should be

; considered as AT YOUR OWN RISK.

; ----------------------------------------------------------------------

;;Revised 8/23/07 CAB to report coordinates in current UCS

(defun c:ptexport ()

(setq sset (ssget '((-4 . "<OR")(0 . "POINT")

                     (0 . "LWPOLYLINE")(-4 . "OR>"))))

(if sset

   (progn

     (setq itm 0 num (sslength sset))

     (setq fn (getfiled "Point Export File" "" "csv" 1))

     (if (/= fn nil)

       (progn

         (setq fh (open fn "w"))

         (while (< itm num)

         (setq hnd (ssname sset itm))

           (setq ent (entget hnd))

           (setq obj (cdr (assoc 0 ent)))

           (cond

             ((= obj "POINT")

               (setq pnt (cdr (assoc 10 ent)))

               (setq pnt (trans pnt 0 1));;**CAB

               (princ (strcat (rtos (car pnt) 2 8) ","

                               (rtos (cadr pnt) 2 8) ","

                               (rtos (caddr pnt) 2 8)) fh)

               (princ "\n" fh)

             )

             ((= obj "LWPOLYLINE")

               (if (= (cdr (assoc 38 ent)) nil)

                 (setq elv 0.0)

                 (setq elv (cdr (assoc 38 ent)))

               )

               (foreach rec ent

                 (if (= (car rec) 10)

                   (progn

                     (setq pnt (cdr rec))

                     (setq pnt (trans pnt 0 1));;**CAB

                     (princ (strcat (rtos (car pnt) 2 8) ","

                                     (rtos (cadr pnt) 2 8) ","

                                    (rtos elv 2 8)) fh)

                     (princ "\n" fh)

                   )

                 )

               )

             )

             (t nil)

           )

           (setq itm (1+ itm))

         )

        (close fh)

       )

     )

   )

)

(princ)

)

 

(princ "\nPoint Export loaded, type PTEXPORT to run.")

(princ)

 

THANKS A LOT FOR YOUR HELP

0 Likes
Accepted solutions (2)
8,259 Views
34 Replies
Replies (34)
Message 2 of 35

john.uhden
Mentor
Mentor

Just add the line in red:

     

 (write-line ID fh)
(foreach rec ent (if (= (car rec) 10) (progn (setq pnt (cdr rec)) (setq pnt (trans pnt 0 1));;**CAB (princ (strcat (rtos (car pnt) 2 8) "," (rtos (cadr pnt) 2 8) "," (rtos elv 2 8)) fh) (princ "\n" fh) ) ) )

Of course you have to supply the value of ID for each polyline, which could be the value of its hyperlink property or even just its handle, of which the latter is easier to obtain and enables you to avoid having to add the hyperlink in the first place.

John F. Uhden

0 Likes
Message 3 of 35

ВeekeeCZ
Consultant
Consultant
Accepted solution

Hi, glad you made it. Add after this line: 

((= obj "LWPOLYLINE")

 

These new:

(princ (geturl hnd) fh)
(princ "\n" fh)

 

In one case you have file name with a relative path... not sure whether you want to keep it or not. Use this if later.

(princ (vl-string-subst "" ".csv" (vl-string-trim ".\\" (geturl hnd))) fh)

(princ "\n" fh)

0 Likes
Message 4 of 35

djurk_haas
Advocate
Advocate

Hello,

 

The lisp works fine now.

The only thing that I don't get to work is to save the export file (txt) directly.

Now it prompts to save the file. All the given defaults like name, extension and location are good so the "only"thing I want to accomplish is an automatic enter (click the save button) at this point.

 

Save-txt.jpg

 

 

Thanks (again) for the help.

 

 

 

0 Likes
Message 5 of 35

ВeekeeCZ
Consultant
Consultant

@djurk_haas wrote:

....The only thing that I don't get to work is to save the export file (txt) directly.

Now it prompts to save the file. All the given defaults like name, extension and location are good so the "only"thing I want to accomplish is an automatic enter (click the save button) at this point.

... 


Well, then you don't need a dialog anymore.

 

There is the following line in the code, where the (getfiled) command shows a dialog and returns a filename inc. filepath. 

(setq fn (getfiled "Point Export File" "" "txt" 1))

 

So replace that line with the command where you manually concatenate a filename:

(setq fn (strcat (getvar 'DWGPREFIX) (vl-string-subst "txt" "dwg" (getvar 'DWGNAME))))

0 Likes
Message 6 of 35

john.uhden
Mentor
Mentor

@djurk_haas asked privately about using the entitiy's handle for an ID, but wasn't sure how.

 

If the entity is just an ENAME, then (cdr (assoc 5 (entget ename)))

 

If the entity is a vla-object, then (vlax-get object 'Handle)

 

Either method will return the entity's unique handle as a hexadecimal string.

John F. Uhden

0 Likes
Message 7 of 35

djurk_haas
Advocate
Advocate

I don't get working

 

Original code (ID is hyperlink) works good:

           
      (princ (geturl hnd) fh)
      (princ "\n" fh)

 

When I put in the code(cdr (assoc 5 (entget ename))), see below, for handle ID it gives an error: bad argument type: lentityp nil

 

      (princ (cdr (assoc 5 (entget ename))) fh)
      (princ "\n" fh)

 

When I put in the code (vlax-get object 'Handle), see below, for handle ID it gives an error: bad argument type: VLA-OBJECT nil

 

      (princ (vlax-get object 'Handle) fh)
      (princ "\n" fh)

 

What am I missing?

 

0 Likes
Message 8 of 35

john.uhden
Mentor
Mentor

My apologies.  I was just using ename and object as examples of a symbol name.

Your code doesn't use either of those names as symbols, so they don't exist and thus the error.

You have to use instead which ever symbol name in your code is applicable.  Perhaps en?

 

Or if ENTITY is what you want to use, then it is already a list of entity data,

so (cdr (assoc 5 ENTITY)) will work.

John F. Uhden

0 Likes
Message 9 of 35

djurk_haas
Advocate
Advocate

Hello John,

 

I have tried the code but it gives for every polyline the ID: 1A1

 

1A1
181961.69375053,25334.52290171,0
181733.65336776,25106.48251894,0
183291.34703831,23548.7888484,0
180865.96922467,21123.41103476,0
180466.45402158,21522.92623784,0
180395.74334346,21452.21555973,0
178292.10054115,23555.85836204,0
181016.22941567,26279.98723656,0
1A1
178428.21859652,28867.99805571,0
175774.80040012,26214.57985931,0
175375.28519704,26614.09506239,0
175304.57451892,26543.38438427,0
173200.9317166,28647.02718659,0
175925.06059112,31371.15606111,0

 

 

0 Likes
Message 10 of 35

john.uhden
Mentor
Mentor

Please post your latest code so that we can see how and where you are getting the handle for each polyline.

John F. Uhden

0 Likes
Message 11 of 35

djurk_haas
Advocate
Advocate

Hi John,

 

 See also my post:

 

Re: Offset Multiple Polylines to (new or current) layer?

 01-04-2018 06:51 AM in reply to: BeekeeCZ

 

There I have added also some drawings.

 

 

0 Likes
Message 12 of 35

john.uhden
Mentor
Mentor
Accepted solution

I think you need to change this line...

 

	      (princ (cdr (assoc 5 ENTITY)) fh)

to

	      (princ (cdr (assoc 5 ent)) fh)

being one for each itm of sset.

John F. Uhden

0 Likes
Message 13 of 35

mihai_bantas
Enthusiast
Enthusiast

Hi everyone, i have a problem similar to "djurk_haas". I have a dwg file from which i try to extract the coordinates of the polylines with the name association attached. I don't understand where i'm wrong. I've been trying to change the code but i can't export, the exported name gives me a nil name. 

I also attach the work files. Thank you in advance.

0 Likes
Message 14 of 35

Kent1Cooper
Consultant
Consultant

I think we would need to see how the (geturl) function is defined.  [Or is it missing?]  I see it referred to earlier in the thread, but I don't see a function definition anywhere, and it's not listed in the AutoLisp Functions Reference.

Kent Cooper, AIA
0 Likes
Message 15 of 35

mihai_bantas
Enthusiast
Enthusiast

Hi, i have to admit that i am lost with this problem. I tried to adapt the code to my file. I adapted my file to the Office.dwg file. I do not understand why it does not work.

Thanks.

0 Likes
Message 16 of 35

Kent1Cooper
Consultant
Consultant

@mihai_bantas wrote:

.... I have a dwg file from which i try to extract the coordinates of the polylines with the name association attached. ....


What is the "name association"?  The (geturl) function seems to be about a hyperlink, but if you want the entity name [which will not be the same the next time you open the drawing], or the text content, or the handle, of the Text object it crosses, something different would be appropriate.

Kent Cooper, AIA
0 Likes
Message 17 of 35

john.uhden
Mentor
Mentor

I don't see what your geturl function is/does, but it looks like it is supposed to create a string, but your hnd variable is an entity name.  I am guessing that your geturl function retrieves the hyperlink attached to the hnd entity.  You should inspect what that function returns to see if it is a valid filename base, or perhaps there is no hyperlink attached.

John F. Uhden

0 Likes
Message 18 of 35

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

I don't see what your geturl function is/does....


It's not their [that is, not @mihai_bantas 's] function, but as far as I can tell first appears in @ВeekeeCZ 's message 3, and that's marked as a Solution, so I hope they can show us a definition, or possibly the OP @djurk_haas who accepted that as a Solution.

Kent Cooper, AIA
0 Likes
Message 19 of 35

mihai_bantas
Enthusiast
Enthusiast

Hi, by activating this function

(princ (geturl hnd) fh)
(princ "\n" fh)

  in the code gives me the following result displayed for example for polyline A1 :

nil (so how do i write the content of the text A1 in the result instead of nil)
579902.95324265,326515.86691102,0.00000000
579957.45804532,326585.73148024,0.00000000

 

(if (= (cdr (assoc 38 ent)) nil) i changed the wave to 38 in 0, 1, 5  and 50 no results

(setq elv 0)

(setq elv (cdr (assoc 38 ent)))

)

0 Likes
Message 20 of 35

john.uhden
Mentor
Mentor

Yes, that was odd of BeekeeCZ not to have included the function definition.  Whatever it is, he should probably be using the handle instead.

John F. Uhden

0 Likes