banner



Are Linetypes Embeded In Autocad Drawing Files

This topic covers all you need to know about drawing lines in the AutoCAD window from VBA macros. You tin depict continuous lines, dashed lines, or whatsoever other linetypes available in AutoCAD. You'll see how to depict your lines highlighted and colored, and even how to draw parallel lines—all from code.

You'll as well learn how to develop an awarding one piece at a time past progressively calculation some tried and tested UserForms and macros. Throughout this topic the pieces will exist adult individually, and after they're tested the awarding volition be extended to incorporate them. Sometimes these macros will exist used intact, and at other times the ideas they correspond will be adjusted slightly to adapt.

Cartoon a line from code is accomplished using the AddLine method, which creates a Line object based on the two endpoints passed to it equally arguments in the call. Both endpoints are specified in the World Coordinate System (WCS). This new Line object is then added to the ModelSpace collection (or PaperSpace collection).

For the examples in this topic, I've assumed all the cartoon is done in the Model tab of the AutoCAD window, so the Model Infinite will be the preferred cartoon space rather than the Paper Space.

Permit's create an application that allows the user to input the coordinates for both endpoints of a line and so click a button to create and describe the line. This application, Practise 6.1, volition be adjusted and extended in other sections throughout this topic.

Exercise 6.ane: Line Input Application

You'll begin by creating a new UserForm that allows the user to enter the coordinates of the endpoints defining a line.

one.    First a new project in the IDE and cull Insert ^ UserForm. Double-click the Characterization icon in the Toolbox, and motion the cursor to the top-left corner of the UserForm. The Characterization control's icon follows the cantankerous cursor as it moves.

Double-clicking a Toolbox control allows you to identify multiple instances of it on a UserForm without having to return to the Toolbox each time. Click anywhere within the Toolbox to let Visual Basic know when you've finished.

ii.    With the cursor however in the superlative-left corner, click the mouse. A default-sized Characterization command appears with its peak-left corner positioned at the center of the cross cursor, as shown here:

tmp757e-249

Notice how the cursor is still a cross and that the Label command'due south icon nonetheless follows it around after the click—this is because double-clicking Toolbox icons lets you lot add together as many instances of a control as you similar.

3.    Move the cursor to roughly halfway down the UserForm, keeping it close to the left boundary. Click the mouse, and a 2d default-sized label appears.

4.    Double-click the TextBox command icon in the Toolbox, and click the cursor roughly in the middle and near the elevation of the UserForm. The mouse arrow changes from a Characterization command icon to a TextBox command icon, and a defaultsized TextBox control appears.

tmp757e-250

five. Position the cross cursor in the bottom-left corner of the TextBox control and click over again. Repeat this until you have placed six text boxes on the UserForm, one below the other (meet Figure 6.1). The six text boxes announced evenly spaced and perfectly aligned—if they're not, apply the Format command to brand whatever small adjustments required.

half-dozen.    Double-click the CommandButton icon in the Toolbox, position the cross cursor in the bottom-left corner of the sixth text box, and click. The control button appears, aligned with the text boxes.

7.    Position the cross cursor in the lesser-left corner of the first control button and click. A second command push appears, aligned with the commencement one.

8.    Alter the names of the UserForm and its text box and command button controls to those shown in Table half dozen.one, by overtyping the names in the Properties window. (Choose View ^ Properties Window if this window is not already displayed.)

Placing TextBox controls in a UserForm so they are drawn aligned

Effigy 6.i Placing TextBox controls in a UserForm and then they are drawn aligned

Giving controls names that reflect their functions makes it easier to call up which control is which as the application is extended. Using the prefix naming conventions enables you to easily know the kind of control referred to past the name. In addition, it ensures that controls of the same type are listed together in the Object drop-down list in the Code window.

Table 6.one Assigning Semantic Names to the Line Input UserForm and Its Controls

Old Name

New Proper name

UserForm1

frmLineInput

TextBoxI

txtStartPointX

TextBox2

txtStartPointY

TextBox3

txtStartPointZ

TextBox4

txtEndPointX

TextBox5

txtEndPointY

TextBox6

txtEndPointZ

CommandButtonl

cmdDrawLine

CommandButton2

cmdExit

9. Change the Caption properties of the UserForm and its controls every bit shown in Table vi.2.

Tabular array 6.2 Caption Backdrop for the Line Input UserForm

Control

New Explanation

frmLineInput

Line Input

Label1

Enter the Ten,Y and Z coordinates for the start of the line.

Label2

Enter the X,Y and Z coordinates for the end of the line.

cmdDrawLine

Draw Line

cmdExit

Exit

10. Modify the Accelerator property of the cmdDrawLine control to D, and to x for the cmdExit control. The D and x appear underlined on the control buttons (see Figure half-dozen.two) to bespeak they can exist "clicked" using the Alt+D or Alt+x fundamental-combinations.

 The Line Input UserForm with new captions and accelerator keys

Figure six.2 The Line Input UserForm with new captions and accelerator keys

The alphabetic character 10 is the pseudo-standard accelerator key for exiting from any Microsoft Windows application.

xi. Enter the code shown in Listing half dozen.1. The CreateLine macro should be placed in the General Declarations section of the UserForm's Code window. This process transfers the values from the text boxes into two arrays, which are used as arguments in the call to the AddLine method.

Listing 6.1: CreateLine Macro

Listing 6.1: CreateLine Macro

Analysis

•    Line 1 starts the CreateLine procedure.

•    Lines 2 and iii declare the ii arrays that will concur the coordinates of the endpoints entered into the text boxes by the user. These must be arrays of the Double type so that they tin can be used as arguments when the AddLine method is called at Line 11.

•    Lines 4 through ix take the ten-, y-, and z-coordinates input by the user into the six text box controls and assign them to the elements of the start and endpoint arrays. All the text box controls are accessed without fully qualifying them with the UserForm's name, and the Text property is implied for all the text boxes because it is the default property for this control.

The default property of the TextBox command is Text, then when the control's name appears in code on its ain, VBA automatically assumes the Text property is required.

•    Line 10 starts the With ThisDrawing.ModelSpace argument block,so that the ModelSpace collection volition be used for unqualified items.

•    Line 11 passes the StartPoint and EndPoint arrays every bit arguments to the AddLine method.This method creates a Line object defined by the coordinates passed to information technology and adds the object to the ModelSpace collection of objects.

•    Line 12 uses the Count holding to determine the number of objects currently in the ModelSpace collection. Since the alphabetize to the first object in the collection is cypher, the concluding object is at Count -1. So the Item method is used to render the terminal object added to the collection (which is the line just added). The returned object's Update method is then chosen to redraw the object in the AutoCAD window—so your new line appears.

• Line 13 ends the With ThisDrawing.ModelSpace statement cake.

• Line 14 ends the procedure.

Exercise six.1: Line Input Application

In the adjacent set up of steps, you're going to code the Click event procedures for the two command buttons, code the macro that runs this Line Input application, and run the application.

1.    Continuing in the UserForm's Code window, select cmdDrawLine from the listing of objects, and select Click from the list of procedures. In the skeleton Click event procedure that appears, insert the phone call to the CreateLine macro (Listing 6.i):

CreateLine

This Click consequence procedure of cmdDrawLine volition run when the user clicks the Draw Line button or uses the Alt+D key-combination.

two.    Place the Unload Me statement in the Click event procedure for the cmdExit command button. This event procedure runs when the user clicks the Exit button or uses the Alt+x key-combination. All the lines input past the user are already preserved in the ModelSpace collection, so there is nothing to be saved before quitting.

iii.    Choose Insert ^ Module. Place the following DrawLine macro in Module1's Code window so that AutoCAD will include information technology in the list of macros (meet footstep four). The DrawLine macro uses the Show method to brandish the frmLineInput UserForm and thus start up the awarding.

tmp89fa-4_thumb

4. To run the DrawLine macro, choose Tools + Macro + Macros, select Draw-Line from the list (shown just below), and click Run. The Line Input UserForm appears. When the user clicks the Draw Line button, its Click effect procedure is executed and calls the CreateLine procedure.

tmp89fa-5_thumb

Every VBA projection should have at least one macro so that you lot tin can boot-start the project from the Tools + Macro + Macros dialog box.

v.    Enter the coordinates of a line. Use the Tab key or the Enter key to move from text box to text box in tab order. Then click the Draw Line push button.

This draws the line with the coordinates just entered in the Model tab of the AutoCAD window. In the background, VBA likewise creates a Line object and adds it to the ModelSpace drove.

If you lot need information about tab order, read the side by side section.

6.    Save your project as LineInput.

A Control's Tablndex and TabStop Properties

The TabIndex property is assigned a numerical value that represents the order in which the object was added to the UserForm. When the UserForm is open, its controls tin can exist given the focus in tab gild by repeatedly pressing either the Tab or the Enter cardinal. The advent of a control when it's given the focus depends on the control's type. For instance, when a text box is given the focus, the I-beam cursor appears inside the text box; when a control button is given the focus, its explanation is enclosed in a dashed rectangle.

Although the TabIndex property of every control is allocated a number, VBA volition automatically ready to Fake the TabStop property of any controls that cannot exist used for interacting with the user (such as Labels). The TabStop belongings determines whether or not a control is allowed to get the focus.

The With Statement

The With statement allows you to specify an particular and then include it in a series of statements that use the item, without the demand to authorize the item each time. Items can be an element from a user-defined type, an object, or a collection. For example:

tmp89fa-6_thumb

Here, the three properties that start with a period character (. ) are all assumed to belong to the object specified in the opening With statement.

VBA allows a With argument to exist nested inside some other With statement, with the unqualified object assumed to belong to the object or drove specified in the With statement cake to which the unqualified object belongs. And then y'all must qualify objects in the outer With block when they are used inside the inner With block. For case:

tmp89fa-7_thumb

Hither, the first Height and Color properties are inside the outer With cake,so Objectl is implied. The second Meridian and Color properties are within the inner With block, so Object2 is unsaid. The Width holding in the second to terminal statement belongs to Objectl because it lies inside the outer With cake.

The advantages of using a With argument are that the code requires less typing, it is easier to read, and it will be more efficient to run, leading to improved performance.

Source: https://what-when-how.com/autocad-vba/drawing-a-line-macro-izing-line-drawing-autocad-vba/

Posted by: watkinsniess1969.blogspot.com

0 Response to "Are Linetypes Embeded In Autocad Drawing Files"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel