Extracting closed polyline vertices along with text inside it.

Extracting closed polyline vertices along with text inside it.

abdul.raufJJ89F
Advocate Advocate
2,966 Views
17 Replies
Message 1 of 18

Extracting closed polyline vertices along with text inside it.

abdul.raufJJ89F
Advocate
Advocate

Hello all,

My test case has a number of closed polylines with the text enclosed in each of them. I need to extract the polyline vertices along with text label into single excel CSV somewhat in the attached image format.

At this stage, I am only able to export vertices only. Can somebody help me with this?
Please find the attached sample drawing and the required format.

Thanks

 
 

 

 

0 Likes
Accepted solutions (2)
2,967 Views
17 Replies
Replies (17)
Message 2 of 18

pbejse
Mentor
Mentor

@abdul.raufJJ89F wrote:

At this stage, I am only able to export vertices only. Can somebody help me with this?


 

This is very similar to what you're asking,  Assuming that means you already have some kind of understanding how the code works.

This is me helping you with a code that you can tweak to work with your requirements

 

Holler if you need help

 

 

 

0 Likes
Message 3 of 18

CodeDing
Advisor
Advisor
Accepted solution

@abdul.raufJJ89F ,

 

@pbejse  is correct, items like this get asked many times. They can easily be found in the forum.

Although I do not promote having to duplicate work often, I sometimes enjoy writing new code for things just to keep my mind sharp. So, here is my offering for you:

(NOTE: You need to update the folder location for your .csv file)

 

 

(defun c:TEST ( / ssPL output cnt ePL pList lyr ssTXT txt f)
;select polylines (we will find text later)
(if (setq ssPL (ssget '((0 . "LWPOLYLINE"))))
  (progn
    (command "_.ZOOM" "o" ssPL "")
    (setq output '("Layer,Vertex X,Vertex Y,Label"))
    (repeat (setq cnt (sslength ssPL))
      (setq ePL (ssname ssPL (setq cnt (1- cnt))))
      (setq pList '() lyr (cdr (assoc 8 (entget ePL))))
      ;save vertices
      (foreach x (entget ePL) (if (= 10 (car x)) (setq pList (cons (cdr x) pList))))
      ;find text inside pline
      (if (setq ssTXT (ssget "_CP" pList '((0 . "MTEXT"))))
        (setq txt (getpropertyvalue (ssname ssTXT 0) "Contents"))
        (setq txt "-No Label Found-")
      );if
      ;add data to output
      (setq output (cons (strcat lyr ",,," txt) output))
      (foreach p pList
        (setq output (cons (strcat "," (rtos (car p) 2 4) "," (rtos (cadr p) 2 4) ",") output))
      );foreach
    );repeat
    (setq output (reverse output))
    ;write to csv
    (setq f (open "c:\\MyFolder\\MySubFolder\\testfile.csv" "w"))
    (foreach i output (write-line i f))
    (close f)
    (prompt "\nCSV created.")
  );progn
;else
  (prompt "\nNo 2D polylines selected.")
);if
;finish up
(prompt "\nTEST Complete.")
(princ)
);defun

 

 

Best,

~DD

Message 4 of 18

abdul.raufJJ89F
Advocate
Advocate

Hi @pbejse 
Thanks for replying.
I am just a beginner in lisp and learning from afralisp site continuously. 
Regarding extracting co-ordinates of the polyline, I am using the below-attached lisp which was not written by me since I am still not so skilled in lisp. 

I have written some simple routines which were somewhat buggy, but I am learning gradually.
Thanks

0 Likes
Message 5 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing 

Thanks for your help., it is working fine
People like you have inspired me to start learning lisp.
I will keep in mind to not create duplicate topics.

0 Likes
Message 6 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing 

 

Just one more help.

Below line  will create a file in the desired location

    (setq f (open "c:\\MyFolder\\MySubFolder\\testfile.csv" "w"))

But what should be changes made to save excel csv in the same folder as dwg and with same name as dwg?

Thanks 

0 Likes
Message 7 of 18

dlanorh
Advisor
Advisor

@abdul.raufJJ89F wrote:

@CodeDing 

 

Just one more help.

Below line  will create a file in the desired location

    (setq f (open "c:\\MyFolder\\MySubFolder\\testfile.csv" "w"))

But what should be changes made to save excel csv in the same folder as dwg and with same name as dwg?

Thanks 


(setq f (open (strcat (getvar 'dwgprefix) (getvar 'dwgname) ".csv") "w"))

I am not one of the robots you're looking for

Message 8 of 18

pbejse
Mentor
Mentor
Accepted solution

@dlanorh wrote:


(setq f (open (strcat (getvar 'dwgprefix) (getvar 'dwgname) ".csv") "w"))

 

better throw in vl-filename-base in there..

(strcat (getvar 'dwgprefix)
		       (vl-filename-base (getvar 'dwgname)) ".csv")

 otherwise you'll get  drawingname.dwg.csv

 

HTH

 

Message 9 of 18

Sea-Haven
Mentor
Mentor

My 2 $0.05 

 

(setq co-ords (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car (entsel "\npick pline" )))))

 

(setq ss (ssget "wp" co-ords  (list (cons 0 "*text")))) ; get text or mtext inside a polygon

 

Like others see attached

 

 

0 Likes
Message 10 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing @pbejse 
Since some of the drawings have text object only or a combination of both text and mtext.I tried to change the following line 

 (setq ssTXT (ssget "_CP" pList '((0 . "MTEXT"))))

to

 (setq ssTXT (ssget "_CP" pList '((0 . "MTEXT,TEXT"))))

 But it is giving me the following error
error: ADS request error

Can you guys guide me where I am going wrong?
Thanks

0 Likes
Message 11 of 18

CodeDing
Advisor
Advisor

@abdul.raufJJ89F 

 

That is because a TEXT object does not use the "contents" property.

Update this part:

      (if (setq ssTXT (ssget "_CP" pList '((0 . "MTEXT"))))
        (setq txt (getpropertyvalue (ssname ssTXT 0) "Contents"))
        (setq txt "-No Label Found-")
      );if

To this:

      (if (setq ssTXT (ssget "_CP" pList '((0 . "MTEXT,TEXT"))))
        (if (eq "MTEXT" (cdr (assoc 0 (entget (setq eTXT (ssname ssTXT 0))))))
          (setq txt (getpropertyvalue eTXT "Contents"))
          (setq txt (getpropertyvalue eTXT "TextString"))
        );if
        (setq txt "-No Label Found-")
      );if

Best,

~DD

Message 12 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing 
Thanks, it was quite helpful.
I am further trying to modify your code for some changes. I am trying to get one additional column (capacity) in csv .So I will search for the attributed block with a single tag inside each polygon whose value always be numeric which I will fetch .So here is my try
change

  (setq output '("Layer,Vertex X,Vertex Y,Label"))

to

  (setq output '("Layer,Vertex X,Vertex Y,Label,Capacity"))

For finding block inside polyline

;find the block inside pline
(if (setq ssBLK (ssget "_CP" pList '((0 . "INSERT"))))
    (setq blkatt (getpropertyvalue (ssname ssBLK 0) "Attributes"))
   (setq blkatt "-No block found-")
);if

further changing 

   (setq output (cons (strcat lyr ",,," txt) output))
      (foreach p pList
        (setq output (cons (strcat "," (rtos (car p) 2 4) "," (rtos (cadr p) 2 4) ",") output))
      );foreach
    );repeat
    (setq output (reverse output))

to

(setq output (cons (strcat lyr ",,," txt "," blkatt) output))
      (foreach p pList
        (setq output (cons (strcat "," (rtos (car p) 2 4) "," (rtos (cadr p) 2 4) ",") output))
      );foreach
    );repeat
    (setq output (reverse output))

Is this approach correct?
Thanks

0 Likes
Message 13 of 18

CodeDing
Advisor
Advisor

@abdul.raufJJ89F ,

 

I'm a bit rusty on my vl- / vla- / vlax- ...approach, so this could probably be done a bit more elegantly. But everything looks good, just update this part..

We need to be sure out Attribute exists, because if it does not, then the function will error out.

.....
(setq attName "MyAttribute")
(if (and (setq ssBLK (ssget "_CP" pList '((0 . "INSERT") (66 . 1))))
         (member (strcase attName)
           (mapcar 'strcase
             (mapcar 'vla-get-TagString
               (vlax-invoke (vlax-ename->vla-object (ssname ssBLK 0)) 'GetAttributes)
             );mapcar
           );mapcar
         );member
    );and
  (setq blkatt (getpropertyvalue (ssname ssBLK 0) attName))
  (setq blkatt "-No block/attribute found-")
);if
.....

Let me know if that works (along with the other changes you already suggested).

best,

~DD

0 Likes
Message 14 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing 

I made the changes suggested by you but it seems that our code is not able to read the attribute value. The excel csv which I am getting has column capacity with every value as  '-NoBlock/attribute found-'  even though each enclosed polygon has attributed block.
Further, the blocks can be static or dynamic within my case but it will always have a single tag whose value will be most probably numeric.

I am attaching the updated lisp code along with sample dwg and the current excel csv file which I am getting. Please have a look.
Thanks



0 Likes
Message 15 of 18

CodeDing
Advisor
Advisor

@abdul.raufJJ89F ,

 

It appears to be working for my testing.

Did you update this line to your attribute name?

(setq attName "MyAttribute")
.....
(setq attName "Meeting")
Message 16 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing 

You were absolutely right.
Just wondering if we can get the tag name from code itself because although there will be a single tag, it will not be of the same name in all the cases.
Thanks

0 Likes
Message 17 of 18

CodeDing
Advisor
Advisor

@abdul.raufJJ89F ,

 

We can do that, but we are now assuming that the FIRST Block found AND the FIRST attribute found will always contain our required value.

As long as you are good with those assumptions, this should work for you..

.....
(if (and (setq ssBLK (ssget "_CP" pList '((0 . "INSERT") (66 . 1))))
         (setq attName
           (car (mapcar 'vla-get-TagString
                  (vlax-invoke (vlax-ename->vla-object (ssname ssBLK 0)) 'GetAttributes)
                );mapcar
           );car
         );setq
    );and
  (setq blkatt (getpropertyvalue (ssname ssBLK 0) attName))
  (setq blkatt "-No block/attribute found-")
);if
.....

Best,

~DD

0 Likes
Message 18 of 18

abdul.raufJJ89F
Advocate
Advocate

@CodeDing 

Yup, it is working.
Thank you very much for your effort and time.
Also, you have cleared my doubt regarding the Mtext and Text property difference.

0 Likes