Drawing a Range Top from a Macro (Macro-izing Solid Areas) (AutoCAD VBA)

This next project combines several of the topics covered in this topic. It uses the Utility object to prompt the user to click the points defining the position and dimensions of the range top, and then draws the range top. Then the InputBox function is used to prompt the user to enter the number of burners required—this is the only GUI employed by the project. It takes yet another approach to drawing circles—the ones that represent the range-top’s burners—calculating their size and position in the macro itself before drawing them on the range top.

Exercise 7.6: The Draw Range Top Application

1.    Start a new project and enter the code shown in Listing 7.13 into the ThisDraw-ing Code window.

2.    Run your project. A prompt appears in the command line, asking you to click the position for the bottom-left corner of the range top.

3.    Click at the point required for the corner. The command line now asks you to specify the opposite corner.

4.    Click the opposite corner. The input box appears, prompting for the number of hot plates required and telling you what sort of input is expected.

tmp89fa-102_thumb


5. Enter a number and click OK. The range top appears in the Model Space, with the number of burners you specified.

Figure 7.16 shows a selection of range tops created using this macro.

Variety of range tops produced by the DrawRangeTop macro

Figure 7.16 Variety of range tops produced by the DrawRangeTop macro

Listing 7.13: DrawRangeTop Macro

Listing 7.13: DrawRangeTop Macro

 

 

Listing 7.13: DrawRangeTop Macro

 

 

 

Listing 7.13: DrawRangeTop Macro

 

 

Listing 7.13: DrawRangeTop Macro

 

Analysis

•    Line 1 starts the DrawRangeTop macro, which interacts with the user to obtain the information needed to draw the outline of the range top, and then calculates the size and position of each of the burners.

•    Lines 19 and 20 prompt the user for the position of two diagonally opposite corners for drawing the range top.

•    Lines 22 through 26 assign the values of the x-, y-, and z-coordinates that make up the four corners of the range top, plus a fifth corner that is a duplicate of the first so that all four edges are drawn.

•    Lines 29 and 30 calculate the width based on the x-coordinates from the Range array, and the depth based on the y-coordinates.

•    Line 32 calls the InputBox function to get input from the user. The first argument is the prompt; the second argument is the caption for the title bar of the input box.

•   Lines 33 through 40 determine how many rows and columns will be used when the hot plates are placed on the range top.

•    Line 42 starts the outer For loop that executes once for each column of hot plates.

•    Line 43 calculates the y-coordinate for the center.

•    Line 44 starts the inner For loop that is repeated for each row of hot plates.

•    Line 45 calculates the x-coordinate for the center.

•    Lines 46 through 50 calculate the largest radius for the hot plates, based on the range top’s dimensions and the number of burners required.

•    Line 51 declares the Adjustment variable that determines how far the four radial lines will be translated from the hot plates’ centers.

•    Line 52 assigns a value to the Adjustment variable. This makes it easy to update the adjustment value, since it only needs to be done in this statement.

•    Line 55 draws the circle representing the hot plate.

•    Lines 57 through 73 create four Line objects and add them to the drawing.

•    Line 74 ends the With statement block.

•    Lines 75 and 76 end the two For loops.

•    Line 77 ends the DrawRangeTop macro.

Summary

After reading this topic, you’ll know how to do the following tasks and understand the related concepts in VBA code:

•    Draw circles.

•    Access points and distances specified in the AutoCAD window from a macro.

•    Fill a circle with color.

•    Draw a circle of bricks from an application.

•    Set the boundaries for hatching outer loops and inner loops.

•    Fill nested shapes using the Island Detection style.

•    Use the Associativity argument of the AddHatch method to determine whether or not the hatch pattern gets updated with the boundary.

•    Draw any shape, fill it with a hatch pattern, and find out its area.

•    Add Hatch objects to the ModelSpace collection.

•    Use the Variant type as a means of passing a three-element array in one assignment statement.

•    Add items to a combo box and determine the one selected by the user at run time.

•    Use the Input Box function to receive input from the user.

•    Place an array of objects into a rectangular area in the Model Space, using a macro to calculate the size and positions.

Next post:

Previous post: