Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Applied to Autodesk® Inventor® API

Similar presentations


Presentation on theme: "Design Patterns Applied to Autodesk® Inventor® API"— Presentation transcript:

1 Design Patterns Applied to Autodesk® Inventor® API
Philippe Leefsma Senior Developer Consultant, Autodesk Developer Network

2 Class Summary This class focuses on developing .Net based add-ins for Autodesk Inventor® using best practices gained from direct hands-on experience of Autodesk software engineers. It exposes design patterns specifically studied for the Inventor® API. The class may benefit any programmer developing applications on top of Autodesk Inventor®.

3 About the Presenter Philippe Leefsma Developer Technical Services
EMEA (Neuchatel, Switzerland) Philippe has a master's degree in Computer Sciences. He carried his studies in Paris at I.S.E.P and in USA, at Colorado School of Mines. He joined Autodesk 6 years ago where he works as developer consultant for Autodesk Developer Network. He supports various products APIs such as AutoCAD®, AutoCAD Mechanical®, and Autodesk® Inventor®. He likes to travel, meet developers from around the world to work with them around challenging programming, CAD and manufacturing topics. Here is my feel free to contact me for any feedback, suggestion or bug report about that material.

4 Learning Objectives At the end of this class, you will be able to:
Boost your add-in development with useful functionalities Strengthen stability and efficiency of your application by using well established software architectures Save time and effort by taking advantage of design patterns turned into templates We do not discuss and assume knowledge of How to program in C# or VB.NET The basics of the Inventor API Inventor product usage

5 I - Creating Custom Project and Item templates

6 Custom Add-In Template
Custom add-in template can be used to enforce design pattern use Let you save time and efforts by automating repetitive tasks Helps standardize development process across company Provides an efficient framework to developers

7 Creating Custom Templates
Creating your own custom add-in template is straightforward from Visual Studio interface: Web references dealing with more advanced functionalities: custom-wizard.aspx

8 Custom template - Parameters
Template parameters are declared in the format $parameter$ Some useful parameters used in the ADN template: destinationdirectory  GUID [1-10] itemname projectname safeitemname safeprojectname time

9 Custom Template - Parameters Example
/////////////////////////////////////////////////////////////////////////////////// // $safeitemname$ Inventor Add-in Command // // Author: $username$ // Creation date: $time$ namespace $rootnamespace$ { class $safeitemname$: AdnButtonCommandBase public $safeitemname$(Inventor.Application Application): base(Application) //... Your code goes here ... }

10 Adding Custom Steps Post-Build: AfterClean:
call "%VS90COMNTOOLS%vsvars32" mt.exe -manifest "$(ProjectDir)$projectname$.X.manifest" -outputresource:"$(TargetPath)";#2 xcopy /y "$(ProjectDir)Autodesk.$projectname$.Inventor.addin" "$(AppData)\Autodesk\Inventor Addins\" AfterClean: <Target Name="AfterClean"> <Delete Files="$(AppData)\Autodesk\Inventor Addins\Autodesk.$projectname$.Inventor.addin" /> </Target>

11 Template Installation
On Windows XP: C:\Documents and Settings\<user>\My Documents\Visual Studio <version> \Templates\ProjectTemplates\ C:\ Documents and Settings \<user>\My Documents\Visual Studio <version> \Templates\ItemTemplates\ On Windows Vista/7: C:\Users\<user>\My Documents\Visual Studio <version> Installing a custom Visual Studio template is very easy: all you need to do is copy the zip files without unzipping them in the locations mentioned above, depending whether it is an item or project template.

12 Templates in Visual Studio Express
Visual Studio Express Edition has a few limitations: Post-Build step not available > Perform manually or use .bat file AfterClean step not available New project created on TEMP folder > Call ‘Save All’, select folder and change .addin and post-build accordingly

13 Template Demo

14 II - Add-In Utilities

15 XML Ribbon Builder Common task for add-ins: create user interface elements XML-based Ribbon builder presents several advantages: Fast and easy definition of Ribbon elements Generate the Ribbon items dynamically at loading Use external XML > Can be replaced / updated by user / application Ribbon Builder

16 XML Ribbon Builder - Tags
Ribbon builder interprets following XML tags: RibbonTab RibbonPanel QAT Button Separator ComboBox Macro Gallery SplitButton SplitButtonMRU ButtonPopup Popup TogglePopup

17 Ribbon items persistence
Ribbon builder implements custom items persistence mechanism: Ribbons.xml <Ribbon> <…> <Ribbon/> Ribbon Builder Add-in loaded Ribbon Builder Add-in unloaded User.Ribbons.xml <Ribbon> <…> <Ribbon/> Add-in loaded Ribbon Builder

18 XML Ribbon Builder - Example
<?xml version="1.0" encoding="utf-8"?> <Ribbon> <RibbonTab ribbons="ZeroDoc|Part|Assembly|Drawing" internalName="id_TabTools" displayName="Tools" targetTabBefore="" targetTabAfter=""> <RibbonPanel internalName="MyCompany.MyAddin.MyPanel" displayName="My Panel" targetPanelBefore="" targetPanelAfter=""> <Button internalName="MyCompany.MyAddin.MyCommand" useLargeIcon="true" isInSlideout="false" targetControlBefore="TargetControlInternalName" targetControlAfter="" isPersistent="true"/> </RibbonPanel> </RibbonTab> </Ribbon> This example xml file will create a Ribbon Button, invoking command «MyCompany.MyAddin.MyCommand» It will be placed in a Panel named « MyPanel » inside the « Tools » Tab of the « Part » Ribbon

19 XML Ribbon Builder - Use
// Create Ribbon items based on a Stream generated // from an XML description file public void CreateRibbonFromStream(Stream stream); // Create Ribbon items based on a Stream name // typically an embedded resource xml file public void CreateRibbonFromResource( Assembly resourceAssembly, string resourcename); // Create Ribbon items based on an existing // xml file on the disk public void CreateRibbonFromFile(string xmlfile);

20 XML Ribbon Builder - Use
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime) { // Add-in Inititialization... Assembly asm = Assembly.GetExecutingAssembly(); string asmName = asm.GetName().Name; if (System.IO.File.Exists(asmName + ".ribbons.override.xml")) AdnRibbonBuilder.CreateRibbonFromFile(asmName + ".ribbons.override.xml"); } else AdnRibbonBuilder.CreateRibbonFromResource(asm, “Namespace.resources.ribbons.xml"); Creating the ControlDefinitions before invoking « AdnRibbonBuilder.CreateRibbon » method is requested, as the Ribbon builder will need to use those existing controls to create ribbon items.

21 Add-in Installer – Registry Free
Utilities can be extended also to installer class: Inventor registry-free add-in mechanism based on “.addin” copied at specific location on the machine. Add-in dll can be placed anywhere on the directory structure To handle this feature from classic installer, a custom post install step can be used Copy add-in dll file to one of following locations: a) Anywhere, then *.addin file <Assembly> setting should be updated to the full path including the dll name b) Inventor <InstallPath>\bin\ folder, then *.addin file <Assembly> setting should be the dll name only: <AddInName>.dll c) Inventor <InstallPath>\bin\XX folder, then *.addin file <Assembly> setting shoule be a relative path: XX\<AddInName>.dll Copy.addin manifest file to one of following locations: a) Inventor Version Dependent Windows XP: C:\Documents and Settings\All Users\Application Data\Autodesk\Inventor 2012\Addins\ Windows7/Vista: C:\ProgramData\Autodesk\Inventor 2012\Addins\ b) Inventor Version Independent C:\Documents and Settings\All Users\Application Data\Autodesk\Inventor Addins\ C:\ProgramData\Autodesk\Inventor Addins\ c) Per User Override C:\Documents and Settings\<user>\Application Data\Autodesk\Inventor 2012\Addins\ C:\Users\<user>\AppData\Roaming\Autodesk\Inventor 2012\Addins\

22 Add-in Installer – Registry Free
public string InstallRegistryFree(IDictionary stateSaver){ // Get addin dll location Assembly Asm = Assembly.GetExecutingAssembly(); FileInfo asmFile = new FileInfo(Asm.Location); FileInfo addinFile = null; foreach (FileInfo fileInfo in asmFile.Directory.GetFiles()) if (fileInfo.Extension.ToLower() == ".addin"){ addinFile = fileInfo; break; } XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(addinFile.FullName); XmlNode node = xmldoc.GetElementsByTagName("Assembly")[0]; node.InnerText = asmFile.FullName; xmldoc.Save(addinFilenameDest);

23 Inventor Utilities – Manager Classes
class AdnAttributeManager class AdnRefKeyManager class AdnInteractionManager class AdnClientGraphicsManager class AdnDrawingViewManager

24 III - Design Patterns

25 Add-In Command Pattern
An Inventor command is different for user and programmer User = single concept | Programmer = Multiple API objects AdnCommand design pattern gathers all API concepts in a single object: Easily instantiated and extended Helps structure your code Handles Form / InteractionEvents workflow Using design patterns also helps to get started the right way Adn Command Control Definition Form Interaction Interaction Events Icons

26 Add-In Command Pattern
abstract public class AdnCommandBase { protected AdnCommandBase(Inventor.Application Application) // Static method to add new command public static void AddCommand(AdnCommandBase command) // Implements ControlDefinition creation in derived classes abstract protected void CreateControl() // Public Command Properties public ControlDefinition ControlDefinition public Application Application public abstract string ClientId }

27 AdnButtonCommand Base
Add-In Command - Use AdnCommandBase is designed to be derived in custom command: AdnCommandBase AdnButtonCommand Base MyButtonCommand AdnCollection CommandBase MySplitCommand MyPopUpCommand

28 Dockable Window Pattern
Provides an easy to use pattern to handle custom Dockable Windows in Inventor Use: create a UserControl and place it inside the Dockable Window Form A static method allows to create or refresh display for the dockable window protected override void OnExecute(NameValueMap context) { MyDockableWnd.MakeVisible( _addInSiteObject, DockingStateEnum.kDockLeft); }

29 ClientFeature Factory Pattern
public abstract class AdnClientFeatureFactoryBase { public abstract string ClientFeatureName; public abstract Bitmap ClientFeatureIcon; public abstract bool IsCustomSolved; public abstract bool HighlightClientGraphicsWithFeature; protected string ClientFeatureId; public abstract bool Edit(ClientFeature feature); public abstract void ExitEdit(); protected ClientFeature CreateClientFeature( Document document, object startElement, object endElement, object inputs) public event OnFeatureDoubleClickEvent; public event OnClientFeatureSolveEvent; }

30 Wrap up Custom Template Add-in Utilities Design Patterns
Project and item templates are powerful time and effort saver Add-in Utilities Using custom tools on top of API can be very advantageous and lighten coding process Design Patterns Rely on design patterns helps structure code and enforce good practices

31 Material Presentation Template Samples Add-In Samples
CP Design Patterns Applied to the Autodesk® Inventor® API.pptx CP Design Patterns Applied to the Autodesk® Inventor® API.docx Template Samples Inventor AddIn Template - C# | VB.Net AdnButtonCommand - C# | VB.Net AdnCollectionCommand - C# | VB.Net AdnClientFeatureFactory - C# | VB.Net DockableWindow - C# Add-In Samples ClientGraphics, DogBone, FxDimensions, Material Profiler, Thread Modeler

32 Resources for Inventor developers
Online Help, Developer's Guide and SDK Samples Inventor Developer Center Webcasts and Trainings on Inventor Programming and News Discussion Group > Inventor API Training Classes ADN, The Autodesk Developer Network Manufacturing DevBlog

33 Autodesk Developer Network
Access to almost all Autodesk software and SDK’s Includes early access to beta software Members-only website with thousands of technical articles Unlimited technical support Product direction through conferences Marketing benefits Exposure on autodesk.com Promotional opportunities Up to three free API training classes Based on user level Some of you may be unfamiliar with ADN. The Autodesk Developer Network is a program providing professional support to programmers writing add-in applications for Autodesk software. If you think the program benefits listed here would be useful to you, then visit this URL and read more about it. You don’t have to be a commercial software developer to join ADN.

34


Download ppt "Design Patterns Applied to Autodesk® Inventor® API"

Similar presentations


Ads by Google