Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Member
raulvaldemar
Posts: 3
Registered: ‎08-29-2007
Message 1 of 1 (392 Views)

blockreplace or block replace multiple blocks at one time

392 Views, 0 Replies
10-08-2012 12:41 PM

The following is a lisp that I wrote that replaces all the blocks within your drawing to the block you choose.  The lisp is different than the express command blockreplace because  one can replace multiple different blocks at one time with a particular block of your chosing.  In addition, the replaced blocks will have the same layer of the block you wanted to replace the others with.   Since I couldn't find a lisp like this in the forums, here's my small contribution:

 

(defun C:CHANGEBLOCKS(/ printDebug ssAll ssAllTotal ss ssTotal ssCount ssCount2 ssTotal ssMainBlock ssMainBlock entListMainBlock ssBlocks entListBlock entListBlock2 entElementNewBlock entElementOldBlock entElementOldBlock2 oldBlockName)
  ;PROGRAM CHANGES MULTIPLE BLOCKS
  ;WRITTEN BY --- RAUL BENITEZ

  ;Command reference
  ;subst - returns a list with a new item substituted for every occurrence of an old item
  ;quote (or ') - easy way to make the string into a list.
  ;cdr - returns a list containing all but the first element of the specified list
  ;assoc - searches an association list for an element and returns that association list entry
  ;entmod - modifies the definition data of an object (entity)
  ;cons - adds an element to the beginning of a list, or constructs a dotted list
  
  (vl-load-com)
  (setvar "CMDECHO" 0)
  (setq printDebug 0)

  (setq ss nil) ;set ss variable to nil
  (princ "\n(1). Pick the blocks you want to replace: ")
  (setq ss (ssget))               ;Gets selection set
  (setq ssTotal (sslength ss))    ;Gets the total number of entities within the selection
  
  (command "PICKADD" "0")         ;Changes the pickadd variable so the user can only select one object at a time
  
  ;Obtain the block you want to replace the others with
  (princ "\n(2). Pick the block you want to replace the others with: ") 
  (setq ssMainBlock (ssget))      ;Create selection set for the main block you want to replace the others with  
  (setq entListMainBlock (entget (ssname ssMainBlock 0)))    ;Obtain entity list (group codes) containing definition data of the block
  (setq entElementNewBlock (assoc 2 entListMainBlock))       ;Searches the block entity list and returns the element associate with 2 (the block name)
  
  (if (= printDebug 1)
  	  (progn
	  (princ "\nEntity list of the block you want to replace others with")
	  (princ "\n-----------------------------------------------------------\n")
	  (princ entListMainBlock)
	  )
  )
  
  ;Cycle thru all the blocks replacing each block one at a time
  (setq ssCount 0) 
  (while (< ssCount ssTotal)
              
	  (setq entListBlock (entget (ssname ss ssCount)))   ;Obtain entity list (group codes) from each of the entities (blocks) in the selection set
	  (setq entElementOldBlock (assoc 2 entListBlock))   ;Searches the block entity list and returns the element associate with 2 (the block name)
          (setq oldBlockName (cdr entElementOldBlock))       ;Returns a list containing all but the first element of the specified list, therefore only the block name

          (setq ssAll nil)				                          ;Set the variable to nil
	  (setq ssAll (ssget "X" (list '(0 . "INSERT") (cons 2 oldBlockName))))   ;Obtain the entire selection of all the blocks throughout the drawing
    	  (setq ssAllTotal (sslength ssAll))                                      ;Gets the total number of entities within the selection
	  (if (= printDebug 1)
		  (progn
	    	  (princ "\n\n\nCount of all the old blocks in the drawing: ")
		  (princ ssAllTotal)
	          )
 	  )
    
          (setq ssCount2 0) 
  	  (while (< ssCount2 ssAllTotal)
		  (setq entListBlock2 (entget (ssname ssAll ssCount2)))   ;Obtain entity list (group codes) from each of the entities (blocks) in the selection set
		  (setq entElementOldBlock2 (assoc 2 entListBlock2))      ;Searches the block entity list and returns the element associate with 2 (the block name)
	          
	          (entmod (subst entElementNewBlock entElementOldBlock2 entListBlock2))   ;Substitute the new block entity name for the old block entity name within the old block entity list
	          (command "MATCHPROP" (ssname ssMainBlock 0) (ssname ssAll ssCount2) "" )  ;Match the properties of the old block(s) to the new block
	          (setq ssCount2 (+ ssCount2 1))  ;Loop counter
	  )
        
          ;Print out information
          (if (= printDebug 1)
		  (progn
		  (princ "\n\n\nssCount: ")
		  (princ ssCount)
		  (princ "\nEntity list of the block that is going to be replaced")
	  	  (princ "\n-----------------------------------------------------------\n")
	          (princ entListBlock)
		  (princ "\n\nName entity of the block that is going to replaced: ")
	          (princ entElementOldBlock)
		  (princ "\nName entity of the new block: ")
	          (princ entElementNewBlock)
		  (princ "\n\n")
		  )
	  )
    
    	  (setq ssCount (+ ssCount 1))  ;Loop counter
  )  

  (command "PICKADD" "2")         ;Changes the pickadd variable so the user select multiple objects at a time
  
  (setvar "CMDECHO" 1)

  (print "Changed multiple blocks.") 
  (princ)  

)

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.