;;; Source code from the book "AutoCAD expert's Visual LISP"
;;; (c) Ediciones ARTUAL, S.L. Barcelona, EspaƱa.
;;; Copyright © 2012-2020 by Reinaldo N. Togores. All rights reserved.
;;; Use of this code is allowed mentioning the book and its author.
;;;Chapter 2. To Begin: A Project, Step by Step
;;; The textstyle "Standard" must be set to enable ;;; the correct options in the TEXT command.
(setvar "TEXTSTYLE" "Standard")
;;;Function numera-dict: ;;;Creates the dictionary if not created yet,
;;;with default values.
(defun numera-dict ()
(vl-load-com)
(if (not (dictsearch (namedobjdict) "VARS-NUMERA"))
(progn (vlax-ldata-put "VARS-NUMERA" "NUMBER" 0)
(vlax-ldata-put "VARS-NUMERA" "INCREM" 10)
(vlax-ldata-put
"VARS-NUMERA"
"HEIGHT"
(getvar "TEXTSIZE")))))
;;;Figure 2.7. Function NUMERA-DICT.
;;;Increments the value of NUMBER
;;;stored in "VARS-NUMERA"
(defun next-num ()
(vlax-ldata-put
"VARS-NUMERA"
"NUMBER"
(+ (vlax-ldata-get "VARS-NUMERA" "NUMBER")
(vlax-ldata-get "VARS-NUMERA" "INCREM"))))
;;;Figure 2.8. Function NEXT-NUM.
;;;Generic text-drawing function.
(defun draw-text (pt-ins height numbering / old-osm)
(setq old-osm (getvar "OSMODE"))
(setvar "OSMODE" 0)
(princ)
(command "._TEXT" pt-ins height "" numbering)
(setvar "OSMODE" old-osm))
;;;Figure 2.10. Text drawing function.
;;;C:NUMERA function.
;;;Defines a new AutoCAD command ;;;that draws numbers incremented by a fixed value
(defun C:NUMERA (/ pt-ins)
(numera-dict)
(setq pt-ins (getpoint
(strcat
"\nPosition for Num. "
(itoa (vlax-ldata-get "VARS-NUMERA" "NUMBER"))
": ")))
(while pt-ins
(draw-text
pt-ins
(vlax-ldata-get "VARS-NUMERA" "HEIGHT")
(itoa (vlax-ldata-get "VARS-NUMERA" "NUMBER")))
(next-num)
(setq pt-ins (getpoint
(strcat
"\nPosition for Num. "
(itoa (vlax-ldata-get "VARS-NUMERA" "NUMBER"))
": "))))
(princ))
;;;Figure 2.11. C:NUMERA function.
;;;Function that modifies default values
(defun C:NUM-OPTIONS (/ option)
(numera-dict)
(if
(setq option (getdist
(strcat "\nNew Text height:<"
(rtos (vlax-ldata-get "VARS-NUMERA" "HEIGHT"))
">: ")))
(vlax-ldata-put "VARS-NUMERA" "HEIGHT" option))
(if
(setq option (getint
(strcat
"\nNew increment <"
(itoa (vlax-ldata-get "VARS-NUMERA" "INCREM"))
">: ")))
(vlax-ldata-put "VARS-NUMERA" "INCREM" option))
(initget "Choose Type")
(setq option (getkword "\nInitial number [Type/Choose]: " ))
(cond
((= option "Type")
(initget 1)
(vlax-ldata-put
"VARS-NUMERA"
"NUMBER"
(getint "\nBegin with: ")))
(t
(setq option (car (entsel "\nContinue numbering from: ")))
(if option
(num-read option))))
(princ))
;;; Figure 2.12. C:NUM-OPTIONS function.
;;;Function that reads text data
;;;Argument: selected text's entity name
;;;Sets the new NUMBER value in VARS-NUMERA.
(defun num-read (ent-name)
(while
(and ent-name
(not (= (cdr (assoc 0 (entget ent-name))) "TEXT")))
(setq ent-name (car
(entsel "\nThis is NOT a text, choose again: "))))
(if
(and ent-name
(distof (cdr (assoc 1 (entget ent-name)))))
(vlax-ldata-put
"VARS-NUMERA"
"NUMBER"
(+ (atoi (cdr (assoc 1 (entget ent-name))))
(vlax-ldata-get "VARS-NUMERA" "INCREM")))))
;;;Figure 2.13. Function num-read.
(defun num-read (ent-name / new-num)
(while
(or
(/= (getpropertyvalue ent-name "LocalizedName") "Text")
(null
(setq new-num (distof (getpropertyvalue ent-name "TextString")))))
(setq ent-name (car
(entsel "\nThis is NOT a number, choose again: "))))
(vlax-ldata-put
"VARS-NUMERA"
"NUMBER"
(+ (fix new-num)
(vlax-ldata-get "VARS-NUMERA" "INCREM")))) ;;;Figure 2.13. Function num-read (alternative)
No comments:
Post a Comment