Jump to content

Export Block Coords to CSV


woodman78

Recommended Posts

Hi,

 

It has been a while since I have been here.  The place is looking great.  

I am looking to get some help with a lisp that I'm sure I got from here in the first place.  Sorry but I don't seem to have recorded the author.

(defun c:Coord_Export ( / block def el en ent file in lst pt ss str tag tags val )
    (while
        (not
            (or
                ;(eq "" (setq block (getstring t "\nBlock Name to Extract: ")))
                (eq "" (setq block "Set_out_info"))
                (and
                   (setq def (tblsearch "BLOCK" block))
                   (= 2 (logand 2 (cdr (assoc 70 def))))
                )
            )
        )
        (princ "\nBlock not Attributed or not Found.")
    )

    (if (not (eq "" block))
        (if
            (setq ss
                (ssget "_X"
                    (list
                        (cons 0 "INSERT")
                        (cons 2 block)
                        (cons 66 1)
                        (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                    )
                )
            )
            (progn
                (setq ent (tblobjname "BLOCK" block))
                (while (setq ent (entnext ent))
                    (if (eq "ATTDEF" (cdr (assoc 0 (entget ent))))
                        (setq tags (cons (strcase (cdr (assoc 2 (entget ent)))) tags))
                    )
                )
                (while
                    (not
                        (member
                            (setq tag
                                (strcase
                                    (cond
                                        (   (eq ""
                                                (setq tag "mytag"
                                                )
                                            )
                                            (last tags)
                                        )
                                        (   tag   )
                                    )
                                )
                            )
                            tags
                        )
                    )
                    (princ "\nAttribute not found in Block.")
                )
                (if (setq file (getfiled "Output File" "" "csv" 1))
                    (progn
                        (repeat (setq in (sslength ss))
                            (setq en (ssname ss (setq in (1- in)))
                                  pt (cdr (assoc 10 (entget en)))
                                  val nil
                            )
                            (while
                                (and
                                    (null val)
                                    (eq "ATTRIB"
                                        (cdr
                                            (assoc 0
                                                (setq el
                                                    (entget
                                                        (setq en (entnext en))
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                                (if (eq (strcase (cdr (assoc 2 el))) tag)
                                    (setq val (cdr (assoc 1 el)))
                                )
                            )
                            (if val
                                (setq lst (cons (cons val pt) lst))
                            )
                        )
                        (if (setq file (open file "w"))
                            (progn
                                (foreach line (vl-sort lst '(lambda ( a b ) (< (atof (car a)) (atof (car b)))))
                                    (setq str (car line))
                                    (foreach x (cdr line)
                                        (setq str (strcat str "," (rtos x)))
                                    )
                                    (write-line str file)
                                )
                                (setq file (close file))
                            )
                            (princ "\nUnable to Open Chosen File.")
                        )
                    )
                    (princ "\n*Cancel*")
                )
            )
            (princ "\nNo Blocks Found.")
        )
    )
    (princ)
)

So this lisp will take blocks within the drawing and create a CSV file with the point number,x,y,z.  I have been asked by our Surveying Dept to see if another column can be added to the file that will simply be a label column that will contain text.  The text can be the exact same for every row.  So something like "Setout".  It is just to make it easier to import it into the surveying equipment.

 

I would appreciate any help with this.

 

Thanks,

Brian.

Link to comment
Share on other sites

I couldn't get this to work on my machine - it just kept looping around and around.

However looking at it, 'str' is used to put together each line in the CSV file (I think). After this:

(foreach x (cdr line)
  (setq str (strcat str "," (rtos x)))
 )

 

add

(setq str (strcat str "," "Setout"))

 

see what that does

Link to comment
Share on other sites

Thanks - with the block it works for me now.

 

That's odd, I have tried this:

 

                          (if (setq file (open file "w"))
                            (progn
                                (foreach line (vl-sort lst '(lambda ( a b ) (< (atof (car a)) (atof (car b)))))
                                    (setq str (car line))
                                    (foreach x (cdr line)
                                        (setq str (strcat str "," (rtos x)))
                                    )
(setq str (strcat str "," "Setout"))
                                    (write-line str file)
                                )
                                (setq file (close file))
                            )

and it works for me. I'll have to think again why it won't work for you.

Link to comment
Share on other sites

Steven, I tried it again and it does work.  Maybe I pasted it into the code incorrectly the first time.  Thank you for your prompt assistance.

Link to comment
Share on other sites

(Original code source)

 

Depending on where you want the label to appear, change:

(setq str (car line))

to:

(setq str (strcat (car line) ",Setout"))

This will result in:

number,Setout,x,y,z

 

If you want it at the start, change it to:

(setq str (strcat "Setout," (car line)))

to yield:

Setout,number,x,y,z

 

If you want it at the end, change:

(write-line str file)

to:

(write-line (strcat str ",Setout") file)

which will yield:

number,x,y,z,Setout

 

Edited by Lee Mac
  • Like 1
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...