Author Topic: Swap block that resides in nest  (Read 5476 times)

0 Members and 1 Guest are viewing this topic.

ArgV

  • Guest
Swap block that resides in nest
« on: August 10, 2009, 12:19:48 AM »
Hello all.

I first did a search to find something like what I want. I found:

1. A routine that replaces all blocks in the drawing from blocks in an external directory.
2. A routine that replaces blocks from a drawing opened by ObjectDBX into the current drawing.

But, my problem is some (person) decided to make a hundred different names of the same block. The block definition is exactly the same, just multiple names (i.e. drillbit5, drillbit23, drillbit50, etc). I want to either A) Swap out each one with the normal block definition with the normal name..
or B) Just rename all of them to the normal name.

haven't had any success getting rename to work since there is more than one.
The "RUB" - These blocks are contained within another block. Therefore when I've tried doing a vla-insertblock method, it inserts them in the wrong area, and not inside the block.

Thanks.

-ArgV

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Swap block that resides in nest
« Reply #1 on: August 10, 2009, 01:15:18 AM »
Do the blocks have attributes ?

ExpressTools has a function called BLOCKREPLACE ... SHOULD work :)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.


ArgV

  • Guest
Re: Swap block that resides in nest
« Reply #3 on: August 11, 2009, 03:48:24 AM »
Do the blocks have attributes ?

ExpressTools has a function called BLOCKREPLACE ... SHOULD work :)



Well, that works great for a few bad blocks, but we have thousands. Standards? whats that? ;) 

ArgV

  • Guest
Re: Swap block that resides in nest
« Reply #4 on: August 11, 2009, 03:56:23 AM »
I assume you tried these routines:
http://www.theswamp.org/index.php?topic=15234.msg184871#msg184871
http://www.theswamp.org/index.php?topic=13283.0
http://www.theswamp.org/index.php?topic=2272.0
http://www.theswamp.org/index.php?topic=28965.0

And this method:
http://www.theswamp.org/index.php?topic=22479.0


Code to look at modifying:
http://www.theswamp.org/index.php?topic=17085.msg206807#msg206807
http://www.theswamp.org/index.php?topic=22999.0

I have looked at/tried I think all the ones above. None of which work within nested blocks as I recall. This is basically for a bunch of huge master drawings, and the block(s) in question reside inside each of those blocks. I wish I could simply re-name them, but the necessary limitation of only having one block name inside a drawing makes that difficult or impossible.

Why would someone put several different names to blocks that are all the same? Am I crazy or does that not make any sense?

Well, I'll keep pecking away at it. I'm currently modifying one of the ones you specified for modifying. It almost seems like I have to explode the blocks, swap the bit, and then re-define the block again. ?

thanks

-ArgV

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Swap block that resides in nest
« Reply #5 on: August 11, 2009, 05:02:16 AM »
Hi,

May be this one ?

Code: [Select]
;; MBLOCKREPLACE (gile)
;; Replace one or more blocks by another one

(defun c:MBlockReplace (/ *error* acdoc blst target source)
  (defun *error* (msg)
    (vla-EndUndoMark acdoc)
    (princ)
  )
  (vl-load-com)
  (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vlax-for b (vla-get-Blocks acdoc)
    (if (and
          (= (vla-get-IsLayout b) :vlax-false)
          (= (vla-get-IsXref b) :vlax-false)
          (not (wcmatch (setq name (vla-get-Name b)) "`**,*|*"))
        )
      (setq blst (cons (vla-get-Name b) blst))
    )
  )
  (setq target (ListBox "MBlockReplace"
                        "Select the block(s) to be replaced"
                        (mapcar 'cons blst blst)
                        2
               )
  )
  (setq source (ListBox "MBlockReplace"
                        "Select a block to replace block(s)"
                        (mapcar 'cons blst blst)
                        1
               )
  )
  (vla-StartUndoMark acdoc)
  (vlax-for blk
            (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
    (vlax-for obj blk
      (if (and
            (= (vla-get-ObjectName obj) "AcDbBlockReference")
            (vl-position (vla-get-Name obj) target)
          )
        (vla-put-Name obj source)
      )
    )
  )
  (vla-regen (vla-get-ActiveDocument (vlax-get-acad-object)) acAllViewports)
  (vla-EndUndoMark acdoc)
  (princ)
)

;; ListBox (gile)
;; DialogBox which allows one more choices in a list
;;
;; Arguments
;; title : The dialog title (STR)
;; msg ; message (STR), "" or nil for none
;; keylab : an association list which type is ((key1 . label1) (key2 . label2) ...)
;; flag : 0 = popup list
;;        1 = single choice list
;;        2 = multipe choices list
;;
;; Return : the option key (flag = 0 or 1) or the option keys list (flag = 2)
;;
;; Example
;; (listbox "Layout" "Choos a layout" (mapcar 'cons (layoutlist) (layoutlist)) 1)

(defun ListBox (title msg keylab flag / tmp file dcl_id choice)
  (setq tmp  (vl-filename-mktemp "tmp.dcl")
file (open tmp "w")
  )
  (write-line
    (strcat "ListBox:dialog{label=\"" title "\";")
    file
  )
  (if (and msg (/= msg ""))
    (write-line (strcat ":text{label=\"" msg "\";}") file)
  )
  (write-line
    (cond
      ((= 0 flag) "spacer;:popup_list{key=\"lst\";")
      ((= 1 flag) "spacer;:list_box{key=\"lst\";")
      (T "spacer;:list_box{key=\"lst\";multiple_select=true;")
    )
    file
  )
  (write-line "}spacer;ok_cancel;}" file)
  (close file)
  (setq dcl_id (load_dialog tmp))
  (if (not (new_dialog "ListBox" dcl_id))
    (exit)
  )
  (start_list "lst")
  (mapcar 'add_list (mapcar 'cdr keylab))
  (end_list)
  (action_tile
    "accept"
    "(or (= (get_tile \"lst\") \"\")
    (if (= 2 flag) (progn
    (foreach n (str2lst (get_tile \"lst\") \" \")
    (setq choice (cons (nth (atoi n) (mapcar 'car keylab)) choice)))
    (setq choice (reverse choice)))
    (setq choice (nth (atoi (get_tile \"lst\")) (mapcar 'car keylab)))))
    (done_dialog)"
  )
  (start_dialog)
  (unload_dialog dcl_id)
  (vl-file-delete tmp)
  choice
)
« Last Edit: August 11, 2009, 11:25:11 AM by gile »
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Swap block that resides in nest
« Reply #6 on: August 11, 2009, 09:54:39 AM »
gile, I think you posted in the wrong thread. No?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Swap block that resides in nest
« Reply #7 on: August 11, 2009, 09:58:39 AM »
ArgV,
I'm supprised the methods don't work.
You do know that the newly replaced block will not display until a regen is done!
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Swap block that resides in nest
« Reply #8 on: August 11, 2009, 10:17:23 AM »
gile, I think you posted in the wrong thread. No?

The code I posted defines a MBLOCKREPLACE function, quite similar to the Express BLOCKREPLACE, except it allows to replace multiple blocks by one.
Insn't it the OP request ?
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Swap block that resides in nest
« Reply #9 on: August 11, 2009, 10:30:00 AM »
gile, I think you posted in the wrong thread. No?

The code I posted defines a MBLOCKREPLACE function, quite similar to the Express BLOCKREPLACE, except it allows to replace multiple blocks by one.
Insn't it the OP request ?

Running the code, it creates a DCL List Box, and using your sample call:
Code: [Select]
(listbox "Layout" "Choos a layout" (mapcar 'cons (layoutlist) (layoutlist)) 1)It lets the user select from the drawing layout names.


I thought it belonged here: http://www.theswamp.org/index.php?topic=29710.0


Oh I see the other routine, So sorry, I'm still waking up here. That's my excuse.   :oops:
« Last Edit: August 11, 2009, 10:44:21 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Swap block that resides in nest
« Reply #10 on: August 11, 2009, 10:40:57 AM »
Maybe something like this:

Code: [Select]
(defun c:rnblk (/ bl tdef bn fe fd nn)

              ;"old_name"  . "new_name"
   (setq bl '(("Drillbit5" . "Drillbit")
              ("Drillbit3" . "Drillbit")))

   (while (setq tdef (tblnext "BLOCK" (not tdef)))
          (setq bn (cdr (assoc 2 tdef))
                fe (cdr (assoc -2 bn)))
          (princ (strcat "\n" bn))
          (while fe
              (and (setq fd (entget fe))
                   (= "INSERT" (cdr (assoc 0 fd)))
                   (assoc bn bl)
                   (setq nn (cdr (assoc bn bl)))
                   (tblsearch "BLOCK" nn)
                   (entmod (substr (cons 2 nn) (assoc 2 fd) fd))))
          (setq fe (entnext fe)))
   (command "REGENALL")
   (prin1))

-David
R12 Dos - A2K

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Swap block that resides in nest
« Reply #11 on: August 11, 2009, 10:42:53 AM »
CAB,

ListBox is a sub routine used twice in c:MBlockReplace (downer) for user inputs:  to select the block(s) to be replaced and to select the block which will replace them.
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Swap block that resides in nest
« Reply #12 on: August 11, 2009, 10:44:56 AM »
Oh I see the other routine, So sorry, I'm still waking up here. That's my excuse.   :oops:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Swap block that resides in nest
« Reply #13 on: August 11, 2009, 11:26:01 AM »
No matter, I put the main routine on the top to avoid confusion.
Speaking English as a French Frog

ArgV

  • Guest
Re: Swap block that resides in nest
« Reply #14 on: August 11, 2009, 09:26:49 PM »
ArgV,
I'm supprised the methods don't work.
You do know that the newly replaced block will not display until a regen is done!


I do know that. I found that out the hard way a long time ago, so I've added a vla-regen for all viewports. :) I'm probably doing something wrong, but I'm going to take all these new routines and try them. Who knows, maybe I'll learn something new! :)

thank you!

-ArgV