You are on page 1of 40

Using Windows Presentation Foundation in AutoCAD

Fenton Webb
Senior Developer Evangelist

About the Presenter


Fenton Webb Developer Technical Services Americas Autodesk, Inc
http://twitter.com/fentonwebb

Fenton has been a member of the Autodesk DevTech team since 2000. Originally a member of our EMEA team, he has recently relocated to California to work for DevTech Americas. Fenton is an expert in all the AutoCAD APIs, AutoCAD OEM, RealDWG and Revit. He particularly enjoys travelling to evangelise the APIs he support at our annual Developer Days conferences. Before joining Autodesk, Fenton worked for an ADN partner developing ObjectARX applications in the Civil and Structural Engineering domain.

Autodesk Developer Network


 

Access to almost all Autodesk software and SDKs




Including early access to Beta software

Members-only website with 1000s of technical articles  Unlimited technical support  Product direction through conferences  Marketing benefits


Exposure on autodesk.com  Promotional opportunities Based on user level

1 to 3 free API training classes




www.autodesk.com/joinadn

DevLab

San Francisco, USA Feb 1-5, 2010  Farnborough, UK Feb 8-12, 2010  Munich, Germany June 7-11, 2010  Other countries TBD

 

www.autodesk.com/apitraining > Schedule stephen.preston@autodesk.com

Course Objective
Im going to
Give an Overview talk about Windows Presentation Foundation  Show you how you can use it in your own applications inside AutoCAD  Let you see how easy and powerful it is and what it can do for you


Im not going to


Teach WPF directly, sorry

Agenda
Part 1

What is WPF? Why Use WPF? WPF in AutoCAD WPF Demos Databinding Migration and Interoperability Questions

What is WPF?
Technical Information

Windows Presentation Foundation  Next Generation Graphical developer Platform from Microsoft


Also known as WinFx

Packaged with .NET Version 3.0  Driven by Direct3D




Uses XAML
  

eXtensible Application Markup Language XML based Declarative Language




Requires very little programming

Why WPF?
Windows Presentation Foundation API Comparison
Windows Forms GUI, (forms, controls,etc) On-screen documents Fixed-format documents Images Audio/Video 2D graphics 3D graphics PDF Windows Forms/ GDI+ Windows Media Player Direct 3D WPF

X X X

X X X X X X X

X X X X

What is WPF?
Lets see it for real...

Windows Presentation Foundation


by Microsoft

What is WPF?
Features Recap

Controls  Similar to existing controls  All Support rich content, regardless of control type


You Can add images or videos to a Listbox for instance

Layouts  Uses different Panel types to handle layout design


   

StackPanel stack either horizontally or vertically WrapPanels stack like standard html DockPanel for sophisticated layouts GridPanel for sophisticated layouts Based on vector graphics

Native resizable


What is WPF?
Features Recap

Vector graphics  Can be scaled and rotated




Without loss of image fidelity Can also be programmatically manipulated

Vectors are totally accessible




Text enhancements  Smooth fonts


 

Sub pixel Clear type Provides Scale clarity Embed them into your own application Open type font support


Can create your own fonts


 

Allowing different capital letters for specific words

What is WPF?
Features Recap

WPF Documents  Fixed Documents




enable a precise presentation, independent of the display or printer hardware. dynamically adjust their content based on variables such as window size.

Flow Documents


Effects  Shadows, Blur, Reflection, Glow, and more  Can be applied Text, Images, Vector graphics


Basically any item with an outline

3D Built in 3D Graphics engine  Uses Direct3D




Why use WPF?


Realisations

User Experience is Key


Its not about the product, its about the user experience integrated with the product

Existing Software platforms dont allow for proper mixing of UI Elements




How about combining a Listbox filled with a list of Videos!?  Or tooltip with a bitmap in it?!  Or a button with different format text on it?

Always been difficult to get Product Designers to communicate their ideas properly to Engineering!

