Working with String Data with autolisp in AutoCAD

To create “clean” and effective displays and prompts, we will need to manipulate string data as shown in the functions below.

Explanation: the (strxxx) functions

  • Grouping strings and symbols:

(setq LASTFILE “tryme.txt”)
(setq PS (strcat “\nEnter file name or [” LASTFILE

“]:“))
Command: Enter file name or [tryme.txt]:

  • the system variable DWGNAME holds the extension as well as the name. To remove the extension you can use (strlen) and (substr) to remove the last four characters (the dot and three-letter extension).

(setq NAME (getvar “dwgname”))
(setq NAME (substr NAME 1 (- (strlen NAME) 4)))

  • Sorting a list of strings:

(setq STRINGLIST (list “Charlottesville” “Richmond”  “Norfolk” “Arlington”))
(acad_strlsort STRINGLIST)
(“Arlington” “Charlottesville” “Norfolk” “Richmond”)

PRACTICE

Add a prompt to the TAG.LSP file that will include the last number used if there is one.

(defun c:TAG (/ PT1 PT2 PT3 ANGLE1 RADIUS HOLD)
(setq CE-SAVE (getvar “cmdecho”))
(setvar “cmdecho” 0)
(setq CLAYER-SAVE (getvar “clayer”))
(if (tblsearch “layer” “keynote”)
(setvar “clayer” “Keynote”)
(progn
(command “-layer” “n” “Keynote” “c” “cyan” Keynote” “”)
(setvar “clayer” “Keynote”)
)
)
(if TAGNUMBER
(if (setq HOLD (getint
(strcat “\nTag starting number or [“(itoa TAGNUMBER)”]:”)
) ;_ end of getint
) ;_ end of setq (setq TAGNUMBER HOLD)
) ;_ end of inner if
(setq TAGNUMBER (getint “\nTag starting number: “))
) ;_ end of outer if

(while (setq PT1
(getpoint “\nPick the leader endpoint [Press Enter to finish]: ” )
) ;_ end of setq
(setq PT2 (getpoint PT1 “\nPick the tag center point: “))
(setq RADIUS (/ (getvar “dimscale”) 4.0))
(setq ANGLE1 (angle PT2 PT1))
(setq PT3 (polar PT2 ANGLE1 RADIUS))
(command “line” PT1 PT3 “” “circle” PT2 RADIUS “text” “m” PT2 (* (getvar “dimscale”) 0.25) 0 (itoa TAGNUMBER))
(setq TAGNUMBER (1+ TAGNUMBER))
) ;_ end of while
(setvar “cmdecho” CE-SAVE)
(setvar “clayer” CLAYER-SAVE)
(redraw)
(princ)
) ;_ end of defun

  • Note that we did not include the TAGNUMBER with the local variables in the function definition. This is so we can include the last number from the previous use as the next number in our prompt.
  • A complete solution to this exercise is on your class disk as TAG-E.LSP
See also  Every List is Evaluated in the Same Specific Manner in Autolisp
Back to top button