By the end of this tutorial series, you'll be able to build a basic, but very useful LISP routine for inserting blocks. Basic LISP knowledge, including loading LISP files and fluency in the Visual LISP editor is recommended (and covered in other videos on my channel). Below is the code for you to copy if you wish to automatically create the tutorial file (don't copy this paragraph, only what's below):
(defun C:makeTheStuff (/)
(command "-layer" "make" "layerRED" "color" 1 "layerRED" "make" "layerYELLOW" "color" 2 "layerYELLOW" "make" "layerGREEN" "color" 3 "layerGREEN" "set" "0" "")
(setq object1 (command "pline" "non" "0,0" "non" "10,0" "non" "10,10" "non" "0,10" "non" "0,0" ""))
(setq object2 (command "line" "non" "0,0" "non" "10,10" ""))
(setq object3 (command "line" "non" "10,0" "non" "0,10" ""))
(setq sset1 (ssget "w" '(-1 -1) '(11 11)))
;;; NOTE: None of my tutorials cover "ssget", see notes at end of file.
(command "-block" "Block1" "non" "5,5" sset1 "")
(command "-insert" "Block1" "20,20" 1 1 0)
(setq object4 (command "circle" "50,0" "5"))
(setq object5 (command "line" "non" "45,0" "non" "55,0" ""))
(setq object6 (command "line" "non" "50,-5" "non" "50,5" ""))
(setq sset2 (ssget "w" '(44 -6) '(56 6)))
(command "rotate" sset2 "" "non" "50,0" 45)
(command "-block" "Block2" "non" "50,0" sset2 "")
(command "-insert" "Block2" "non" "50,20" 1 1 0)
(setq object7 (command "pline" "non" "100,0" "non" "110,10" "non" "120,0" "close"))
(setvar "cetransparency" 75)
(setq object8 (command "solid" "non" "100,0" "non" "110,10" "non" "120,0"
"" ""))
(setvar "cetransparency" -1)
(command "draworder" "last" "" "back")
(setq sset3 (ssget "w" '(99 -1) '(121 11)))
(command "-block" "Block3" "non" "110,5" sset3 "")
(command "-insert" "Block3" "non" "80,20" 1 1 0)
(command "zoom" "All")
);end defun
;;; The "ssget" function grabs a selection set from everything within a
;;; window.
;;; The window's bottom-left corner is located at the coordinates in the first
;;; set of brackets
;;; and the top-right corner in the next set of brackets.
;;; Example: (setq sset3 (ssget "w" '(99 -1) '(121 11)))
;;; I create a variable called "sset3" which stores my selection set created by
;;; ssget
;;; The selection set uses a window (by way of using the 'w' argument with
;;; the ssget function)
;;; The window's bottom left corner is located at 99,-1 and it's top right
;;; corner at 121,11.
;;; Within that rectangle exists all my objects that I want to turn into 'Block3'.
;;; A google search of "AutoLISP selection sets" will cover everything in
;;; much more depth.
;;; Selection sets are a tricky, but integral part of AutoLISP programming,
;;; and admittedly,
;;; a weak point of mine.
;;; Block insert code, works with most blocks in AutoCAD 2020:
;;; (command "-insert" "Block1" pause 1 1 0)
;;; Replace the "pause" with a variable if "getpoint" is used prior to initiating
;;; the code.
;;; *This code might be required for certain versions of AutoCAD and blocks
;;; that either scale uniformally
;;; or are annotative: (command "-insert" "Block1" userPickPoint 1 0)
;;; The above DOES NOT work in AutoCAD 2020; always test the code and
;;; pay attention to the command line.
;;; Blocks with Attributes need the attributes to be "preset", or else the code
;;; will crash/fail.
;;; crashing typically means the code just kicks you out of the command
;;; (it rarely crashes AutoCAD).