You are on page 1of 30

Visual LISP for Building Services Contractors

Dale Goody
dale.goody@yahoo.com

Table Of Contents
Disclaimer of Warranty ........................................................................................................................................................................ 4
Introduction ......................................................................................................................................................................................... 5
Program Files ...................................................................................................................................................................................... 6
File layers ................................................................................................................................................................................................................................... 7
Create a new Layer ................................................................................................................................................................................................................. 7
On and Thawed ....................................................................................................................................................................................................................... 9
File linetypes ............................................................................................................................................................................................................................ 10
Load Linetype ........................................................................................................................................................................................................................ 10
File styles ................................................................................................................................................................................................................................. 12
Create Text Style TTF ........................................................................................................................................................................................................... 12
File sheetInformation................................................................................................................................................................................................................ 13
Drawing Name....................................................................................................................................................................................................................... 13
User ....................................................................................................................................................................................................................................... 14
Checked ................................................................................................................................................................................................................................ 15
Date ....................................................................................................................................................................................................................................... 16
File utils .................................................................................................................................................................................................................................... 17
Convert Viewport ................................................................................................................................................................................................................... 17
Convert Leaders .................................................................................................................................................................................................................... 18
Convert Degrees to Radians ................................................................................................................................................................................................. 19
Dump objects Properties and Methods ................................................................................................................................................................................. 19
File text..................................................................................................................................................................................................................................... 20
Convert Text Style ................................................................................................................................................................................................................. 20
File eraseSheetItems ............................................................................................................................................................................................................... 21
Erase Sheet Items ................................................................................................................................................................................................................. 21
Points List .............................................................................................................................................................................................................................. 22
Write Points ........................................................................................................................................................................................................................... 23

Read Points ........................................................................................................................................................................................................................... 24


File drawingSheets................................................................................................................................................................................................................... 26
Main Input to the program ..................................................................................................................................................................................................... 26
Determine Sheet.................................................................................................................................................................................................................... 26
Determine Path...................................................................................................................................................................................................................... 27
Exchange Drawing Sheets .................................................................................................................................................................................................... 28

Disclaimer of Warranty
Please feel free to use, modify or distribute the following program/functions, however, note the following disclaimer:
This software is provided by the copyright holder as is and any express or implied warranties, including, but not limited to, the implied warranties of
merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner be liable for any direct, indirect, incidental, special,
exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services, loss of use, data or profits, or business
interruption however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out
of the use of this software, even if advised of the possibility of such damage.

Introduction
This document contains a collection of LISP files, each containing a selection of LISP functions that I use as a program to update AutoCAD drawings. I
combine the LISP functions with MS-DOS batch files and AutoCAD Scripts to run the files over an unlimited number of AutoCAD drawings, updating them
automatically without user interaction. I use the LISP routines and variations of them to convert consultants Design drawings into contractors As Built or
Construction drawings. The program provided will only work for Design Consultants Drawings whose drawing sheet is a block that contains attributes. The
contractors drawing sheet (block) to insert must be drawn in advance and any xrefs to be used must be prepared (cleaned up from Architects GAs) in
advance. The program performs the following tasks:

Exchanges the consultants drawing sheet with the contractors drawing sheet, extracting all title information from the consultants sheet and inserting it
into the contractors sheet
Obtains information from the system such as user, date and drawing name and inserts the information into the sheet
Converts all viewport and leader layers to company standards
Converts the style and layer of all text in the drawing to chosen text style/layer convention
Erases unwanted data from the drawings

