BLOCKREPLACE lisp w/ IF/THEN statements w/ NO dialog popup...

BLOCKREPLACE lisp w/ IF/THEN statements w/ NO dialog popup...

Anonymous
Not applicable
1,936 Views
16 Replies
Message 1 of 17

BLOCKREPLACE lisp w/ IF/THEN statements w/ NO dialog popup...

Anonymous
Not applicable

I need someone who is willing to help create a BLOCKREPLACE lisp routine.  I have about 1000 drawings which have multiple versions of blocks which I need to consolidate.  These were exports from Microstation and we are doing cleanup.

I need to be able to hard code the exact block name TO/FROM so I can run these in a batch script overnight/over weekend. 

 

Here is an example of the repeated block name definition which needs to be consolidated to just BREAK.   There are several more types like this which I need to apply this to.

 

BREAK_1
BREAK_10
BREAK_11
BREAK_12
BREAK_13
BREAK_14
BREAK_15
BREAK_16
BREAK_2
BREAK_3
BREAK_4
BREAK_5
BREAK_6
BREAK_7
BREAK_8
BREAK_9

 

Thanks in Advance for any/all help from the community...

0 Likes
Accepted solutions (1)
1,937 Views
16 Replies
Replies (16)
Message 2 of 17

ВeekeeCZ
Consultant
Consultant

Perhaps something like this.

 

(defun ScriptBlokReplacement (old newpath / )  
  (princ
    (if (tblsearch "BLOCK" old)
      (progn
        (command "._insert" (strcat old "=" newpath) nil)
        (strcat "Block '" old "' successfully replaced."))
      (strcat "Block '" old "' didn't found.")))
  (princ)
  )

;; usage
 ;(ScriptBlokReplacement "block1" "c:/Users/***/Desktop/_Download/block1.dwg")
; Note the FORWARDSLASH

(defun c:ReplaceAllMyBlocks nil
  (ScriptBlokReplacement "block1" "c:/Users/***/Desktop/_Download/block1.dwg")
  (ScriptBlokReplacement "block2" "c:/Users/***/Desktop/_Download/block2.dwg")
  )

 

Then you'll need ScriptPro HERE or THIS Lee's tool to run it thru all your drawings. Good luck.

0 Likes
Message 3 of 17

dbhunia
Advisor
Advisor

Hi,

 

Try this........(lightly tested, if you Enter Block Name with Replace as "BREAK" it will Replace all Blocks Named as "BREAK******" of all drawings within a folder..... with Block name "BREAK"....)

 

First put all the drawings in a single folder & run the code it will ask to select the Folder Containing the drawings you just select the folder & move forward......Code will work itself on all the drawings in side that folder.....

 

But before running the Lisp make sure about two things-

       1. There is only one Drawing is open (other than any drawing which one you want to edit), otherwise SDI Variable can not be reset.

       2. Wait for complete execution of the command, otherwise SDI Variable will not restored back.

 

