You are on page 1of 23

Combine Dynamic Blocks, AutoCAD .

NET API, and Enterprise Data to Automate Drawing Creation


Jon Sprang Daktronics Inc. Josh Spahr Daktronics Inc.

CP3688 Do you have designers creating drawings from a data sheet? Why not give the data to AutoCAD and let it build the drawing for you? This class will show you how to use Windows Presentation Foundation (WPF), C# Class Libraries (using the AutoCAD .NET API) and database connections within AutoCAD, combined with dynamic AutoCAD blocks, to create a scalable and maintainable solution for multiple users across your company.

Learning Objectives
At the end of this class, you will be able to:
y y y y y Use a custom WPF User Control inside AutoCAD Set up a dynamic block to be used by the AutoCAD .NET API Use the AutoCAD .NET API to set a dynamic block property Use enterprise data to drive drawing creation with dynamic blocks Understand the benefits of dynamic blocks in drawing automation

About the Speaker


Jon is an AutoCAD user turned software developer who is using his experience in both disciplines to implement enterprise solutions for drawing automation. With seven years of AutoCAD experience and five years of .NET development experience, he has discovered that the AutoCAD .NET API can be an incredibly powerful tool, and is excited to share what he has learned. Jon has an associates degree as an electronics technician and a bachelors degree in computer science. E-mail Address: Jon.Sprang@daktronics.com

Josh is currently a business analyst, working with engineers to discover ways to have CAD tools simplify engineering processes. As an engineer himself, he has worked with AutoCAD for 10 years and has pushed the power of dynamic blocks to the limit for drawing automations. Also, he is serving as one of his companys AutoCAD administers for over 200 AutoCAD users. Josh is currently finishing his MS in industrial management, and holds a BS in electrical engineering from South Dakota State University. E-mail Address: Josh.Spahr@daktronics.com

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Introduction
Businesses are striving to become lean in these years where the economy is slow and is very competitive. They are asking their employees to reduce costs in all possible areas. This includes looking into our CAD tools to find ways that they can assist engineers to reduce cost, limit human errors, and improve productivity. Our company is no different, at Daktronics we design large video boards for major league sports, college sports, and other businesses in marquee locations. Each video board is different in physical size and specs, so even though the engineers produce the same type of drawings for each display, they are each unique and needed individual time to create. The task that we want to share with you is how we helped to automate custom drawings based off of corporate project data using Dynamic Blocks and AutoCAD .NET API to give the engineers a quick and easy tool to create drawing in the matter of minutes rather the hours. Another goal was to give engineers more flexibility to make changes to the drawing without requiring software development time. There are five major steps that we want to share in this class. 1. Setting up a dynamic block to be used by the AutoCAD .NET API 2. Using the AutoCAD .NET API to set a dynamic block property 3. Using enterprise data to drive drawing creation with the dynamic block 4. Understand the benefits of dynamic blocks in drawing automation 5. Use a custom WPF User Control inside AutoCAD. We decided to build an example project for this class which we posted online along with our handout with the hopes that you could use for reference if needed. It will be out on the AU site for seven days after AU2011 is over. The example project is to use AutoCAD to build a rocket from enterprise data, using dynamic blocks.

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Use a custom WPF User Control inside AutoCAD


Create a new C# Class Library in Visual Studio by clicking File >> New >> Project

Select Windows >> Class Library, enter your folder location and project name, then click OK

Right click on your project >> Add Reference to the acmgd.dll and acdbmgd.dll, both located in the installation directory of AutoCAD. 3

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Right click on your project and change the Target Framework to .NET Framework 3.5. This will work if you are using AutoCAD 2010 (I believe you can use 4.0 if you are on AutoCAD 2012).

Right-click on your project >> Build This will save a .dll file in the bin of your project folder that we can use within AutoCAD. Now we will add the acad.exe to the project so that we can launch and debug AutoCAD from within our project. Right-click on your Solution >> Add >> Existing Project

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Browse to the AutoCAD installation folder and select the acad.exe, click OK.

