;---------------------------------------------------------- ; Extended data Utilities and Example of use ; From 1999 Autodesk University course PR31 ; taught by Bill Kramer ; ; Utilities: ; X_DATA_GET - get extended data list from entity ; X_DATA_ADD - add extended data to entity ; X_DATA_DEL - remove extended data from entity ; X_DATA_APPIDS - build list of registered applications ; NTH_SUBST - substitute nth member in list ; ; C:EST - estimation example application shows use of ; extended data to house bill of material info ; about objects. ;----------------------------------------------------------;; ;; X_DATA_GET: Utility to get the extended data ;; list for an entity (EN) for a given application name (APID). ;; (defun X_DATA_GET (EN APID / EL) (setq EL (entget EN (list APID))) (cdadr (assoc -3 EL)) ;;return only data items ) ;;--------------------------------------------------------- ;; X_DATA_ADD add extended data list DLST with ;; application name APID to the entity list ;; of entity name EN. ;; Overwrites existing. ;; (defun X_DATA_ADD (EN APID DLST / EL TMP1) (if (null (tblsearch "APPID" APID)) ;;registered? (regapp APID)) ;;register it (setq EL (entget EN) ;;get Entity list TMP1 (list -3 (cons APID DLST)) ) (if (< (xdsize TMP1) (xdroom EN)) ;;got enough room? (entmod (append EL (list TMP1)))) ;;modify database ) ;;--------------------------------------------------------- ;; X_DATA_DEL - Utlity to remove the extended data for ;; an application (APID) from an entity object (EN). ;; (defun X_DATA_DEL (EN APID / EL TMP1 TMP2) (if (setq TMP2 (X_DATA_APPIDS)) ;;find X data? (progn (setq EL (entget EN TMP2) ;;get ALL data TMP1 (assoc -3 EL) ;;get X data ) ;;is APID in X data info for object? (if (assoc APID (cdr TMP1)) (progn (setq TMP1 (cdr TMP1) ;;take off -3 TMP1 ;;remove existing X data (append ;;rebuild X data list (list -3) (reverse ;;X data before APID (cdr (member (assoc APID TMP1) (reverse TMP1)))) (cdr ;;X data after APID (member (assoc APID TMP1) TMP1))) EL (subst TMP1 (assoc -3 EL) EL) ) (entdel EN) ;;remove previous member (entmake EL))))));;add with modified X data ;;--------------------------------------------------------- ;; X_DATA_APPIDS - Creates a list of all application ;; names in the current drawing by stepping through ;; the APPID table. ;; (defun X_DATA_APPIDS ( / TMP1 TMP2) ;;Build list of X data ID's (setq TMP1 (tblnext "APPID" 'T)) ;;get 1st APPID (while TMP1 ;;loop until no more APPID's in table (setq TMP2 (cons ;;add APPID to list (cdr (assoc 2 TMP1)) TMP2) TMP1 (tblnext "APPID") ;;get next APPID ) ) TMP2 ;;return TMP2 list ) ;;--------------------------------------------------------- ; ; Utility routine for substitution ; in a list at the Nth specified position. ; (defun NTH_SUBST (NN New Dlist) (cond ((null Dlist) nil) ((= NN 0) (cons New (cdr Dlist))) (t (cons (car Dlist) (NTH_SUBST (1- NN) New (cdr Dlist)))))) ; ;;--------------------------------------------------------- ;; ;; C:EST - Estimator application example ;; (defun C:EST ( / TMP DONE) (setq APID "ESTIMATOR") (prompt "\nESTimator tool for AutoCAD.") (while (null DONE) (initget 0 "Sum Edit Look Add Del Xit") (setq TMP (getkword "\nEST: Sum/Edit/Look/Add/Del/: ")) (cond ((or (null TMP) (= TMP "Xit")) (setq DONE 'T)) ((= TMP "Sum") (EST_SUM APID)) ;;see listing 18 ((= TMP "Edit") (EST_EDIT APID)) ;;see listing 15 ((= TMP "Look") (EST_LOOK APID)) ;;see listing 17 ((= TMP "Add") (EST_ADD APID)) ;;see listing 14 ((= TMP "Del") (EST_DEL APID)) ;;see listing 16 ) ) (princ) ) ;--------------------------------------------------------- ; ; Add new Xdata for the estimator. ; (defun EST_ADD (APID / EN T1 T2) (setq EN (car (entsel "\nSelect object: "))) (if EN (progn (setq T1 (getstring 1 "\nDescription: ")) (if (/= T1 "") (progn (setq T2 (getreal "\nAmount: ")) (if T2 (X_DATA_ADD EN APID (list (cons 1000 T1) (cons 1040 T2))))))))) ;;--------------------------------------------------------- ; ; Edit description and amount data. ; (defun EST_EDIT (APID / EN TMP T1 T2) (setq EN (car (entsel "\nSelect object: "))) (if (setq TMP (X_DATA_GET EN APID)) (progn (setq T1 (getstring 1 (strcat "\nDescription <" (cdar TMP) ">: ")) T2 (getreal (strcat "\nAmount <" (rtos (cdadr TMP)) ">: ")) TMP (list (cons 1000 (if T1 T1 (cdar TMP))) (cons 1040 (if T2 T2 (cdadr TMP)))) ) (X_DATA_ADD EN APID TMP)))) ;;--------------------------------------------------------- ; ; Remove estimator data ; (defun EST_DEL (APID / EN) (setq EN (car (entsel "\nSelect object: "))) (if EN (X_DATA_DEL EN APID))) ;;--------------------------------------------------------- ; ; Highlight all objects containing extended data ; (defun EST_LOOK (APID / SS1 TMP) (setq SS1 (ssget "X" (list (list -3 (list APID))))) (if SS1 (progn (setq TMP (sslength SS1)) (repeat TMP (redraw (ssname SS1 (setq TMP (1- TMP))) 3))))) ;;--------------------------------------------------------- ; Sum amount data and output to screen report ; (defun EST_SUM (APID / TMP SUM SS1) (setq SS1 (ssget "X" (list (list -3 (list APID))))) (if SS1 (progn (setq TMP (sslength SS1) SUM 0.0) (repeat TMP (setq TMP (1- TMP) T1 (X_DATA_GET (ssname SS1 TMP) APID) SUM (+ SUM (cdadr T1)))) (prompt (strcat "\nSum total = " (rtos SUM))))))