Jump to content

Lisp to insert blocks from csv or excel file


Ramees

Recommended Posts

Hello CAD Wizards,

I am pretty new to the idea of lisp. However I have been using autocad scripts to get my job done. So what I want to do is create a lisp function so that it reads the x , y coordinate and the point number from a csv or excel file and inserts an existing block with an attribute at that coordinate with the attribute taken the point number. Any help in writing this lisp is hugely appreciated. And let me say this again Im a noob to auto lisp :(

Heres the excel file

coo.xlsx

The data is in the Format

Point Xcor Ycor

Link to comment
Share on other sites

Its is Block1

Can it be prompted from user if its not too much complex:notworthy:

Because I have different types of blocks that needs to be inserted

Link to comment
Share on other sites

Its is Block1

Can it be prompted from user if its not too much complex:notworthy:

Because I have different types of blocks that needs to be inserted

 

Sure ;)

 

Give this a go and let me know .

 

(defun c:test (/ _parse do sp bk bn f on st l)
 ;; Tharwat 13.08.2015	;;
 (setq do (vla-get-activedocument (vlax-get-acad-object))
       sp (vla-get-block (vla-get-activelayout do))
       )
 (cond
   ((not (and (setq
                bn
                 (getstring t
                            "\nSpecify name of the Attributed Block Name :"
                            )
                )
              (/= bn "")
              (tblsearch "BLOCK" bn)
              )
         )
    (princ (strcat "\nEmpty value or Block name < "
                   bn
                   " > is not existed in current drawing !"
                   )
           )
    )
   ((setq f (getfiled "Select Excel file :"
                      (getvar 'dwgprefix)
                      "csv"
                      16
                      )
          )
    (setq on (open f "r"))
    (defun _parse (s / pos lst)
      (while (setq pos (vl-string-search ";" s 0))
        (progn (setq lst (cons (substr s 1 pos) lst))
               (setq s (substr s (+ pos 2) (strlen s)))
               )
        )
      (if (and s (/= s ""))
        (setq lst (cons s lst))
        )
      (setq lst (reverse lst))
      )
    (while (setq st (read-line on))
      (if (and (setq l (_parse st))
               (= (length l) 3)
               (setq bk (vla-insertblock
                          sp
                          (vlax-3d-point (read (cadr l)) (read (caddr l)))
                          bn
                          1.0
                          1.0
                          1.0
                          0.
                          )
                     )
               )
        (vla-put-textstring
          (car (vlax-invoke bk 'getattributes))
          (car l)
          )
        )
      )
    (close on)
    )
   )
 (princ)
 )(vl-load-com)

Link to comment
Share on other sites

Hmm Tried It but not working...

It asks for the Block name and on giving the block name it asks to open the csv file. But on opening the file nothing happens:unsure:

Heres the block if u want to test just in case

800.dwg

 

And here is the script that i currently use to do this

800.scr

Link to comment
Share on other sites

I gave you a complete program and there is no need for any other scripts or so.

 

1- You should have the attributed block existed into your current drawing that you run the program on .

2- The Excil file should be in format csv.

Link to comment
Share on other sites

The contents of the Excel file coo.csv that loaded in post #9 is not the same as the one you loaded in your first post format :x

 

Go back to the program and replace the semicolon ( ; ) with comma ( , ) .

 

(vl-string-search [color="#ff00ff"]";"[/color] s 0)

 

To become like this .

 

(vl-string-search [color="#ff00ff"]","[/color] s 0)

Link to comment
Share on other sites

YAAY!!!!! It Worked :D

Sorry for the misuderstanding due to file. I converted it using excel itself.

 

And Thank you soo Much:roll:

 

Nice, you are most welcome. :D

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