Setting Linetypes (Macro-izing Line Drawing) (AutoCAD VBA) Part 1

AutoCAD has a Linetypes collection that contains all the Linetype objects associated with the active drawing ThisDrawing. You can load as many Linetype objects as you like from the linetype library files. These files all have an extension .lin and are loaded by name into Linetype objects that become members of the Linetypes collection for your drawing.

After a linetype has been loaded, you can access it from the collection in the normal way—using the Item method with an index number. The index number must be less than the number of Linetype objects contained in the collection; otherwise, your program will grind to a halt.

Take care when accessing the Linetypes collection, to ensure that the Index value used is less than the number of Linetype objects. Trying to access a nonexistent Linetype will cause your program to terminate immediately.

Exercise 6.1: Line Input Application

Next, you’ll add the AddNewLineType macro to the Line Input application.

From AutoCAD: The following steps show you how to load a linetype definition into a Linetype object from the AutoCAD window. This material may already be familiar to you, but I’ve included it here in order for you to compare this process with how it’s done in VBA code.

1.    Choose Format 0- Linetype. The Linetype Manager dialog box appears (Figure 6.3).

2.    Click the Load button. The Load or Reload Linetypes dialog box appears next, as shown in Figure 6.4. Notice that the filename acad.lin is displayed in the Filename box at the top—this is the default AutoCAD Linetypes file.


Linetype Manager dialog box

Figure 6.3 Linetype Manager dialog box

Load or Reload Linetypes dialog box

Figure 6.4 Load or Reload Linetypes dialog box

3. Select the DASHED linetype and click OK. When you return to the Linetype Manager dialog box, it now includes DASHED in the list of linetypes.

4. Select the DASHED linetype, click the Current button, and then click OK. The Linetype Manager dialog box closes and you return to the AutoCAD window, where the DASHED linetype is displayed in the Linetype box.

tmp89fa-10_thumb

From VBA: Now let’s take a look at how this same thing is achieved using VBA.

1.    The AddNewLineType macro shown in Listing 6.2 loads the DASHED2 Line-type object and makes it the current linetype. Enter this macro into Module1 beside the DrawLine macro.

2.    Run your macro from the Macros dialog box (AutoCAD window)—you’ll only be able to do this once for each linetype; otherwise, line 2 will cause a run-time error when you try to load the same linetype twice.

3.    If you want to run your macro again, you’ll need to delete the DASHED2 linetype. However, since you cannot delete the current linetype, you’ll need to make another linetype current before the deletion can take place. Here are the steps to do that:

a.    Choose Format ^ Linetype to open the Linetype Manager dialog box (Figure 6.3).

b.    Select any Linetype from the list except DASHED2 and click Current. The selected linetype becomes the current linetype.

c.    Select DASHED2 from the Linetype list and click Delete. The DASHED2 linetype disappears from the list.

d.    Click OK to return to the AutoCAD window.

Listing 6.2: AddNewLineType Macro

Listing 6.2: AddNewLineType Macro

Analysis

•    Line 1 declares and starts the AddNewLinetype macro.

•    Line 2 declares DashedLine as a variable that can refer to an AcadLineType object.

•    Line 3 uses the Load method of the Linetypes collection to retrieve the linetype named DASHED2 from the file named acad.lin. When you compare this with the AutoCAD steps, in which the linetype was set manually from the AutoCAD window, the statement in Line 3 is equivalent to a combination of step 2, clicking Load, and step 3, selecting the linetype and clicking OK.

•    Line 4 uses the Item method to retrieve the last Linetype object added to the Linetypes collection—the object named DASHED2 just loaded. The last object added has an index value equal to one less than the number of objects in the collection (Count property less one), since the first object starts at zero. The Set statement is used to assign a reference to this object to the variable DashedLine.

•    Line 5 takes the Linetype object referenced by the DashedLine variable and assigns it to the ActiveLinetype property for the drawing. This is equivalent to step 4 in the AutoCAD procedure, where the DASHED linetype was selected and the Current button clicked.

The AddNewLineType procedure suffers from the fact that it can’t be run more than once without updating the linetype’s name in line 3.

Getting a Linetype from the User

Let’s create a new UserForm containing a list box that displays a list of all the linetypes currently available and allows the user to select the one required. This Linetypes UserForm can then be used in conjunction with the LineInput UserForm developed as the first application in this topic. The LineInput UserForm will get the coordinates from the user, and the Linetypes UserForm will get the line type; together they will be used to generate the new line.

Exercise 6.1: Line Input Application

1. Start AutoCAD and choose Tools –Macro – Load Project. The Open VBA Project dialog box appears, for you to select the project you want to load (Figure 6.5).

The Open VBA Project displays a selection of folders and VBA projects.

Figure 6.5 The Open VBA Project displays a selection of folders and VBA projects.

2.    Select the project file containing your LineInput UserForm and click Open. The IDE opens with your UserForm.

3.    Choose Insert ^ UserForm to add a new UserForm. The Code window for UserForm1 appears. (Remember, the frmLineInput UserForm started life named UserForm1.)

4.    Drag two command buttons and a label onto the UserForm. Click the ListBox icon in the Toolbox, and add a list box.

tmp89fa-13_thumb

The UserForm should look like this:

tmp89fa-14_thumb

5. Change the Caption properties to those shown in Table 6.3.

Visual Basic assigns default names for UserForms and controls starting from UserForm1 on, rather than basing names on the number of UserForms or controls of the same type that already exist.

Table 6.3 Caption Properties for the Linetypes UserForm

Control

New Caption

UserForm1

Linetypes

Label1

Please select a linetype from the list:

CommandButton1

OK

CommandButton2

Cancel

6.    Change the Accelerator property of CommandButton1 to the letter O, and CommandButton2 to C. The familiar underscored characters will appear under these accelerator characters on the buttons.

7.    Change the Cancel property of CommandButton2 to True, so that it will respond to the user’s pressing of the Esc key—this is a Windows pseudostandard.

8.    Rename the UserForm and controls as shown in Table 6.4.

Table 6.4 Name Properties for the Linetypes UserForm

Control

NewName

UserForm1

frmLinetypes

CommandButton1

cmdOK

CommandButton2

cmdCancel

ListBox1

lstTypes

9. Type the code shown in Listing 6.3 into the skeleton for the Click event procedure of cmdOK.

The code in Listing 6.3 uses While loops rather than For statements. If For statements had been used, every item from the list box would need to be accessed, and the selected item compared with the Name property of every Linetype object in the Linetypes collection. Using the While statements allows us to stop searching as soon as we find what we are looking for—resulting in fewer accesses and faster results. This increase in efficiency becomes even more important when long lists or large collections are involved.

Listing 6.3: The OK Button’s Click Event Procedure

Listing 6.3: The OK Button's Click Event Procedure

Analysis

•    Line 1 starts cmdOK’s Click event procedure and is provided as a skeleton procedure when you select cmdOK from the Object list and Click from the Procedure list in the Code window.

•    Line 2 tests to see if the ListIndex property of the list box is set to zero or more, which indicates that an item has been selected. ListIndex is -1 if nothing is selected from a list box.

•    Line 3 only runs if a list-box item has been selected; this line assigns the name of the selected item to the ActiveLineType property of ThisDrawing. Passing the ListIndex property of the lstTypes list box provides the index number of the selected item; the List method of the lstTypes provides the selected item’s name.

•    Line 5 unloads Me, which is the frmLinetypes UserForm, since it is no longer required.

•    Line 6 ends the event procedure and is provided as part of the skeleton code.

Next post:

Previous post: