The World of AutoCAD Programming Platforms, Part 319 Jul, 2012 By: Andrew G. Roe
Learn how to create and modify entities with Visual Studio.NET.
In the previous installment of this series about AutoCAD programming, we developed examples that obtained basic drawing information in three different environments: Visual Basic for Applications (VBA), Visual Studio.NET, and AutoLISP. Because Visual Studio.NET seems to be Autodesk’s favored platform for the future, let’s look closer at a .NET example that creates some AutoCAD entities and modifies them programmatically.
This example adds a circle and some text to a drawing, then changes the color of the text to red. It uses the Visual Basic.NET programming environment, although you can use Visual C#.NET instead. To complete this exercise, you'll need either Visual Studio.NET or the free Visual Studio Express. (If you're using AutoCAD 2013, you'll also need to download the ObjectARX SDK to run this example. Even though it's not an ObjectARX example, the necessary references are located in the SDK for 2013.)
Step-by-Step Entity Creation
- Start Visual Studio and create a new project by clicking File > New > Project.
- In the New Project dialog box, select Windows Forms Application, then type a name and file location at the bottom of the box.
- Click OK to close the New Project dialog box.
- Click View > Solution Explorer to display the Solution Explorer.
- Double-click on My Project in the Solution Explorer; the Project Properties screen will appear.
- Click the References tab on the left.
- Click the Add… button to display the Add Reference dialog box.
- In the Add Reference dialog box, click the Browse tab, and navigate to the ObjectARX 2013\inc-win32 or ObjectARX 2013\inc-win64 folder, depending on which version of AutoCAD you are using. Select the Autodesk.AutoCAD.Interop.Common.dll and Autodesk.AutoCAD.Interop.dll files by clicking on one and Ctrl-clicking on the other. (Note: This example is for AutoCAD 2013. If you are using AutoCAD 2012 or older versions, you can click the COM tab and select the appropriate type library and ObjectDBX common type library: axdb18enu.tlb and acax18enu.tlb for AutoCAD 2012. Alternatively, you can download the ObjectARX SDK for older versions and reference the two dll files listed earlier.)
- Click OK to close the Add Reference dialog box.
- Click on the Form1.vb[Design] tab to display the form. From the Toolbox, click and drag a button onto the form.
- Click View > Properties Window. In the Properties window, change the Name property of the button to CreateEntities and the Text property to Create Entities.
- On the form, resize the button by clicking and dragging the handles until all the text is visible.
- Double-click the button to display the Code window. A subroutine is already set up for the Create Entities button you just double-clicked.
- Modify the code to read as shown below, inserting the first two lines at the top of the code window and the others to fit within the preset code.
Public Class Form1
' AutoCAD must be open before running this example.
Dim AcadApp As AcadApplication
Dim acadDoc As AcadDocument
Dim myCircle As AcadCircle
Dim myText As AcadMText
Dim centerPoint(0 To 2) As Double
Dim radius As Double
Private Sub CreateEntities_Click(sender As System.Object, e As System.EventArgs) Handles CreateEntities.Click
AcadApp = GetObject(, "AutoCAD.Application")
acadDoc = AcadApp.ActiveDocument
' Define the circle
centerPoint(0) = 2.0 : centerPoint(1) = 2.0 : centerPoint(2) = 0.0
radius = 4.0
' Create the Circle object in model space
myCircle = acadDoc.ModelSpace.AddCircle(centerPoint, radius)
myText = acadDoc.ModelSpace.AddMText(centerPoint, 1, "Text starts at center point.")
myText.color = ACAD_COLOR.acRed
15. With AutoCAD running, click the Play button (or press F5) to build and run the application.
16. Click the Create Entities button to create the circle and text.
17. Click the X in the upper right of the form to stop execution of the program and close the form.
Let’s take a look at the key elements of the code, taking particular note of the objects, properties, and methods. With references properly set, the Imports statements are used to specify which libraries, or namespaces, will be accessed by the application. Dim statements declare variables for the AutoCAD Application and Document objects, as well as for the circle object and MText objects. Double-precision variables are declared to store values for the circle center point coordinates and radius.
Within the CreateEntities subroutine, the GetObject method is used to access AutoCAD from the .NET environment. The acadDoc variable is assigned to the active drawing using the ActiveDocument property. The AddCircle and AddMText methods can then be accessed to add the circle and text objects, respectively. Finally, the color property of the MText object is changed to red.
You can click File > Save All to save the code, the form, and the other elements of your .NET application. Visual Studio creates an EXE file that can be run as a standalone application outside the .NET environment. You can also create a plug-in, or a module that can be run from within the AutoCAD environment. Autodesk describes this process in an online training document, "My First Plug-in Training."
This example illustrates simple creation and modification of AutoCAD entities, using hard-coded values for entity dimensions and locations. It requires AutoCAD to be running and contains no error handling. In a real application, you'd want to check to see if AutoCAD is running, and if not, start it. You would also probably want more flexibility in entity creation, but the example shows you how to get started.
In future installments of this series, we'll look at more advanced examples and explore some new twists to AutoCAD programming, such as working with mobile devices and cloud computing. If you would like to suggest a topic, feel free to drop me an e-mail.
About the Author: Andrew G. Roe
Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!
Moon Express MX-1 - To the Moon! 12 Dec, 2013
ARTGOS Goes PTC Creo, Drops AutoCAD 11 Dec, 2013
Revit 2014 Update Release 1 Now Available For Download - Crash Reductions or Enhancements? 19 Jul, 2013
How to Replace the Viewport Compass with your Company Logo 5 Jul, 2013
Tricks of the Trade: How to Sharpen Pencils with Artisanal Pencil Sharpener David Rees 11 Dec, 2013