Código Fuente‎ > ‎

Capítulo 2.lsp

;;;Código fuente del libro "Experto AutoCAD con Visual LISP"
;;; (c) Ediciones ARTUAL, S.L. Barcelona, España.
;;; (c) 2012-2020 Reinaldo Togores. Todos los derechos reservados
;;; Se permite su uso mencionando la obra y su autor.
;;;Capítulo 2.  Para comenzar: un Proyecto paso a paso

;;; Debe establecerse el estilo de texto "Standard"
;;; patra habilitar las opciones correctas en el comando TEXTO.
(setvar "TEXTSTYLE" "Standard")

;;;Función numera-dicc:
;;;Crea el diccionario si no existiera,
;;;con valores por defecto.
(defun numera-dicc () 
  (vl-load-com)
  (if (not (dictsearch (namedobjdict) "VARS-NUMERA")) 
    (progn (vlax-ldata-put "VARS-NUMERA" "CIFRA" 0) 
           (vlax-ldata-put "VARS-NUMERA" "INCREM" 10)
           (vlax-ldata-put 
             "VARS-NUMERA"
             "ALTURA"
             (getvar "TEXTSIZE")))))
;;;Figura 2.7. Función numera-dicc.

;;;Incrementa el valor de CIFRA
;;;guardado en "VARS-NUMERA"
(defun num-prox () 
  (vlax-ldata-put 
    "VARS-NUMERA"
    "CIFRA"
    (+ (vlax-ldata-get "VARS-NUMERA" "CIFRA") 
       (vlax-ldata-get "VARS-NUMERA" "INCREM"))))
;;;Figura 2.8. Función num-prox.

;;;Función genérica para dibujar textos
(defun dib-texto (pt-ins altura numeracion / ant-osm) 
  (setq ant-osm (getvar "OSMODE"))
  (setvar "OSMODE" 0)
  (princ)
  (command "._TEXT" pt-ins altura "" numeracion)
  (setvar "OSMODE" ant-osm))
;;;Figura 2.10. Función para dibujo del texto.

;;;Define un nuevo comando de AutoCAD
;;;que dibuja números incrementados 
;;;por un valor fijo
(defun C:NUMERA (/ pto-ins) 
  (numera-dicc)
  (setq pto-ins (getpoint 
                  (strcat 
                    "\nPosición para el Núm. "
                    (itoa (vlax-ldata-get "VARS-NUMERA" "CIFRA"))
                    ": ")))
  (while pto-ins 
    (dib-texto 
      pto-ins
      (vlax-ldata-get "VARS-NUMERA" "ALTURA")
      (itoa (vlax-ldata-get "VARS-NUMERA" "CIFRA")))
    (num-prox)
    (setq pto-ins (getpoint 
                    (strcat 
                      "\nPosición para el Núm. "
                      (itoa (vlax-ldata-get "VARS-NUMERA" "CIFRA"))
                      ": "))))
  (princ))
;;;Figura 2.11. Función C:NUMERA.

;;;Función que modifica los valores en el Diccionario
(defun C:NUM-OPCIONES (/ opcion) 
  (numera-dicc)
  (if 
    (setq opcion (getdist 
                   (strcat "\nNueva altura de texto<" 
                           (rtos (vlax-ldata-get "VARS-NUMERA" "ALTURA"))
                           ">: ")))
    (vlax-ldata-put "VARS-NUMERA" "ALTURA" opcion))
  (if 
    (setq opcion (getint 
                   (strcat 
                     "\nNuevo incremento<"
                     (itoa (vlax-ldata-get "VARS-NUMERA" "INCREM"))
                     ">: ")))
    (vlax-ldata-put "VARS-NUMERA" "INCREM" opcion))
  (initget "Seleccionar Teclear")
  (setq opcion (getkword "\n¿Teclear o <Seleccionar> el número inicial?: "))
  (cond 
    ((= opcion "Teclear")
     (initget 1)
     (vlax-ldata-put 
       "VARS-NUMERA"
       "CIFRA"
       (getint "\nComenzar con: ")))
    (t
     (setq opcion (car (entsel "\Numerar a continuación de: ")))
     (if opcion 
       (num-lee opcion))))
  (princ))
;;; Figura 2.12. Función C:NUM-OPCIONES.

;;;Función que lee los datos del texto
;;;Recibe: nombre de la entidad del texto seleccionado
;;;Devuelve: el valor del texto
(defun num-lee (nom-ent) 
  (while (not (= (getpropertyvalue nom-ent "LocalizedName") "Texto")) 
    (setq nom-ent (car 
                    (entsel "\nEsto NO es un texto, seleccione de nuevo: "))))
  (if (setq val (distof (getpropertyvalue nom-ent "TextString"))) 
    (vlax-ldata-put 
      "VARS-NUMERA"
      "CIFRA"
      (+ (fix val) 
         (vlax-ldata-get "VARS-NUMERA" "INCREM")))
    (prompt "\n¡El texto seleccionado no es un número!")))
;;;Figura 2.13 Función NUM-LEE.

(defun num-lee (nom-ent / nuevo-num) 
  (while 
    (or 
      (/= (getpropertyvalue nom-ent "LocalizedName") "Texto")
      (null 
        (setq nuevo-num (distof (getpropertyvalue nom-ent "TextString")))))
    (setq nom-ent (car 
                    (entsel "\nEsto NO es un número, seleccione de nuevo: "))))
  (vlax-ldata-put 
    "VARS-NUMERA"
    "NUMBER"
    (+ (fix nuevo-num) 
       (vlax-ldata-get "VARS-NUMERA" "INCREM"))))
;;;Figure 2.13. Función num-lee (alternativa)