AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Page I. Page II. Home.

Into the Database - Page III

The AutoCad Database does not only consist of entities but also includes several other sections, such as the Tables Section.
Tables store information about entities that are maintained globally within the drawing. For example, when you insert a block into a drawing, how does AutoCAD know what the block looks like? The definition of a block is stored in the Block Table. What happens, for example, if you need to create a layer? You have to know if the layer already exist because if you try to create a layer that already exists your program will crash. Therefore, you would search the Layers Table first to see if the layer exists.

There are nine (9) Tables that you can access :

	Layer Table				"LAYER"
	Linetype Table				"LTYPE"
	Named View Table			"VIEW"
	Text Style Table			"STYLE"
	Block Table				"BLOCK"
	Named UCS Table				"UCS"
	Named Application ID Table		"APPID"
	Named Dimension Style Table		"DIMSTYLE"
	Vport Configuration Table		"VPORT"

A Table is split into 2 parts: The 'names' of the entries in the Table and the 'details' of each entry. For example, in the Layers Table, the name of the entries would be the names of the layers that exist. The details of an individual layer would be colour, linetype, on, off, frozen, thawed, locked, unlocked or current.

To access a Table we would use the (tblsearch) function. Let's have a look at
an example :

Assume that you want to know whether a layer called STEEL exists in your drawing. First create the layer STEEL then type the following :

	(setq t (tblsearch "LAYER" "STEEL"))

The entity list of the layer STEEL should be returned :

((0 . "LAYER") (2 . "STEEL") (70 . 64) (62 . 7) (6 . "CONTINOUS"))

The first part of the entity list is '0', indicating Associative 0.
In this case it's an entry in the "LAYER" Table.
Associative 2 indicates the name of the layer. STEEL in our case.
Associative 70 is the state of the entity. 1 is Frozen, 2 is Frozen on new paper space view ports and 4 is locked. These numbers are added to 64. In this case the layer is neither frozen nor locked.
Associative 62 is the colour of the layer. Ours is white which is colour number 7. If the colour number is a negative number then the layer is off.
Associative 6 is the linetype of the layer, in this case, "CONTINUOUS".

If the (tblsearch) had not found the layer then it would have returned 'nil' and you would know that the layer did not exist.


Sometimes you don't know the name of a layer or a block but you need a list of them. This is when the (tblnext) function comes into play.

Let's assume that 4 layers exist in your drawing. The layer names are MECH, STEEL, PIPE and TXT. Enter the following :

(tblnext "Layer")

AutoLISP should return something like this :

((0 . "LAYER") (2 . "PIPE") (70 . 0) (62 . 7) (6 . "CONTINUOUS"))

Repeat the same command four additional times. You will get a new entity
list for each of the layers.
The last time you type the command AutoLISP will return 'nil' because there are no more additional layers.

Let's have a look at this in action. We are going to design a dialogue box that displays a drop down list of all the available layers in a specific drawing. The user can then choose any layer which can then be used within his routine.

Lisp 18

The Dialogue Coding looks like this :

getlay  : dialog {				//dialog name

        label = "Get Layer" ;			//give it a label


	: popup_list {				//define list box

        key = "sel1";				//give it a name

	value = "0";				//initial value

        }					//end list


        ok_cancel ;				//predifined OK/Cancel

     					
}						//end dialog


Now the AutoLisp Coding :
(defun c:getlay ( / NAMES SIZ1)
;define funcion

	(setvar "cmdecho" 0)
	;switch off command echo

 	(setq siz1 "0")
	;initiliase variable

	(setq userclick T)
	;set flag

	(setq f 1)
	;rewind pointer

	(while
	;start while loop

 	  (setq t1 (tblnext "Layer" f))
	  ;get the next layer

	  (setq f nil)
	  ;reset pointer

	  (setq b (cdr (assoc 2 t1)))
	  ;get the layer name

	    (if (/= b "DEFPOINTS")
	    ;if name is not equal to DEFPOINTS

	       (setq NAMES (append NAMES (list b)))
	       ;Add the layer name to the list

	    );if

	);while


	(setq dcl_id (load_dialog "getlay.dcl"))
	;load dialogue

	(if (not (new_dialog "getlay" dcl_id)
	;check for errors

	      );not

	     (exit)
	     ;if problem exit

	);if
  
  (set_tile "sel1" "0")
  ;initilise list box

  (start_list "sel1")
  ;start the list

  (mapcar 'add_list NAMES)
  ;add the layer names

  (end_list)
  ;end the list

    (action_tile

    "cancel"	

    "(done_dialog) (setq userclick nil)"

    );action_tile
    ;if cancel set flag to nil

  (action_tile

    "accept"	

    (strcat	

      "(progn 

        (setq SIZ1 (get_tile \"sel1\"))"	

       " (done_dialog)(setq userclick T))"	

    );strcat

  );action tile
  ;if OK get the layer selected

  (start_dialog)	

  (unload_dialog dcl_id)	

   (if userclick	
   ;if flag true

    (progn

     (setq SIZ1 (nth (atoi SIZ1) NAMES))
     ;get the name of the layer from the list

     (alert (strcat "\nYou Selected Layer  " SIZ1))
     ;display the name

    );end progn

   );end if

(princ)

);defun C:getlay

(princ)

 


If you would like the source coding for this AutoLisp Routine then simply Click Here. Cheers and keep well......

Page I. Page II. Home.
 
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