You should now have a project called acad in your solution, as shown below.

Right-click on the acad project and select Set as StartUp Project. Your solution will now launch AutoCAD when you want to debug your application.

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Once again, Right-click on the acad project, but this time select Properties. In the Properties window, set the Debugger Type to Managed (v2.0, v1.1, v1.0)

Add a breakpoint to the editor line in your class.

Click the to start debugging. Notice that AutoCAD launches. Once AutoCAD has launched, type netload in the command line and press enter.

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

This will open a dialog box. Browse to the folder location of your project and go in to the Bin >> Debug folder, select the .dll (with the same name as your project) and select OK.

Look back at your command in your project and find the value place in the CommandMethod. The value I have in mine is MyFirstWpfUserControl.

Go back to AutoCAD and type that value in the command-line and press enter.

This will execute the code that we wrote and hit your breakpoint in your project.

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Click

to continue on.

Back in AutoCAD, you will notice that we wrote Hello AU2011 to the command-line, just like we told it to. Click the to stop debugging. This will also close AutoCAD.

Were going to shift gears a bit now and create a WPF User Control to use within AutoCAD. We will use the command that we just wrote to launch the user control. Right-click on your Solution, select Add >> New >> Project. Select Visual C# >> Windows >> WPF User Control Library. Name it, and select OK.

Right-click in your user control project and select Add >> User Control Name it (I named mine

CommandLineWriter.xaml), change the framework to .NET Framework 3.5, and click OK

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

We will be using the MVVM (Model-View-ViewModel) pattern, so we need to add a new class to the project and call it CommandLineWriter_ViewModel.cs

Set the DesignHeight to 150 and the DesignWidth to 400 in the CommandLineWriter.xaml

Set the background color of your control, and add a TextBox and a Button as shown below.

Set the DataContext of your View to the ViewModel that we created. 9

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

In the CommandLineWriter.xaml.cs, create a new private property called viewModel, of type CommandLienWriter_ViewModel and set it equal to the DataContext of the view.

In our ViewModel, we need to implement INotifyPropertyChanged so that we can alert the View when we want it to update. Your ViewModel should now look like this.

10

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Add a property called CommandLineText to our ViewModel. We will use this property to bind our text box to.

11

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Hook up the binding on your TextBox

We need to create another project that our user control can use to interact with AutoCAD. In the solution, right-click and Add >> New Project >> C# Class Library (set the framework to .NET Framework 3.5). Name this one AcadLogic.

Right click on the initial Class1.cs that is created and rename it to AcadEditor. In this project, also add a reference to the acmgd.dll and the acdbmgd.dll like we did for the AcadClassLibrary project. Make the AcadEditor a static class with one static method. Well use this static method to write to the AutoCAD command-line.

In the CustomControls project, we will need to add a reference to the AcadLogic.dll.

12

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

In your view model, create a public method called WriteTextToCommandLine. Here we will call our AcadEditor to write text to the AutoCAD command line.

13

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Double click on your button in the view, this will create a click event in the CommandLineWriter.xaml.cs that the button will trigger when its clicked. In that event, call the WriteTextToCommandLine() method on the ViewModel. We should now be able to use our WPF Control to write text to the command line of AutoCAD.

We now need to add some code to the AcadClassLibrary to make it show our new user control in AutoCAD. You will notice that a Guid is used. To get this, in Visual Studio, go to Tools >> Create GUID >> Copy. This will copy a GUID to your clipboard so you can paste it in your code in place of the one thats there.

14

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

You are going to need to add a couple more references to your CustomControls project before this will work. Add references to PresentationCore, PresentationFramework, and WindowsBase.

You should now be able to execute your code and see your new user control appear in AutoCAD. (you will need to do a netload again in AutoCAD to load your .dll before it will work)

15

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

You are now setup to interact with AutoCad from a WPF application. All you need to do is implement your code using the AutoCAD API in the AcadLogic project

