Professional Documents
Culture Documents
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
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
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
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
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
;********************************************************************************************
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
);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
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
23
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
27
28
29
);cond
);foreach
);progn
);main if
(princ)
);end C:EXCHANGEDRAWINGSHEETS
;********************************************************************************************
30