(defun C:REP_BLK ( / sh folder folderobject result)

   (vl-load-com)
   (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application" ))
   (setq folder (vlax-invoke-method sh 'BrowseForFolder 0 "" 0 ))
   (vlax-release-object sh)
   (setq SDI_Val (getvar "SDI"))
   (setq LISPI_Val (getvar "LISPINIT"))
   (vl-cmdf "SDI" 1)
   (vl-cmdf "LISPINIT" 0)
   (if folder
      (progn
         (setq folderobject (vlax-get-property folder 'Self))
         (setq result (vlax-get-property FolderObject 'Path))
         (vlax-release-object folder)
         (vlax-release-object FolderObject)

	(setq Files_Folder (vl-directory-files result "*.dwg"))
	(setq Blk_Name (getstring "\nEnter Block Name With Replace: "))
	(command "save" (strcat (getvar "dwgprefix") (getvar "dwgname")) "Y")
	(setq n 0)

	(while (< n (length Files_Folder))

	(command "fileopen" (strcat result "\\" (nth n Files_Folder)))
	(setq ss (ssget "_X" (list(cons 2 (strcat Blk_Name "*")))))
	(cond ((/= ss nil)
		(progn
		    (repeat (setq NO (sslength ss))
			      (setq Data (entget (ssname ss (setq NO (1- NO)))))
			      (entmod (subst (cons 2 Blk_Name) (assoc 2 Data) Data))
		    ); repeat
		)
      	       )
	)
	(vl-cmdf "save" (strcat result "\\" (nth n Files_Folder)))
	(setq n (+ 1 n))
        )
     )
   )
   (vl-cmdf "SDI" SDI_Val)
   (vl-cmdf "LISPINIT" LISPI_Val)
)

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 4 of 17

Anonymous
Not applicable
Okay...

Can you send me the actual file?
0 Likes
Message 5 of 17

Anonymous
Not applicable
Can you attach or send the actual file?
0 Likes
Message 6 of 17

ВeekeeCZ
Consultant
Consultant

Copy-paste the whole thing, open Notepad and save it as MyLisp.lsp

0 Likes
Message 7 of 17

Anonymous
Not applicable

This one redefines the block, but what I am looking for is the BLOCKREPLACE functionality so the block definitions go away totally.  This function works, I just need it wrapped up in a lisp routine.

0 Likes
Message 8 of 17

dbhunia
Advisor
Advisor

Hi

 

I am getting confused with your first post and last post.........

 

Do you want something like this.........(check C:BRE).....

 

https://www.cadtutor.net/forum/topic/21571-replace-selected-block-or-blocks-with-another-block/

 

Or elaborate with example & attached Sample Drawings......


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 9 of 17

Anonymous
Not applicable

This is the command which I am currently using... one at a time.

 

Command: BLOCKREPLACE
Select the block to be replaced or [?/= (select object)]: ARHD_1
Select a block to replace ARHD_1 or [?/= (select object)]: ARHD
Purge unreferenced items when finished? <Y>:

 

Command: BLOCKREPLACE
Select the block to be replaced or [?/= (select object)]: ARHD_3
Select a block to replace ARHD_1 or [?/= (select object)]: ARHD
Purge unreferenced items when finished? <Y>:

 

The Block Definition is totally gone and all instances of that ARHD_1 are now ARHD...

 

block-definition.jpg

 

When I use this command the "old" block reference name is purged out and no longer active in the drawing.  What I am looking for is a way to use this command and have it do all of the them.  Some drawings may have _3 variances and some may have _30 variances.  I need some way of handling those that it will search/find all variances of a named block such as ARHD within the drawing, run the BLOCKREPLACE and keep going.  I would also like to define a list of BLOCKS for it to search for these variables such as... these are just a few.

ARHD

BREAK

PTR

PV

SPECBK

SPIDSL

block-definition-1.jpg

 

 

0 Likes
Message 10 of 17

ВeekeeCZ
Consultant
Consultant

Yeah, I see what you need. I even think that I could have something like that already, because I'm sometimes facing the same issue. You basically need merge all the clones block_* into the main one block. I'll look into that tomorrow. If I don't find anything I would write something, if @dbhunia won't  take care of it in between.

0 Likes
Message 11 of 17

Anonymous
Not applicable

Yes...  Thank you so much!!!  Here is a list of some of the main ones which have been reoccurring in drawings... I am sure there will be more as I dig thru them.


ARHD
ARHD_1
ARHD_2
ARHD_3

BREAK
BREAK_1
BREAK_10
BREAK_11
BREAK_12
BREAK_13
BREAK_14
BREAK_15
BREAK_16
BREAK_2
BREAK_3
BREAK_4
BREAK_5
BREAK_6
BREAK_7
BREAK_8
BREAK_9

BV
BV_1
BV_2

FCYOS
FCYOS_1

IF
IF_1

IVCIR
IVCIR_1

MM
MM_1

N2
N2_1

PHCF
PHCF_1

PHCM
PHCM_1

PI
PI_1

PINST
PINST_1
PINST_2
PINST_3
PINST_4
PINST_5
PINST_6
PINST_7

PLV
PLV_1
PLV_2
PLV_3

PN
PN_1
PN_2


PTR_1
PTR_2
PTR_3
PTR_4

PV
PV_1
PV_2

PVCL
PVCL_1

RPAR
RPAR_1

SPECBK
SPECBK_1
SPECBK_2
SPECBK_3
SPECBK_4

SPID
SPID_1
SPID_2

SPIDSL
SPIDSL_1
SPIDSL_2
SPIDSL_3
SPIDSL_4
SPIDSL_5
SPIDSL_6
SPIDSL_7
SPIDSL_8
SPIDSL_9

0 Likes
Message 12 of 17

ВeekeeCZ
Consultant
Consultant

Just thinking about that not being in the office... Are the clones really all the same or are some with rotated geometry? you know... block's rotation is 0, but geometry inside is rotated. Not sure if that could be the case.

0 Likes
Message 13 of 17

Anonymous
Not applicable

They are all the same...

block-definition-2.jpg

0 Likes
Message 14 of 17

dbhunia
Advisor
Advisor

Try this.....

 

(defun C:REP_BLK ( / sh folder folderobject result)

   (vl-load-com)

   (defun *error* (msg)
     (and f *AcadDoc* (vla-endundomark *AcadDoc*))
     (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
       (princ (strcat "\nError: " msg))
     )
   )

   (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application" ))
   (setq folder (vlax-invoke-method sh 'BrowseForFolder 0 "" 0 ))
   (vlax-release-object sh)
   (setq SDI_Val (getvar "SDI"))
   (setq LISPI_Val (getvar "LISPINIT"))
   (vl-cmdf "SDI" 1)
   (vl-cmdf "LISPINIT" 0)
   (if folder
      (progn
         (setq folderobject (vlax-get-property folder 'Self))
         (setq result (vlax-get-property FolderObject 'Path))
         (vlax-release-object folder)
         (vlax-release-object FolderObject)

	(setq Files_Folder (vl-directory-files result "*.dwg"))
	(setq Blk_Name (getstring "\nEnter Block Name to Replace: "))
	(command "save" (strcat (getvar "dwgprefix") (getvar "dwgname")) "Y")
	(setq n 0)

	(while (< n (length Files_Folder))

	(command "fileopen" (strcat result "\\" (nth n Files_Folder)))

	(if (/= (tblsearch "BLOCK" Blk_Name) nil)
	    (progn
		(setq ss (ssget "_X" (list(cons 2 (strcat Blk_Name "*")))))
		(command "_.insert" Blk_Name "0,0,0" "" "" "")
		(setq blk (vlax-ename->vla-object (setq IB (entlast))))

	        (setq f (not (vla-startundomark
	                     (cond (*AcadDoc*)
	                           ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
	                     )
	                   )
	              )
	        )
	        (vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*))
	          (setq temp (vla-copy blk))
	          (mapcar (function (lambda (p)
	                            (vl-catch-all-apply
	                              (function vlax-put-property)
	                              (list temp p (vlax-get-property x p))
	                            )
	                          )
	                )
	                '(Insertionpoint Rotation XEffectiveScaleFactor YEffectiveScaleFactor
	                  ZEffectiveScaleFactor
	                 )
	          )
	          (vla-delete x)
	        )
	        (vla-delete ss)
	        (*error* nil)
	        (command "_.erase" IB "")
	    )
	)
	(vl-cmdf "purge" "B" "" "N")
	(vl-cmdf "save" (strcat result "\\" (nth n Files_Folder)))
	(setq n (+ 1 n))
        )
     )
   )
   (vl-cmdf "SDI" SDI_Val)
   (vl-cmdf "LISPINIT" LISPI_Val)
)

 

For more detail check......

 

 https://www.cadtutor.net/forum/topic/21571-replace-selected-block-or-blocks-with-another-block/


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 15 of 17

ВeekeeCZ
Consultant
Consultant
Accepted solution

OK, slightly tested, this should work. It requires ET. 

And yes, in my case, I do have some blocks with a rotated geometry.

 

Usage:

As a user, run BMergeClonesToRoot, then list the roots delimited by a comma: arhd,bf,blkins,break... not case-sensitive.

Within a script, use this form: (BMergeClonesToRoot "arhd,bf,blkins,break")

 

Ok, hope this helps you too.

Message 16 of 17

dbhunia
Advisor
Advisor

Hi,

 

Try this ........ (this use the "BLOCKREPLACE" command using script file over all drawings within a folder in one go...)

 

First it asks for "Enter Block Name to be Replace (Few Matching Word): " (Let your input is ARHD or BREAK i.e. only one)

Then it asks for "Enter Block Name to Replace:(Let your input is ARHD

 

Then it finally replace all the Blocks named like "ARHD**** or BREAK******" with the Block "ARHD" ........

 

Actually I am unable to understand is this the thing what are you looking for?.........First check it........

 

If this is your requirement..........

 

Then it is possible to do that you only feed Blocks name like this way ARHD,BREAK,PTR,PV......... then code will find all the Blocks named like "ARHD**** , BREAK******,PTR*****, PV******........ and will replace them all with the Blocks ARHD, BREAK, PTR, PV......... respectively......

 

Another thing I used the path "D:/REPB.scr" to save the script file inside the code, if you do not have D-Drive then change the path into a write accessible folder.......

 

This is lightly tested in AutoCAD 2007 hopefully will work in upper version.....

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 17 of 17

Anonymous
Not applicable

Hello

I have a new twist on this problem...

 

I have exported ALL the blocks from a drawing into folder c:\dwgs and then fixed each one.  Now I want to reinsert those fixed blocks back into the drawing and REDEFINE them all  at the same time.  Any ideas on a routine that would looks at this folder and use just those items?  Not looking to consolidate just one-for-one replace/redefine.

 

 

0 Likes