Why use WPF?


Solutions
User Experience is Key

.NET 3.0 Technology with WPF


    

Offers interoperability between Window Forms, and even Win32 and MFC Allows for easy migration path to use WPF Current .NET Release 3.5 AutoCAD 2009 installs .NET 3.0 Based on .NET 2.0

Existing Software platforms dont allow for proper mixing of UI Elements

Integrates UI, Media and Documents into the platform.


 

Allows you to mix, match and Blend UIs, Controls, Data binding Thus allowing easy integration of media elements such as 2D, 3D, Audio, Video, Animation etc

Always been difficult to get Product Designers to communicate their ideas properly to Engineering!

Introduces XAML (more next Slide)

Why use WPF?


What is XAML?

XAML  Declarative mark-up language  Separates application design from the implementation
 

External Resource Files Designers and developers can now share the same technology base

Designers create the user interface saved to XAML




XAML can then be loaded directly into Visual Studio.

Localisation much easier


 

Built into XAML Allows for Post Release modification

More in the demo section about XAML

WPF Already in AutoCAD! (since 2009)


New Ribbon Bar!

Toolbars are Ribbons




Old toolbars still available

Autodesk.Windows namespace
RibbonTab  RibbonPanel 


WPF Already in AutoCAD! (since 2009)


New Style Task Dialogs!

Confirmation dialogs are now Task Dialogs Autodesk.Windows namespace


TaskDialog  TaskDialogButton  TaskDialogCommandButtons  Etc


WPF Already in AutoCAD 2009!


New Tooltip Styles!

Expanding ToolTips Content defined using XAML resources




Can be dynamically created

Autodesk.Customization namespace


ToolTip  ToolTipContent 

WPF Demonstrations
Show how...

Demo
AutoCAD.NET WPF Application

Demonstrating
Visual Studio 2005 sp1/2008 Creating an WPF Dialog with a button Modifying the button to use WPF features

WPF Demonstrations
Creating AutoCAD.NET WPF Gotchas

Error CS0103: The name InitializeComponent does not exist in the current context
Happens when adding WPF functionality to old Projects


Edit your project file so that the single line  It seems Visual Studio 2008 doesnt have the problem

<Import Project=$(MSBuildBinPath)\Microsoft.CSharp.targets /> becomes... <Import Project=$(MSBuildBinPath)\Microsoft.CSharp.targets /> <Import Project=$(MSBuildBinPath)\Microsoft.WinFX.targets />

WPF Demonstrations
Show how...

Demo
Expression Blend AutoCAD.NET WPF Application

Demonstrating
Using Expression Blend 2 Visual Studio 20008 Creating an WPF Dialog with a button

WPF Demonstrations
Sample Demos...

Demo
AutoCAD Migrated WPF Samples

Demonstrating
Existing WPF Samples Ported to AutoCAD

WPF Demonstrations
Sample Demos...

Demo
More WPF Samples

Demonstrating
Existing WPF Samples

More Demos

WPF Databinding
One of the best features of WPF

Synchronizes data between 2 properties


 

Generally used for binding a UI Element to backend data (2 way) Works for an entire collection too!

Source

Target

Source Source Source

Target Target Target

WPF Databinding
Harnessing

Can be implemented in 2 ways




DependencyProperty
 

Easy implementation Not much control

Implement INotifyPropertyChanged / INotifyCollectionChanged


   

More work to implement Total control Good for hooking in other tasks AutoCADs Ribbon uses INotifyPropertyChanged exclusively

WPF Databinding
Using a DependencyProperty

// pseudo code class myClass { // create member variable DependencyProperty mVisible; myClass() { // set the type of the DependencyProperty mVisible = DependencyProperty.Register("Visible", typeof(Boolean), typeof(myClass)); } }

WPF Databinding
Using the Databinding Interfaces

INotifyPropertyChanged

Source
INotifyCollectionChanged

Target

Source Source Source

Target Target Target

