Drawing a Cilinder in Autocad
Creating a Cylinder
The AddCylinder method is used to create a Cylinder solid, given the cylinder's center, radius, and height. The edges defining the cylindrical tube are all parallel to the z-axes of the WCS. Listing 15.8 shows this method being called from the DrawCylinder macro with the following variables used as arguments:
CylinderCenter A three-element array defining the coordinates of the center of the cylinder, and also the bounding box that encloses the cylinder.
CylinderRadius A positive value representing the radius of one of the cylinder's flat ends, which both lie in x-y planes.
CylinderHeight A positive value that represents the height of the cylinder. The cylinder's axis is parallel to the z-axis of the WCS.
In the next series of steps, you enter the DrawCylinder macro and run it. Figure 15.8 shows the Cylinder solid generated by the macro.
Exercise 15.1: Building the 3D Solids Application
1. Using the same project, enter the DrawCylinder macro's code (Listing 15.8) into ThisDrawing's Code window.
2. Return to the AutoCAD window, choose Tools ^ Macro ^ Macros and run your DrawCylinder macro from the Macros dialog box.
3. Follow the prompts in the command line to enter the position and dimensions required.
Figure 15.8 Cylinder produced by the DrawCylinder macro (Listing 15.8)
Listing 15.8: DrawCylinder Macro
Analysis
• Line 1 starts the DrawCylinder macro, which prompts the user to enter the position and dimensions of the cylinder, creates it to the user's specifications, and draws it.
• Line 3 declares the CylinderObject variable as being capable of referencing a 3DSolid object.
• Line 4 declares the CylinderCenter variable as a Variant type so that it can be assigned the three-element array returned by the GetPoint method at Line 8.
• Lines 5 and 6 declare the variables that will represent the cylinder's radius and height.
• Line 7 uses the With statement block so that the Utility object's methods can be used without being fully qualified.
• Line 8 calls the GetPoint method to prompt the user to select the position for the center of the cylinder's base. This position is initially assigned to the CylinderCenter variable; then this variable is adjusted at Line 13, updating it to the center of the bounding box.
• Lines 9 and 10 call the GetDistance method to prompt the user to enter the cylinder's radius and height.
• Line 12 updates the number of contours per surface by setting the ContourLinesPerSurface property to 12.
• Line 13 updates the z-coordinate of the center of the base, so that the CylinderCenter variable contains the coordinates of the center of the bounding box.
• Line 14 calls the AddCylinder method to create the cylinder with the attributes specified by the user, and sets up the CylinderObject variable as a reference to the 3DSolid object that represents the cylinder.
• Line 15 calls the Update method to ensure that the cylinder solid is drawn on the screen.
• Line 16 calls the ChangeViewDirection macro so that the cylinder doesn't appear as a 2D rectangle.
• Line 17 ends the DrawCylinder macro.
Creating Elliptical Cones and Cylinders
AutoCAD provides the AddEllipticalCone method to create elliptical cones, and the AddEllipticalCylinder method to create elliptical cylinders. These methods differ from the AddCone and AddCylinder methods by replacing their radius argument with the following two arguments:
ConeXDistance, CylinderXDistance A positive value representing the distance along the x-axis for the elliptical base of the cone or cylinder.
ConeYDistance, CylinderYDistance A positive value representing the distance along the y-axis for the elliptical base of the cone or cylinder.
Listings 15.9 and 15.10 contain the DrawEllipticalCone and DrawEllipticalCylinder macros that generate the elliptical cone and the elliptical cylinder shown in Figures 15.9 and 15.10.
Figure 15.9 Elliptical cone generated by the DrawEllipticalCone macro (Listing 15.9)
Figure 15.10 Elliptical cylinder generated by the DrawEllipticalCylinder macro (Listing 15.10)
Exercise 15.1: Building the 3D Solids Application
The following instructions show you where to enter the DrawEllipticalCone macro and how to run it.
1. Continuing with the same project, enter the DrawEllipticalCone macro's code given in Listing 15.9 into the Code window for ThisDrawing.
2. Return to the AutoCAD window, choose Tools ^ Macro ^ Macros, and run the DrawEllipticalCone macro from the Macros dialog box.
3. Follow the prompts in the command line to enter the position and dimensions required.
Listing 15.9: DrawEllipticalCone Macro
Analysis
• Line 1 starts the DrawEllipticalCone macro that prompts the user for the position and dimensions for the elliptical cone, creates it to those specifications, and draws it.
The code for DrawEllipticalCone was copied from the DrawCone macro and adjusted to include the extra argument. The two macros could be incorporated into one with a message box prompting the user to specify whether or not the solid is elliptical. If an elliptical solid is required, then the GetDistance method would be called to get the y distance; otherwise,thex distance is sufficient.
• Line 3 declares the ConeObject variable as being capable of referencing a 3DSolid object.
• Line 4 declares the ConeCenter variable as a Variant type so that it can be assigned the 3D point returned by the GetPoint method.
• Line 5 replaces the ConeRadius declaration from the DrawCone macro with the two variables that will represent the distances in the x and y directions.
• Line 6 declares the ConeHeight variable that will be assigned the value returned by the GetDistance method in Line 11.
• Line 7 starts the With statement so that the methods of the Utility object can be used without being fully qualified.
• Line 8 prompts the user for the position for the cone's center.
• Lines 9 and 10 prompt the user for the distance in the x and y directions for the elliptical base; these lines replace the prompt for the radius in the DrawCone macro.
• Line 11 prompts the user for the cone's height.
• Line 12 ends the With statement.
• Line 13 assigns a value to the ContourLnesPerSurface variable ofthe Preferences object.
• Line 14 calculates the y-coordinate for the center of the cone.
• Line 15 calls the AddEllipticalCone method with the two arguments passing the x and y distances, replacing the radius argument in the call to the AddCone macro.
• Line 16 calls the Update method of the ConeObject to ensure that the cone is redrawn in the Model space.
• Line 17 calls the ChangeViewDirection macro from Listing 15.2 to ensure that the cone is distinguishable from a 2D triangle.
• Line 18 ends the DrawEllipticalCone macro.
Exercise 15.1: Building the 3D Solids Application
The following instructions show you where to enter the DrawEllipticalCylinder macro and how to run it.
1. Continuing with the same project, enter the DrawEllipticalCylinder macro's code (Listing 15.10) into the Code window for ThisDrawing.
2. Return to the AutoCAD window, choose Tools ^ Macro ^ Macros, and run the DrawEllipticalCylinder macro from the Macros dialog box.
3. Follow the prompts in the command line to enter the position and dimensions required.
Listing 15.10: DrawEllipticalCylinder Macro
Analysis
• Line 1 starts the DrawEllipticalCylinder macro, which prompts the user for the position and dimensions required to create and draw an elliptical cylinder. The code from this macro was copied from the DrawCylinder macro and adjusted to suit.
• Line 3 declares the CylinderObject variable as being capable of referencing a 3DSolid object.
• Line 4 declares the CylinderCenter variable as a Variant type so that it can be assigned the x-,y-,and z-coordinates returned by the GetPoint method.
• Line 5 replaces the radius variable from the DrawCylinder macro with the two variables that represent the x and y directions for the ellipsis at each end of the cylinder.
• Line 6 declares the CylinderHeight variable as Double so that it can be assigned the Double value returned by the GetDistance method at line 11.
• Line 7 starts the With statement so that the Utility object's methods can be used without being fully qualified.
• Line 8 assigns the CylinderCenter variable the point returned by the GetPoint method.
• Lines 9 and 10 replace prompts to the user for the radius, with prompts for the x and y distances to define the ellipse.
• Line 11 assigns to the CylinderHeight variable the value returned by the GetDistance method.
• Line 12 ends the With statement.
• Line 13 sets the ContourLinesPerSurface variable to 12.
• Line 14 calculates the y value of the cylinder's center.
• Line 15 calls the AddEllipticalCylinder method, with the x and y distances replacing the radius argument from the call to the AddCylinder method.
• Line 16 regenerates the cylinder to make sure that the current version appears in the Model Space.
• Line 17 calls the ChangeViewDirection macro to ensure that the cylinder does not appear as a 2D rectangle.
• Line 18 ends the DrawEllipticalCylinder macro.
Now that you've seen how to add a variety of individual solids to your drawings, the next step is to combine them to form a more complex solid.
Source: http://what-when-how.com/autocad-vba/creating-3d-solids-creating-and-drawing-3d-solids-autocad-vba-part-4/
Post a Comment for "Drawing a Cilinder in Autocad"