The program below is used to produce a set of As Built drawings. If a set of Construction drawings is required (in preparation for coordination/details/sections
etc), the same procedure as below is followed, but with the addition of a routine that determines what floor the drawing relates to and then inserting the
appropriate xref.
Some functions are generic and could be adapted for use by Architects/Consultants etc and take a number of parameters passed to the function. Others are
specific and are hard coded due to the fact that this suits specific other tasks in which they are used.
This example program when combined with DOS and Scripts will open AutoCAD, open a drawing, perform the tasks listed above, save the drawing and then
repeat the process for any number of selected drawings. It takes approximately 20 seconds per drawing and although only prepares the drawings ready for
engineering, e.g. coordination/As Fitted, it does take away the element of CAD labour which would otherwise be involved. The functions have been used to
produce a set of 150 As Built drawings in approximately 50 minutes and 200 Construction drawings in approximately 75 minutes (due to xref insertion,
additional layers rqd etc).
Functions that can be called from the AutoCAD command line are preceded by a C (e.g. C:theFunctionName) and can be attached to custom toolbars and
menus simply by typing the function name in the appropriate box in the AutoCAD CUI Menu (the files must be set up as listed below in the Program Files
section). LISP functions do provide time/cost savings when used in this way, running over an individual drawing, but come into their own when they run over
multiple drawings. For example, the eraseSheetItems file contains routines that are basically the AutoCAD erase method. But, the functions store window
points of objects that are not required in Consultants drawings that are often located in the same/similar place on the drawing sheet and erases items within
these windows across multiple drawings.

The main input to the program is entered from within the LISP file drawingSheets . The data could also be entered using DCL and writing the data to a text
file or entered from the command line, but it serves its purpose as is as the functions are only used within a small CAD Dept. Perhaps if multiple users used
the program, DCL/an alternative user interface would be beneficial.

Program Files
The program files are contained in a folder named LISP that is in the AutoCAD support file search path. Within this folder I have a file named acaddoc.lsp
that automatically loads any functions/files it contains into a drawing, when the drawing opened. For this program, the acaddoc file contains nothing more
than load statements that load any required LISP files, that must be contained within the same LISP folder. The load statements are as follows:
(load "utils" "\nError loading file utils")
This loads in the utils.lsp file contained in the LISP folder and any functions within the utils folder into the drawing, such as C:ConvertViewport,
C:ConvertLeaders, UTIL:Deg->Rad and C:Dump. In maintaining LISP files in this way all functions are available every time you open a drawing. It is essential
you load files in this way if they contain functions that are linked with custom toolbar buttons and menus. Storing all functions relating to a specific task e.g.
layers or text in a folder with that name and loading that file from acaddoc.lsp keeps file sizes down and aids in debugging.
Each LISP file also contains a vl-load-com statement to avoid calling this in individual functions and (if required) a load statement to load any functions that
are required by that file.
This program is just one example of how LISP can be used to automate AutoCAD tasks, resulting in increased productivity/cost savings.

