Results 1 to 5 of 5

Thread: block insertion with layer definition and attributes with name values from csv

  1. #1
    Login to Give a bone
    0

    Default block insertion with layer definition and attributes with name values from csv

    Hi all, quick recap of where I'm at:

    I've got a full suite of LISP routines for several different automated annotation and calculation tools which worked beautifully until I moved employer. Previously I have been dealing with Trimble survey data exchanged as dxf's and the data in that format gave me "DIAG_CROSS" blocks with the point attributes written into the block. Having moved companies I now find myself dealing with Leica and LisCAD and the only reliable way I have of extracting point data with an extensive attribute set is via csv.

    What I need is a LISP to read in a csv and create a "DIAG_CROSS" block at each of the points and generate 8 block attributes that match the points attribute tables, the last column in the csv contains the target layer designation for the block.

    Attached are a sample dxf in the format I use to deal with and a csv (saved as a txt since I can't attach a csv) of invented points in the format I'll be using, the headers in the csv are what I need the block attributes to be called in the block, which I can then call on with the rest of my LISP suite.

    The format of the csv is Pt No.,E,N,Z,CODE,DEPTH,SIZE,MATERIAL,COMMONNOTES,END,NUMBEROF,METHOD,NOTES,LAYER and the attributes I need to be named and attached to the blocks are PtNo.,CODE,DEPTH,SIZE,MATERIAL,COMMONNOTES,END,NUMBEROF,METHOD,NOTES. I don't need the elevation as displayed in the dwg.

    Thanks in advance
    Attached Files Attached Files

  2. #2
    I could stop if I wanted to
    Join Date
    2002-08
    Posts
    231
    Login to Give a bone
    0

    Default Re: block insertion with layer definition and attributes with name values from csv

    Try this if can be help you!

    Code:
    ; str2lst
    ;; Transforme un chaine avec séparateur en liste de chaines
    ;;
    ;; Arguments
    ;; str : la chaine à transformer en liste
    ;; sep : le séparateur
    ;;
    ;; Exemples
    ;; (str2lst "a b c" " ") -> ("a" "b" "c")
    ;; (str2lst "1,2,3" ",") -> ("1" "2" "3")
    (defun str2lst (str sep / pos)
    	(if (setq pos (vl-string-search sep str))
    		(cons
    			(substr str 1 pos)
    			(str2lst (substr str (+ (strlen sep) pos 1)) sep)
    		)
    		(list str)
    	)
    )
    (defun c:readCSV2BLK ( / input f_open l_read key_sep str_sep l_data l_ini count count_coor mess_att key_data x_data y_data z_data l_rmv l_var inc gab_l last_y l_str l_mes blk_scl pt)
    	(setq
    		input (getfiled "Select a CSV file" "" "csv" 2)
    		f_open (open input "r")
    		l_read (read-line f_open)
    	)
    	(close f_open)
    	(initget "SPace Comma SEmicolon Tabulation")
    	(setq key_sep (getkword "\nSeparator [SPace/Comma/SEmicolon/Tabulation]? <SEmicolon>: "))
    	(cond
    		((eq key_sep "SPace") (setq str_sep " "))
    		((eq key_sep "Comma") (setq str_sep ","))
    		((eq key_sep "Tabulation") (setq str_sep "\t"))
    		(T (setq str_sep ";"))
    	)
    	(setq l_data (str2lst (vl-string-right-trim str_sep l_read) str_sep) count 0 count_coor 0 l_ini "Data Xlocation Ylocation Zlocation Ignore")
    	(foreach el l_data (princ " ")(princ el))
    	(initget "Yes No")
    	(if (eq (getkword "\nThis 1st line is it a header line which can constitute the attribute labels [Yes/No]? <No>: ") "Yes") (setq mess_att T) (setq mess_att nil))
    	(foreach el l_data
    		(initget 1 l_ini)
    		(setq key_data (getkword (strcat "\nElement " el " is [" (vl-list->string (subst 47 32 (vl-string->list l_ini))) "]?: ")))
    		(cond
    			((eq key_data "Xlocation") (setq x_data count_coor l_ini (vl-string-subst "" (strcat " " key_data) l_ini) l_rmv (cons (if l_rmv (+ count (length l_rmv)) count) l_rmv) count (1- count)))
    			((eq key_data "Ylocation") (setq y_data count_coor l_ini (vl-string-subst "" (strcat " " key_data) l_ini) l_rmv (cons (if l_rmv (+ count (length l_rmv)) count) l_rmv) count (1- count)))
    			((eq key_data "Zlocation") (setq z_data count_coor l_ini (vl-string-subst "" (strcat " " key_data) l_ini) l_rmv (cons (if l_rmv (+ count (length l_rmv)) count) l_rmv) count (1- count)))
    			((eq key_data "Ignore") (setq l_rmv (cons (if l_rmv (+ count (length l_rmv)) count) l_rmv) count (1- count)))
    			(T
    				(if mess_att
    					(progn
    						(set (read el) count)
    						(setq l_var (cons (read el) l_var) l_str (cons el l_str))
    					)
    					(progn
    						(set (read (strcat "DATA" (itoa count))) count)
    						(setq l_var (cons (read (strcat "DATA" (itoa count))) l_var))
    					)
    				)
    			)
    		)
    		(setq count (1+ count) count_coor (1+ count_coor))
    	)
    	(cond
    		((and (numberp x_data) (numberp y_data))
    			(setq count 0 inc (/ 5.0 3.0) gab_l (length l_var))
    			(mapcar
    				'(lambda (x y / ) 
    					(if (not (tblsearch "LAYER" x))
    						(entmake
    							(list
    								'(0 . "LAYER")
    								'(100 . "AcDbSymbolTableRecord")
    								'(100 . "AcDbLayerTableRecord")
    								(cons 2 x)
    								'(70 . 0)
    								(cons 62 y)
    								'(6 . "Continuous")
    								'(290 . 1)
    								'(370 . -3)
    							)
    						)
    					)
    				)
    				'("CSV2BLK" "CSV2BLK_x-y" "CSV2BLK_z" "CSV2BLK_ATT")
    				'(1 3 3 4)
    			)
    			(if (not (tblsearch "STYLE" "$CSV2BLK"))
    				(entmake
    					'(
    					(0 . "STYLE")
    					(5 . "40")
    					(100 . "AcDbSymbolTableRecord")
    					(100 . "AcDbTextStyleTableRecord")
    					(2 . "$CSV2BLK")
    					(70 . 0)
    					(40 . 0.0)
    					(41 . 1.0)
    					(50 . 0.0)
    					(71 . 0)
    					(42 . 2.5)
    					(3 . "arial.ttf")
    					(4 . "")
    					)
    				)
    			)
    			(if (not (tblsearch "BLOCK" "CSV2BLK"))
    				(progn
    					(entmake
    						'((0 . "BLOCK") (2 . "CSV2BLK") (70 . 2) (8 . "0") (62 . 0) (6 . "ByBlock") (370 . -2) (10 0.0 0.0 0.0))
    					)
    					(setq last_y 0.0 count -1 l_str (if mess_att l_str nil) l_mes nil)
    					(mapcar
    						'(lambda (tag mes / )
    							(entmake
    								(list
    									'(0 . "ATTDEF")
    									'(67 . 0)
    									'(8 . "0")
    									'(62 . 0)
    									'(6 . "ByBlock")
    									'(370 . -2)
    									(cons 10 (list 1.0 last_y 0.0))
    									'(40 . 1.0)
    									'(1 . "")
    									'(50 . 0.0)
    									'(41 . 1.0)
    									'(51 . 0.0)
    									'(7 . "$CSV2BLK")
    									'(210 0.0 0.0 1.0)
    									(cons 3 mes)
    									(cons 2 tag)
    									'(70 . 0)
    								)
    							)
    							(setq last_y (+ last_y (- (* inc 2))))
    						)
    						(append (list "ID-X" "ID-Y" "ID-Z") (reverse (if mess_att l_str (repeat (length l_var) (setq l_str (cons (strcat "DATA" (itoa (setq count (1+ count)))) l_str))))))
    						(append (list "Coordinate X: " "Coordinate Y: " "Coordinate Z: ") (repeat (length l_var) (setq l_mes (cons "Data: " l_mes))))
    					)
    					(entmake
    						'(
    						(0 . "POINT")
    						(67 . 0)
    						(8 . "0")
    						(62 . 0)
    						(6 . "ByBlock")
    						(370 . -2)
    						(10 0.0 0.0 0.0)
    						(210 0.0 0.0 1.0)
    						)
    					)
    					(entmake '((0 . "ENDBLK") (8 . "0") (62 . 0) (6 . "ByBlock") (370 . -2)))
    				)
    			)
    			(initget 2)
    			(if (not (setq blk_scl (getreal "\nBlock scale? <1>: "))) (setq blk_scl 1.0))
    			(setq f_open (open input "r"))
    			(while (setq l_read (read-line f_open))
    				(setq
    					l_data (str2lst (vl-string-right-trim str_sep l_read) str_sep)
    					pt (list (atof (nth x_data l_data)) (atof (nth y_data l_data)) (if z_data (atof (nth z_data l_data)) 0.0))
    				)
    				(if l_rmv
    					(foreach el l_rmv
    						(setq l_data
    							((lambda (n lst / i rtn)
    								(reverse
    									(progn
    										(setq i -1)
    										(foreach x lst
    											(if (/= n (setq i (1+ i)))
    												(setq rtn (cons x rtn))
    											)
    										)
    										rtn
    									)
    								)
    							)
    							el
    							l_data
    							)
    						)
    					)
    				)
    				(setq
    					count 0
    					l_var nil
    				)
    				(repeat gab_l
    					(set (read (strcat "DATA" (itoa count))) count)
    					(setq
    						l_var (cons (read (strcat "DATA" (itoa count))) l_var)
    						count (1+ count)
    					)
    				)
    				(foreach n l_var
    					(set n (nth (eval n) l_data))
    				)
    				(entmake
    					(append
    						'(
    							(0 . "INSERT")
    							(100 . "AcDbEntity")
    							(67 . 0)
    							(410 . "Model")
    							(8 . "CSV2BLK")
    							(100 . "AcDbBlockReference")
    							(66 . 1)
    							(2 . "CSV2BLK")
    						)
    						(list
    							(cons 41 (* 1.0 blk_scl))
    							(cons 42 (* 1.0 blk_scl))
    							(cons 43 (* 1.0 blk_scl))
    						)
    						'(
    							(50 . 0.0)
    							(70 . 0)
    							(71 . 0)
    							(44 . 0.0)
    							(45 . 0.0)
    						)
    						(list (cons 10 pt) '(210 0.0 0.0 1.0))
    					)
    				)
    				(setq last_y (+ (cadr pt) (* blk_scl (+ inc))) count 0 l_str (if mess_att l_str nil) l_mes nil)
    				(mapcar
    					'(lambda (val tag lay / )
    						(entmake
    							(append
    								(list
    									'(0 . "ATTRIB")
    									'(100 . "AcDbEntity")
    									'(67 . 0)
    									'(410 . "Model")
    									'(8 . "CSV2BLK_x-y")
    									'(100 . "AcDbText")
    								)
    								(list
    									(cons 8 lay)
    									(cons 10 (list (+ (car pt) (* blk_scl 1.0)) (setq last_y (+ last_y (* blk_scl (- inc)))) (caddr pt)))
    									(cons 1 val)
    									(cons 40 (* 1.0 blk_scl))
    								)
    								(list
    									'(50 . 0.0)
    									'(41 . 1.0)
    									'(51 . 0.0)
    									'(7 . "$CSV2BLK")
    									'(71 . 0)
    									'(72 . 0)
    									'(11 0.0 0.0 0.0)
    									'(210 0.0 0.0 1.0)
    									'(100 . "AcDbAttribute")
    								)
    								(list (cons 2 tag))
    								(list
    									'(70 . 0)
    									'(73 . 0)
    									'(74 . 0)
    								)
    							)
    						)
    					)
    					(append (list (rtos (car pt) 2) (rtos (cadr pt) 2) (rtos (caddr pt) 2)) (mapcar 'eval (reverse l_var)))
    					(append (list "ID-X" "ID-Y" "ID-Z") (reverse (if mess_att l_str (repeat (length l_var) (setq l_str (cons (strcat "DATA" (itoa (setq count (1+ count)))) l_str))))))
    					(append (list "CSV2BLK_x-y" "CSV2BLK_x-y" "CSV2BLK_z") (repeat (length l_var) (setq l_mes (cons "CSV2BLK_ATT" l_mes))))
    				)
    				(entmake '((0 . "SEQEND") (8 . "CSV2BLK") (62 . 0) (6 . "ByBlock") (370 . -2)))
    			)
    			(close f_open)
    		)
    		(T (princ "\nNo coordinates introduced, implementation impossible!"))
    	)
    	(mapcar '(lambda (x) (eval (set x nil))) l_var)
    	(prin1)
    )

  3. #3
    Login to Give a bone
    0

    Default Re: block insertion with layer definition and attributes with name values from csv

    Hey Bruno, thanks for that, looking at the length of this script it was well beyond my abilities.

    What I'm getting out of it is a block called "CSV2BLK" in a layer called "CSV2BLK" that doesn't seem to have any dimensions on screen.

    I already have a block "DIAG_CROSS" that is called on in the rest of my LISPS so I need to insert those blocks at the coordinates from the csv.

    In the csv I posted all the blocks should be written to the layer "ELCTRIC PIPE QLA", I tried generating the layer "ELECTRIC PIPE QLA" and then running the lsp but that didn't make any difference the blocks still wrote into the layer CSV2BLK.

    TY

  4. #4
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    560
    Login to Give a bone
    0

    Default Re: block insertion with layer definition and attributes with name values from csv

    It would make sense to have a look up lisp that matches the code to a layer, this is pretty std for civil software like CIV3d or others where you have a library of codes and properties to match. For me my template has 200+ codes. Again use say a file of codes,layers read it in and make a list.

    Code:
    Name,Layer
    001*,SU_PSM
    002*,SU_BENCHMARK
    003*,SU_TPEG
    004*,SU_STN
    005*,SU_SURVEY_MARK_GEN
    006*,SU_PHOTO_CONTROL
    007*,SU_CHECK
    008*,SU_TRIG_STATION
    012*,SU_REF_MARK
    013*,SU_REF_MARK
    014*,SU_REF_MARK
    016*,SU_DUMPY_PEG
    017*,SU_NAIL
    018*,SU_SPIKE
    027*,SU_ENG_PEG
    100*,DR_P100

  5. #5
    Login to Give a bone
    0

    Default Re: block insertion with layer definition and attributes with name values from csv

    yeah, when I started encountering the problem I realised that the data format from Leica captivate was going to be a headache, so I created an extra 'fixed attribute' for each code that I called "LAYER" and that fixed layer name is the last cell in each line. So I was thinking that somewhere in the LISP it would set the layer for the insertion based on the last cell value for each line.

Similar Threads

  1. Replace existing block definition with new definition of the same name
    By Wish List System in forum AutoCAD Wish List
    Replies: 1
    Last Post: 2016-10-31, 07:37 PM
  2. Replies: 0
    Last Post: 2012-09-13, 03:26 PM
  3. Replies: 2
    Last Post: 2008-05-09, 03:27 PM
  4. Replies: 21
    Last Post: 2007-03-20, 02:03 PM
  5. Extract Dynamic Block Attributes, values change as Block changes
    By dave.buckberry in forum Dynamic Blocks - Technical
    Replies: 11
    Last Post: 2006-09-05, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •