1 Overview

Since its introduction in 1982, AutoCAD, developed by Autodesk of the United States, has remained a leading CAD software due to its user-friendly interface, powerful functionality, and open system architecture. It has become the go-to choice for various engineering disciplines. In the field of water conservancy and hydropower engineering, the use of AutoCAD as a platform for secondary development has led to significant achievements. Systems such as the hydropower station ground-floor plant CAD system, gravity dam CAD system, tunnel CAD system, water machine oil soda CAD system, and geological histogram CAD have been developed since the 1990s, achieving good results. However, the complexity of hydropower engineering design means that off-the-shelf CAD software often has limitations. Many design challenges can only be addressed through custom methods. This paper explores the application of the Lisp language in the secondary development of AutoCAD within the context of hydropower engineering, using several examples to demonstrate its value.

The Visual Lisp language is a new development environment introduced after AutoCAD R14. It is embedded within AutoCAD and integrates the Lisp language with the AutoCAD system, making it an intelligent programming tool. With AutoLisp, users can easily add new commands to AutoCAD and extend its capabilities almost infinitely, offering a flexible way to enhance the software’s functionality.

2 Application Examples

2.1 Coordinate Line Drawing

Using known coordinate points to draw lines is a common task in design work. For example, data processing involving river cross-sections, embankments, water level and flow curves, and surface curves all involve coordinate line drawing. For curved functions, Excel can be used to generate coordinate points at desired intervals. For data in the form of starting point distance-elevation, Excel can also convert them into coordinate format.

(1) Using Script Files (.SCR)

Create a plain text file named EX1.SCR using any text editor like Notepad, Uedit, Word, or Excel. Ensure the file extension is .SCR. Coordinates should be separated by commas without spaces. The last line must be an empty line. Once created, you can copy and paste coordinate tables from Excel or other files directly into the script file.

Pline

345.66,238.65

213.45,124.56

128.44,235.66

After saving the file, select "Run Script" under the "Tools" menu in AutoCAD. Choose EX1.SCR, and the line will be drawn on the screen.

(2) Using LSP Procedure

Create a coordinate table file named EX2.DAT with any suffix. You can input multiple segments sequentially, separating them by curve names. The program automatically draws multiple curves and labels the coordinates.

"Curve 1"

213.45,124.56

128.44,235.66

345.66,231.78

"Curve 2"

433.34,567.23

434.12,464.12

Use the "VisualLisp Editor" under the "Tools" menu in AutoCAD to create the DRLINE.LSP file as follows:

(defun C:drline()

(if fn

(setq fn (getfiled "point file name" fn "" 2))

(setq fn (getfiled "point file name" "" "" 2))

)

(setq f (open fn "r"))

(setq p0 (getpoint "/n starting point:"))

(setq bz 0 mm 1)

(command "pline")

(while (/= bz 1)

(setq md (read-line f))

(if (/= md nil)

(setq zbb (read md))

(setq bz 1)

)

(setq dx (car zbb) dy (cadr zbb))

(setq p1 (list (+ dx (car p0))

(+ dy (cadr p0)))

)

(command p1)

(setq mm (1+ mm))

)

(command "")

(close f)

)

Once saved, loading and running the file adds a new DRLINE command. Select the coordinate file according to the prompt to automatically draw the curve.

2.2 Sectional Cross-Section

In hydropower project design, problems involving cross-sections on topographic maps are common. With Visual Lisp, rapid sectioning is achievable.

(1) Initial Topographic Map Processing

Transform the terrain plan with z-coordinates and set the z-values as layer names. Convert both "LWPOLYLINE" and "SPLINE" to "LINE" to speed up the sectioning process. The procedure is as follows:

(defun c:pltol()

; LWPOLYLINE converted to LINE

(setq n 0)

(setq e (ssget "X" (list (cons 0 "LWPOLYLINE"))))

(setq sh (sslength e))

(if e

(while (< n sh)

(setq e1 (ssname en))

(command "pedit" e1 "d" "")

(setq x (entget e1))

(setq ngc (atof (setq la (fld 8 x))))

(command "explode" e1)

(setq n (+ n 1))

)))

(defun fld (num lst)

(cdr (assoc num lst))

)

(defun c:spltol ()

; SPLINE converted to LINE

(setq n 0)

(setq e (ssget "X" (list (cons 0 "SPLINE"))))

(setq sh (sslength e))

(if e

(while (< n sh)

(setq x (entget (setq e1 (ssname en))))

(setq nla (itoa (fix (caddr (fld 10 x)))))

(if (= (fld 0 x) "SPLINE")

(progn

(command "layer" "n" nla "c" "6" nla "s" nla "")

(command "line")

(setq nm (length x)

Dzs (fld 73 x) dzs1 (fld 74 x))

(while (> nm 5)

(if (= (car (nth nm x)) 10)

(progn

(setq b1 (nth nm x))

(setq x1 (cadr b1))

(setq y1 (caddr b1))

(setq z1 (cadddr b1))

(setq glb (list x1 y1))

(command glb)

))

(setq nm (- nm 1))

)))

(command "")

(setq n (+ n 1))

)))

(2) Cut Section

Enter the section number, specify two points on the topographic map to determine the section line, and specify the starting point. Use AutoCAD's inters function to find all intersection points between the section line and the topographic map. Automatically calculate the intersection coordinates, then compute the distance from the starting point to each intersection. Form the profile coordinates based on the height and distance, and generate the topographic section using the method described in Example 1.

2.3 Labeling Along Curves

Labeling along a given curve is a common problem in design, such as marking excavation lines in floor plans or rock symbols in profile views. These tasks are usually handled using AutoCAD's Measure or Divide commands. The main difference is that Measure marks points based on specified distances, while Divide divides the curve into equal segments. For better results, the curve should be a polyline. Pre-made blocks or symbols can be used, and the Measure or Divide command can be executed to place the labels. If the spacing is incorrect, the Erase command can be used to remove unwanted labels. Adjustments can be made using the Property Editor to change size and rotation angle.

2.4 Table Generation

AutoCAD does not have built-in table processing capabilities. Engineering drawings often require tables for scales, rebar details, etc. Two approaches are available:

(1) Directly paste tables from Word or Excel into the CAD drawing. Double-click the table to edit it in Word or Excel, then return to AutoCAD. This method is convenient but lacks CAD-specific editing features like adjusting height, color, or line width.

(2) Create an LSP procedure to generate tables. Input parameters like title, rows, columns, row height, column width, and insertion point. The table is generated with text formatted like Excel. Text can be edited using word processing software.

(defun C:mtab()

(setq tb1 nil tb2 nil tb3 nil)

(setq rows (getint "table rows"))

(setq cols (getint "table column number"))

(setq rowh (getreal "row height"))

(setq colw (getreal "column width"))

(setq p1 (getpoint "/n table upper left corner point location:"))

(command "pline" p1 "w" "0.5" "0.5"

(setq p2 (list (+ (car p1) (* cols colw)) (cadr p1)))

(setq p4 (list (car p2) (- (cadr p2) (* rows rowh))))

(setq p3 (list (car p1) (- (cadr p2) (* rows rowh))))

"C")

(setq n 1)

(while (< n rows); drawing horizontal lines

(command "pline" (list (car p1) (- (cadr p1) (* n row h))) "w" "0" "0"

(list (car p2) (- (cadr p2) (* rowh n))) "")

(setq n (+ n 1))

)

(setq n 1)

(while (< n cols) ; painting vertical lines

(command "pline" (list (+ (car p1) (* n colw)) (cadr p1)) "w" "0" "0" (list (+ (car p1) (* n colw)) (cadr p3 ))

"")

(setq n (+ n 1))

)

(command "text" "m" (list (+ (car p1) (* 0.5 cols colw))

(+ (cadr p1) 5)) "3" "0" "TITLE" )

(setq n 0)

(while (< n rows)

(setq m 0)

(while (< m cols)

(cond

((< m 26) (setq bzstr (chr (+ 65 m))))

((>= m 26) (setq bzstr (strcat (chr (+ 64 (/ m 26))) (chr (+ 65 (- m (* 26 (/ m 26))))))))

)

(command "text" "m" (list (+ (car p1) (* 0.5 colw) (* m colw));

(- (cadr p1) (* 0.5 rowh) (* n rowh)))

(getvar "TEXTSIZE") "0" (strcat bzstr (itoa (+ n 1))))

(setq m (+ m 1))

)

(setq n (+ n 1))

))

2.6 Decimal Places Processing for Elevations

Adjusting decimal places in dimensions is easy, but changing decimals in tables like elevations can be difficult. Using an LSP procedure makes this task simple. Select the numbers to adjust and enter the desired number of decimal places. The program will automatically modify all selected values.

(defun entsgc()

(setq n 0)

(setq sh (sslength e1))

(while (< n sh) (setq x (entget (ssname e1 n))))

(if (= (fld 0 x) "TEXT")

(progn

(setq agc (fld 1 x))

(setq c (substr agc 1 1))

(if (or (= c "+") (= c "-") (and (>= c "0") (<= c "9")))

(progn

(if (or (= c "+") (= c "-"))

(setq bgc (substr agc 2))

(setq bgc agc)

)

(setq cgc (atof bgc))

(setq zh (rtos cgc 2 gcws))

(if (or (= c "+") (= c "-"))

(setq zh (strcat c zh)))

(setq x (subst (cons 1 zh) (assoc 1 x) x))

(entmod x)

)

))

))

(setq n (+ n 1)))

)

(defun c:yxws()

(setq e1 (ssget))

(setvar "dimzin" 0)

(setq gcws (getint "reserve decimal places:"))

(if e1 (entsgc) (print "/n not found"))

(setq x nil)

)

2.7 Pre- and Post-Processing with Software

Owing to slow software updates, some post-processing functions may be weak or missing. LSP programs can be combined with AutoCAD to enhance or simplify pre- and post-processing tasks. Below is a description of how this is implemented with the STES plane seepage analysis software:

(1) Pre-processing

STSE is a plane seepage finite element program. The largest workload involves unit division and node numbering. Other general finite element software like Ansys, Algor, and SAP84 can be used for preliminary processing, generating unit and node numbers along with their coordinates. Then, the data file is edited to match the STSE format.

(2) Post-processing

The main post-processing results include cell grids, immersion lines, and equipotential lines. Node and unit numbers are visible in the grid diagram. Different permeability coefficients are represented by different colors, allowing for quick verification of geometric and material properties in the data file.

First, analyze the STSE output file format, filter out key parameters such as total nodes and materials, store node coordinates and cell information separately, and generate a unit grid through a loop over the units.

(defun c:seepmesh()

(if fn

(setq fn (getfiled "percolation result file name" fn "" 2))

(setq fn (getfiled "percolation result file name" "" "" 2)))

(setq f (open fn "r"))

(setq md (read-line f))

(while (/= (substr md 25 8) "the total number of units")

(setq md (read-line f))

); Position the total number of units

(setq dyzs (atoi (substr md 58 5))); Read total number of units

(setq md (read-line f))

(setq jdzs (atoi (substr md 58 5))); Read total number of nodes

(setq md (read-line f))

(setq clh (atoi (substr md 58 5))); Read total number of materials

(setq m 0)

(repeat clh

(setq m (+ 1 m))

(setq tcm (strcat "zclh" (itoa m)))

(command "layer" "m" tcm "c" (itoa m) tcm "")

); Generate layer names by material type

(setq m 0 n 0)

(repeat dyzs; Loop over cells, storing cell info in dycfb

(setq m (+ m 1))

(setq clh (nth 0 (nth m dycfb)))

(setq jdh1 (nth 1 (nth m dycfb)))

(setq jdh2 (nth 2 (nth m dycfb)))

(setq jdh3 (nth 3 (nth m dycfb)))

(setq jdh4 (nth 4 (nth m dycfb)))

(command "layer" "s" (strcat "zclh" (itoa clh)) "")

(command "pline"; Draw cell grid

(nth jdh1 jdzbb)

(nth jdh2 jdzbb)

(nth jdh3 jdzbb)

(nth jdh4 jdzbb) "C")

(setq bzdzb (mapcar '+ (nth jdh1 jdzbb) (nth jdh2 jdzbb)

(nth jdh3 jdzbb) (nth jdh4 jdzbb)))

(setq bzdzb (list (/ (car bzdzb) 4.0) (/ (cadr bzdzb) 4.0)))

(command "text" "m" bzdzb "0.5" "0" (itoa m))

; Mark unit number

(setq m 0)

(repeat jdzs

(setq m (+ m 1))

(setq bzdzb (nth m jdzbb))

(setq bzdzb (list (+ (car bzdzb) 0.0) (+ (cadr bzdzb) 0.0)))

(command "text" "m" bzdzb "0.5" "0" (itoa m))

)); Label node numbers

(close f)

)

Due to space constraints, the process of generating equipotential and saturation lines will not be repeated. Similarly, other engineering software such as STAB slope stability analysis and SAP84 output files can undergo similar post-processing steps.

3 Conclusion

AutoCAD has gained widespread adoption in design units. The emergence of Visual Lisp has significantly improved the efficiency of Lisp programming and the overall application level of AutoCAD. Several examples discussed in this paper have already played a vital role in practical work, demonstrating that combining engineering design requirements with the development of simple and practical Lisp tools can effectively reduce the workload of designers. AutoCAD is no longer just a drawing tool but has evolved into a powerful design platform that enhances productivity and efficiency in engineering projects.

Auto Parts

Auto Parts,Alu Profile Part Of A Car,Aluminium Car Part Profile,Aluminium Parts Of A Car

Jiangsu Yuejia Metallic Technology Co.,Ltd , https://www.yuejiametal.com