AutoLISP Solutions: Inserting Doors by Automatically Breaking Walls10 Mar, 2006 By: Tony Hotchkiss Cadalyst
AutoLISP helps automate the insertion of door blocks into walls.
Mr. Thierry Georges of Charleroi, Belgium, wants to automate the process of inserting door blocks into walls with automatic breaking of the walls and with a selection of the orientation of the doors from a dialog box. Thierry currently uses door blocks with attributes that give the locations of points to break the wall entities. He wants to use a variety of entity types for the walls such as lines, polylines or multilines. As Thierry does not use the architectural version of AutoCAD, I have proposed a solution using AutoLISP.
The AutoLISP Solution is DOORS.LSP and DOORS.DCL, which allow the user to select the size (thickness and width) of the door and to select the orientation from eight choices. The door then is placed in the wall at a user-selected location, and the wall is trimmed (broken) to fit the door. The door thickness must match that of the wall, and the user can select the hinge side of the door location. The code repeats by prompting for another door until the user selects No.
Get the Code
Download the DOORS.LSP and DOORS.DCL files from Cadalyst's CAD Tips site. Save the file in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then use the browser to select the file.
How to Use the Code
Type DRS to start the program, and the Doors Detail dialog box will appear (figure 1).
Figure 1. The Door Details dialog box.
The dialog box includes an orientation button and boxes for the door width and thickness. An image shows the current door orientation. If you select the door orientation button, a Select Orientation dialog box opens (figure 2).
Figure 2. The Select Orientation dialog box.
Select any of the images shown in figure 2 to set the door orientation. The first dialog box pops up with the new door orientation shown in the image. Figure 3 shows the new door details, including the orientation image.
Figure 3. The Door Details dialog box with new selections.
Clicking the OK button sets the object snap mode to nearest, and the program will prompt you to pick the location of the hinge point in the wall. The wall may have lines, multilines or polylines. The hinge point is on the opening side of the door. After the position is selected, the door is inserted and the wall is broken. If the wall is a multiline or a polyline, it is exploded before the door is inserted.
The door actually is drawn automatically as a single polyline. Figure 4 shows a variety of wall types, and figure 5 shows the door appearance zoomed in. Thierry Georges provided the shape of this doorframe. Figure 4 also shows the prompt to place another door, where Yes is the default response. A simple Enter selects the Yes option.
Figure 4. Some typical doors placed into walls.
Figure 5. An inserted door zoomed to show the frame detail.
The program was written in AutoCAD 2006, and it was tested only in AutoCAD 2006. However, it may work in earlier versions back to 2000 because Visual LISP has not changed much since then.
After my usual error and system variable functions, I used the DOORS function to manage the door detail dialog box. The functions INIT and DRAW-IMAGE are called to initialize the appearance of the dialog box. I opted to create global variables for the door width, thickness and orientation and assigned their values depending on the user selections of the dialog box tiles. The function DOOR-ORIENT is called if the user chooses to change the orientation, and the DO-DOOR function is called when the user selects the OK button (the action tile accept).
INIT simply sets the values of the orientation, width and thickness values if they do not already exist.
DRAW-IMAGE uses the value of the orientation global variable to draw the image of the door in any of the eight predetermined orientations. The functions refers to slide1, although I did not use a slide to make the image as I prefer to draw line diagrams for image tiles and buttons in dialog boxes. This preference means more programming but fewer files to manage. The images consist of a single arc and a single line controlled by three points. The standard COND function tests for the value of the orientation variable (from 1 to 8), and sets the locations of three points in the image based on the maximum x- and y-limits of the image tile. In all of the cases, the IMAGE-ARC function is called to draw the arc, and, after the COND function is finished, the DO-VECTOR function is called to draw the straight line.
DOOR-ORIENT is the driver for the Door Orientation dialog box, and it calls the ORIENTATION-IMAGE function to draw each of eight image tiles that display the orientation choices. IMAGE-ARC and DO-VECTOR are called many times to draw the image tiles rather than using slides. DOOR-ORIENT also sets the value of the orientation variable to one of its eight possible values before returning to the DOORS function.
DO-DOOR sets the object snap mode to Nearest with the value 512, using my SETV function. DRAW-DOOR and POSITION-DOOR then are called to complete the job of placing the doors in the drawing.
DRAW-DOOR takes the point location for the hinge of the door, and it creates a series of 19 points that will create a polyline object. A list of pairs of x,y coordinates is created before MAKE-LWPOLYLINE is called. The polyline is created in one orientation only, according to the list of coordinates supplied. Here is the code for the MAKE-LWPOLYLINE function:
(defun make-LWpolyline (plst / arr lwpline)
(setq arr (vlax-make-safearray vlax-vbDouble
(cons 0 (- (length plst) 1))))
(vlax-safearray-fill arr plst)
(setq lwpline (vla-AddLightweightPolyline
) ;_ end of vla-AddLightweightPolyline
) ;_ end of setq
) ;_ end of make-LWpolyline
The function returns the polyline object, so that a call to set the bulge of the polyline can include the arc segment. The code to make the polyline and to set the bulge is shown here near the end of the DRAW-DOOR function:
(setq polyobj (make-LWpolyline plist))
; polyline object
(vla-SetBulge polyobj 9 bulge)
(setq polyent (vlax-vla-object->ename polyobj))
; polyline entity
) ;_ end of draw-door
The polyline entity returned by DRAW-DOOR is used by POSITION-DOOR through appropriate calls to the VL-CMDF commands Mirror and Rotate to set the orientation depending on the value of the orientation variable.
Finally, TRIM-DOORS explodes the wall objects that are not line entities before calling TRIMIT to break the walls to suit the door opening widths. I used the terminology trim here, but I in fact resorted to breaking the lines of the walls.
As always, I look forward to receiving your comments and requests for AutoLISP Solutions. Contact me using the links below.
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Poll not yet available.