Jump to content

Insert blocks and set attribute values from .txt file


robwalker24

Recommended Posts

Hi guys,

 

I currently have a .txt file which has x and y coordinates as well as an IDname for around 200 items. I would like to create a lisp routine that reads this text file and inserts a block (point.dwg) to the coordinates and enters the IDname into the Name attribute.

 

The text file is in the following format:

60000.000 6000000.000 Structure1

 

I have found this helpful so far but cannot figure out how to modify it to suit my needs:

 

(defun c:insertBlocks (/ txtFile xyData expertVar attreqVar)

(setq expertVar (getvar "expert"))

(setq attreqVar (getvar "attreq"))

(setvar "expert" 2)

(setvar "attreq" 0)

(setq txtFile (open "c:\\cad\\textfile.txt" "r")) ; give the path & file name of x,y data

(while (setq xyData (read-line txtFile))

(command "-insert" "point" xyData "1" "1" "0")

)

(close txtFile)

(setvar "expert" expertVar)

(setvar "attreq" attreqVar)

(princ)

)

 

Any help would be very much appreciated.

 

Thanks very much!

Link to comment
Share on other sites

Not my best code, but should do your bidding :)

 

(defun c:test ( / *error* _str->lst bl fn fo ln ov vl x y )

 (setq fn "C:\\cad\\textfile.txt"  ;; Filename of Text file (nil for prompt)
       bl "point"                  ;; Block Name (must be in Drawing or Support Path)
 )

 (defun *error* ( msg )
   (if ov (mapcar 'setvar vl ov))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (defun _str->lst ( str del / pos )
   (if (setq pos (vl-string-search del str))
     (cons (substr str 1 pos) (_str->lst (substr str (+ pos 1 (strlen del))) del))
     (list str)
   )
 )

 (cond
   ( (not
       (or
         (setq fn (findfile fn))
         (setq fn (getfiled "" "" "txt" 16))
       )
     )
     (princ "\n*Cancel*")
   )
   ( (not
       (or
         (tblsearch "BLOCK" bl)
         (setq bl (findfile (strcat bl ".dwg")))
       )
     )
     (princ "\n--> Block not Found.")
   )
   ( (not (setq fo (open fn "r")))

     (princ "\n--> Error Opening File.")
   )
   (t
     (setq vl '("CMDECHO" "OSMODE" "ATTREQ") ov (mapcar 'getvar vl))
     (mapcar  'setvar vl '(0 0 1))

     (while (setq ln (read-line fo))
       (if
         (and
           (< 2 (length (setq ln (_str->lst ln " "))))
           (setq x (distof (car  ln)))
           (setq y (distof (cadr ln)))
         )
         (command "_.-insert" bl "_S" "1" "_R" "0" (list x y 0.0) (caddr ln))
       )
     )
     (setq fo (close fo)) (mapcar 'setvar vl ov)
   )
 )
 (princ)
)
(vl-load-com) (princ)

Maybe also try my Point Manager program.

Edited by Lee Mac
Link to comment
Share on other sites

Lee,

Thanks very much for your quick reply.

 

I am unable to get that particular code working. When I run it I get the following message in the command line:

 

Value must be nonzero.

Enter Y scale factor :

 

I apologise if I am missing something here, pretty new to this stuff!

 

Thanks again,

 

Cheers

Link to comment
Share on other sites

Lee,

Thanks very much for your quick reply.

 

I am unable to get that particular code working. When I run it I get the following message in the command line:

 

Value must be nonzero.

Enter Y scale factor :

 

I apologise if I am missing something here, pretty new to this stuff!

 

Thanks again,

 

Cheers

 

Rob, I think the program is perhaps using the wrong Point block that has too many attributes. Make a new block and call it something like point1 having only one attribute named "structure". Modify Lees code to now look for your new block called point1 or whatever. save it to where acad can find it and I think all will be cool.

Steve

Link to comment
Share on other sites

Sorry Rob, I don't use the "-Insert" command in LISP too often, but it appears the prompts vary from version to version - I have now updated the code above to explicitely state the scale and rotation and bypass the prompts.

 

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