+ All Categories
Home > Documents > v1 CP3688 Handout

v1 CP3688 Handout

Date post: 06-Apr-2018
Category:
Upload: sybold
View: 217 times
Download: 0 times
Share this document with a friend
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 Use a custom WPF User Control inside AutoCAD® y Set up a dynamic block to be used by the AutoCAD® .NET API y Use the AutoCAD® .NET API to set a dynamic block property y Use enterprise data to drive drawing creation with dynamic blocks y 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 associate¶s degree as an electronics technician and a bachelor¶s degree in computer science. E-mail Address: [email protected]  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 draw ing automations. Also, he is serving as one of his company¶s AutoCAD administers for over 200 AutoCAD users . Josh is currently finishing his MS i n industrial management, and holds a BS in electrical engineering from South Dakota State University. E-mail Address: [email protected]  
Transcript
Page 1: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 1/23

 

Combine Dynamic Blocks, AutoCAD® .NET API, andEnterprise Data to Automate Drawing CreationJon Sprang ± Daktronics Inc.Josh Spahr ± Daktronics Inc.

CP3688 Do you have designers creating drawings from a data sheet? Why not give the data to AutoCADand 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 withdynamic 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 Use a custom WPF User Control inside AutoCAD®

y Set up a dynamic block to be used by the AutoCAD® .NET API

y Use the AutoCAD® .NET API to set a dynamic block property

y Use enterprise data to drive drawing creation with dynamic blocks

y 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 anincredibly powerful tool, and is excited to share what he has learned. Jon has an associate¶s degree as

an electronics technician and a bachelor¶s degree in computer science.

E-mail Address: [email protected] 

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 company¶s 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: [email protected] 

Page 2: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 2/23

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

2

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. Thisincludes 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 leaguesports, college sports, and other businesses in marquee locations. Each video board is differentin physical size and specs, so even though the engineers produce the same type of drawingsfor 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 basedoff of corporate project data using Dynamic Blocks and AutoCAD® .NET API to give theengineers 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 withoutrequiring 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 API2. Using the AutoCAD® .NET API to set a dynamic block property3. Using enterprise data to drive drawing creation with the dynamic block4. Understand the benefits of dynamic blocks in drawing automation5. 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 dynamicblocks.

Page 3: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 3/23

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

3

Use a custom WPF User Control inside AutoCAD® 

Create a new C# ClassLibrary 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.

Page 4: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 4/23

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

4

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

Page 5: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 5/23

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

5

Browse to the AutoCAD

installation folder and selectthe 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.

Page 6: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 6/23

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

6

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.

Page 7: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 7/23

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

7

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.

Page 8: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 8/23

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

8

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.

We¶re 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

Page 9: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 9/23

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

9

We will be using the MVVM (Model-View-ViewModel)

pattern, so we need to add a new class to the projectand 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.

Page 10: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 10/23

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

10

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.

Page 11: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 11/23

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

11

 Add a property called µCommandLineText¶ to our ViewModel. We will use this property to bind

our text box to.

Page 12: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 12/23

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

12

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. We¶ll use this static method to writeto the AutoCAD command-line.

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

Page 13: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 13/23

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

13

In your view model, create a public

method called

WriteTextToCommandLine. Here

we will call our AcadEditor to writetext to the AutoCAD command line.

Page 14: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 14/23

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

14

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 it¶s clicked. In that event, call theWriteTextToCommandLine() 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 that¶s

there.

Page 15: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 15/23

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

15

You are going to need to add a couple more references to your CustomControls project before

this will work. Add references to PresentationCore, PresentationFramework, andWindowsBase.

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)

Page 16: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 16/23

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

16

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 tostretch, 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 codeto insert, adjust, and edit blocks into drawings without having to maintain a large block library.

Page 17: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 17/23

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

17

In our example we will break down one of the dynamic blocks and look at how the properties aredriving the block and how it¶s 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 drivethe block to represent what we want it to look like. The values of these properties will be thevalues that are set through the API with .NET code.

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

Page 18: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 18/23

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

18

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 youto you to name to properties the same. So the same radius value will be set for both Radius_1and Radius_2. There are a number of different actions in this block that that use the samevalue.

The insertion point is also an important attribute to take note of. We put a few different blocks ontop of each other, so having the same insertion point is very important so every block lines upwith each other.

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

Page 19: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 19/23

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

19

Use enterprise data to drive drawing creation

For the enterprise data example, we have constructed an Access database that holds questionsand answers for configured rockets. These rockets could have been configured by a salesteam, somebody creating proposals, etc« Since the data is already there, we should use it tocreate the drawing rather than re-entering the data.

In our little project here, we used Microsoft Access to be a data provider, however, this doesn¶tneed to be the case. Your data provider could come from many different sources: text file,Microsoft Excel file, SQL Database, etc«

Here¶s a relationship diagram of our Access database.

I wrote a quick little WPF application that isdynamically driven from this database. With thisapplication, we can create new or edit existingrockets. This project will be available for you todownload, so you should be able to take a look at itto get more experience with WPF and the MVVMpattern if you¶d like.

Page 20: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 20/23

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

20

Here¶s an example of what the datalooks like for ³My Rockin¶ Rocket.´ Wewill load this data into AutoCAD and use

it to build our drawing.

Here¶s an example of the dataset that willbe returned to AutoCAD. Notice theproperty description and value, thesematch up with the dynamic blockproperties and the value that they will beset to.

Take a look at the dynamic block below. Notice that ishas a height of ³FinHeight.´ ³FinHeight´ is also aproperty (Id 4) in our dataset above. In our AutoCADapplication, we will find the ³FinHeight´ property in thedynamic block and set it to the value that is stored inthe database.

Page 21: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 21/23

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

21

Below is a view of the .NET object that was built from the database. Notice that the value thatwe 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 thatis stored in the database.

With the application set up like this, we have the ability to change the drawing if we¶d likewithout modifying the code. If I¶d like the rocket to have a stripe, I could add that block to thedatabase and it would add it to the drawing and set the properties that the new block hasdefined.

Set a dynamic Block property from the AutoCAD APIBelow 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.

Page 22: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 22/23

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

22

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.

Page 23: v1 CP3688 Handout

8/3/2019 v1 CP3688 Handout

http://slidepdf.com/reader/full/v1-cp3688-handout 23/23

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

23


Recommended