chulse Posted May 15, 2010 Posted May 15, 2010 I have been trying (lisp noob:oops:) to insert blocks from a csv file containing coordinates and attributes. I can't seem to see where this is going wrong. This is my version so far from a combination of code mostly from Lee Mac and some from Fixo and others. Many thanks to all who contributed and guided me so far, especially Lee Mac. I appologize for not commenting where each bit came from or what I have changed as I have been through so many trial versions, I have lost track. I have tried to get it to read the block name and layer from the csv file also, but no luck. Please help. ; READS FROM CSV FILE (X, Y, Z, BLOCK NAME, TREE#, SPECIES, AVERAGE CANOPY RADIUS, layer name) (defun c:AOC (/ *lst* *error* StrBrk BNME DDEL DOC FILE LNAME NL OBJ OFILE PT SPC TAG UFLAG VALLST X Y Z LINE TNUM SPECIES CANOPY att pt1 ) ;; original code by Lee Mac ~ 06.01.10 (setq lname "L-PLNT-TREE-test") ;; Layer Name (setq BNme "dt") ;; Block Name, MUST BE IN SUPPORT PATH AS DWG (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) (vla-get-modelspace doc) (vla-get-paperspace doc) ) (vla-get-modelspace doc) ) ) ;; --{ Error Handler Function }-- (defun *error* (msg) (and ofile (close ofile)) (and uflag (vla-EndUndoMark doc)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **")) ) (princ) ) ;; --{ StrBrk Function }-- By Lee Mac (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)) ) ;;***********MAIN FINCTIONS************** (if (and (setq file (getfiled "Select Input File" (cond (*load_file*) ("") ) "csv;txt" 16 ) ) (setq *ROT* (cond ((getangle "\nEnter Block Rotation Angle <0.0> : ")) (0.0) ) ) ) (progn (setq uflag (not (vla-StartUndoMark doc))) (if (not *lst*) (progn (setq DDEL (if (eq ".CSV" (strcase (vl-filename-extension file))) 44 32 ) *block_file* bNme *load_file* file ofile (open file "r") ) (while (setq nl (read-line ofile)) (setq *lst* (cons (StrBrk nl DDEL) *lst*)) ) (setq ofile (close ofile) *lst* (reverse *lst*) ) ) ) (while (setq LINE (car *lst*)) (setq x (atof (nth 0 line)) y (atof (nth 1 line)) ;;z (atof (nth 2 line)) ;;bNme (nth 3 line) ;;rot (dtr (atof (nth 4 line))) ; xscale (atof (nth 4 line)) ; yscale (atof (nth 5 line)) tnum (atof (nth 4 line) species (nth 5 line) canopy (atof (nth 6 line)) ;;lname (nth 7 line) ) ;end setq (setq PT1 (list x y 0.0)) ; CHECK FOR LAYER AND ADD IF NOT EXIST ; (or (tblsearch "LAYER" Lname) ; (vla-add (vla-get-layers doc) Lname)) ;check for block and load if not in the dwg ; (or (tblsearch "BLOCK" bNme) ; (setq bNme (findfile (strcat BNme ".dwg")))) ;;insert block (if (vl-catch-all-error-p (setq OBJ (vl-catch-all-apply (function vla-InsertBlock) (list spc (vlax-3D-point (trans pt 1 0)) bNme 1. 1. 1. *ROT* ) ) ) ) (princ "\n** Error Inserting Block **") (progn ;;set layer (vla-put-layer OBJ Lname) ;populate atributes (foreach att (vlax-safearray->list (vlax-variant-value (vla-getAttributes OBJ) ) ) (cond ((eq "ID" (vla-get-TagString att)) (vla-put-TextString att TNUM) ) ((eq "BOTANICAL" (vla-get-TagString att)) (vla-put-TextString att SPECIES) ) ) ) ;;populate dynamic parameter "CANOPY" (if (eq :vlax-true (vla-get-isDynamicBlock OBJ)) (progn (setq ValLst (mapcar 'cons '("CANOPY") (mapcar (function (lambda (i) (if (equal 0.0 (distof i) 0.0001) "1" i ) ) ) (CANOPY) ) ) ) (foreach dAtt (vlax-safearray->list (vlax-variant-value (vla-GetDynamicBlockProperties obj) ) ) (if (setq tag (assoc (strcase (vla-get-propertyName dAtt)) ValLst ) ) (vla-put-value dAtt (vlax-make-variant (cdr tag) (vlax-variant-type (vla-get-value dAtt)) ) ) ) ) ) ) ) ) (setq *lst* (cdr *lst*)) ) ) (setq uFlag (vla-EndUndoMark doc)) ) ) (princ) ) Quote
alanjt Posted May 15, 2010 Posted May 15, 2010 What's the format of your csv file? eg. Number,Northing,Easting,Elevation,Description Quote
chulse Posted May 15, 2010 Author Posted May 15, 2010 What's the format of your csv file? eg. ; READS FROM CSV FILE ; (X, Y, Z, BLOCK NAME, TREE#, SPECIES, AVERAGE CANOPY RADIUS, layer name)... Thanks Quote
alanjt Posted May 15, 2010 Posted May 15, 2010 Just for clarity, would you mind posting the block you are wanting to use? Quote
chulse Posted May 16, 2010 Author Posted May 16, 2010 Just for clarity, would you mind posting the block you are wanting to use? Attached There are 7 different ones, but with identical construction, attribute tags and dynamic parameters. The attribute tags are "ID" (for a tree number) and "BOTANICAL" (for genus and species names). One thing I am not sure of, is if this will work with the attribute "BOTANICAL" set to be "multiple lines"? Each has a linear parameter "CANOPY" to control the outer diameter. blocks.zip Quote
alanjt Posted May 16, 2010 Posted May 16, 2010 Cary, I just noticed you are running C3D. Have you played with the built in descriptor codes? I'll still happily help you code this, but you should be able to accomplish all of this with Civil 3D's built in descriptor code manager. When I get to work tomorrow I'll try and put an example together. Quote
chulse Posted May 16, 2010 Author Posted May 16, 2010 Cary, I just noticed you are running C3D. Have you played with the built in descriptor codes? I'll still happily help you code this, but you should be able to accomplish all of this with Civil 3D's built in descriptor code manager. When I get to work tomorrow I'll try and put an example together. no I have not learned any of the C3D stuff yet - still using it like vanilla. I'd like to learn though, thanks. For this specific project, I ned to send the blocks (as a cad file) to our client compatible with vanilla 2010. Not sure if thr C3D method will do that...? Quote
alanjt Posted May 17, 2010 Posted May 17, 2010 If they have an object enabler (free update), then the C3D objects can be viewed as proxy objects, or you can run ExportToAutocad and they will just be viewed as regular blocks. Quote
chulse Posted May 17, 2010 Author Posted May 17, 2010 If they have an object enabler (free update), then the C3D objects can be viewed as proxy objects, or you can run ExportToAutocad and they will just be viewed as regular blocks. Thanks, I'll have to look into that. I know C3D has way more functionality that I am aware of. Long way to go... As for my lisp, I get a syntax error when I load it, but I'm not sure what's causing it. Any thoughts? Quote
chulse Posted May 20, 2010 Author Posted May 20, 2010 Ok, here is my finished product. Many thanks to Lee, Fixo and Alan and several others for your help and contributions. My question is: could this have been put together better? It works, but slowly. How could this be improved? I have attached a test data set (rename it to .CSV). Thanks ;;; <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> ;;; ;;; READS FROM CSV FILE to insert blocks with attribs and dynamic linear parameter ;;;CSV must be in this format:(X, Y, Z, BLOCK NAME, TREE#, SPECIES, AVERAGE CANOPY RADIUS, LAYER NAME) ;;; ;;; original code by Lee Mac ~ 06.01.10 for inserting TCOT-TREE symbols, REVISED/ADAPTED BY CHULSE 05.2010 for AOC project (DEFUN c:aoc ( / *lst* *error* strbrk bnme ddel doc file lname nl obj ofile pt spc tag uflag vallst x y z line tnum species canopy att pt) (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) (VLA-GET-MODELSPACE doc) (VLA-GET-PAPERSPACE doc) ) ;_ end of if (VLA-GET-MODELSPACE doc) ) ;_ end of if ) ;_ end of setq ;; --{ Error Handler Function }-- by Lee Mac ;;*************************************************** (DEFUN *error* (msg) (AND ofile (CLOSE ofile)) (AND uflag (VLA-ENDUNDOMARK doc)) (OR (WCMATCH (STRCASE msg) "*BREAK,*CANCEL*,*EXIT*") (PRINC (STRCAT "\n** Error: " msg " **")) ) ;_ end of or (PRINC) ) ;_ end of defun ;; --{ StrBrk Function }-- By Lee Mac ;;**************************************************** (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)) ) ;_ end of setq ) ;_ end of while (REVERSE (CONS str lst)) ) ;_ end of defun ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Changes a Dynamic Block Property Value ;; Args: obj ~ Dynamic Block VLA-Object ;; prop ~ Property Name ;; val ~ New Value ;; Returns: New Value (val) (defun PutDynPropValue (obj prop val) ;; Lee Mac ~ 07.04.10 (mapcar (function (lambda (x) (if (eq (strcase prop) (strcase (vla-get-propertyName x))) (vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))))) (vlax-invoke obj 'GetDynamicBlockProperties)) val) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;***************MAIN FINCTIONS****************** (IF (AND (SETQ file (GETFILED "Select Input File" (COND (*load_file*) ("") ) ;_ end of cond "csv;txt" 16 ) ;_ end of getfiled ) ;_ end of setq (SETQ *rot* (COND ((GETANGLE "\nEnter Block Rotation Angle <0.0> : ")) (0.0) ) ;_ end of cond ) ;_ end of setq ) ;_ end of and (PROGN (SETQ uflag (NOT (VLA-STARTUNDOMARK doc))) (IF (NOT *lst*) (PROGN (SETQ ddel (IF (EQ ".CSV" (STRCASE (VL-FILENAME-EXTENSION file))) 44 32 ) ;_ end of if *block_file* bnme *load_file* file ofile (OPEN file "r") ) ;_ end of setq (WHILE (SETQ nl (READ-LINE ofile)) (SETQ *lst* (CONS (strbrk nl ddel) *lst*)) ) ;_ end of while (SETQ ofile (CLOSE ofile) *lst* (REVERSE *lst*) ) ;_ end of setq ) ;_ end of progn ) ;_ end of if (WHILE (SETQ line (CAR *lst*)) (SETQ ;;setq method by FIXO @ CadTutor x (ATOF (NTH 0 line)) y (ATOF (NTH 1 line)) z (ATOF (NTH 2 line)) bNme (nth 3 line) ;;; rot (dtr (atof (nth 4 line))) ;;; xscale (atof (nth 4 line)) ;;; yscale (atof (nth 5 line)) tnum (ATOF (NTH 4 line)) species (NTH 5 line) canopy (ATOF (NTH 6 line)) lname (nth 7 line) );_end of setq (SETq PT (LIST x y z)) ;;;***CHECK FOR LAYER AND ADD IF NOT EXIST******** by LeeMac (or (tblsearch "LAYER" Lname) (vla-add (vla-get-layers doc) Lname)) ;;;****CHECK FOR BLOCK AND LOAD IF NOT IN THE DWG****** by LeeMac (or (tblsearch "BLOCK" bNme) (setq bNme (findfile (strcat BNme ".dwg")))) ;;;******** INSERT BLOCK ************************** LEE MAC's method (if (vl-catch-all-error-p (setq OBJ (vl-catch-all-apply (function vla-InsertBlock) (list spc (vlax-3D-point (trans pt 1 0)) bNme 1. 1. 1. *rot*)))) (princ "\n** Error Inserting Block **") ;;;******* set attributes ************************** (progn (foreach att (vlax-invoke obj 'GetAttributes) (COND ((EQ "ID" (VLA-GET-TAGSTRING att)) (VLA-PUT-TEXTSTRING att tnum) ) ((EQ "BOTANICAL" (VLA-GET-TAGSTRING att)) (VLA-PUT-TEXTSTRING att species) ) ) ;_ end of cond );_ end of foreach ;;;******* apply dynamic parameter ***************** (if (eq :vlax-true (vla-get-isDynamicBlock obj)) (PutDynPropValue obj "canopy" canopy) );_end if;; ;;;****** set layer ******************************* (VLA-PUT-LAYER obj lname) );_end progn );_end if (SETQ *lst* (CDR *lst*)) );_end while (SETQ uflag (VLA-ENDUNDOMARK doc)) );_end progn );_end if (princ "blocks inserted successfully") (princ) );_end defun TESTBook1.txt Quote
Recommended Posts
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.