Next Titel Inhalt Vorwort 1 2 3 4 5 6 7 8 9 10 11 12 Appendix @ CAAD Teachers

KAPITEL 7
AutoCAD II - Datenbanken, geometrische Einheiten, Menues und Dialogboxen

7.1 Database Management

A database is a collection of ordered data. A database management system (DBMS) is a tool to manipulate the data in the collection. Sometimes, data are used to represent something in the real world, namely, to be an abstraction. A real world problem can then be simplified and hopefully be solved by manipulating the abstraction. A database system consists of ordered data and operators which manipulate the data.

A data element is a unit of information relevant to a task context. A data structure is a selected way to organize data using primitives in the computing system. Typical database operators include (i) retrieval to select some data and translate them into another level of abstraction, and (ii) modification to create, erase, or edit data in the database.

In some respects, a program like AutoCAD can be viewed as a database system which works on a database of drawings as follows:

Data elements
drawing entities
lines, plines, texts, circles, dimensions....
Data structure
specific to AutoCAD developed by programmers
Data operators for retrieval
select, list, zoom, layer, dview, vpoint, vports....
Data operators for modification
line, pline, text, circle, insert....
layer, rotate, move, change, color, break, trim....

7.1.1 AutoCAD Blocks

One specific drawing element supported in AutoCAD is called a block and it can be used an example to understand the concepts of DBMS. A block is a collection of geometric and non-geometric information. Like a function in LISP, a block has a set of parameters which are assigned values when the block is inserted. Scaling factors, insertion point, and rotation angle are predefined as parameters for all blocks, and they are used to describe the preferred geometric transformation for the original block definition. Additional non-geometric parameters can be defined using attributes, when blocks are to be used as basic elements to build a customized database.
Geometric data
insertion point, rotation, scaling, and
body definition.
Non-geometric data
attributes
mode: visible, constant, verify, preset.
tag: the name of the attribute
value: the value of the attribute
prompt: the prompt to the user
Data operators
retrieve information
attext
modify information
block, wblock, insert
attdef, attedit

7.1.2 AutoCAD System Variables

Additionally, AutoCAD environment is globally managed by an extensive number of system variables. These variables and their values may affect drawing operations.

System variables likes clayer, cmdecho, filedia, etc. are examples of global AutoCAD data elements. Operators like getvar and setvar can be used to retrieve and modify values associated with system variables. Note that you cannot create or erase AutoCAD system variables, only some of them can be modified, i.e. assigned different values or settings.

7.2 Developing a LISP Database

In many situations, it is desirable to create a customized database specific to a task at hand. This can be achieved using a few LISP primitives, namely association lists to define and create data elements and their structure, assoc primitive to retrieve data, and subst primitive to modify data.

7.2.1 Association Lists

In LISP programming, we generally use symbols and assign values to them; whenever we refer to the symbol, we actually access its value. We can also group symbols and their values, and access them or modify them together by using association lists.

An association list is a list of embedded sublists, in which the first element of each sublist is a key.

7.2.1.1 Setting up an association list

Following creates an association list assigned to a variable BRICK1, with its sublists made up of a key (also known as property) and its value.

	(setq BRICK1 '((color red)
		       (supported_by BRICK2)
		       (size (x y z)))
	)
In the above example, color, supported_by and size are keys or properties.

7.2.1.2 Accessing an association list

To access the value of any property of such lists, procedure assoc is used:

(assoc <key> <assoc_list>)
where the first argument specifies the key, and the second argument specifies the association list that is to be searched for that key.

For example, to find color of BRICK1, following call will be made:


(assoc 'color BRICK1)	=>  (color red)
Note that assoc returns the entire sublist, not just the value of specified key.

7.2.1.3 Modifying an association list

Association lists can be modified using procedure subst with three arguments:

(subst <new_value> <old_value> <a_list>)
where the first argument provides new_value to be substituted wherever old_value occurs (one level deep) in the a_list.

For example, to change value of the key size from (x y z) in the above example to (XX YY ZZ), following call may be made:

(setq BRICK1 (subst '(size (XX YY ZZ)) (assoc 'size BRICK1) BRICK1))

7.2.2 Dotted Pairs and cons function

So far, we have seen lists expressed as left parenthesis, one or more atoms, and right parenthesis. There is another kind of list in LISP which is made up of what are known as dotted pairs of atoms.

Generally, cons function takes an atom or a list and inserts it into another list:


(cons 'a '(b))	=>  (a b)
and
(cddr '(a b))	=>  nil
It is also possible to use cons to join two atoms into a list:

(cons 'a 'b)	=>  (a . b)
This notation is referred to as dotted pairs and should be used carefully.

(cdr '(a . b))	=>  b	and not   (b)
and
(cddr '(a . b))	=>  Causes an error condition!

7.2.3 File I/O in AutoLisp

In order to save and retrieve data, you can use external files. Before you can read or write to an external file, you need to open them as follows:
 
(open <file name> <mode>)
Mode can be r for reading, w for writing, and a for appending data to a file. Once a file is properly open, you can read or write to the opened file as follows. Note that it is advisable to properly close files when they are no longer needed.

	(setq infptr (open "telephone.data" "r"))
	(setq telephone (read (read-line infptr)))
	(close infptr)
	
	(setq outfptr (open "telephone.data" "w"))
	(print telephone outfptr)
	(close outfptr)
Other useful file related functions are:

	(read-char <file descriptor>)
	(write-line <string> <file descriptor>)
    

7.3 AutoCAD Database

AutoLisp provides a set of functions to access and modify graphic entities and their properties. Additionally, some other functions provide direct access to input devices like keyboard, digitizer, and output devices like screen. These functions are specific to AutoLisp and are not part of the standard LISP programming language.

Drawing entities created as part of any AutoCAD drawing are stored internally in a database of information. Whenever you perform any operation- addition of a new entity, modification of an entity, or deletion of an entity, the drawing database is updated with new information. To access this database, AutoLisp provides two special data types, called entity names and selection sets.

7.4 Entity Access

7.4.1 Entity Name Functions

(entnext)
Returns the name of the first nondeleted entity in the database.
(entnext ename)
Returns the name of the first nondeleted entity following ename in the database.
(entlast)
Returns the name of the last nondeleted entity in the database.
(entsel)
Prompts the user for selecting an entity by a point pick, and returns a list containing selected entity name and coordinates of point used to select that entity.
(entsel prompt)
Same as above but uses the value of prompt string for entity selection.

7.4.2 Entity Data Functions

(entdel ename)
If entity ename is currently in the drawing, it is deleted; and undeleted if it has been deleted during the current editing session.
(entget ename)
Returns an association list of definition data for entity ename. Data in the association list follow group code specification.
Example: If a line is drawn on layer "test", starting from (1 1 0) to (2 3 0), then its entity data may look like the following:

(entget (entlast))

((-1 . <Entity name: 60000021>) ;entity name

(0 . "LINE") ;entity type

(8 . "TEST") ;layer

(10 1.0 1.0 0.0) ;starting point

(11 2.0 3.0 0.0) ;ending point

)

(entmod elist)
Performs entity data modification. Argument elist to this function is an association list- the kind returned by the function entget. Entity data can be modified using a function like subst, and then invoking this function- entmod.
Example: If the line entity in above example were to be changed to another layer "test2", following sequence will be required:

(setq linedata (entget (entlast)))

;get definition data for line

(setq linedata

(subst

(cons 8 "test2") ;new layer value

(assoc 8 linedata) ;old layer value

linedata ;old entity data

)

)

(entmod linedata);finally update database

(entupd ename) Entity is updated and regenerated on screen following any modification after above function entmod is executed.

7.4.3 Selection Set Functions

(ssget)
Selects entities using standard Select Objects: prompt
(ssget mode)
Same as above using one of the valid mode values:
W (world), C (crossing), L (last), P (previous)
(ssget pt1)
Selects entity passing through point pt1
(ssget "X" filter)
Selects entities matching the filter.
Example:
(ssget "X" (list (cons 8 "test")))
will return a selection set containing all entities on layer "test".
A complete list of group codes for filters is given in
AutoLisp Programmer's Reference, Pages 68-69.
(sslength selset)
Returns the number of entities in the selection set- selset.
(ssname selset num)
Returns the name of num'th entity in the selection set- selset.
(ssadd)
Constructs a new selection with no members.
(ssadd ename)
Constructs a new selection set with ename as its member.
(ssadd ename selset)
Add entity ename to the selection set- selset.
(ssdel ename selset)
Deletes entity ename from the selection set- selset.

7.4.4 Symbol Table Access

Information about layer, linetypes, views, text styles, and blocks is stored in symbol tables. This information can be accessed using following functions.

(tblnext tname)
Returns data stored in symbol table specified by tname. Valid values for tname are: layer, ltype, view, style, block, ucs, vport. If this function is called repeatedly, it returns the next entry in the same table.
(tblsearch tname sym)
Searches the symbol table tname for the symbol specified by sym, and returns the data if any match is found.

7.5 Device Access

(grclear)
Clears the current viewport.
(grdraw pt1 pt2 color)
Draws a vector from pt1 to pt2 in the color specified.
(grtext box text)
Displays string text in the menu area specified by box.
(grread)
Returns a list whose first element is the input device code, and the second element is either an integer or a point list.
After using any of these functions, you may need to use redraw to regenerate drawing.

7.6 Pull Down Menus

AutoCAD provides a set of standard and customizable interaction techniques, using which users can execute various commands. A subset of these techniques involves using menus of various kinds- screen, pull down, icon, button, and tablet menus. Commands to implement these techniques are placed in the acad.mnu file.

A menu file is a plain text file containing command strings. All menu files must have extension .mnu (compiled version will have extension .mnx). Each menu item may consist of a command, a parameter, or a sequence of commands and parameters. By convention, each menu item resides on a separate line in the menu file.

Example: Following is a simple command menu file.

	circle
	zoom e
	zoom p

When you select any of these options, AutoCAD will execute corresponding command.

Menu File Section Labels

Section labels in a menu file assign various options/commands to different devices. In the following example,

  
labels preceded by *** specify sections of command menu to different devices; in this case, first section to screen menu, and the next one to pop up menu 1. Also note that commands may be given descriptive titles enclosed in [] followed by actual commands to be executed when that option is selected.

Submenus and referencing

Menu items can be broken down into logical units and organized to form a hierarchy of main and submenus. Submenus are identified in a menu file by a label of the form **name where name is a valid character string. Submenus can be activated or deactivated using the following construct:

$S = submenu_name ; for screen submenu

$Pn = submenu_name ; for pop up menu where n between 1-10

 


7.7 Dialog Boxes

The latest version of AutoCAD supports another, more sophisticated form of user input device known as a dialog box. Dialog boxes may include: buttons, sliders, small images and further sub-dialog boxes. They are a convenient way for the user to set variables and start functions. As is the case with standard menus, users can also define their own dialog boxes. Files that define dialog boxes must be written in a special programming language called "Dialog Control Language" (file extension .dcl). The following is a short introduction to dialog box description and a simple example. The example is also available in img_dialog.lsp and 07_img_dialog.dcl. (Another example, 07_list_dialog, is also available in the same directory.). Further examples and references can be found in the book "Das grosse AutoCAD 12 Programmierbuch".

7.7.1 Structure

Dialog boxes can contain: rows and columns, buttons, toggle switches, sliders, text elements, edit boxes, images and other devices. They can be represented with nested blocks.

	+---------+

| dialog |

| +----+----+

| | row |

| | +----+----+

| | | column |

| | | +----+----+

| | | | image |

| | | | image |

| | | +----+----+

| | +----+----+

| | row |

| | +----+----+

| | | column |

| | | +----+----+

| | | | image |

| | | | image |

| | | +----+----+

| | +----+----+

| |ok_button|

| +----+----+

+---------+

This figure shows the nested hierarchy of the following example (07_img_dialog.dcl). The file describes the contents of the dialog box and how the elements are arranged.

7.7.2 Example: A Dialog Box with Icons

    /* Sample dcl file for creating dialog box with images. 
	     Actual slide images are set up in the Lisp file.
	     hello: name of the dialog box. */	
    hello:dialog {
	    label = "Select data category";	/* Title of dialog */
	    :row {
	    :column {
		      :image_button {
			     key = "img_button1";	/* name of image button */
			     width = 7;				 
			     aspect_ratio = 1.0;
			     allow_accept = true;
			     color = 0;
		      }
		      :image_button {
			     key = "img_button2";	/* name of image button */
			     width = 7;
			     aspect_ratio = 1.0;
			     allow_accept = true;
		      }
	    }
	    :column {
		      :image_button {
			     key = "img_button3";	/* name of image button */
			     width = 7;
			     aspect_ratio = 1.0;
			     allow_accept = true;
		      }
		      :image_button {
			     key = "img_button4";	/* name of image button */
			     width = 7;
			     aspect_ratio = 1.0;
			     allow_accept = true;
		      }
		    }
	    }
	    ok_cancel_help;	/* predefined tile with three buttons */
    }
In order to control a dialog box from your program and to get results back from it, some lisp functions must be invoked, usually from a menu option. The next example shows a possible lisp implementation for the previously described dialog box.

    (defun c:imageButtonSample ( / dcl_id pickf ret_val prevB)
	    ;dcl_id:  dialog ident
	    ;ret_val: result of dialog box (cancel/accept/help)
	    ;pickf:   selected image button
	    ;prevB:   previous image button
    
	    (setq dcl_id (load_dialog "07_img_dialog.dcl"));load dcl file 	(if (not (new_dialog "hello" dcl_id))  ;open the dialog
		(exit))                            ;check if it succeeded
    
	    ;Get the height and width of image button
	    (setq x (dimx_tile "img_button1")
		  y (dimy_tile "img_button1"))
    
	    ;Assign slide file to button using the next three functions
	    (start_image "img_button1")            ;name of image button
	    (slide_image 0 0 x y "img_sample1")    ;slide file name 
	    (end_image)                            ;end of specification
	    (mode_tile "img_button1" 0)            ;set the state of tile
    
	    (start_image "img_button2")
	    (fill_image 0 0 x y 1) 
	    (end_image)
    
	    (start_image "img_button3")
	    (fill_image 0 0 x y 2)
	    (end_image)
	    (mode_tile "img_button1" 0)
    
	    (start_image "img_button4")
	    (slide_image 0 0 x y "img_sample2")
	    (end_image)
    
	    (setq prevB nil)
	    
     ;Selected image button key is assigned to 'pickf' string
     ;flip_button defined in file: 
     ;/homes5/prog/ausgabe/07_img_dialog.lsp
	    
	    (action_tile "img_button1" "(setq pickf $key) 
			 (setq prevB (flip_button 1 prevB))")
	    (action_tile "img_button2" "(setq pickf $key) 
			 (setq prevB (flip_button 2 prevB))")
	    (action_tile "img_button3" "(setq pickf $key) 
			 (setq prevB (flip_button 3 prevB))")
	    (action_tile "img_button4" "(setq pickf $key) 
			 (setq prevB (flip_button 4 prevB))")
    
	    ;For the predefined tile, use 'done_dialog' 
	    ;(predefined) function to return an integer value
	    (action_tile "cancel" "(done_dialog 0)")
	    (action_tile "accept" "(done_dialog 1)")
	    (action_tile "help"   "(done_dialog 2)")
    
	    ;to insert our help file as a dialog box, it will need a	;modification (action_tile "help" 
	    ;             "(help_dialog1 \"help.hlp\" \"appload\")")
	    
	    (setq ret_val (start_dialog))          ;run the dialog box
	    ;use returned value for further processing
	    (unload_dialog dcl_id)                 ;close dialog window
    
	    ;evaluate the results from the dialog box
	    (cond 
	      ((= 0 ret_val) (princ "Action cancelled...") (print))
	      ((= 1 ret_val) (princ "Extracting selected data...")
			     (print)))
	    (if pickf
	      (princ (strcat "Selected items: " pickf))
	      (princ "No selections made...")
	    )
	    (print)
    )

7.8 Übung 7a

Modify the program that you wrote for Übung 5 so that it can support the following menus and options.

Menu G_Design with following menu options:

Menu M_Design with following menu options: A template menu file is provided: /homes5/prog/ausgabe/07_name.mnu. Copy this file into your directory and add appropriate commands and functions to it.

7.9 Übung 7b

Use the dialog box technique to design a better interface for the different house types. For example, make small slides of the roof form options and let the user select a roof type by clicking on the image in the dialog box. Create a text input fields that can be used to enter the size of a building. Use the more complicated examples in the ausgabe folder for further ideas and help.

You are required to submit your program: 07_name.lsp and all related interface files, including the menu file: 07_name.mnu.

Next Titel Inhalt Vorwort 1 2 3 4 5 6 7 8 9 10 11 12 Appendix @ CAAD Teachers

This website has been archived and is no longer maintained.