TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hyposmurf on August 28, 2004, 06:07:31 AM

Title: Swap two blocks and blockreplace
Post by: hyposmurf on August 28, 2004, 06:07:31 AM
How would I go about the following?
I often find that I need to do a straight swap for two blocks.A bit like the BLOCKREPLACE command, but I dont want to it to affect all my blocks globally just the ones Im working on.The way I currently acheive this is to draw a line to each blocks insertion points(centre) and then move them about,using the endpoint point of the line for my insertion point and delete the lines once Im done.Am I the only one who draws these guidance lines? :) I dont think that BLOCKREPLACE command was very developed by Autodesk,it has numerous bugs,it doesnt handle attributes to well,theyre either misplaced or at a different scale ratio with their associated block.I was previously thinking that BLOCKREPLACE command will be a really helpful feature well done Autodesk,I think not!
Title: Swap two blocks and blockreplace
Post by: Crank on August 28, 2004, 07:18:45 AM
Just replace the blockname in the database:
Code: [Select]

;   BLOCKREP.lsp by J.J.Damstra
;   Copyright (c) 2004
(defun c:BLOCKREP (/ blockname loop vervang)
(setq blockname (entsel "\nSelecteer new block: "))
(if blockname
(progn
(setq blockname (cdr (assoc 2 (entget (car blockname)))))

(Princ "\nSelect blocks to replace.")
(setq loop (entsel))
(if loop
(while loop
(setq vervang (entget (car loop)))
(setq vervang (subst (cons 2 blockname)(assoc 2 vervang) vervang))
(entmod vervang)
(setq loop (entsel))
)
(princ "\nmsg: CANCELED")
)
)
(princ "\nmsg: CANCELED")
)
(princ)
)
Title: Swap two blocks and blockreplace
Post by: hyposmurf on August 28, 2004, 08:23:39 AM
Cheers thats kind of half way there!It will copy a block to anothers insertion point,but how would I manage to actually do a straight swap.Say I have a block called Hendie and another called Dent.I want to reposition Hendie in Dent's position and Dent in Hendie's position.
Title: Swap two blocks and blockreplace
Post by: Keith™ on August 28, 2004, 08:27:20 AM
Hypo...Not sure what you are trying to achieve here...

Are you simply trying to change the reference of a block "A" with block "B" but only in certain areas?

If so I have a program I developed a while back that allows you to manipulate a selected entity based on the DXF code group. In this way, you could simply replace DXF 2 with the block reference you want to display.
Title: Swap two blocks and blockreplace
Post by: CAB on August 28, 2004, 10:55:19 AM
Perhaps this is what you are looking for.

Code: [Select]
;;   BlockSwap.lsp   -  CAB 08/28/04
;;   Swap locations of two blocks based on there insertion points
;;
(defun c:bswap (/ blk1 blk2 pt1 pt2)
  (prompt "\n*-*  Select the two blocks to swap locations.  *-*")
  (if (and (setq blk1 (car (entsel "\nSelect first block: ")))
           (= (cdr (assoc 0 (setq blk1 (entget blk1)))) "INSERT")
      )
    (if (and (setq blk2 (car (entsel "\nSelect second block: ")))
             (= (cdr (assoc 0 (setq blk2 (entget blk2)))) "INSERT")
        )
      (progn
        (setq pt1  (assoc 10 blk1)
              pt2  (assoc 10 blk2)
              blk1 (subst pt2 (assoc 10 blk1) blk1)
              blk2 (subst pt1 (assoc 10 blk2) blk2)
        )
        (entmod blk1)
        (entmod blk2)
        (prompt "\n*--->     Block locations were swapped.")
      ) ; progn
    ) ; endif
  ) ; endif
  (if (null pt1)
    (prompt "\n*-*  ERROR - One object was not a block.  *-*")
  )
  (princ)
) ;defun
(prompt "\n*-*  Block Swap Loaded:  Enter BSwap to run.  *-*")
(princ)
Title: Swap two blocks and blockreplace
Post by: hyposmurf on August 28, 2004, 01:41:56 PM
Sure is :D !!! Thank you CAB 8)