Results 1 to 3 of 3

Thread: LISP for selecting block and replacing with another

  1. #1
    Member
    Join Date
    2006-04
    Posts
    2
    Login to Give a bone
    0

    Default LISP for selecting block and replacing with another

    Hi,

    I'm fairly new at creating LISPs and I need help creating a LISP where I could select a block, prompt if I want to replace that particular block globally or a single instance, then another prompt asking to specify a replacement block, and then it completes the replacement at each particular location where the selected block was placed.

    Thanks!

    Ken Nofuente
    Engineering Harmonics Ltd.
    Toronto, Ontario, Canada

  2. #2
    All AUGI, all the time
    Join Date
    2015-12
    Location
    Central Oregon
    Posts
    591
    Login to Give a bone
    0

    Default Re: LISP for selecting block and replacing with another

    Well, since no one helped you all day long......

    Here's something that is nearly complete. I'll leave it to you to play with and improve/modify to suit:
    Code:
    (defun c:blkreplacer (/ answr ent idx newname obj ss)
      (vl-load-com)
      ;;setup default on first run
      (if (not jmm-replaceall)
        (setq jmm-replaceall "Single")
        )
      (command ".undo" "be")
      ;;if the user selects something, inputs a ne block name AND it exists in the dwg...
      (if (and (setq ss (ssget ":S" '((0 . "INSERT"))))
    	   (progn
    	     (initget "Single Global")
    	     (if (setq answr (getkword "\nReplace just this block or Globally replace?[Single/Global]: "))
    	       (setq jmm-replaceall answr)
    	       (setq answr jmm-replaceall)
    	       )
    	     )
    	   (setq newname (getstring "\nBlock name to replace with: "))
    	   (tblobjname "BLOCK" newname)
    	   )
        (progn
          (if (eq jmm-replaceall "Global");;get ALL occurances if it's Global, else use the original ss
    	(setq ss (ssget "x" (list '(0 . "INSERT") (assoc 2 (entget (ssname ss 0))))))
    	)
          (setq idx -1)
          (while (setq ent (ssname ss (setq idx (1+ idx))))
    	(setq obj (vlax-ename->vla-object ent))
    	(vla-put-name obj newname);;change the name
    	(vla-update obj)
    	)
          )
        )
      (command ".undo" "end")
      (princ (strcat "\nReplaced " (itoa idx) " blocks......"))
      (princ)
      )
    Note that this may not work as expected with attributed blocks! If you need that functionality, I leave that for you to investigate

    Jeff

  3. #3
    100 Club
    Join Date
    2004-12
    Posts
    153
    Login to Give a bone
    0

    Default Re: LISP for selecting block and replacing with another

    The express tools for 2005 and up has a global replace block with another block in it as well.

Similar Threads

  1. Replies: 1
    Last Post: 2015-04-30, 08:52 PM
  2. Replacing blocks with a block with the same name
    By Wish List System in forum AutoCAD Wish List
    Replies: 3
    Last Post: 2013-10-28, 12:22 PM
  3. PR31-2: Selecting The Right Objects Every Time In Visual LISP
    By Autodesk University in forum Computer Programming
    Replies: 0
    Last Post: 2012-11-24, 07:38 PM
  4. insert block by selecting a points
    By windowsxp5 in forum AutoLISP
    Replies: 8
    Last Post: 2006-04-24, 12:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •