Draw Rectangle in Autocad Using Vba

A Shape object can contain a mixture of lines, arcs, and circles that collectively define a shape. The AddShape method is used to create a Shape object based on a template shape, which is identified by its name in an SHX file. AddShape has four parameters: the shape's name, its position in the drawing area, the scaling factor, and the rotation angle.

Before you can add a shape to your drawing, it must be loaded into AutoCAD. The LoadShapeFile method is used to load a shape file and to make the various shapes it contains available for use in the current drawing. (Shape files can contain more than one shape.) Here are the steps to view a shape file and the shapes it contains:

1.    From the AutoCAD window, type load in the command line and press Enter. The Select Shape File dialog box appears.

2.    Double-click the Support folder in the list and select the ltypeshp file. Click Open. The Select Shape File dialog box closes and you return to the AutoCAD window.

3.    Type shape in the command line and press Enter. AutoCAD responds with the prompt Enter shape name or [?]:.

4.    Enter ?, and you're prompted to Enter shape name(s) to list <*>:.

5.    Press Enter without typing anything. The Available Shapes list appears, as shown in Figure 13.8.

The sequence of prompts and responses required to see the shapes available

Figure 13.8 The sequence of prompts and responses required to see the shapes available

Loading and Adding Shapes from a Macro

If you know the name of the shape file you want to use, you can load this file and use it to add a shape to the active drawing document. Listing 13.4 gives the code for the GetShapeFromFile macro, which names the box shape as being the one required. Figure 13.9 shows this box drawn in the Model Space as a result of running the macro.

The box shape drawn by running the GetShapeFromFile macro (Listing 13.4)

Figure 13.9 The box shape drawn by running the GetShapeFromFile macro (Listing 13.4)

If you would like to try this macro, enter it into ThisDrawing's Code window and add the following declaration statement to the General Declarations section:

tmp6f1a-104_thumb

You may need to update the filename in Line 4 as required by your installation.

Listing 13.4: GetShapeFromFile Macro

Listing 13.4: GetShapeFromFile Macro

Analysis

•    Line 1 starts the GetShapeFromFile macro,which retrieves the shape named "box" from the shape file and displays it in the Model Space.

•    Line 2 declares the ShapeObject variable as being capable of referencing a Shape object.

•    Line 3 declares the InsertionPoint array that will be assigned the coordinates of the position where the shape will be placed.

•    Line 4 uses the LoadShapeFile method to load the shape file into AutoCAD so that shapes can be retrieved from it to add to the current drawing.

•    Lines 5 through 7 assign values to the InsertionPoint to specify where the shape's single point is placed. The single point appears when the shape is selected. The single point for the box shape is shown later in Figure 13.11.

•    Line 8 calls the AddShape method to create a Shape object, with the box shape being passed as the first argument. The InsertionPoint variable is passed as the second argument to specify where the positioning point is to be placed (see Figure 13.11). The third argument is the scaling factor, passed explicitly as 2 so that the shape doubles in size when drawn. The last argument is the rotation angle if the shape is to be rotated—in this case it's pi / 2, which will rotate the shape by 90°.

•    Line 9 redraws the shape in the Model Space.

•    Line 10 calls the ZoomAll method to ensure that all the shapes are visible in the Model Space.

•    Line 11 ends the GetShapeFromFile macro.

Shape objects have the advantage that only one positioning point is needed, rather than a collection of points to define their outlines. This becomes an advantage if you ever need to select a Shape object. For example, Figure 13.10 appears to contain two rectangles. The one on the left was drawn using the Rectangle icon from the Draw toolbar, and the one on the right is the Shape object, box. When these rectangles are selected, four points are highlighted on the one to the left, while only one point is highlighted on the right one.

The rectangle on the left drawn using the Draw Toolbar is defined by four points, whereas the box shape on the right is defined by only one point.

Figure 13.10 The rectangle on the left drawn using the Draw Toolbar is defined by four points, whereas the box shape on the right is defined by only one point.

The single point that is highlighted in a shape generated from a Shape object represents its position on the screen. If you want to move the shape to a new position, you may find it helpful to drag it to its new position using the positioning point as the basis, as shown in Figure 13.11.

Dragging the box shape around the screen by its positioning point

Figure 13.11 Dragging the box shape around the screen by its positioning point

mottdreatenty.blogspot.com

Source: http://what-when-how.com/autocad-vba/adding-and-loading-shapes-working-with-2d-drawing-techniques-autocad-vba/

0 Response to "Draw Rectangle in Autocad Using Vba"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel