Convert Attributes to Texts and Undynamic Blocks

Convert Attributes to Texts and Undynamic Blocks

lzedCB
Advocate Advocate
658 Views
7 Replies
Message 1 of 8

Convert Attributes to Texts and Undynamic Blocks

lzedCB
Advocate
Advocate

Hello folks,

 

I've found this LISP in forum to undynamic all blocks inside a drawing and to convert them to normall/regular blocks, without renaming the original block. I would like to add an extra step to this LISP, converting the atrributes inside the blocks to normall dtexts aswell. Someone could help with this task adding this extra step to this amazing LISP?

 

Thanks. 

0 Likes
Accepted solutions (1)
659 Views
7 Replies
Replies (7)
Message 2 of 8

paullimapa
Mentor
Mentor

fount att2txt.lsp on-line but will require you to select the blocks that contain attributes to convert to text:

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-take-an-attribute-out-of-blo...

download latest lisp from message 16 of 19


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 8

komondormrex
Mentor
Mentor
Accepted solution

this is not a compilation but rather another quick-written lisp, which kills dynamicy in every dynamic block and  attributes to text in every block present in a dwg database. however original attributes will stay in block definition till specially written to kill them too.

 

;**************************************************************************************************************************************

;	komondormrex, mar 2023

;**************************************************************************************************************************************

(defun c:Kill_Dynamicy_Attributes (/
									 block_before_name block_after_name block_collection layers_collection
									 attributes_list dtext_dxf block_layer true_color_object
									 original_block_insertion_point original_block_scale_XYZ_list
									 inserts_sset_object block_attributes_list
									 group_code group_data
									 xref_field xref_start xref_end xref camera_number
								  )
	(vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))
	(setq block_collection (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
		  layers_collection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	)
	(if (null block_index) (setq block_index 0))
	(if (null (vl-catch-all-error-p (vl-catch-all-apply 'vla-item (list (vla-get-selectionsets (vla-get-activedocument (vlax-get-acad-object))) "Inserts_Collection"))))
			(vla-delete (vla-item (vla-get-selectionsets (vla-get-activedocument (vlax-get-acad-object))) "Inserts_Collection"))
	)
	(setq inserts_sset_object (vla-add (vla-get-selectionsets (vla-get-activedocument (vlax-get-acad-object))) "Inserts_Collection")
		  group_code (vlax-make-safearray vlax-vbinteger '(0 . 0))
	)
	(vlax-safearray-put-element group_Code 0 0)
	(setq group_data (vlax-make-safearray vlax-vbvariant '(0 . 0)))
	(vlax-safearray-put-element group_data 0 "insert")
	(vla-selectonscreen inserts_sset_object group_code group_data)
	(vlax-map-collection inserts_sset_object
		'(lambda (object)
			(cond
				(
					(and
						 (= "AcDbBlockReference" (vla-get-objectname object))
;						 (/= "*" (substr (setq block_before_name (vla-get-effectivename object)) 1 1))
						 (setq block_before_name (vla-get-effectivename object))
					)
						(setq original_block_insertion_point (vlax-get object 'insertionpoint)
							  original_block_scale_XYZ_list (list (vla-get-xscalefactor object) (vla-get-yscalefactor object) (vla-get-zscalefactor object))
							  attribute_name_list (mapcar '(lambda (attribute) (vla-get-tagstring attribute))
											  		       (vlax-invoke object 'getattributes)
												  )
						)
						(if (minusp (vlax-get object 'isdynamicblock))
							(progn
								(vla-converttostaticblock object (setq block_after_name (strcat block_before_name "_" (itoa (setq block_index (1+ block_index))))))
							)
						)
						(if (and
								 (minusp (vlax-get object 'hasattributes))
								 (setq attributes_list (vlax-invoke object 'getattributes))
								 (setq block_attributes_list (mapcar '(lambda (attribute) (cons (vla-get-tagstring attribute) attribute)) attributes_list))
								 (null (setq block_attribute_definition_list nil))
								 (vlax-for every_block_object (vla-item block_collection (vla-get-name object))
									(if (= "AcDbAttributeDefinition" (vla-get-objectname every_block_object))
										(setq block_attribute_definition_list (append block_attribute_definition_list
																					 (list (cons (cdr (assoc 2 (entget (vlax-vla-object->ename every_block_object))))
																					 			  every_block_object
																						   )
																					 )
																			  )
										)
									)
								 )
								 (if (assoc "HYPERLINK" block_attributes_list)
										(setq xref_field (cdr (assoc 2 (entget (cdr (assoc 360 (dictsearch (cdr (assoc -1 (dictsearch (cdr (assoc 360 (entget (vlax-vla-object->ename (cdr (assoc "HYPERLINK" block_attributes_list)))))) "ACAD_FIELD"))) "TEXT"))))))
											  xref_start (vl-string-search "http" xref_field)
											  xref_end (vl-string-search "##" xref_field)
											  xref (substr xref_field (1+ xref_start) (- xref_end xref_start))
											  camera_number (vla-get-textstring (cdr (assoc "TXT" block_attributes_list)))
										)
										t
								 )
							)
								(progn
									(foreach attribute attributes_list
										(if (zerop (vlax-get (vla-item layers_collection (vla-get-layer attribute)) 'lock))
											(progn
												(if (zerop (vlax-get attribute 'invisible))
													(progn
														(setq dtext_dxf (vl-remove-if '(lambda (group) (member (car group) '(0 -1 330 5 280 2 70 74)))
																						(entget (vlax-vla-object->ename attribute))

																		)
														)
														(entmake (append '((0 . "TEXT")) dtext_dxf))
														(setq dtext_object (vlax-ename->vla-object (entlast)))
														(if (not (zerop (vla-get-alignment attribute)))
																(progn
																	(vla-put-alignment dtext_object (vla-get-alignment attribute))
																	(vla-put-textalignmentpoint dtext_object (vla-get-textalignmentpoint attribute))
																)
														)
														(setq dtext_in_block_object
																(vlax-invoke (vla-get-activedocument (vlax-get-acad-object))
																			'copyobjects
																			(list dtext_object)
																			(vla-item block_collection (vla-get-name object))
																)
														)
														(setq parent_attribute (cdr (assoc (cdr (assoc 2 (entget (vlax-vla-object->ename attribute)))) block_attribute_definition_list)))
														(if (not (zerop (vla-get-alignment parent_attribute)))
															(if (member "DIAM" attribute_name_list)
																(vla-put-textalignmentpoint (car dtext_in_block_object) (vla-get-textalignmentpoint parent_attribute))
																(vla-put-textalignmentpoint (car dtext_in_block_object) (vlax-3d-point (mapcar '/ (mapcar '- (vlax-get dtext_object 'textalignmentpoint) original_block_insertion_point) original_block_scale_XYZ_list)))
															)
															(if (member "DIAM" attribute_name_list)
																(vla-put-insertionpoint (car dtext_in_block_object) (vla-get-insertionpoint parent_attribute))
																(vla-put-insertionpoint (car dtext_in_block_object) (vlax-3d-point (mapcar '/ (mapcar '- (vlax-get dtext_object 'insertionpoint) original_block_insertion_point) original_block_scale_XYZ_list)))
															)
														)
														(vla-put-height (car dtext_in_block_object) (vla-get-height parent_attribute))
														(vla-put-rotation (car dtext_in_block_object) (vla-get-rotation parent_attribute))
														(vl-catch-all-apply 'vla-put-layer (list (car dtext_in_block_object) "0")) ;(vla-get-layer object)))
														(vla-put-truecolor (car dtext_in_block_object) (vla-get-truecolor parent_attribute))
														(vla-erase dtext_object)
													)
												)
												(vla-erase attribute)
											)
											(princ (strcat "\nSkip killing attribute on locked layer \"" (vla-get-layer attribute) "\""))
										)
									)
									(foreach assoc_attribute block_attribute_definition_list
										(vla-delete (cdr assoc_attribute))
									)
									(if (assoc "HYPERLINK" block_attributes_list)
											(entmod (append (entget (vlax-vla-object->ename object)) (list (list -3 (list "PE_URL"
																														   (cons 1000 xref)
																														   (cons 1002 "{")
																														   (cons 1000 camera_number)
																														   (cons 1002 "{")
																														   (cons 1071 1)
																														   (cons 1002 "}")
																														   (cons 1002 "}")
																													)
																											)
																									 )
													 )
											)
									)
								)
						)
				)
				(
					t
				)
		 	)
		)
	)
	(vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
	(princ)
)

;**************************************************************************************************************************************

 

 

0 Likes
Message 4 of 8

lzedCB
Advocate
Advocate

It almost did it my friend. All the blocks with attributes got messy after running the lisp. The attributes were spread all over the place, not keeping their original positions. Is it possible to keep the attributes that were converted to texts inside the static block after running the lisp?

0 Likes
Message 5 of 8

komondormrex
Mentor
Mentor

could you link a problem file with a couple of such blocks?

0 Likes
Message 6 of 8

lzedCB
Advocate
Advocate

For sure! Here it goes...

0 Likes
Message 7 of 8

komondormrex
Mentor
Mentor

well, autocad itself can't explode such complex dynamic blocks properly... can't see what may have been done with imho. but minor corrections may improve attribute->text alignment. 

0 Likes
Message 8 of 8

komondormrex
Mentor
Mentor

since your similarly named dynamic blocks have variable geometry they cannot be converted to static blocks with that one name, they rather must have different names, which adds a lot of definitions to the blocks table, but they will look as same as were as dynamic. lookups also will mess.

try updated lisp above.

0 Likes