Author Topic: Replacing selected group of blocks...  (Read 4812 times)

0 Members and 1 Guest are viewing this topic.

sezbw

  • Guest
Replacing selected group of blocks...
« on: July 18, 2006, 03:36:42 AM »
I am searching for a way to replace a selected group of blocks with a block of another type.... :|

I have a schedule that has lots of different types of blocks in it.  I would really like to be able to manually select some blocks and have them be replaced by another type.  Not throughout the whole drawing, just those I select. 

All I could find in CAD is the Express menu command replace block with another block.  This doesn't suit cause it replaces one type with another throughout the drawing.

Can any guru out there point me in the right direction.

Thanks from a very grateful novice.  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Replacing selected group of blocks...
« Reply #1 on: July 18, 2006, 05:01:44 AM »
A couple of questions ..

Do the blocks have attritutes
.. in relation to the block instance being replaced ..
Will the insertion point be the same.
Will the Scale be the same.
Will the rotation be the same.
Will the layer be the same.

Is there any logic to the choice of name of the replacement block.
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Replacing selected group of blocks...
« Reply #2 on: July 18, 2006, 05:11:17 AM »
Something rough to play with ..



(DEFUN c:test (/           DBXBLOCKNAME
               DWGBASENAME ELIST
               INDEX       SSET
              )
  (PROMPT "\nSelect blocks to be replaced:")
  (SETQ sset         (SSGET '((0 . "INSERT")))
        DbxBlockName (GETFILED
                       "Select Replacement Block"
                       (GETVAR "DWGPREFIX")
                       "dwg"
                       0
                     )
  )
  (IF
    (NOT
      (TBLSEARCH "BLOCK"
                 (SETQ
                   dwgbaseName (VL-FILENAME-BASE
                                 DbxBlockName
                               )
                 )
      )
    )
     (PROGN (COMMAND "_INSERT" DbxBlockName)
            (COMMAND)
     )
  )
  (SETQ index 0)
  (REPEAT (SSLENGTH sset)
    (SETQ elist (ENTGET (SSNAME sset index))
          index (1+ index)
    )
    (ENTMOD (SUBST (CONS 2 dwgbaseName)
                   (ASSOC 2 elist)
                   elist
            )
    )
  )
  (PRINC)
)

« Last Edit: July 18, 2006, 05:26:41 AM by Kerry Brown »
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.

Arizona

  • Guest
Re: Replacing selected group of blocks...
« Reply #3 on: July 18, 2006, 06:16:48 AM »
Great response! What was that, all of 10 min?

hudster

  • Gator
  • Posts: 2848
Re: Replacing selected group of blocks...
« Reply #4 on: July 18, 2006, 07:14:52 AM »
cool lisp.

One improvement would be if you had the option to select a block already in the drawing to replace the first blocks.
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

sezbw

  • Guest
Re: Replacing selected group of blocks...
« Reply #5 on: July 20, 2006, 02:31:52 AM »
 :-D Thanks Kerry that is fantastic.  I has made the job so much simpler already.

Nice one!

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Replacing selected group of blocks...
« Reply #6 on: July 21, 2006, 12:05:12 PM »
I threw this together to copy a selected source block over another selection of blocks. Not the fastest routine but it works. :)

Code: [Select]
;;  Function to copy blocks from one point to another

(defun c:Replaceblocks
       (/      ent1   ent2   obj1   obj2   pt1
pt2    x      ss     index  keyw   rot
r2d    copyobj
       )
  (while
    (= ent1 nil)
     (setq ent1 (car (entsel "\n Select block to copy: ")))
     (if (= ent1 nil)
       (alert "\n You missed, try again...")
     )
  )
  (while
    (= ent2 nil)
     (setq ent2 (car (entsel "\n Select block you want to swap: ")))
     (if (= ent2 nil)
       (alert "\n You missed, try again...")
     )
  )
  (setq obj1 (vlax-ename->vla-object ent1)
pt1  (trans (vlax-get obj1 'insertionpoint) 0 1)
  )
  (setq obj2 (vlax-ename->vla-object ent2)
x    (vlax-get-property obj2 'Name)
  )
  (if
    (and
      (= (vla-get-ObjectName Obj1) "AcDbBlockReference")
      (not (vlax-property-available-p Obj1 'Path))
    )
     (progn
       (if (not *default*)
(setq *default* "Select")
       )
       (initget 0 "Select All")
       (setq keyw
      (cond
((getkword
   (strcat "\nEnter selection option (Select / All): <<"
   *default*
   ">>: "
   )
)
)
(*default*)
      )
       )
       (setq *default* keyw)
       (cond
((= Keyw "Select") (setq SS (ssget (list (cons '2 x)))))
((= Keyw "All") (setq SS (ssget "X" (list (cons '2 x)))))
       )
       (setq
index -1
       )
       (while (< (setq index (1+ index)) (sslength ss))
(setq obj     (ssname ss index)
       obj2    (vlax-ename->vla-object obj)
       pt2     (trans (vlax-get obj2 'insertionpoint) 0 1)
       rot     (vlax-get obj2 'Rotation)
       copyobj (vlax-invoke obj1 'Copy)
)
(vla-put-rotation copyobj rot)
(vlax-invoke copyobj 'Move pt1 pt2)
       )
     )
  )
  (princ)
  (command "_erase" ss "")
  (princ)
  (princ (strcat "\nBlocks swapped - " (itoa index)))
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CADaver

  • Guest
Re: Replacing selected group of blocks...
« Reply #7 on: July 21, 2006, 12:33:21 PM »
I've got this one from a decade ago...

Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:REPLs (/ ENT1 BL1 bl2 OLD ODNM)
(command "undo" "begin")
 (prompt "\nSelect Replacement Block: ")
 (setq bl2 (cdr (assoc 2 (entget (car (entsel))))))
 (prompt "Select blocks to replace: ")
 (setq ENT1 (ssget))
 (setq N (sslength ENT1))
 (setq I 0)
 (repeat N
  (setq BL1 (entget (ssname ENT1 I)))
  (setq NWNM (cons 2 bl2))
  (setq OLD (assoc 2 BL1))
  (setq ODNM (cdr OLD))
  (entmod (subst NWNM OLD BL1))
  (setq I (1+ I))
 )
(command "undo" "end")
 (princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;