Creating 3D Solids (Creating and Drawing 3D Solids) (AutoCAD VBA) Part 3

Creating a Torus

The AddTorus method is used to create a Torus solid, given its center, its radius, and the radius of its tube. Listing 15.6 shows this method being called from the DrawTorus macro with the following variables as arguments:

TorusCenter A three-element array representing the center of the torus, and also the center of the bounding box that encloses the torus.

TorusRadius A positive value representing the radius of the torus. This is the distance between the center of the torus to the center of the tube.

TubeRadius A positive value representing the radius of the tube of the torus.

In the next series of steps, you enter the DrawTorus macro and run it. Figure 15.5 shows the torus created by the macro. In order to get a clearer view of this torus, I’ve changed the z-coordinate of view direction vector ViewDirection(2) from 1.5 to 4.5 (line 4 of Listing 15.2).

Torus generated by the DrawTorus macro (Listing 15.6)

Figure 15.5 Torus generated by the DrawTorus macro (Listing 15.6)


Exercise 15.1: Building the 3D Solids Application

1.    Continuing with the same drawing, enter the DrawTorus macro’s code (Listing 15.6) into ThisDrawing’s Code window.

2.    Return to the AutoCAD window, choose Tools ^ Macro ^ Macros, and run the DrawTorus macro from the Macros dialog box.

3.    Answer the prompts in the command line to enter the information required.

Listing 15.6: DrawTorus Macro

Listing 15.6: DrawTorus Macro

Analysis

•   Line 1 starts the DrawTorus macro, which prompts the user to enter the position and dimensions of the torus and creates a solid to these specifications.

•    Line 3 declares the TorusObject variable as being capable of referencing a 3DSolid object.

•    Line 4 declares the TorusCenter variable as a Variant type so that it can be assigned the three-element array returned by the GetPoint method.

•    Line 5 declares the TorusRadius variable that will be assigned the distance between the position of the center of the hole and any central position along the tube.

•    Line 6 declares the TubeRadius that will be assigned the radius of the tube, to determine its volume.

•    Line 7 declares the PointOnRadius array that will be assigned the point at the radius’s end that is opposite to the torus’s center, which also happens to be at the center of the tube.

•    Line 8 uses the With statement so that the methods of the Utility object can be used without being fully qualified.

•    Line 9 prompts the user for the position of the center of the torus’s hole, which is also the center of its bounding box.

•    Line 10 prompts the user to enter the radius of the torus.

•    Lines 11 through 13 assign to the PointOnRadius array the coordinates of the point in the x-axis direction at the distance denoted by the radius. This point is at the center of the tube.

•    Line 14 calls the GetDistance method with the center of the tube as the first argument, and prompts the user to enter the radius for the tube.

•    Line 16 assigns 12 to the ContourLinesPerSurface property.

•    Line 17 calls the AddTorus method to create a Torus solid and sets up the TorusObject variable as a reference to the 3DSolids object that represents the torus.

•    Line 18 calls the Update method to draw the torus in Model Space.

•    Line 19 calls the ChangeViewDirection macro to display the torus from a different viewing angle. In order to make the shape of the torus even clearer, I changed the z component of the direction vector in this macro from 1.5 to 4.5 (line 5 of Listing 15.2).

•    Line 20 ends the DrawTorus macro.

Creating a Cone

The AddCone method is used to create a Cone solid, given the center of its box enclosure, its radius, and its height. Listing 15.7 shows this method being called from the DrawCone macro with the following arguments:

ConeCenter A three-element array that defines the coordinates of the center of the bounding box that encloses the cone.

ConeRadius A positive value representing the radius at the base of the cone, which by default lies in the x-y plane.

ConeHeight A positive value representing the height of the cone, which is its length along the z-axis.

In the next series of steps, you enter the DrawCone macro and run it. Figure 15.6 shows the Cone solid generated by the macro, using the default number of contour lines per surface, which is four.

Cone drawn by the DrawCone macro (Listing 15.7), using the default four contour lines per surface

Figure 15.6 Cone drawn by the DrawCone macro (Listing 15.7), using the default four contour lines per surface

Exercise 15.1: Building the 3D Solids Application

1.  Continuing with the same project, enter the DrawCone macro’s code (Listing 15.7) into the Code window for ThisDrawing.

2.    Return to the AutoCAD window and run your macro from the Macros dialog box.

3.    Following the prompts in the command line, enter the position and dimensions required.

Listing 15.7: DrawCone Macro

Listing 15.7: DrawCone Macro

Analysis

•    Line 1 starts the DrawCone macro, which draws a Cone solid after prompting the user for its position, the radius at its base, and its height.

•    Line 3 declares the ConeObject variable as being capable of referencing a 3DSolid object.

•    Line 4 declares the ConeCenter as a Variant type so that the three-element array returned by the GetPoint method can be assigned in a single statement.

•    Lines 5 and 6 declare the variables representing the radius of the circular base of the cone, and its height.

•    Line 7 uses the With statement so that the methods of the Utility object can be used without the need to fully qualify them each time.

•    Line 8 calls the GetPoint method to prompt the user to select the position for the center of the circular base of the cone. This is used later at Line 12 to calculate the center of the bounding box.

•    Lines 9 and 10 call the GetDistance method to prompt the user to enter the cone’s radius and height.

•    Line 12 updates the z-coordinate of the point entered by the user, to represent the center of the base of the cone so that it is positioned halfway between the base and the cone’s tip.

•    Line 13 calls the AddCone method to create a Cone solid, and sets up the ConeObject to refer to the 3DSolid object that represents the cone.

•    Line 14 calls the Update method to draw the Cone object on the screen.

•    Line 15 calls the ChangeViewDirection macro to change the direction from which the cone is viewed, so that it doesn’t look like a 2D triangle.

•    Line 16 ends the DrawCone macro.

Adding Contour Lines

By updating the ContourLinesPerSurface property,you can adjust the number of lines per surface. You simply insert the following statement after line 10 in Listing 15.7:

ThisDrawing.Preferences.ContourLinesPerSurface = 20

Now when you run the DrawCone macro, any cones and other solids in the current drawing plus any added in the future will be generated with the extra contour lines, as shown in Figure 15.7.

Updating the ContourLinesPerSurface property of the Preferences object updates all existing 3DSolid objects associated with the current drawing.

Cone generated by DrawCone macro after including the statement updating the ContourLinesPerSurface property to 20

Figure 15.7 Cone generated by DrawCone macro after including the statement updating the ContourLinesPerSurface property to 20

Next post:

Previous post: