LISP Blocks in CADopia


Sometimes you may have a need to replace Block(s) by another Block in a specified
region of the drawing. If the Block is redefined then the effect will be seen
throughout the drawing. In this tutorial, we will first show how to select objects
through Lisp and later we will write the program for substituting the block.

Collecting User Input

To replace a block by another block, we have to select the required block first and
retrieve its base point. Next, select the Blocks that should be replaced.

To select objects, we will use the function ‘ssget’ . The syntax of ‘ssget’ is as follows:

(ssget [mode] [point1 [point2]] [point-list] [filter-list])

This function prompts the user to select entities. When no arguments are provided, the
ssget function prompts the user with the “Select entities:” prompt. This is the only situation
when ssget highlights selected entities. The mode argument specifies the entity selection method:

Mode Meaning Points Example
(none) Single pick point point1 (ssget ‘(10 15)) or (ssget)
“C” Crossing point1, point2 (ssget “C” ‘(1 1) ‘(2 2))
“CP” Crossing Polygon points-list (ssget “CP” ptlist)
“F” Fence points-list (ssget “F” ptlist)
“L” Last none (ssget “L”)
“O” Outside points-list (ssget “O” ptlist)
“OP” Outside Polygon points-list (ssget “OP” ptlist)
“P” Previous none (ssget “P”)
“PO” Point point1 (ssget “PO” ‘(1 1))
“W” Window point1, point2 (ssget “W” ‘(1 1) ‘(2 2))
“WP” Window Polygon points-list (ssget “WP” ptlist)
X” All none (ssget “X”)

The point1 (and point2) argument specifies the pick point for the selection mode, as noted
in the table above.

The filter-list argument is a list that specifies entity properties in dotted-pair format,
such as:

(ssget “X” ‘((0 . “CIRCLE”) (62 . 1)))

Which selects all circles colored red (color 1). In addition, the special dotted-pair of -4
lets you include a relational test, such as:

(ssget “X” ‘((0 . “CIRCLE”) (62 . 1) (-4 . “e;< "e;) (40 . 15.0))

Write the program in Notepad:

Now that we’ve defined how to get the user input, we write the code using Notepad.

The code defines a custom command, “BlkRepl”, includes the elements we described earlier, and uses
the (command ) function to replace the blocks.


(defun C:BlkRepl(/ ss n ent prp cenpt)
(princ “Select replacement block:”)
(setq objs (ssget))
(princ “select base point:”)
(setq bs (getpoint)
n 0)
(princ “Select the blocks to be replaced”)
(setq ss (ssget))
(initget “Yes No”)
(setq ans (getkword “Want To Replace (Yes/No)?”))
(if (= ans nil)
(setq ans “Yes”)
)
(repeat (sslength ss)
(setq ent (ssname ss n)
prp (entget ent)
cenpt (cdr (assoc 10 prp))
);setq
(command “copy” objs “” bs cenpt)
(if (= ans “Yes”)
(command “Erase” ent “”)
);IF
(setq n (+ n 1))
);REPEAT
)


Save the file as BlkReplace.lsp.

Now load the Lisp file using LoadApplication command and use ‘BLKREPL’ command.

LISP Blocks in CADopia

Fig. 1 : Before running the program

LISP Blocks in CADopia

Fig. 2 : After running the program