Adding Text to Arrows (Creating Drawings with Text) (AutoCAD VBA)

It is common practice to include annotated arrows in a drawing; the arrow points at a particular item to give some information about it. This can also be done from code using the Leader object, which combines text with an arrow that points to a drawing object. The arrow’s head can be one of several types, and its tail can be made up of one or more straight lines or spline segments.A Leader object is added to the ModelSpace collection using the AddLeader method.

The following exercise shows you how to include annotated arrows in your drawings.

tmp89fa-206_thumb

Old Name

New Name

Caption

UserForm

frmArrows

Draw Arrow

ComboBox1

cboArrowhead

TextBox1

txtArrowText

CommandButton1

cmdContinue


Continue

7. Copy the GetShape macro from the earlier section “Creating Polar Arrays From A Macro” into the ThisDrawing Code window, and change its name as follows:

tmp89fa-207_thumb

8. Enter the code shown in Listing 9.15 into the ThisDrawing module Code window. This listing contains the DrawArrow macro that is used to open the application from the Macros dialog box, and the RedrawPolyline macro that re-creates a new polyline, which includes the last point clicked.

Listing 9.15: Declarations and Macro for Running the Application

Listing 9.15: Declarations and Macro for Running the Application

Analysis

•    Lines 1 through 5 declare the module level variables that are used by various procedures.

•    Line 7 starts the DrawArrow macro that opens the frmArrow UserForm.

•    Line 8 calls the Show method of the frmArrows UserForm to display it on the screen.

•    Line 9 unloads the Draw Arrow UserForm.

•    Line 10 ends the DrawArrow macro.

•    Line 12 starts the RedrawPolyline procedure that deletes the last polyline and creates a new one containing the point just entered by the user.

•    Line 14 starts the With statement block so that the properties and methods from the Model Space tab of the active drawing can be used without being fully qualified.

•   Line 15 deletes the Polyline object named PolygonObject.

•   Line 16 creates a new Polyline object based on the latest PolyArray, including the last point selected,and assigns it to the PolygonObject array.

•    Line 17 sets the color of the Polyline object to blue.

•    Line 18 calls the Update method of the PolygonObject to redraw it in the Model Space so that the last point is included.

•    Line 19 ends the With statement.

•    Line 20 ends the RedrawPolyline macro.

Exercise 9.4: Draw Arrow Application

Now enter the code from Listing 9.16 into the ThisDrawing’s Code window This code contains the CreateArrow macro that creates an arrowto the user’s specifications.

Listing 9.16: CreateArrow Macro

Listing 9.16: CreateArrow Macro

 

Listing 9.16: CreateArrow Macro

Analysis

•    Line 1 starts the CreateArrow macro, which creates an arrow according to the user’s specifications.

•    Line 3 declares the LeaderObject variable as being capable of referring to an AcadLeader object. This object comprises an arrowhead, a tail, and an object containing the annotation.

In this example, the annotation is provided using an MText object, but a Tolerance or a BlockRef object can also be used.

•    Line 4 declares the TextObject variable to refer to the MText object that will be associated with the Leader object.

•    Line 5 declares EndTail as an array of doubles so that it can be used as an argument in the call to the AddMText method at Line 13.

•    Line 6 calls the GetTail procedure that prompts the user to input the arrow’s tail.

•    Lines 8 through 10 set the elements of the EndTail array to the coordinates of the last point selected by the user.

•    Line 12 uses the With statement,so that methods from the ModelSpace object can be used without being fully qualified.

•    Line 13 creates a new MText object based on the position stored in EndTail, which is the last point selected for the arrow’s tail.

•    Line 14 creates a new Leader object by passing its parameters: the points selected by the user, a reference to the MText object, and the type of tail required for the arrow.

•    Line 15 ends the With statement block.

•    Line 17 assigns the ArrowHeadType set in an event procedure to the ArrowHeadType property of the Leader object.

•    Line 18 ends the CreateArrow macro.

Exercise 9.4: Draw Arrow Application

In the next set of steps, you will finish off your application and run it to try it out.

1. Insert a standard Module and add the following two declarations:

tmp89fa-211_thumb

2. Type the event procedures given in Listing 9.17 into the skeleton event procedures provided in the frmArrows Code window.

Listing 9.17: Event Procedures for Draw Arrow Application

Listing 9.17: Event Procedures for Draw Arrow Application

 

 

Listing 9.17: Event Procedures for Draw Arrow Application

 

 

Listing 9.17: Event Procedures for Draw Arrow Application

Analysis

•    Line 1 starts the cmdContinue Click event procedure.

•    Line 2 starts the If statement and determines whether the text box is empty.

•    Line 3 calls the MsgBox function to let the user know the text for the arrow hasn’t been entered.

•    Line 4 starts the Else part of the If statement.

•    Line 5 hides the frmArrows UserForm so that the user can gain full access to the Model Space in the AutoCAD window.

•    Line 6 calls the CreateArrow macro from the ThisDrawing module.

•    Lines 7 and 8 end the If statement and the Click event procedure, respectively.

•    Line 10 starts the Change event procedure of the combo box. This procedure is responsible for assigning the arrowhead selected by the user to the Arrowhead-Type variable.

•    Lines 11 through 44 contain the Select Case statement block that assigns the value of an AutoCAD constant to the ArrowheadType variable according to the item selected by the user from the combo box.

•    Line 45 ends the Change event procedure of the combo box.

•    Lines 47 through 49 and Lines 51 through 53 contain the Click event procedures of the two option buttons that set the ArrowTailType variable to the appropriate AutoCAD constant.

•    Line 55 starts the Initialize event procedure of the frmArrows UserForm.

•    Lines 56 through 74 contain the With statement block that enables the AddItem method to create the arrowhead type items in the combo box.

•    Line 75 ends the Initialize event procedure of the frmArrows UserForm.

Exercise 9.4: Draw Arrow Application

Now all the coding is finished. All you have left to do is run the application to test it and see if it performs as you want it to.

1. Run the DrawArrow macro from the Macros dialog box in AutoCAD. Select an arrowhead type from among the 16 options provided, and an arrow tail type.

2.    Enter the text to be displayed at the end of your arrow into the Annotation box and click Continue.

Figure 9.17 shows the text I entered, and Figure 9.18 shows the results.

Selecting the Open 30 arrowhead type and the Straight Line Segments arrow tail

Figure 9.17 Selecting the Open 30 arrowhead type and the Straight Line Segments arrow tail

Results of selections shown in Figure 9.17

Figure 9.18 Results of selections shown in Figure 9.17

3. Try drawing a zigzag arrow tail, using straight-line segments and using spline segments, and compare the difference. Figure 9.19 shows a polyline through the points I selected for my arrow’s tail, and Figure 9.20 shows the spline curve that used these points as control points.

Polyline through the points selected for the spline segments shown in Figure 9.20

Figure 9.19 Polyline through the points selected for the spline segments shown in Figure 9.20

Arrow drawn with spline tail segments

Figure 9.20 Arrow drawn with spline tail segments

Summary

After reading this topic, you should be able to do all of the following:

•    Create a new drawing document from a macro.

•    Create a rectangular array based on a freeform shape from a macro.

•    Create a polar array based on a freeform shape from a macro.

•    Format text from a macro.

•    Use frames and option buttons.

•    Format numbers, dates, and times using the VBA Format function.

•    Validate numerical input from a macro.

•    Develop an application to add annotated arrows to your drawing.

Next post:

Previous post: