AutoLISP: Replace Selected Points with Block

Today’s featured routine lets you replace selected points in a drawing with a defined block in the drawing. This routine could be more rubust but it is still helpful. It does let you specify the scale of the block and rotation.

Here’s how:

  • PT2BLOCK
  • Enter the name of the block that will replace the point objects with ,enter>
  • Specify the insertion Scale Factor
  • Specify the rotation angle
  • Select Point objects that will be replaced by the block.
    Note: this is not a global point replacement program by default. If you would like to replace all point objects, enter ALL at the command line.

~enjoy

Link to http://www.autocadtips.wordpress.com


;|
                            PT2BLOCK.LSP
                           Michael Weaver
                          2175 George Road
                      Fairbanks, Alaska 99712
                    (907)488-3577 voice and fax
                  Email:71461.1775@compuserve.com
                 Mike_Weaver_Alascad@compuserve.com
                           Fri 02-23-1996
                          (c)1996 Alascad



This routine will replace points (nodes) in the current drawing will
insertions of a specified block.  The insertion scale factor and the
rotation angle for block are supplied by the operator.

Example:

command: PT2BLOCK
Name of block to insert: MYBLOCK
Insertion scale factor: 1
Insertion rotation angle: 0
Select points: <Select points to replace>

   Replace MYBLOCK with the name of your block.  An enter at the
Select points prompt will select all points in the drawing database
(current space and excluding points found on layer DEFPOINTS).

The blocks will be inserted on the same layer as the points.

|;

(defun c:pt2block(;		replace points with blocks
  /;				no arguments
  attreq;			value to restore
  cmdecho;			value to restore
  bname;			block name to insert
  temp;				temp variable
  ent;				entity name
  elist;			entity list
  scf;				insertion scale factor
  rotang;			insertion rotation angle
  ss1;				selection set of points
  indx;				index through selection set
  sslen;			number of points selected
  inspt;			insertion point
 );				end of local variable list
 (if (and
   (setq
    bname (getstring "\nName of block to insert: ")
    temp (/= "" bname)
   )
   (progn
    (if (or
      (tblsearch "BLOCK" bname);	the block exists in the drawing
      (findfile (strcat bname ".dwg"));	the block can be pulled from disk
     );				end or
     T;				continue
     (progn
      (alert (strcat "Block " bname " not found."))
      nil
     );				end progn
    );				end if block found?
   );				end progn check for block
   (setq scf (getreal "\nInsertion scale factor: "))
   (setq rotang (getangle "\nInsertion rotation angle: "))
   (setq
    ss1 (ssget
     '((0 . "POINT");		get points
       (-4 . "<NOT");		not on 
        (8 . "DEFPOINTS");	layer DEFPOINTS
       (-4 . "NOT>");		end not
      );			end the quoted filter list
     );				end ssget
    temp (if (and ss1 (< 0 (sslength ss1)));	was anything selected
     T
     (setq 
      ss1 (ssget
       "X"
       '((0 . "POINT");		get points
        (-4 . "<NOT");		not on 
         (8 . "DEFPOINTS");	layer DEFPOINTS
        (-4 . "NOT>");		end not
       );			end the quoted filter list
      );			end ssget
     );				end setq (nested)
    );				end if?
   );				end setq (outer)
   (if (< 0 (sslength ss1))
    T
    (progn
     (alert "No points found.")
     nil
    );				end progn
   );				end if points found?
  );				end and
  (progn
   (setq
    attreq (getvar "attreq");	value to restore
    cmdecho (getvar "cmdecho");	value to restore
    indx -1;			a counter
    sslen (sslength ss1);	number of points selected
   )
   (setvar "attreq" 0)
   (setvar "cmdecho" 0)
   (while (> sslen (setq indx (1+ indx)))
    (setq
     ent (ssname ss1 indx);	entity name
     elist (entget ent);		entity list
     inspt (cdr (assoc 10 elist));location of the point
     inspt (trans inspt ent 1)
    );				end setq
    (entmake
     (list
      '(0 . "INSERT")
      (cons 2 bname)
      (assoc 8 elist)
      (cons 10 inspt)
      (cons 41 scf)
      (cons 42 scf)
      (cons 43 scf)
      (cons 50 (* rotang (/ pi 180)))
      (assoc 210 elist)
     );				end list
    );				end entmake
    (entdel ent);		get rid of the point
    (princ ".");			indicate progress
   );				end while
   (setvar "attreq" attreq)
   (princ (strcat "\t" (itoa sslen) " points replaced. "))
   (command "_.redraw")
   (setvar "cmdecho" cmdecho)
  );				end progn
 );				end if valid input?
 (princ)
);				end c:pt2block

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, AutoLISP: Blocks, AutoLISP: Creating, AutoLISP: Modify. Bookmark the permalink.

18 Responses to AutoLISP: Replace Selected Points with Block

  1. ynteligent@azet.sk says:

    Thank you very very much. that is exactly what I need…Peter

  2. 204 says:

    i’m using Civil 3D 2011; I get
    Command: PT2BLOCK Unknown command “PT2BLOCK”. Press F1 for help.

  3. Acad_H says:

    Gets rid of points but does not insert block…

  4. how can text or mtext to block with text in to block???

  5. pnoraa says:

    Thanks, It works fine for normal blocks but not annotative blocks, Is it easy to edit it so that it works for them too?, I’m guessing that you just need to have an input for annotative scale.

  6. ibrews says:

    Super helpful, even years later :D

  7. GKS says:

    The name of my block has spaces in it. How do I get around that without editing my block?

    • HasanCAD says:

      Replace this line
      (getstring “\nName of block to insert: “)
      with
      (getstring T “\nName of block to insert: “)
      Be note that we added T

  8. Solomon Woldeyesus says:

    Thanks. It works

  9. Amazing!
    Still working on v2019!! :-)
    Great work!

  10. Pingback: AutoCAD: sustituir puntos por bloques – happservicios

  11. Dustin Harrington says:

    Amazing routine. Thank you so much!!

  12. Antonio says:

    I tried but it gives me a syntax error when I load the lsp file

  13. John Oetting says:

    I have the routine loaded correctly, but when I go to select my points, no points are selected. Is there a work around for this?

  14. thucnguyen says:

    Could you please change write block name by select block existing?

Leave a comment