Jump to content

Extract text Label and Dimension's to a CSV file


BrianTFC

Recommended Posts

Hi all,

 

I need some help with a lisp routine that I'm attempting to write. What I need to do is extract a panel label by selecting the text and then select the dimensions that I need extract, I've managed to extract the dimensions correctly but when the information is extracted I get is "nil" in the first cell where the panel label should be and the dimensions in the next to cells. I don't Have Excel on my workstation so I have to use CSV file, I've already wrote an extraction code to run in Excel to retrieve the information in the CVS file. Also is there a way to save the CVS file in the same location that I have the drawing open? If someone could help me I would appreciate it.

 

(defun c:test( / s tx fn i d dl m file)
(setq ss (ssget '((0 . "*text,*DIMENSION")))
      fn "f:\\BrianD\\Dims.csv")
(repeat (setq i (sslength ss))
  (setq d (ssname ss (setq i (1- i)))
        dl (entget d)
        m (cdr (assoc 42 dl)))
        
      (setq tx (cons m tx)))
(if tx
     (progn
(setq file (open fn "a"))
   (write-line "" file)
   (foreach ss tx
     (princ ss file)
       (princ "," file)
      )

   (if file (close file))
)
)
)
(princ)

 

Thanks Brian

Link to comment
Share on other sites

Not exactly sure what you are trying to accomplish in the first part of your question without an example, but the second part is easy. To put the CSV in the same folder as your drawing you just need to get the "dwgprefix" variable. ie:

 

(setq fn (getvar 'dwgprefix))

 

That will put it in the same folder.

 

You state that you are selecting the text THEN selecting dimensions, but you only have one SSGET. Could be part of your problem.

Link to comment
Share on other sites

I've managed to extract the dimensions correctly but when the information is extracted I get is "nil" in the first cell where the panel label should be and the dimensions in the next to cells.

 

Consider that DXF Group 42 will only be present for *DIMENSION entities, and will not be present in the DXF data for *TEXT entities, meaning your variable 'm' will be null for some iterations of the repeat loop.

Link to comment
Share on other sites

Lee,

how do I make this work? do I need to have a separate ssget for the text? I added the new dxf group for text but now I get just the text. for some reason I can't put this code in the code tags.

 

 

(defun c:test2( / s tx fn i d dl m file)
(setq ss (ssget '((0 . "*text,*DIMENSION")))
      fn "C:\\Lisp routines\\myDims.csv")
(repeat (setq i (sslength ss))
  (setq d (ssname ss (setq i (1- i)))
        dl (entget d)
        m (cdr (assoc 42 dl))
        m (cdr (assoc 1 dl)))
      (setq tx (cons m tx)))
(if tx
     (progn
(setq file (open fn "a"))
   (write-line "" file)
   (foreach ss tx
     (princ ss file)
       (princ "," file)
      )

   (if file (close file))
)
)
)
(princ)

Edited by SLW210
added code tags
Link to comment
Share on other sites

[ Not Tested ]

 

(defun c:Test (/ fn ss o i e v)
 (if (and (findfile (setq fn "f:\\BrianD\\Dims.csv"))
          (setq ss (ssget '((0 . "TEXT,MTEXT,*DIMENSION"))))
     )
   (progn
     (setq o (open fn "a"))
     (write-line "" o)
     (repeat (setq i (sslength ss))
       (setq e (entget (ssname ss (setq i (1- i)))))
       (if (wcmatch (cdr (assoc 0 e)) "TEXT,MTEXT")
         (write-line (cdr (assoc 1 e)) o)
         (write-line
           (if (not (eq (type (setq v (cdr (assoc 42 e)))) 'STR))
             (rtos v 2)
             v
           )
           o
         )
       )
     )
     (close o)
   )
 )
 (princ)
)

Link to comment
Share on other sites

Tharwat,

 

 

thanks,

 

 

I tested it and it gives me all three in row A in the CSV file, can you make it put the "TEXT" in row A and the first "Dimension" in row B and the second "Dimension" in row C?

Link to comment
Share on other sites

I tested it and it gives me all three in row A in the CSV file, can you make it put the "TEXT" in row A and the first "Dimension" in row B and the second "Dimension" in row C?

 

Assuming you mean columns instead of rows, try the following:

(defun c:dimtxtexp ( / des dim enx idx sel txt )
   (if (and (setq sel (ssget '((0 . "TEXT,MTEXT,*DIMENSION"))))
            (setq des (open "f:\\BrianD\\Dims.csv" "a"))
       )
       (progn
           (repeat (setq idx (sslength sel))
               (setq enx (entget (ssname sel (setq idx (1- idx)))))
               (if (wcmatch (cdr (assoc 0 enx)) "*DIMENSION")
                   (setq dim (cons (rtos (cdr (assoc 42 enx))) dim))
                   (setq txt (cons (cdr (assoc 1 enx)) txt))
               )
           )
           (while (or dim txt)
               (write-line
                   (strcat
                       (cond ((car  txt)) ("")) ","
                       (cond ((car  dim)) ("")) ","
                       (cond ((cadr dim)) (""))
                   )
                   des
               )
               (setq txt (cdr  txt)
                     dim (cddr dim)
               )
           )
       )
   )
   (if (= 'file (type des)) (close des))
   (princ)
)

  • Like 1
Link to comment
Share on other sites

Lee,

 

 

thank you, yes I meant columns. it works great. Is there a way to have the CSV file save in the same place as the drawing? and have the name of the CSV be the same as the name of the drawing?

Edited by BrianTFC
Link to comment
Share on other sites

Is there a way to have the CSV file save in the same place as the drawing? and have the name of the CSV be the same as the name of the drawing?

 

(strcat (getvar 'DWGPREFIX) (vl-filename-base (getvar 'DWGNAME)) ".csv")

Link to comment
Share on other sites

Lee & Tharwat,

 

 

First of all I would to thank you for all of your help, I'm still learning how to write lisp routine correctly. I don't know where to put the line of code that Tharwat wrote in the lisp routine that Lee wrote. After running my excel extracting macro I found out that I would have to save all of my files in one location but with just the drawing name, so if you could show me where it goes in the lisp that would help me a lot. I have a feeling from looking at the two different lisp written by you guys that it not that easy just to put the line of code in the lisp..

 

 

Thanks Brian.

Link to comment
Share on other sites

Hi Brian,

 

Thank you for your gratitude, I'm glad the program is working well.

 

The expression:

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

Will construct & return the filepath of a CSV file with the same filename as the active drawing, and residing in the same location as the active drawing (i.e. in the working directory).

 

My current code is writing to the filepath:

"f:\\BrianD\\Dims.csv"

Therefore, replacing this filepath with the expression given above with produce the required result.

 

And so:

(setq des (open [color=red]"f:\\BrianD\\Dims.csv"[/color] "a"))

Becomes:

(setq des (open [color=red](strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".csv")[/color] "a"))

Link to comment
Share on other sites

Lee,

 

 

ok now im starting to understand, so what if I just wanted to save the file i.e. "f:\\BrianD\\dwgname.csv" . what would the code look like?

Link to comment
Share on other sites

Brian .

 

Open the Visual Lisp editor with the command name vlisp then activate the console window then copy and paste every single line of codes separately as shown below to see the return of each line of codes , so that would explain the aim of these codes in your example very clearly . ;)

 

(getvar 'dwgprefix)

 

Then this .

 

(getvar 'dwgname)

 

Finally this one .

 

(vl-filename-base (getvar 'dwgname)) 

Link to comment
Share on other sites

Lee,

 

What line determines what the type of dimension output is going to be? i.e. 5'-7" to 67. for my extracting program I need to have it be just the numbers, no feet or inch marks.

 

Thanks Brian

Link to comment
Share on other sites

What line determines what the type of dimension output is going to be? i.e. 5'-7" to 67. for my extracting program I need to have it be just the numbers, no feet or inch marks.

 

The formatting is determined by the following rtos expression:

(rtos (cdr (assoc 42 enx)))

Since the rtos function is being called without the units & precision arguments, the dimension measurement value is formatted based on your values for the LUNITS & LUPREC system variables.

 

You can override this formatting by providing the units (mode) & precision arguments as described by the rtos documentation, e.g. for decimal formatting with precision controlled by LUPREC, you would use:

(rtos (cdr (assoc 42 enx)) 2)

For decimal formatting to 4 decimal places:

(rtos (cdr (assoc 42 enx)) 2 4)

Link to comment
Share on other sites

Lee, Tharwat

 

Thank you guys for explaining everything to me. I've managed to get it to work just the way I need it.

Edited by BrianTFC
Link to comment
Share on other sites

Lee, Tharwat

 

Thank you guys for explaining everything to me. I've managed to get it to work just the way I need it.

 

I am Happy to help and good luck with your work Brian .

Link to comment
Share on other sites

  • 2 years later...

Hi Mr. Lee,

 

Can you please help me with this program?

It is almost the same as what is mentioned before but I want it to be written in csv file like this:

code1 code2 dimension dimension quantity

it will ask the user to select 2 text and 2 dimensions then input quantity.

 

Thanks!

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