Replace subroutine from directory folder

Replace subroutine from directory folder

Anonymous
Not applicable
555 Views
6 Replies
Message 1 of 7

Replace subroutine from directory folder

Anonymous
Not applicable

Ok so i have been using this routine bre.lsp and it has been amazing but i dont know how to write a routine for autocad 2004 so i wanted to know the best way to write a routine to replace objects on a draw from a folder or directory search. if this already exist that would be great, if not please let me know..thanks

0 Likes
556 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

The Cadalyst CAD Tips site has several things related to replacing Blocks [if that's what you're talking about -- it's not quite clear to me], some of which may do what you're after -- try a Search there.  Otherwise, can you post the routine, or a link to where it can be downloaded, so we can see exactly what it does, and a more detailed description of what you want it to do differently?

Kent Cooper, AIA
0 Likes
Message 3 of 7

Anonymous
Not applicable
is there a way that you know of where i can make a simple function to
replace any object in a drawing with any object in a folder...lets say i
want to select an item, type replace, then select the item from a list of
items on screen to replace it with
0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
is there a way that you know of where i can make a simple function to replace any object in a drawing with any object in a folder...

What kind(s) of object(s)?  If you're talking about Blocks, it shouldn't be too hard, and some of the routines in the Search in my previous Reply may be close to what you want.  But if you want to replace, for example, a Circle with an Ellipse, the Ellipse can't be "in a folder" except as the content of a drawing file, so it would need to come in by way of something like Inserting it as a Block [though it can be Exploded in the process, so it doesn't remain a Block].  And if you are talking only about Blocks, when you say "replace," do you mean you want to replace individual Block references with different ones, or you want to replace the Block definition so that all such Blocks in the drawing are replaced?  The more detail you can provide about exactly what you want to do, including the range of possible object types, how you would want the location of the replacement object to determined, etc., the better.

Kent Cooper, AIA
0 Likes
Message 5 of 7

Anonymous
Not applicable
the subroutine that allows me to select a block on the draw as a source and then replace all following selected blocks with the source that has been selected. now i have a directory of shapes and items setup as blocks but bre.lsp wont work with those because they are not already on the draw. what i want to do is create a button or command that can be pressed or typed after a block is selected and replace it (scale, size and direction) with any one of the items from a drop down list. below is the text of the routine that i have loaded into autocad so basically if i could figure out a way to either create a button that would do this or re write the routine below to allow item selection to be from a list of created items instead of just whats on the drawing then it would be perfect similar to the command "blockreplace" but instead of typing in a search bar what i want, all the items that i have are presented in a list . (defun c:BRE (/ *error* blk f ss temp) ;; Replace multiple instances of selected blocks (can be different) with selected block ;; Size and Rotation will be taken from original block and original will be deleted ;; Required subroutines: AT:GetSel ;; Alan J. Thompson, 02.09.10 ;; Found at: http://www.cadtutor.net/forum/showthread.php?48458-Replace-Selected-Block-Or-Blocks-With-Another-Blo... (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)) ) ) (if (and (AT:GetSel entsel "\nSelect replacement block: " (lambda (x / e) (if (and (eq "INSERT" (cdr (assoc 0 (setq e (entget (car x)))))) (/= 4 (logand (cdr (assoc 70 (tblsearch "BLOCK" (cdr (assoc 2 e))))) 4)) (/= 4 (logand (cdr (assoc 70 (entget (tblobjname "LAYER" (cdr (assoc 8 e)))))) 4)) ) (setq blk (vlax-ename->vla-object (car x))) ) ) ) (princ "\nSelect blocks to be repalced: ") (setq ss (ssget "_:L" '((0 . "INSERT")))) ) (progn (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 xScaleFactor YScaleFactor ZScaleFactor ) ) (vla-delete x) ) (vla-delete ss) (*error* nil) ) ) (princ) ) (defun AT:GetSel (meth msg fnc / ent good) ;; meth - selection method (entsel, nentsel, nentselp) ;; msg - message to display (nil for default) ;; fnc - optional function to apply to selected object ;; Ex: (AT:GetSel entsel "\nSelect arc: " (lambda (x) (eq (cdr (assoc 0 (entget (car x)))) "ARC"))) ;; Alan J. Thompson, 05.25.10 (setvar 'errno 0) (while (not good) (setq ent (meth (cond (msg) ("\nSelect object: ") ) ) ) (cond ((vl-consp ent) (setq good (cond ((or (not fnc) (fnc ent)) ent) ((prompt "\nInvalid object!")) ) ) ) ((eq (type ent) 'STR) (setq good ent)) ((setq good (eq 52 (getvar 'errno))) nil) ((eq 7 (getvar 'errno)) (setq good (prompt "\nMissed, try again."))) ) ) ) …
0 Likes
Message 6 of 7

Anonymous
Not applicable
so it would replace blocks on the draw, individual references and not globally from a list of blocks that i have already created that are stored on my computer
0 Likes
Message 7 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
so it would replace blocks on the draw, individual references and not globally from a list of blocks that i have already created that are stored on my computer

Here's a start, using two routines just for now.  If they work together for you, they can be combined into one routine.

 

If your Blocks are all drawing files in a single folder, the following will call up a File Dialog Box which will be the list from which to select one of them:

 

(vl-load-com); in case needed
(defun C:GBFD (/ fname dname); = Get Block from File Dialog-box (command "_.INSERT" (setq fname ; = File name -- will bring in as Block definition (getfiled "Select drawing:" "X:/Your/Folder/Location/" "dwg" 0);<--EDIT drive & file path ); setq ); command (command); cancel without actually Inserting one (setq dname (substr fname (+ (vl-string-position 92 fname 1 T) 2)); = Drawing name bname (substr dname 1 (- (strlen dname) 4)); = Block name [dname with ".dwg" removed] ); setq (setvar 'insname bname); set as default for Insert command ); defun (prompt "\nType GBFD to Get a Block from a File Dialog-box in YourFolder.");<--EDIT

Run the GBFD command defined by that.  It will import the selected drawing into the current drawing as a Block definition [but not actually Insert one], and will both store that Block name in the 'bname' variable and make it the current default Block name for the Insert command.

 

Then, run the BRS [= Block Replace by Selection] command from BlockReplace.lsp, available here.  The first time you run it, it will offer Insert's default Block name [the one you just picked from the File Dialog Box] -- accept that default, and select as many existing Blocks [of any name] as you want, and they will all be turned into that Block, keeping their Layers, Scale factors, Rotation angles, etc.  Thereafter, BRS will offer the Block name last used within itself, so if you bring in another using GBFD, you will [for now] need to type its name when the BRS command asks for a Block name.  That can be made automatic in a combination routine if this pair of routines works for you otherwise.

Kent Cooper, AIA
0 Likes