AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Page I. Home. Page III.

Into the Database - Page II

When you first start to delve into the AutoCAD database it is, I admit, quite daunting. But, although entity access and manipulation is fairly complex, it can be divided into component parts that make it much easier to understand.
Let's have a look at an AutoLisp routine that can be used, as a sort of template
which you can apply to numerous, similar applications. Have a close look at this coding :

(defun C:CHLAYER ( / a1 a2 n index b1 b2 b3 d1 d2)
		
	(prompt "\nSelect Entities to be Changed : ")
	(setq a1 (ssget))
	(prompt "\nPoint to Entity on Target Layer : ")
	(setq a2 (entsel))
	(setq n (sslength a1))
	(setq index 0)
	(setq b2 (entget (car a2)))
	(setq d2 (assoc 8 b2))
	(repeat n
		(setq b1 (entget (ssname a1 index)))
		(setq d1 (assoc 8 b1))
		(setq b3 (subst d2 d1 b1))
		(entmod b3)
		(setq index (1+ index))
	);repeat
   (princ)
);defun
(princ)

This routine allows you to select any number of objects and change them to a different layer. The target layer is choosen by simply pointing to an object on the desired layer. (To test this routine, you will need to create a drawing with objects on different layers.) Let's have a look line by line :

(defun C:CHLAYER ( / a1 a2 n index b1 b2 b3 d1 d2)

Defines the function and declares all variables as local.

	(prompt "\nSelect Entities to be Changed : ")

Prompts the user.

	(setq a1 (ssget))

Allows the user to select the objects to be changed. The selection set is assigned to variable 'a1'.

	(prompt "\nPoint to Entity on Target Layer : ")

Prompts the user to select the Target Layer.

	(setq a2 (entsel))

This is a special type of selection statement that only allows you to
select one entity.

	(setq n (sslength a1))
Counts the number of entities in the selection set 'a1' and stores this number in variable 'n'.
	(setq index 0)

Sets the loop control variable 'index' to zero.

	(setq b2 (entget (car a2)))

This statement retrieves the entity list from 'a2' and assigns it to 'b2'.

	(setq d2 (assoc 8 b2))

This looks for the code 8 in the entity list 'a2', and then assigns the sub list to 'd2'.

	(repeat n

This begins the loop that pages through the selection set.

		(setq b1 (entget (ssname a1 index)))

This gets the entity list and assigns it to 'b1'.

		(setq d1 (assoc 8 b1))

Gets the sublist code 8. (The Layer)

		(setq b3 (subst d2 d1 b1))

Substitutes the new 'd2' layer for the old 'd1' layer in the entity list 'a1', and assigns it to the new entity list 'b3'.

		(entmod b3)

Updates the new entity list in the database.

		(setq index (1+ index))

Increases the 'index' variable by 1, priming it for the next loop.

	);repeat

Closes the repeat loop.

   (princ)

Finish cleanly.

);defun

Closes the function.

(princ)

Clean Loading.


Listed below is another routine that allows you to globally change the height of text without affecting other entities. As you will see, the only difference is, is that we have added a conditional filter to the routine.
(defun C:CHGTEXT ( / a ts n index b1 b2 b c d)
	
	(setq a (ssget))
	(setq ts (getreal "\nEnter New Text Height : "))
	(setq n (sslength a))
	(setq index 0)
	(repeat n
		(setq b1 (entget (ssname a index)))
		(setq index (index+ 1))
		(setq b (assoc 0 b1))
		(if (= "TEXT" (cdr b))
			(progn
				(setq c (assoc 40 b1))
				(setq d (cons (car c) ts))
				(setq b2 (subst d c b1))
				(entmod b2)
			);progn
		);if
	);repeat
   (princ)
);defun
(princ)

 


Well I bet your brain hurts after that lot!!!
On the text page we'll have a quick look at Tables.

Page I. Home. Page III.
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website