;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ;;;; ;;;; Copyright (c) Feb. 1997 by Takehiko Nagakura. ;;;; ;;;; All rights reserved. ;;;; ;;;; ;;;; ;;;; Do not copy, use, modify or distribute this software ;;;; ;;;; without written permission by Nagakura. Nagakura will ;;;; ;;;; not be responsible for any consequence of its use. ;;;; ;;;; ;;;; ;;;; Takehiko Nagakura (e-mail: takehiko@mit.edu) ;;;; ;;;; Massachusetts Institute of Technology ;;;; ;;;; 77 Massachusetts Ave. 10-472M, Cambridge, MA 02139 ;;;; ;;;; ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Original Feb 21, 1997 by TN ;;;; Last updated Dec 10, 1998 by TN ; This file provides a function to write/read ; a data set attached to an entity. ; ; The program works in AutoCAD R13. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; The following 3 functions let you write/read your extended ; data set to and from 'entity. ; To write/read a set of data of your own to an entity, you will ; need to register a name of your data set first, so that you ; can distinguish it from other people's data. These data sets ; specified by the users other than the standard AutoCAD data ; is called Extended Data (xdata). ; 'app_name is the name of your extended data set. It must be ; registered with (regapp "xxx") command before you manipulate ; your extended data set (see below for example). Use capital ; leters for this name. ; ; function ; (X_print_entity_data app_name entity) ; (X_get_data_for_group_code app_name group_code entity) ; (X_change_data_for_group_code app_name group_code entity new_value) ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This prints regular entity data plus the extended data set ; specified by app_name (defun X_print_entity_data (app_name entity / all_data) (setq all_data (entget entity (list app_name))) (while all_data (print (car all_data)) (setq all_data (cdr all_data)) )) ; This extracts a data entry specified by the group_code. ; An extended data set can consist of multiple data entry ; and each entry is associated with a 'group_code. ; Group_code also indicates the type of data that you can ; store. Refer to AutoCAD Customization Manual, page 274 ; for more detail. For example, ; ; group_code type of the value stored ; 1000 a string value ; 1010 three real values, contained in a point ; 1040 a real value ; 1070 a 16-bit integer ; (defun X_get_data_for_group_code (app_name group_code entity / all_data -3_data app_data pair_list) (setq all_data (entget entity (list app_name))) (setq -3_data (assoc -3 all_data)) ; this list starts with -3 (setq app_data (assoc app_name (cdr -3_data))) ; this list starts with app_name (setq pair_list (assoc group_code (cdr app_data))) ; a data associated with group_code ;(print -3_data) (print app_data) (print pair_list) ; use this for debug (cdr pair_list) ; return value of this function ) ; This writes a data entry specified by the group_code. ; It tries to ; 1. write a data entry into appropriate position in ; the whole entity data ; 2. if there is no exisiting data entry that corresponds ; to the group_code and the app_name, it creates one. ; Otherwise, the old one is replaced. (defun X_change_data_for_group_code (app_name group_code entity new_value / old_all_data pair_list ) (setq old_all_data (entget entity (list app_name))) (setq old_-3_data (assoc -3 old_all_data)) (setq old_app_data (assoc app_name (cdr old_-3_data))) (setq old_pair_list (assoc group_code (cdr old_app_data))) (setq new_pair_list (cons group_code new_value)) (setq new_app_data (cond (old_pair_list (subst new_pair_list old_pair_list old_app_data)) (old_app_data (append old_app_data (list new_pair_list))) (t (list app_name new_pair_list)))) (setq new_-3_data (cond (old_app_data (subst new_app_data old_app_data old_-3_data)) (old_-3_data (append old_-3_data (list new_app_data))) (t (list -3 new_app_data)))) (setq new_all_data (cond (old_-3_data (subst new_-3_data old_-3_data old_all_data)) (t (append old_all_data (list new_-3_data))))) ;(print new_pair_list) (print new_app_data) (print new_-3_data) (print new_all_data) (entmod new_all_data) ; this actually replaces the old data with the new one (entupd entity) ; updates the graphics and regenerates the drawing new_value ; returns the new value ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Now try this example ; ;1) First, I will register a name of application. (setq 4207app "4207TUTORIAL") ; make sure to use all capital letters! (regapp 4207app) ;2) Sets up some circles encoded with data (command "zoom" (list -85 -85) (list 210 210)) ;Sets up the first circle (command "circle" '(0 0) 60) (setq col_1 (entlast)) (X_change_data_for_group_code 4207app 1000 col_1 "a marble column") ; group code 1000 is for storing string (X_change_data_for_group_code 4207app 1010 col_1 (list 0.0 0.0 0.0)) ; group code 1010 is for storing 3D point (X_change_data_for_group_code 4207app 1070 col_1 0) ; group code 1070 is for storing integer ;The second circle (command "circle" '(120 20) 40) (setq col_2 (entlast)) (X_change_data_for_group_code 4207app 1000 col_2 "a concrete column") (X_change_data_for_group_code 4207app 1010 col_2 (list 120.0 20.0 0.0)) (X_change_data_for_group_code 4207app 1070 col_2 0) ;The third circle (command "circle" '(70 150 0) 30) (setq col_3 (entlast)) (X_change_data_for_group_code 4207app 1000 col_3 "a granite column") (X_change_data_for_group_code 4207app 1010 col_3 (list 70.0 150.0 0.0)) (X_change_data_for_group_code 4207app 1070 col_3 0) ;(X_print_entity_data 4207app col_1) ;3) some demo functions ; This generates a random number between -10 and 10. ; See the tutorial of random number generator for detail. (setq *SeedRand* nil) ; initialize the global (defun rand20 ( / s rand16) (if (null *SeedRand*) (progn (setq s (getvar "date")) (setq *SeedRand* (fix (* 86400 (- s (fix s))))) ) ; progn ) ; if (setq *SeedRand* (+ (* *SeedRand* 1103515245) 12345)) (setq rand16 (logand (/ *SeedRand* 65536) 32767)) (- (rem rand16 40) 20) ) ; This is copied from the entdata_01.lsp of the tutorial. (defun change_data_for_group_code (group_code entity new_value / old_all_data pair_list the_data) (setq old_all_data (entget entity)) (setq old_pair_list (assoc group_code old_all_data)) (setq new_pair_list (cons group_code new_value)) (setq new_all_data (subst new_pair_list old_pair_list old_all_data)) (entmod new_all_data) (entupd entity) ) ; This moves and scales the circle randomly. (defun c:shaffle (/ entity size s) (prompt "Click and select an entity:") (setq entity (car (entsel))) (if (null entity) (progn (terpri) (princ "No entity selected.") (princ)) (command "move" entity "" '(0 0) (list (rand20) (rand20) (rand20)) "") ) ) (defun c:reset (/ entity pos) (prompt "Click and select an entity:") (setq entity (car (entsel))) (if (null entity) (progn (terpri) (princ "No entity selected.") (princ)) (progn (setq pos (X_get_data_for_group_code 4207app 1010 entity)) (change_data_for_group_code 10 entity pos) )) ) (defun c:what ( / entity name pos qtime) (prompt "Click and select an entity:") (setq entity (car (entsel))) (if (null entity) (progn (terpri) (princ "No entity selected.") (princ)) (progn (setq name (X_get_data_for_group_code 4207app 1000 entity)) (setq pos (X_get_data_for_group_code 4207app 1010 entity)) (setq qtime (X_get_data_for_group_code 4207app 1070 entity)) (terpri) (princ "I am ") (princ name) (princ ". ") (princ "My original position was ") (princ pos) (princ ".") (terpri) (terpri) (cond ((= qtime 0) (princ "You asked about me for the first time!")) ((= qtime 1) (princ "You have once asked me this question before.")) (t (princ "You have asked me this for ") (princ qtime) (princ " times.")) ) (if (>= qtime 3) (progn (terpri) (princ "Do you have a memory problem? Stop asking me any more."))) (X_change_data_for_group_code 4207app 1070 entity (+ qtime 1)) (princ)) ) ; if ) ; defun