Jump to content

block from spreadsheet


rookie37

Recommended Posts

It looks like it could be a powerful program. However, I couldn't get it to work. I'll keep trying.

 

Do you know of any other programs?

Link to comment
Share on other sites

I made this a while back, it will read from txt or csv:

 

;; Block Insertor by Lee McDonnell
;; 03.06.2009

(defun c:blkIns (/ doc spc bdef blk file ofile ex lst iBlk line)
 (vl-load-com)

 (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object))
       spc (if (zerop (vla-get-activespace doc))
             (if (= (vla-get-mspace doc) :vlax-true) ; vPort
               (vla-get-modelspace doc)
               (vla-get-paperspace doc))
             (vla-get-modelspace doc)))

 (setq bdef (getvar "INSNAME"))
 (while (not blk)
   (setq blk
     (getstring t
       (strcat "\nSpecify Block to be Inserted <" bdef ">: ")))
   (cond ((eq "" blk)
          (setq blk bdef))
         ((and (snvalid blk)
               (tblsearch "BLOCK" blk)))
         ((and (snvalid blk)
               (findfile (strcat blk ".dwg")))
          (command "_.insert" blk) (command))
         (T (princ "<!> Please Re-Select <!>")
          (setq blk nil bdef ""))))
 
 (if (setq file (getfiled "Select File to Read"
       (if *load$file* *load$file* "") "txt;csv" )
   (progn
     (setq *load$file* file ofile (open file "r")
           ex (vl-filename-extension file))
     (while (setq nl (read-line ofile))
       (setq lst (cons (StrBrk nl (if (eq ex ".txt") 32 44)) lst)))
     (close ofile)
     (foreach line lst
       (setq iBlk
         (vla-InsertBlock spc
           (vlax-3D-point
             (mapcar 'distof (list (car line) (cadr line) "0.0")))
           blk 1. 1. 1. 0.))      
       (if (and (eq :vlax-true (vla-get-HasAttributes iBlk))
                (setq line (cddr line)))
         (foreach att  (vlax-safearray->list
                         (vlax-variant-value
                           (vla-getAttributes iBlk)))
           (if line
             (vla-put-TextString att (car line)))
           (setq line (cdr line))))))
   (princ "\n<!> No File Selected <!>"))
 (princ))
     

(defun StrBrk (str chrc / pos lst)
 (while (setq pos (vl-string-position chrc str))
   (setq lst (cons (substr str 1 pos) lst)
         str (substr str (+ pos 2))))
 (reverse (cons str lst)))

Link to comment
Share on other sites

It looks great!

can I get an example of the syntax for the text file?

 

In the meantime I found part of an old program.

I used it before. It’s clumbsy but works ok. I copy and paste from a spreadsheet into a lisp.

First I define p1, p2, p3. That’s the part I lost. I don’t know how to do it anymore.

Then I insert block lspt (long section point)

(COMMAND "-insert" "lspt" p1 "" "" "")

(COMMAND "-insert" "lspt" p2 "" "" "")

(COMMAND "-insert" "lspt" p3 "" "" "")

How can I cut and paste to define p1, p2, and p3?

Link to comment
Share on other sites

I found another old program

 

It worked great 1 year ago. Now it don't work. Can somebody tell me why?

 

I'm calculating the sun position

 

defun c:sun ()

 

(setq fp (open "d:\\sundial calc\\acad\\txt coords\\coords.txt" "r") lst '() i 0)

 

(while (setq a (read-line fp))

 

(setq lst (cons a lst))

 

)

 

(close fp)

 

(repeat (length lst)

 

(command "._insert" "sun" (nth i lst) "" "" "")

 

(setq i (1+ i))

 

)

 

)

Link to comment
Share on other sites

I found another old program

 

It worked great 1 year ago. Now it don't work. Can somebody tell me why?

 

I'm calculating the sun position

 

defun c:sun ()

 

(setq fp (open "d:\\sundial calc\\acad\\txt coords\\coords.txt" "r") lst '() i 0)

 

(while (setq a (read-line fp))

 

(setq lst (cons a lst))

 

)

 

(close fp)

 

(repeat (length lst)

 

(command "._insert" "sun" (nth i lst) "" "" "")

 

(setq i (1+ i))

 

)

 

)

How is your text file looks like?

 

~'J'~

Link to comment
Share on other sites

I found another old program

 

It worked great 1 year ago. Now it don't work. Can somebody tell me why?

 

 

Guess there is extra double quotes in command

Change on this one:

(command "._insert" "sun" (nth i lst) "" "")

 

~'J'~

Link to comment
Share on other sites

Hello all!

 

@rookie 37:

i tested the program with acad 2005, it works fine for me.

There is another one: http://cadtips.cadalyst.com/standard-blocks/insert-blocks-a-database

Give it a try.

 

@Lee:

Looks verry interesting, but i couldn't get it to work.

Whats the syntax of the input-file?

 

There should be not too much choices, I tried:

blockname;x-coord;y-coord;z-coord;attribute-value

blockname;x,y,z-coord;attribute-value

x-coord;y-coord;z-coord;blockname;attribute-value

blockname;x,y,z-coord; blockname;attribute-value

 

Any hint?

 

regards Wolfgang

Link to comment
Share on other sites

I apologise guys, I should have explained that :oops:

 

You can use either a Txt or CSV file for the input, and in each case the format is this:

 

x-coord y-coord attrib1 attrib2 attrib3...

If the block is attributed, the attributes will be entered, else they will be ignored.

 

When I wrote this, it was only for a 2D purpose, but I can alter it to include Z-coord if you wish.

Link to comment
Share on other sites

Here is an upgraded Version:

 

;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;
;;                                                                               ;;
;;                                                                               ;;
;;                          --=={  Block Insertor  }==--                         ;;
;;                                                                               ;;
;;  Program will insert a specific block at points as listed in either txt or    ;;
;;  csv file. If attribute values follow the points listed, then these will be   ;;
;;  entered into the block.                                                      ;;
;;                                                                               ;;
;;  Input File format should be as follows:-                                     ;;
;;  --------------------------------------------                                 ;;
;;  x-coord y-coord z-coord attrib(1) attrib(2) attrib(3) ... attrib(n)          ;;
;;                                                                               ;;
;;  If txt file is used, these values should be separated by a space, if a csv   ;;
;;  file is used, the values should be in adjacent cells.                        ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  FUNCTION SYNTAX:  BlkIns                                                     ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  AUTHOR:                                                                      ;;
;;                                                                               ;;
;;  Copyright © Lee McDonnell, December 2009. All Rights Reserved.               ;;
;;                                                                               ;;
;;      { Contact: Lee Mac @ TheSwamp.org, CADTutor.net }                        ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  VERSION:                                                                     ;;
;;                                                                               ;;
;;    ø 1.0   ~¤~   05 December 2009   ~¤~   º First Release                     ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;


(defun c:blkIns (/ *error* StrBrk

                  BDEF BLK DEL DOC FILE I IBLK LST NL OFILE SPC)
 (vl-load-com)

 (defun *error* (msg)
   (and uFlag (vla-EndUndoMark doc))
   (and ofile (close ofile))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))
 

 (defun StrBrk (str chrc / pos lst)
   (while (setq pos (vl-string-position chrc str))
     (setq lst (cons (substr str 1 pos) lst)
           str (substr str (+ pos 2))))
   (reverse (cons str lst)))


 (setq doc (vla-get-ActiveDocument
             (vlax-get-Acad-Object))
       
       spc (if (zerop (vla-get-activespace doc))
             (if (= (vla-get-mspace doc) :vlax-true)
               (vla-get-modelspace doc)
               (vla-get-paperspace doc))
             (vla-get-modelspace doc)))
 

 (setq bdef (getvar "INSNAME"))
 (or (tblsearch "BLOCK" bdef)
     (setq bdef (findfile (strcat bdef ".dwg")))
     (setq bdef ""))
 

 (while
   (progn
     (setq blk (getstring t
                 (strcat "\nSpecify Block to be Inserted <" bdef "> : ")))

     (cond (  (eq "" blk)  (setq blk bdef) nil)

           (  (or (tblsearch "BLOCK" blk)
                  (setq blk (findfile (strcat blk ".dwg")))) nil)

           (  (princ "\n** Block not Found **")))))
          
 
 (if (setq i 0 file (getfiled "Select File to Read" (cond (*load$file*) ("")) "txt;csv" 16))
   (progn
     (setq uFlag (not (vla-StartUndoMark doc)))
     
     (setq *load$file* file ofile (open file "r")

           del (if (eq ".TXT" (strcase (vl-filename-extension file))) 32 44))
     
     (while (setq nl (read-line ofile))
       (setq lst (cons (StrBrk nl del) lst)))
     (setq ofile (close ofile))
     
     (foreach line lst
       
       (if (vl-catch-all-error-p
             (setq iBlk
               (vl-catch-all-apply 'vla-insertblock
                 (list spc (vlax-3D-point
                             (mapcar 'distof
                               (list (car line) (cadr line) (caddr line))))
                       blk 1. 1. 1. 0.))))
         (setq i (1+ i))
         
         (if (and (eq :vlax-true (vla-get-HasAttributes iBlk))
                  (setq line (cdddr line)))

           (foreach att  (vlax-safearray->list
                           (vlax-variant-value
                             (vla-getAttributes iBlk)))
             
             (if line (vla-put-TextString att (car line)))
             (setq line (cdr line))))))

     (or (zerop i) (princ (strcat "\n** " (itoa i) " Blocks Failed to be Inserted **")))      
     (setq uFlag (vla-EndUndoMark doc)))

   (princ "\n*Cancel*")))


;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;                                                                               ;;
;;                             End of Program Code                               ;;
;;                                                                               ;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;

All instructions are in the Header.

Link to comment
Share on other sites

Lee,

 

thank you for the updated version.

 

Your programs are allways worth some study.

 

Is the "Vla-insertblock"-Statement faster than the >>command "._insert"

Where do you find all this vla-statements? Couldn't find it in the help-file :(

 

regards

Wolfgang

Link to comment
Share on other sites

Lee,

 

thank you for the updated version.

 

Your programs are allways worth some study.

 

Is the "Vla-insertblock"-Statement faster than the >>command "._insert"

Where do you find all this vla-statements? Couldn't find it in the help-file :(

 

regards

Wolfgang

 

Thanks Wolfgang,

 

vla-Insertblock or entmake is generally a lot faster than any command statement.

 

I am in the process of upgrading this program and will post the new program soon.

 

As for help on vla- functions, they are in the help file, but written for VBA instead of Visual LISP, so you have to make the connections :)

 

Lee

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