Sunday, October 16, 2011

Programming/Automating #Autocad LT

As far as I am aware the main reasons for opting for the full version of Acad are:

1) AutoLISP
2) COM automation
3) Database connectivity and SQL
4) Extended data
5) Complex entities/objects, 3D and otherwise

The seemingly most dominant reason that full Acad is chosen is because it is programmable through AutoLISP. The common myth is that AutoLISP is required to program AutoCAD. Since AcadLT does not have AutoLISP it is not programmable or customisable. Wrong!

AcadLT can be customised and its is programmable without any tricks or add-in LISP engines. Forget about the pretty toolbars, AutoCAD has a command langauge, and supports script files for those commands. The command language and script support is not as powerful as that in say the old DBase II/III, but it is none the less available.

The common criticism aimed at scripting is that it cannot do anything. For example to draw a line, the script would be:

Line 0,0 1000,1000

where as in AutoLISP it would be:

(command "line" pt1 pt2 "")

Where pt1 and pt2 can have any coordinates desired, thus the AutoLISP script can draw different lines, whilst the command script can only draw one line. However, the AutoLISP script is part of a much larger program which determines the values of pt1 and pt2, and instead of sending instructions to the Acad command line, it could equally well write the instructions to a file as follows:

(write-line (strcat "LINE " (ptstr pt1) " " (ptStr pt2) " ") fp)

Where (ptstr) is a user written function to convert the point coordinate lists to text strings, which can be written to a file (fp). The resultant plain text file can then be excuted from within Acad or Acad LT via the run script command. For simplified usage the script can always be written to the same location and file, such as default.scr, and this script can be executed from either a menu macro or toolbar button: once again in either Acad or Acad LT.

'_script default.scr 

Now such plain text files containing Acad command line instructions can be generated by any available programming language. On old MS DOS machines the most readily available programming language was GWBASIC, but other high level programming languages like Turbo Pascal and Turbo C may have been available. In the world of Windows XP and higher, every machine has available windows scripting host and can run VBscript or JScript.

For example in VBscript, can replace the AutoLISP instruction with:

fp.WriteLine("LINE " & ptStr(x1,y1) & " " & ptStr(x2,y2) & " ")

However most offices have MS Excel installed on most machines, and a few may have access to MS Access, in which case vba can be used to generate scripts. Script instructions can be generated in an Excel worksheet then copy/pasted into a text file, and run by the script command, alternatively the instructions can be pasted directly to the Acad LT command line. Similarly queries in MS Access can be used to generate a sequence of script commands either pasted to a file or direct to the Acad Lt command line. This approach is useful when converting simple data into graphics.

Example of Coldformed steel section generated from script pasted to Acad command line, from Excel worksheet. Worksheet can be found here.

For more advanced automation Excel/vba combination provides the means of collecting input in the worksheet and then writing a script file based on this worksheet data. In vba the typical script would be something like:

Print #fp, "LINE " & ptStr(pt1) & " " & ptStr(pt2) & " "

Where pt1 and pt2 are coordinates stored in a data structure as follows:

Public Type TCoord
  x As Double
  y As Double
  z As Double
End Type

To avoid keep writing the instructions for a line the following subroutine can be defined and used.

Public Sub write_line(fp As Integer, pt1 As TCoord, pt2 As TCoord)
  Print #fp, "LINE " & ptStr(pt1) & " " & ptStr(pt2) & " "
End Sub

call write_line(fp, pt1,pt2)

Similar subroutines can be written for other commonly used Acad LT commands. By building a library of such commands, it is possible to simply convert the program from one CAD package to another simply by rewriting the library to suit the CAD package. That is the main program written uses the users own subroutines and data structures, and should never need rewriting, but the library can be converted to use the data structures, objects and methods of a chosen CAD package. For example write libraries for scripts, DXF, Acad COM automation, TurboCAD, DesignCAD, MultiFrame, even the Excel shapes layer.

Now the problem with the script is it cannot select Acad entities, nor points. Many AutoLISP routines simply pick a single point on the screen then generate a parametric detail about this point. Most of the time (0,0,0) is a good enough start point and a new file is the place to start drawing, therefore no point needs to be selected. By generating a new file, and using xref to reference this file into the primary drawing, interaction with the Acad drawing editor can be minimised. Unless using handles and links to an external data base it is seldom necessary to modify entities. The primary objective is to automate drawing production, not interact with the drawing editor. If a full set of drawings for some parametric object can be generated in a few seconds there is no need to revise or modify the previous: simply regenerate using the modified parameters. Further more if critical parameters start out in Excel and are used to generate a drawing, then there is no need to extract such information from the Acad drawing database.

Interaction with the drawing editor is not highly productive, nor are dialogue boxes. A common flaw with AutoLISP routines is that they have dialogue boxes collecting mutiple parameters. The number of parameters collected by these dialogue boxes exceeds the number of parameters used by engineering. The problem is that engineering calculates the other parameters, used to specify a part. Due to the division of labour between drafter and engineer: the drafter draws and the engineer crunches numbers, there is consequently a lack of integration in the design activity.

It is more productive to write the AutoLISP routines without dialogue input boxes, rather write as subroutines requiring passing of parameters. Then write additional subroutines using dialogue boxes to collect the parameters and call the routine which does the actual drawing. The reason for this is so that the drawing routine can be used in much larger drawing application. Just as machines and buildings are made from component parts, also is a drawing using blocks and xrefs, and similarly a computer program using functions and subroutines. Just as blocks simplify building large drawings, so to do subroutines simplify writing large programs.

