Find Coordinates of Image Tiles (AutoLISP Solutions AutoCAD Tutorial)1 Jan, 2007 By: Tony Hotchkiss
Program lists the name and coordinates of a set of image tiles.
Kelly Francis of Salt Lake City requested a routine to produce a text file that lists the name and coordinates of a set of image tiles. The image tiles are constructed to fit over a drawing, as in the figure below. The text file must be space-delimited, and the fields are the name of the rectangular tile, the minimum x, maximum y, maximum x and minimum y coordinates for each image tile.
A drawing with image tiles in place.
COORDS-LIST.LSP also lets you choose the precision of the coordinates. The program automatically selects all rectangles in the current drawing.
Get the Code
Download the COORDS-LIST.LSP file from Cadalyst's CAD Tips site and save it in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then select the COORDS-LIST.LSP file from where you stored it.
How to Use the COORDS-LIST.LSP Code
To start the program, enter COO and you will see the Precision prompt.
When you start the program, you see the Precision prompt.
Enter the required precision as 0, 0.0, 0.00, etc. for the number of decimal places you want displayed in the output file. You may also leave off the 0 before the decimal point and enter 0, .0, .00, etc. instead. You will then see the Results File dialog box.
The Results File dialog box.
Browse to any required folder and enter the name of the output text file for the list of tile names and coordinates. If you select an existing file, you'll see the Results File Warning.
The Results File Warning, letting you know that you've chosen a filename that already exists.
You can either overwrite the existing file, or supply a different name. In either case, the file is created as a text file suitable for opening with the Notepad text editor.
A list of names and coordinates in the Notepad format.
The program starts with my usual error handler and system variable management functions. If an error occurs during the program for any reason, such as a cancellation by the user, any system variables that have been changed are reset to their previous values by calling the RESETTING function.
Program Major Functions
The main function is COORDS, which begins with some standard code to initialize the program to recognize Visual LISP functions and sets global variables for the current drawing, model space and paper space. The COORDS function is short enough to list here completely.
(defun coords (/ ssets ssetcount ssobj)
(setq *thisdrawing* (vla-get-activedocument
) ;_ end of vla-get-activedocument
*modelspace* (vla-get-ModelSpace *thisdrawing*)
*paperspace* (vla-get-PaperSpace *thisdrawing*)
) ;_ end of setq
(setq *prec* (getstring "\nPrecision (0, 0.0, 0.00, etc): "))
(setq ssets (vla-get-selectionsets *thisdrawing*)
ssetcount (vla-get-count ssets)
) ;_ end of setq
(if (> ssetcount 0)
(vla-delete (vla-item ssets 0))
) ;_ end of repeat
) ;_ end of if
(setq ssobj (vla-add ssets "selection1"))
(vla-select ssobj acSelectionSetAll)
(setq xlist (get-xlist ssobj))
) ;_ end of coords
The GETSTRING function is used to return the precision as a number of decimal places. This is followed by some housekeeping to delete any selection sets that may exist in the drawing before making a selection set of all drawing objects. Finally, COORDS calls GET-XLIST to return a list of the image coordinates. The coordinates are written to the text file via a call to WRITE-FILE.
GET-XLIST consists of a repeat loop that extracts the coordinates from a selection set of all drawing polyline objects. A test is done to determine that the polyline objects are rectangles (they have a total of eight elements in the coordinates array) before they are included in the list returned by GET-XLIST. The rectangle handles were included in the list of coordinates in case they may be needed as a unique identifier later in the program. I considered sorting the list of coordinates by reference to the handles, but I decided to sort by the maximum y coordinate instead.
The WRITE-FILE function opens the text file, sorts the coordinate values, applies the required precision as decimal places, adds a sequenced list of names and writes the names and coordinates to the text file. The list of names is created by adding a number to the word SHEET, as follows:
(setq i 0)
(foreach xy xylist
(strcat "\"" "SHEET" (get-number) "\""
(rtos (nth 1 xy) 2 prec)
(rtos (nth 2 xy) 2 prec)
(rtos (nth 3 xy) 2 prec)
(rtos (nth 4 xy) 2 prec)
) ;_ end of strcat
) ;_ end of write-line
) ;_ end of foreach
The GET-NUMBER function creates a new number string with leading zeros added if the number has a single digit:
(defun get-number ()
(setq i (1+ i))
(setq numstr (itoa i))
(if (= (strlen numstr) 1)
(setq numstr (strcat "0" numstr))
) ;_ end of if
) ;_ end of get-number
Note that I have treated "i" as a global variable, and I would normally regard that as poor programming. However, it works because the program is short and simple enough that the "i" variable was not used anywhere else in the program. I can hear you real programmers out there muttering, "Excuses, excuses ..."
As always, I look forward to receiving your comments and requests for AutoLISP Solutions. Contact me using the links below.
Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!