File layers
(vl-load-com)
(load "linetypes" "\nError loading file linetypes")
Create a new Layer
;********************************************************************************************
;
defun LA:CREATELAYER
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Creates a new layer with various attributes
;********************************************************************************************
; Parameters: name(string) - the new layer name
;
color(int) - AutoCAD index color
;
linetype(string) - a valid linetype from the 'acadiso.lin' file in the AutoCAD
;
support file search path
;
plottable(boolean) - T for plottable, nil for non-plottable layer
;
locked(boolean) - T for locked layer, nil for unlocked layer
;********************************************************************************************
; Example: (LA:CREATELAYER "NewLayerName" 8 "Continuous" T T) creates a new layer name with
;
color 8, the continuous linetype, that is plottable and locked
;********************************************************************************************
(defun LA:CREATELAYER (name color linetype plottable locked / layCol lay res)
(setq doc (vla-get-activedocument (vlax-get-acad-object))
layCol (vla-get-layers doc))
(if (not (tblsearch "Layer" name));if layer does not exist
(progn
(setq lay (vla-add layCol name));add new layer name to layers collection
(vla-put-color lay color);assign color to layer
(if (not plottable)
(vla-put-plottable lay :vlax-false);make layer non-plottable

);if
(if locked
(vla-put-lock lay :vlax-true);lock the layer
);if
(if (tblsearch "lType" linetype);if linetype exists
(progn
(setq res (vl-catch-all-apply
'vla-put-linetype (list lay linetype)));add the linetype to the new layer
(if (vl-catch-all-error-p res);if an error occurred
(progn
(princ (strcat "\nError: Could not assign linetype to layer";error message
"\nLisp File: Layers";file where error occurred
"\nFunction: LA:CREATELAYER";function where error occurred
"\nMethod: vla-put-linetype";method that failed
"\nVariable: res\n";variable storing error object
(vl-catch-all-error-message res));error message
);princ
);progn
);if
);progn
(progn;if linetype does not exist
(LI:LOADLINETYPE linetype);load the required linetype (file:linetypes)
(setq res (vl-catch-all-apply
'vla-put-linetype (list lay linetype)));add the linetype to the new layer
(if (vl-catch-all-error-p res);if an error occurred
(progn
(princ (strcat "\nError: Could not assign linetype to layer";error message
"\nLisp File: Layers";file where error occurred
"\nFunction: C:CREATELAYER";function where error occurred
"\nMethod: vla-put-linetype";method that failed
"\nVariable: res\n";variable storing error object
(vl-catch-all-error-message res));error message
);princ
(vla-delete lay);delete the new layer if error occurred

);progn
);if
);progn
);if
);progn
(princ "\nlayer exists")
);if
(princ)
);end LA:CREATELAYER
;********************************************************************************************
On and Thawed
;********************************************************************************************
;
defun LA:ONANDTHAWED
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Determines if an objects layer is on and thawed
;********************************************************************************************
; Parameters: obj(vla-object) - the object to be checked
;********************************************************************************************
; Returns: variable 'result' - True if objects layer is on and thawed and nil if objects
;
layer is either turned off or frozen
;********************************************************************************************
; Example: (LA:ONANDTHAWED (any valid vla-object))
;********************************************************************************************
(defun LA:ONANDTHAWED (obj / theLayers layerName layerObj i
frozen on result)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq theLayers (vla-get-layers doc));get layers collection
(setq layerName (vla-get-layer obj));get layer name of object
(vlax-for i theLayers

(if (= layerName (vla-get-name i));search for object layer in collection


(progn ;if layer is found
(setq layerObj i) ;get the vla-layer
(setq frozen (vla-get-freeze layerObj));check if frozen
(setq on (vla-get-layerOn layerObj));check if on
);progn
);if
);vlax-for
(if (and (= frozen :vlax-false) on);if layer is thawed and on
(setq result T)
(setq result nil);if layer is frozen or turned off
);if
result
);end LA:ONANDTHAWED
;********************************************************************************************

File linetypes
(vl-load-com)
Load Linetype
;********************************************************************************************
;
defun LI:LOADLINETYPE
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Loads a new linetype into the drawing
;********************************************************************************************
; Parameters: lTypeName(string) a valid linetype name to be loaded. Linetype must exist in
;
the 'acadiso.lin' file that is in the AutoCAD support file search path
;********************************************************************************************
; Example: (LI:LOADLINETYPE "CENTER")
;********************************************************************************************

10

(defun LI:LOADLINETYPE (lTypeName / linTypeCol res)


(setq doc (vla-get-activedocument (vlax-get-acad-object))
linTypeCol (vla-get-linetypes doc))
(setq exists (tblsearch "lType" lTypeName));check if linetype is loaded
(if (not exists);if not loaded
(setq res (vl-catch-all-apply
'vla-load (list linTypeCol lTypeName "acadiso.lin")));;load linetype checking
(if (vl-catch-all-error-p res);if error
for error;;
(princ (strcat "\nError: Could not load new linetype";error message
"\nLisp File: Layers";file where error occurred
"\nFunction: C:CREATELINETYPE";function where error occurred
"\nMethod: vla-load";method that failed
"\nVariable: res\n";variable storing error object
(vl-catch-all-error-message res));error message
);princ
);if
);if
(princ)
);end LI:LOADLINETYPE
;********************************************************************************************

11

File styles
(vl-load-com)
Create Text Style TTF
;********************************************************************************************
;
defun ST:CREATETEXTSTYLETTF
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Creates a new text style using a true type font
;********************************************************************************************
; Parameters: styleName(string) - the new text style name
;
font(string) - the name of the true type font (found in the Windows Control
;
Panel 'Fonts' folder, omit the .ttf extension)
;********************************************************************************************
; Example: (ST:CREATETEXTSTYLETTF "NewTextStyleName" "Arial")
;********************************************************************************************
(defun ST:CREATETEXTSTYLETTF (styleName font / styleCol path)
(setq doc (vla-get-activedocument (vlax-get-acad-object))
styleCol (vla-get-textstyles doc);textstyles collection
path (strcat "C:\\WINDOWS\\Fonts\\" font ".ttf")
);setq
(setq newStyle (vl-catch-all-apply
'vla-add (list styleCol styleName)));add stylename to collection
(if (vl-catch-all-error-p newStyle);if error adding new style
(princ (strcat "\nError: Could not create new textstyle";error message
"\nLisp File: Text";file where error occurred
"\nFunction: C:CREATETEXTSTYLETTF";function where error occurred
"\nMethod: vla-add";method that failed
"\nVariable: newStyle\n";variable storing error object
(vl-catch-all-error-message newStyle));activex error message
);princ
(progn;if no error adding new style

12

(setq res (vl-catch-all-apply


'vla-put-fontfile (list newStyle path)));add font file to textstyle
(if (vl-catch-all-error-p res);if error adding fontfile to style
(progn
(princ (strcat "\nError: Could not find/assign font " font
"\nLisp File: Text"
"\nFunction: C:CREATETEXTSTYLETTF"
"\nMethod: vla-put-fontfile"
"\nVariable: res\n"
(vl-catch-all-error-message res))
);princ
(vla-delete newStyle);reset all (delete new style on error)
);progn
);if
);progn
);if
(princ)
);end ST:CREATETEXTSTYLETTF
;********************************************************************************************

File sheetInformation
(vl-load-com)
Drawing Name
;********************************************************************************************
;
defun SI:DRAWINGNAME
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Extracts and formats the drawing name from the saved file name
;********************************************************************************************
; Returns: variable 'name' - The drawing name without the file extension

13

;********************************************************************************************
(defun SI:DRAWINGNAME ( / fullName i fullNameLen position name)
(setq fullName (getvar "DWGNAME"))
(setq i 1)
(setq fullNameLen (strlen fullName));length of drawing name
(repeat fullNameLen
(if (= (substr fullName i 1) ".");search for file name extension location
(progn
(setq position (- i 1));position before the period
(setq name (substr fullName 1 position));return name without extension
);progn
);if
(setq i (+ i 1))
);repeat
name
);end SI:DRAWINGNAME
;********************************************************************************************
User
;********************************************************************************************
;
defun SI:USER
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Obtains and formats the users name
;********************************************************************************************
; Returns: The users initials
;********************************************************************************************

(defun SI:USER ( / userName i nameLen initial1 initial2 position)


(setq userName (getvar "loginname"))

14

(setq i 1)
(setq nameLen (strlen userName));length of user name
(setq initial1 (strcase (substr userName 1 1) nil));first initial
(repeat nameLen
(if (= (substr userName i 1) ".");search for surname
(progn
(setq position (+ i 1));set surname start position
(setq initial2 (strcase (substr userName position 1) nil));get second initial
);progn
);if
(setq i (+ i 1))
);repeat
(strcat initial1 initial2);combine initials
);end SI:USER
;********************************************************************************************

Checked
;********************************************************************************************
;
defun SI:CHECKED
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Simple routine to switch between user initials
;********************************************************************************************
; Returns: variable - 'checked' The person not doing the drawing
;********************************************************************************************
(defun SI:CHECKED ( / drawn checked)
(setq drawn (SI:USER))
(setq checked "")
(if (= drawn "Users initials here")
(setq checked "Other users initials here")
)

15

(if (= drawn " Other users initials here ")


(setq checked " Users initials here ")
)
checked
);end SI:CHECKED
;********************************************************************************************
Date
;********************************************************************************************
;
defun SI:DATE
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Obtains and formats the current date
;********************************************************************************************
; Returns: The formatted current date
;********************************************************************************************
(defun SI:DATE ()
(setq
(setq
(setq
(setq
(setq

dateVar (rtos (getvar "CDATE")))


year (substr dateVar 1 4))
month (substr dateVar 5 2))
day (substr dateVar 7 2))
date (strcat day "." month "." year))

);end SI:DATE
;********************************************************************************************

16

File utils
(vl-load-com)
(load "layers" "\nError loading file layers")
Convert Viewport
;********************************************************************************************
;
defun C:CONVERTVIEWPORT
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Places all Viewports on chosen layer
;********************************************************************************************
(defun C:CONVERTVIEWPORT ()
(LA:CREATELAYER "new layer name here" 2 "Continuous" F F)
(setq vportColl (ssget "X" '((0 . "VIEWPORT"))))
(if vportColl
(progn
(setq ssMax (sslength vportColl))
(setq i 0)
(repeat ssMax
(setq vportEnt (ssname vportColl i))
(setq vportObj (vlax-ename->vla-object vportEnt))
(vla-put-layer vportObj "new layer name here")
(vla-put-color vportObj 2)
(setq i (+ i 1))
);repeat
);progn
(princ "Error in C:CONVERTVIEWPORT: No selection set exists")
);if
(princ)

17

);end C:CONVERTVIEWPORT
;********************************************************************************************
Convert Leaders
;********************************************************************************************
;
defun C:CONVERTLEADERS
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Places all Leaders on chosen layer
;********************************************************************************************
(defun C:CONVERTLEADERS ()
(LA:CREATELAYER "new layer name here" 1 "Continuous" T F)
(setq leadersColl (ssget "X" '((0 . "LEADER"))))
(if leadersColl
(progn
(setq ssMax (sslength leadersColl))
(setq i 0)
(repeat ssMax
(setq leaderEnt (ssname leadersColl i))
(setq leaderObj (vlax-ename->vla-object leaderEnt))
(vla-put-layer leaderObj "new layer name here")
(vla-put-dimensionlinecolor leaderObj acBylayer)
(setq i (+ i 1))
);repeat
);progn
(princ "Error in C:CONVERTLEADERS: No selection set exists")
);if
(princ)
);end defun C:CONVERTLEADERS
;********************************************************************************************

18

Convert Degrees to Radians


;********************************************************************************************
;
defun UTIL:DEG->RAD
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Converts degrees to radians
;********************************************************************************************
(defun UTIL:DEG->RAD (degrees)
(* pi (/ degrees 180.0))
);end UTIL:DEG->RAD
;********************************************************************************************

Dump objects Properties and Methods


;********************************************************************************************
;
defun C:DUMP
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Programming Aid - Dispalys an objects properties and methods
;********************************************************************************************
(defun C:DUMP ( / ent obj)
(setq ent (entsel))
(setq obj (vlax-ename->vla-object (car ent)))
(vlax-dump-object obj T)
(princ)

19

);end C:DUMP
;********************************************************************************************

File text
(vl-load-com)
(load "layers" "\nError loading file layers")
(load "styles" "\nError loading file styles")
Convert Text Style
;********************************************************************************************
;
defun C:CONVERTTEXTSTYLE
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Selects all visible text and MText in the drawing and converts the textstyle
;
and layer of each text/MText object
;********************************************************************************************
(defun C:CONVERTTEXTSTYLE ( / i filText ssText ssMax entText objText)
(LA:CREATELAYER "new layer name here" 2 "Continuous" T F);from 'layers' file
(ST:CREATETEXTSTYLETTF "new text style name here" "Arial");from 'styles' file
(setq i 0
filText '((-4 . "<OR") (0 . "MTEXT") (0 . "TEXT") (-4 . "OR>"));filter text and MText
ssText (ssget "X" filText);select all text and MText
ssMax (sslength ssText);number of text entities in selection set
)
(if ssText
(repeat ssMax;for each text entity
(setq entText (ssname ssText i));get text entity
(setq objText (vlax-ename->vla-object entText));convert to vla object
(if (LA:ONANDTHAWED objText);only convert if text is on and thawed

20

(progn
(vla-put-stylename objText "new text style name here");change textstyle of text object
(vla-put-layer objText " new layer name here ");assign text object to layer
(vla-put-color objText acByLayer);assign color to text object
(setq i (+ i 1));increment counter and repeat
);progn
(setq i (+ i 1)) ;increment counter and repeat if object is off or frozen
);if
);end repeat
);end if
(princ)
);end C:CONVERTTEXTSTYLE
;********************************************************************************************

File eraseSheetItems
Erase Sheet Items
;********************************************************************************************
;
defun ES:ERASESHEETITEMS
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Reads a list of window points from a file and erases all items within the
;
selection windows
;********************************************************************************************
(defun ES:ERASESHEETITEMS ( / mainPtsList firstPoint secondPoint
ssetLen ent obj i)
(if (setq mainPtsList (ES:READPOINTS));read list of points from file
(progn
(foreach i mainPtsList
(setq firstPoint (nth 0 i)

21

secondPoint (nth 1 i)
sset (ssget "W" firstPoint secondPoint)
ssetLen (ssLength sset)
i 0
)
(repeat ssetLen
(setq ent (ssname sset i)
obj (vlax-ename->vla-object ent))
(vla-delete obj)
(setq i (+ i 1))
);repeat
);foreach
);progn
);if
(princ)
);end ES:ERASESHEETITEMS
;********************************************************************************************
Points List
;********************************************************************************************
;
defun ES:POINTSLIST
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Creates a list of window selection coordinates until the user stops input
;********************************************************************************************
(defun ES:POINTSLIST ( / ptsList pt1 pt2 tempList ptsList)
(vl-load-com)
(setq ptsList nil); create empty list
(while ;user selects window
(and (setq pt1 (getpoint "\nSelect first corner"))
(setq pt2 (getcorner "\nSelect other corner" pt1))); user selects window

22

(setq tempList (list pt1 pt2)); place window points into temporary list
(setq ptsList (cons tempList ptsList)); add temp window points to main list
);while
);end ES:POINTSLIST
;********************************************************************************************
Write Points
;********************************************************************************************
;
defun C:WRITEPOINTS
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Writes a list of window coordinates to a text file
;********************************************************************************************
(defun C:WRITEPOINTS ( / file setOfPoint stPt
xCoordSt yCoordSt zCoordSt oppCorner
xCoordOp yCoordOp zCoordOp)
(setq listOfPoints (ES:POINTSLIST)); get points list
(if (setq file (open "C:\\DG\\Lisp\\points.txt" "W")); open file for writing
(foreach setOfPoint listOfPoints ; for every set of window points
(setq stPt (nth 0 setOfPoint)); get the start point of window
(setq xCoordSt (rtos (nth 0 stPt))); get x coordinate of start point
(write-line xCoordSt file); write x coordinate to file
(setq yCoordSt (rtos (nth 1 stPt))); get y coordinate of start point
(write-line yCoordSt file); write y coordinate to file
(setq zCoordSt (rtos (nth 2 stPt))); get z coordinate of start point
(write-line zCoordSt file); write z coordinate to file
(setq oppCorner (nth
(setq xCoordOp (rtos
(write-line xCoordOp
(setq yCoordOp (rtos

1 setOfPoint)); get the end point of window


(nth 0 oppCorner))); get x coordinate of end point
file); write x coordinate to file
(nth 1 oppCorner))); get y coordinate of end point

23

(write-line yCoordOp file); write y coordinate to file


(setq zCoordOp (rtos (nth 2 oppCorner))); get z coordinate of end point
(write-line zCoordOp file); write z coordinate to file
);foreach
(princ "Error reading file")
);if
(close file)
(princ)
);end C:WRITEPOINTS
;********************************************************************************************
Read Points
;********************************************************************************************
;
defun ES:READPOINTS
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Reads and formats a list of window coordinates from a text file
;********************************************************************************************
(defun ES:READPOINTS ( / mainPtsList readList startpt oppositecorner file
coord listMax iterate count)
(setq mainPtsList nil
readList nil
startpt nil
oppositecorner nil
);use double backslashes for paths e.g. C:\\Folder\\file.ext
(if (setq file (open "full path to .txt file here" "R")); open file for reading
(while (setq coord (read-line file)); read first coordinate
(setq coord (atof coord))
(setq readList (cons coord readList)); construct a list of coordinates
);while
(princ "Error opening file filename")

24

);if
(close file)
(setq listMax 0)
(foreach i readList
(setq listMax (+ listMax 1)); count number of items in list
)
(setq iterate (/ listmax 6)); get number of window coordinates
(setq count 0)
(repeat iterate
(repeat 3 ; create window end point coordinate list
(setq startpt (cons (nth count readList) startpt))
(setq count (+ count 1))
);repeat
(repeat 3 ; create window end point coordinate list
(setq oppositecorner (cons (nth count readList) oppositecorner))
(setq count (+ count 1))
);repeat
(setq pointslist (list startpt oppositecorner); create individual window coordinate list
pointslist (reverse pointslist)
mainPtsList (cons pointsList mainPtsList); add window/additional window coordinates
startpt nil; reset
oppositecorner nil; reset
)
);repeat iterate
mainPtsList
);end ES:READPOINTS

;********************************************************************************************

25

File drawingSheets
(vl-load-com)
(load "sheetInformation" "\nError loading file sheetInformation")
(load "layers" "\nError loading file layers")
Main Input to the program
;********************************************************************************************
; Description: Main Input for the exchanging of drawing sheets.
;
Input data and call (C:EXCHANGEDRAWINGSHEETS) to run
;
This goes at the top of the exchange drawing sheets function
;********************************************************************************************
(setq FINAL_CONSSHEETNAME1 "name of design consultants A0 sheet block here";A0 Sheet Block name, if none ""
FINAL_A0_PATH "full path of A0 block to insert"
FINAL_CONSSHEETNAME2 "name of design consultants A1 sheet block here";A1 Sheet Block name, if none ""
FINAL_A1_PATH "full path of A1 block to insert"
FINAL_CONSTITLE1 "Tag name of attribute value to extract";Tag name of first title line
FINAL_CONSTITLE2 "Tag name of attribute value to extract";Tag name of second title line
FINAL_CONSTITLE3 "Tag name of attribute value to extract";Tag name of third title line
FINAL_APPROVAL "initials of whoever approves drawings"
)
;*******************************************************************************************
Determine Sheet
;********************************************************************************************
;
defun DS:DETERMINESHEET
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Determines whether an A0 or an A1 sheet has been selected
;********************************************************************************************

26

; Returns: Variable 'sheetName' - The name of the drawing sheet block


;********************************************************************************************
(defun DS:DETERMINESHEET ( / sset borderEnt borderObj sheetName)
(if (setq sset (ssget "X" (list (cons 0 "INSERT") (cons 2 FINAL_CONSSHEETNAME1))));if A0
(progn
(setq borderEnt (ssname sset 0))
(setq borderObj (vlax-ename->vla-object borderEnt))
(setq sheetName (vla-get-name borderObj));get A0 Sheet name
);progn
);if
(if (setq sset (ssget "X" (list (cons 0 "INSERT") (cons 2 FINAL_CONSSHEETNAME2))));if A1
(progn
(setq borderEnt (ssname sset 0))
(setq borderObj (vlax-ename->vla-object borderEnt))
(setq sheetName (vla-get-name borderObj));get A1 Sheet name
);progn
);if
sheetName
);end DS:DETERMINESHEET
;********************************************************************************************
Determine Path
;********************************************************************************************
;
defun DS:DETERMINEPATH
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: Determines the path of the drawing sheet block to insert based on
;
return value from (DS:DETERMINESHEET)
;********************************************************************************************
; Returns: Variable 'path' - filepath to the block to insert
;********************************************************************************************

27

(defun DS:DETERMINEPATH ( / sheetName path)


(if (setq sheetName (DS:DETERMINESHEET))
(cond
((= sheetName FINAL_CONSSHEETNAME1)(setq path FINAL_A0_PATH));if A0 sheet
((= sheetName FINAL_CONSSHEETNAME2)(setq path FINAL_A1_PATH));if A1 sheet
);cond
);if
path
);end DS:DETERMINEPATH
;********************************************************************************************
Exchange Drawing Sheets
;********************************************************************************************
;
defun C:EXCHANGEDRAWINGSHEETS
;********************************************************************************************
; Author: Dale Goody
;********************************************************************************************
; Description: The main function for exchanging consultant & contractor drawing sheets
;********************************************************************************************

(defun C:EXCHANGEDRAWINGSHEETS ( / consSheetName ssetx borderEnt borderObj atts path pt bor


atts)
(if (setq consSheetName (DS:DETERMINESHEET));determine A0 or A1 sheet
(progn
(setq doc (vla-get-activedocument (vlax-get-acad-object))
pspace (vla-get-paperspace doc)
;select the consultants drawing sheet
ssetx (ssget "X" (list (cons 0 "INSERT") (cons 2 consSheetName)))
borderEnt (ssname ssetx 0)
borderObj (vlax-ename->vla-object borderEnt)

28

;get attributes and convert to list


atts (vlax-safearray->list (vlax-variant-value (vla-getattributes borderObj)))
);setq
(foreach att atts
(cond ;select all consultants title attributes and extract the values
((and (= (vla-get-tagstring att) FINAL_CONSTITLE1) (/= FINAL_CONSTITLE1 ""))
(setq title1 (vla-get-textstring att)))
((and (= (vla-get-tagstring att) FINAL_CONSTITLE2) (/= FINAL_CONSTITLE2 ""))
(setq title2 (vla-get-textstring att)))
((and (= (vla-get-tagstring att) FINAL_CONSTITLE3) (/= FINAL_CONSTITLE3 ""))
(setq title3 (vla-get-textstring att)))
);cond
);foreach
(vla-delete borderObj);delete consultants drawing sheet
(setq path (DS:DETERMINEPATH);determine A0 or A1 file path for sheet to insert
pt (vlax-3d-point '(0.0 0.0 0.0));sheet insertion point
bor (vla-insertblock pspace pt path 1 1 1 0);insert drawing sheet
;get attributes and convert to list
atts (vlax-safearray->list (vlax-variant-value (vla-getattributes bor)))
)
(LA:CREATELAYER "new sheet layer name" 0 "Continuous" T F);create layer for inserted drawing sheet
(vla-put-layer bor "new sheet layer name")
(foreach j atts
(cond ;select all attributes and insert values
((and (= (vla-get-tagstring j) "TITLE_LINE_1") (/= title1 nil)) your attribute tag names where shown in
red
(vla-put-textstring j title1))
((and (= (vla-get-tagstring j) "TITLE_LINE_2") (/= title2 nil))
(vla-put-textstring j title2))
((and (= (vla-get-tagstring j) "TITLE_LINE_3") (/= title3 nil))
(vla-put-textstring j title3))
((= (vla-get-tagstring j) "DRAWN_BY") (vla-put-textstring j (SI:USER)))
((= (vla-get-tagstring j) "CHECKED_BY") (vla-put-textstring j (SI:CHECKED)))
((= (vla-get-tagstring j) "DRG_NUMBER") (vla-put-textstring j (SI:DRAWINGNAME)))
((= (vla-get-tagstring j) "DRAWN_DATE") (vla-put-textstring j (SI:DATE)))
((= (vla-get-tagstring j) "APPROVED_BY") (vla-put-textstring j FINAL_APPROVAL))

29

);cond
);foreach
);progn
);main if
(princ)
);end C:EXCHANGEDRAWINGSHEETS
;********************************************************************************************

30

You might also like