The first use of a drawing subroutine, may be to call it from a routine using dialogue boxes to collect all the drawing parameters. A future use may collect far fewer drawing parameters and calculate the rest. But of far greater value is the potential to string multiple subroutines together in sequence to draw something much larger and more complex from a few simple parameters.
Similarly, if this can be done using AutoLISP it can equally well be done using Excel/vba and the generation of scripts. What is more without the use of Acad or Acad LT, an engineer or other designer can generate a series of scripts in a common location, these scripts are then executed by the drafter using either Acad or Acad LT.

Excel/vba can also be used to grab a list of files, either drawings (dwg), DXF and then execute a set of script commands on each of the drawings. Similarly vba can get a list of scripts, and combine them into a single script to be launched in Acad LT. An alternative approach is to execute each script one at a time by calling Acad LT with command line switches, unfortunately with Windows multitasking causes a a timing problem. It is however the approach that was taken with Acad when using MS DOS. It was the failure of my Turbo C multiScripting program in Windows using Acad LT which led me to experimenting with generating scripts using QuattroPro spreadsheet with which I could easily grab a list of file names.

To launch Acad automatically from an application and run the script call Acad using command line switches, for example.

"C:\Program Files\AutoDesk\AutoCAD LT 2000\aclt.exe" /b default.scr

This can be configured in an Excel worksheet, so that can vary CAD applications used and the script names used. More samples can be found here.

Here is some sample output generated by scripts (*.scr).

From a few parameters steel framing plans, elevations and section for simple building generated in model space and paper space, all text with exception of grids placed in paper space. The above view shows the model space 2D stick diagrams, and the lower view a close up of the framing plan.

The following was originally written using DesignCAD, generating a 3D stick diagram of a shed, but then translated to use Acad scripts and draw more detailed elevation.

Simple stick diagram above. Below, model with detailed elevation, and permitting any number of spans.

The following is a close up of the knee connection.

A limitation of Acad Lt is not being able to generate 3D elements. I have used Lights for analysing tension membranes, and whilst it generated an Acad Script file, I was unable to run the file in Acad Lt since it could not create the 3D face elements, whilst intelliCAD 2000 used a different set of parameters to draw 3D faces so the script was incompatible. I therefore modified my Acad script library to use the intelliCAD COM objects. To produce the following, which is viewed in Acad LT.

I could have written the program using scripts in intelliCAD, but I used the opportunity to try out COM automation. The program reads the results from Lights, and then shades membranes elements in tension green, and elements in compression red. It is all on layers, so the membrane can be switched off, and the tension and compression elements of the supporting cables can also be viewed.

Another use of scripting is a simplified geographical information system (GIS). The following made use of MS Access, which was used to count the number of projects in a given area: mainly a UBD map. This data was then transfered to Excel, where a vba macro further sorted the data and generated a script which inserted a block on the appropriate layer. Red blocks represent areas with most projects, and paler blue blocks represent areas with least projects.

Each square represents one map from the UBD Adelaide street directory. The vba routines translate UBD map grid references into Australian Map Grid (AMG) coordinates so that can be located in Acad by (x,y) coordinates. In addition to the coloured blocks. circles were drawn at more localised positions, with the size of circle reflecting the number of projects in the area. The reference grid on each UBD map is about 250m x 250m, so objects typically located at the centre of these grids. For many things don't require the use of a fully blown GIS, since only concerned with relative distances between locations. For example nearest neighbours and hinterlands, or catchments of common facilities like shopping centres, hospitals, business competitors. The map shows that a lot of our work is dependent on the North East Road. It doesn't show projects outside the metropolitan area spread around the state and the country, and the occasional few outside the country. For an alternative GIS can take a look at MapMaker gratis this I have experimented with to generate contours whilst in the trial period with the professional version. The contours being for soil heave, caused by reactive clay soils. We have collected lots of bore logs over 16 years, and therefore, it should be possible to produce a contour map. At the moment I don't have subroutines to generate contours, but have found routines for generating Delaunay triangles, though generating Voronoi polygons would be even better for assessment of catchment areas. This far have written Acad scripts to do the triangulation, the rest is for a future date.

Any case if automation is the primary objective, and doesn't involve creation of 3D entities, then Acad LT can be used to automate a considerable amount of 2D work, or if stick diagrams are all that is required then it can also automate 3D models.

Recommended Reading
1 Microsoft Press (1997),"Micosoft Office 97 Visual Basic Programmers Guide", Microsoft Press
2 Jim Boyce, et al (1997), "Using Microsoft Office 97 Professional: Special Edition", Que
3 George Omura (1989),"AutoCAD instant Reference", Sybex
4 C W Sharp and W W Hamm (1989),"AutoCAD Advanced Techniques",Que
5 D Raker and H Rice (1990), "Inside AutoCAD: 5th Edition (metric)",New Riders Publishing
6 J Smith and R Gesner (1989),"Customising AutoCAD: 2nd Edition",New Riders Publishing
7 J Smith and R Gesner (1989),"Inside AutoLISP",New Riders Publishing
8 George Omura (1990),"The ABC's of AutoLISP", Sybex
9 Dennis N Jump (1989), "AutoCAD Programming", TAB Professional and Reference Books
10 AutoDesk(1995),"Users Guide: AutoCAD LT Release 2 for Windows",AutoDesk
11 Acad LT on line help system.
12 Burchard, Pitzer, Soen, et al (1997), "Inside AutoCAD 14", New Riders Publishing
13 AutoDesk(1999),"Getting Started: AutoCAD LT 2000",AutoDesk

Related Posts:

AutoCAD Speed Test
Automated Drawing List Update Acad LT (revised)

Cold-Formed Steel Shed Industry: part:#1 (Shed Framing Drawings)
Cold-Formed Steel Shed Industry: part:#2 (Shed Framing Drawings)