WPF Demonstrations
Show how...

Demo
Ribbon Bound to PaletteSet

Demonstrating
Creating a PaletteSet using WPF Custom Ribbon Control Binding the Text size to Slider

WPF Databinding
CLR Properties have issues

For normal CLR properties (normal .NET properties)




They know nothing about DependencyProperty


 

Before WPF was invented No way to fire notification of an update

 

You can implement it by hand though using




INotifyPropertyChanged / INotifyCollectionChanged

More work  But much better control




You can also implement it like I did, but its non standard

WPF Demonstrations
Show how...

Demo
Modal Modeless

Demonstrating
Data Binding Synchronization UI with Database

WPF Demonstrations Data Binding Modal


Lessons Learned

Databinding made data transfer very easy


Yet we had to enumerate the layers manually


AutoCAD requires that we Open/Close objects before modifying Normal WPF Binding easy after that This will not work for a modeless dialog/palette

We must have a Transaction open for the duration of the dialog


 

Transaction

WPF Demonstrations Databinding Modeless


Lessons Learned

Much more challenging, we must consider User actions on the UI


lock the document before each database access  open/close database objects  Perhaps issue commands instead?

 

Enables the user to discover the command line interface Enables the action recorder to record the commands

User actions inside of AutoCAD


Changes to drawing, update our UI  make sure the UI updates dont happen too frequently



Who needs 100s frames per second?

Modeless Solution
Data layer

Encapsulates
UI to drawing data transfer  Drawing to UI data transfer


CollectionChanged

PropertyChanged

Idle event

Layer1

Layer2

Layer3

Layer4

ObjectClosed event

Layer1

Layer2

Layer3

Layer4

WPF Migration and Interoperability


How do I use this stuff in my existing application?

Yes but our project


is millions of lines of unmanaged C++, and (excuses excuses) Make it a Mixed Mode application, you can then easily call WPF! has lots of unmanaged MFC dialogs, and (excuses excuses) You can host WPF controls inside of unmanaged MFC Dialogs! needs rewriting from scratch in .NET but some of our MFC controls have many months of work invested in them and we just dont have time to rewrite them in .NET (more excuses) You can even host MFC controls inside a WPF Window!

WPF Migration and Interoperability


Lets get some details WPF in Win32  Mixed Mode DLL required (use /clr compiler setting)  WPF code should be written in C# or VB.NET


XAML wont compile under a C++ project.

WPF Controls in Win32/MFC  Mixed Mode DLL required (use /clr compiler setting)  Use the HwndSource


Wraps WPF content in a Win32 window

WPF code should be written in C# or VB.NET

Win32/MFC Controls in WPF UI  Mixed Mode DLL required (use /clr compiler setting)  Use the HwndHost


Wraps the HWND in a WPF element, which can be added to a WPF element tree.

WPF code should be written in C# or VB.NET

WPF/E WPF Everywhere


Code name SilverLight

Microsoft SilverLight
Cut down version of WPF mainly for use inside of browsers  Take your XAML and paste it directly into a SilverLight Web application  First steps towards cross platform WPF  Who knows where this is going.


WPF
Further reading...

Getting Started - WPF - Get Started Accessibility - Accessibility Best Practices Localization - WPF Globalization and Localization Overview Performance - Optimizing WPF Application Performance Security - Windows Presentation Foundation Security Interoperation - WPF and Win32 Interoperation Overview

WPF
More Further reading...

SDK and Samples


www.objectarx.com

Developer Center
www.autodesk.com/developautocad

Discussion Groups
http://discussion.autodesk.com

API Training Classes


www.autodesk.com/apitraining

Keans Through the Interface Blog


http://blogs.autodesk.com/through-the-interface

Autodesk Developer Network


www.autodesk.com/joinadn

Thank you Any questions?


Using Windows Presentation Foundation in AutoCAD
Fenton Webb
Senior Developer Evangelist

You might also like