Setting up a dynamic block to be used by the AutoCAD .NET API


There are always advantages to using dynamic blocks in drawings, especially when you need to stretch, move, or change information within a block, but still not losing its basic properties. Using dynamic blocks with .NET API is no different. Dynamic blocks allow flexibility for the code to insert, adjust, and edit blocks into drawings without having to maintain a large block library. 16

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

In our example we will break down one of the dynamic blocks and look at how the properties are driving the block and how its set up. The following block that we will look at is the ROCKET_FIN block. In the image below, you can see that by selecting the block the properties are showing a list of custom properties that are built with in the dynamic block. These are the properties that drive the block to represent what we want it to look like. The values of these properties will be the values that are set through the API with .NET code.

Opening the block editor you can see how the different properties are set, some like the FinHeight is a stretch action. Others like the Radius_1 is a move actions, and the Number_of_Fins is a visibility action. If there are a lot of actions you want to happen there is also the Lookup action that allows one property to control multiple actions at once. I would encourage you open the block and see how each property is set and what is controls within the block. I know that there is a lot of helpful information on the AU websites that dig deeper into Dynamic Blocks, but this class is more focused on the .net side of controlling these blocks.

17

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

One thing that you might notice is that there are a number of properties that have the similar names like Radius_1 and Radius_2. This needs to happen because AutoCAD will not allow you to you to name to properties the same. So the same radius value will be set for both Radius_1 and Radius_2. There are a number of different actions in this block that that use the same value. The insertion point is also an important attribute to take note of. We put a few different blocks on top of each other, so having the same insertion point is very important so every block lines up with each other.

Once everything is set in the block it is time to let the code do its magic to automate a drawing.

18

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Use enterprise data to drive drawing creation


For the enterprise data example, we have constructed an Access database that holds questions and answers for configured rockets. These rockets could have been configured by a sales team, somebody creating proposals, etc Since the data is already there, we should use it to create the drawing rather than re-entering the data. In our little project here, we used Microsoft Access to be a data provider, however, this doesnt need to be the case. Your data provider could come from many different sources: text file, Microsoft Excel file, SQL Database, etc Heres a relationship diagram of our Access database.

I wrote a quick little WPF application that is dynamically driven from this database. With this application, we can create new or edit existing rockets. This project will be available for you to download, so you should be able to take a look at it to get more experience with WPF and the MVVM pattern if youd like.

19

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Heres an example of what the data looks like for My Rockin Rocket. We will load this data into AutoCAD and use it to build our drawing.

Heres an example of the dataset that will be returned to AutoCAD. Notice the property description and value, these match up with the dynamic block properties and the value that they will be set to.

Take a look at the dynamic block below. Notice that is has a height of FinHeight. FinHeight is also a property (Id 4) in our dataset above. In our AutoCAD application, we will find the FinHeight property in the dynamic block and set it to the value that is stored in the database.

20

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Below is a view of the .NET object that was built from the database. Notice that the value that we will set the FinHeight property to is 20.

All dynamic properties that have a name that exists in the database will be set to the value that is stored in the database. With the application set up like this, we have the ability to change the drawing if wed like without modifying the code. If Id like the rocket to have a stripe, I could add that block to the database and it would add it to the drawing and set the properties that the new block has defined.

Set a dynamic Block property from the AutoCAD API


Below is some Pseudo code with very high level overview of the process that we took to build the drawing from the enterprise data. We will walk through the code during the presentation.

21

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

Understand the benefits of dynamic blocks in drawing automation


The nice thing about using dynamic blocks the way that we did is the flexibility, and diversity of them. We used only five different dynamic blocks in this example

We are able to create different sizes of rockets and even the number of fins on the rock from these five dynamic blocks and the data values that where provided. The following are a few examples of different rockets built.

22

Combine Dynamic Blocks, AutoCAD .NET API, and Enterprise Data to Automate Drawing Creation

23

You might also like