Basic concepts of the Inventor API Presenter Developer

Basic concepts of the Inventor API Presenter Developer Technical Services Autodesk 2012 1

Agenda Ø API COM model Ø How do I access the API? Ø The Object model Ø Object model tools: Object browser, VBA debugger Ø Collection, Enumerator, Inheritance Ø The Application Object Ø How to access the Application Object? Autodesk 2012 2

COM API Model Visual Basic C++ Other. . Net C# VB. Net Inventor COM Automation API Inventor Autodesk 2012 3 Other. . . Inventor Type Library

How do I access the API? Ø VBA Ø Add. In Dll or Exe Ø Standalone Exe Ø Apprentice Autodesk 2012 4

Where is the SDK? • SDK (Software Development Kits) • Contains C++ header files, samples of C++, VB. NET, C# • Provided with product • Location of the SDK Windows XP: <Inventor install folder>SDK Windows Vista: § C: UsersPublicDocumentsAutodeskInventor <version>SDK Windows 7: C: UsersPublicDocumentsAutodeskInventor <version>SDK § • API help reference § Before 2013: <Inventor *>Help_Liteadmapi_*_0. chm § 2013: <Inventor *>Local Helpadmapi_*_0. chm § Where * is the version number Autodesk 2012 5

API Objects and the Object Model • In a COM Automation API the functionality is exposed as objects, where each object corresponds to something within the application. • Each object supports various methods, properties, and possibly events. • The objects are accessed through the object model. • The top most object is the Application object. Autodesk 2012 6

Basics of Object Oriented Programming § API is exposed as a set of objects. § Object oriented terminology: § Object – Represents a logical object. The finished chair in this example. § Property – The various attributes of the chair. The Style, Color, and Size are properties of the chair. § Method – An action performed on the chair; move, fold, throw away. § Event – Notification when something happens to the chair. § Class – The template of an object. The order form for a chair. Autodesk 2012 7

Inventor Object Model Example § Inventor’s objects are accessed through the Object Model. Public Sub Get. Extrude. Feature() Dim o. Part. Doc As Part. Document o. Part. Doc = This. Application. Active. Document Dim o. Extrude As Extrude. Feature o. Extrude = o. Part. Doc. Component. Definition. Features. Extrude. Features( "Extrusion 1") Msg. Box("Extrusion " & o. Extrude. Name & " is suppressed: " & o. Extrude. Suppressed) End Sub Autodesk 2012 8

Object Model Tools –Object Browser • Provides user-interface to the contents of the type library • Accessed in VBA using F 2, the Object Browser command in the View menu, or pressing the toolbar button. • Accessed in. NET using Ctrl + W, J, or the Object Browser command in the View menu. Autodesk 2012 9

Object Model Tools - VBA Debugger • Provides a “live” view of the object model. • Shows the values of an object properties. • Shows the contents of collections. • VBA debugger provides more information that. Net debugger. Autodesk 2012 10

Object Model Tools -. NET Debugger • Provides a “live” view of the object model. • Shows the values of an object properties. • Shows the contents of collections. Autodesk 2012 11

Collection Objects § Special § Count § Item object that provides access to a list of objects. property returns number of objects in the collection. property returns a specific object in the collection. § Can specify the index of the object within the collection. The first item is index 1 for all collections within Inventor API. § In some cases you can specify the name of the object within the collection. Autodesk 2012 12

Collection vs. Enumerator Objects • Some collections support the functionality to create new objects. • Enumerators are also collections but only support the Count and Item properties. Autodesk 2012 13

Iterating Through a Collection • Iterating using Count and Item: Dim o. Extrude As Extrude. Feature Dim i As Long For i = 1 To o. Extrude. Features. Count Debug. Print(o. Extrude. Features. Item(i). Name) Next • Iterating using For Each statement (more efficient): Dim o. Extrude As Extrude. Feature For Each o. Extrude In o. Extrude. Features Debug. Print(o. Extrude. Name) Next Autodesk 2012 14

Derived Objects • Similar to animal taxonomy or classification. § All items under a specific classification share common traits. Autodesk 2012 15

Derived Objects – Example Public Sub Save. Doc() 'Get Active. Document, could be Part, Assembly or Drawing Dim o. Doc As Document o. Doc = This. Application. Active. Document 'Call the Save method on the generic 'Document' object o. Doc. Save() End Sub Autodesk 2012 16

The Application Object § Represents the Inventor Application. § Provides access to the other API objects. § Supports general functionality not specific to a document. § Provides overall event notifications. Autodesk 2012 17

Application Window • The Application object provides access to the main window through various methods and properties. Caption § Left, Top, Width, Height, Get. App. Frame. Extents, Move § Window. State § Visible § Main. Frame. Hwnd § Autodesk 2012 18

Utility Objects • Software. Version – Provides information about the version of Inventor. • Change. Manager, Command. Manager, File. Manager, Help. Manager, Transaction. Manager, Measure. Tools, User. Interface. Manager – Provide access to functions related to a particular area. • Transient. Geometry – Temporary geometry objects. • Transient. Objects – Temporary utility objects • Transient. Brep – Temporary Boundary Representation objects Autodesk 2012 19

Shortcuts to “Active” Objects • Properties that provide direct access to various “Active” objects. § Active. Color. Scheme § Active. Document § Active. View § Active. Environment § Active. Edit. Object – Returns the object currently being edited. This can be a document that’s been opened, a document that’s been inplace edited, a sketch, a sheet, and a flat pattern. § Active. Edit. Document – Returns the document currently be edited. Autodesk 2012 20

Application Options • Provides access to objects that expose the various application options. Assembly. Options § Display. Options § File. Options § General. Options § Hardware. Options § Notebook. Options § Part. Options § Save. Options § Sketch 3 DOptions § Sketch. Options § i. Feature. Options § Color. Schemes § Autodesk 2012 21

Application Level Objects • Documents – All currently open documents, including those that are referenced by other documents. • Application. Add. Ins – The available Add-Ins. • VBAProjects – VBA related objects. Autodesk 2012 22

Events • Provides access to several different sets of events: § Application. Events § Assembly. Events § File. Access. Events § File. UIEvents § Modeling. Events § Representation. Events § Sketch. Events § Style. Events Autodesk 2012 23

Miscellaneous • Silent. Operation – Causes all dialogs to be suppressed and take the default behavior. Useful for batch processing operations where warning dialogs would normally be displayed as files are opened, processed, and closed. • Language. Name, Locale – Language information. • MRUEnabled, MRUDisplay – Controls display of most recently used file list at the bottom of the File menu. • Ready – Indicates if Inventor is fully initialized. • Status. Bar. Text – Text shown in the status bar. Autodesk 2012 24

How to access the Application Object? – VB. NET § Access in Inventor’s VBA using the This. Application property. § From an Add. In: see Module 10 § Access from outside Inventor using either the Get. Object or Create. Object methods: Dim _Inv. Application As Inventor. Application = Nothing Try ' Try to get an active instance of Inventor _Inv. Application = System. Runtime. Interop. Services. Marshal. Get. Active. Object( "Inventor. Application") Catch ex As Exception End Try If _Inv. Application Is Nothing Then ' If not active, create a new Inventor session Dim inventor. App. Type As Type = System. Type. Get. Type. From. Prog. ID("Inventor. Application") _Inv. Application = System. Activator. Create. Instance(inventor. App. Type) 'Must be set visible explicitly _Inv. Application. Visible = True End If End Try Autodesk 2012 25

How to access the Application Object? – C# § Similar to VB. NET, access from outside Inventor using either the Get. Object or Create. Object methods: string prog. Id = "Inventor. Application"; Type inventor. Application. Type = Type. Get. Type. From. Prog. ID(prog. Id); Inventor. Application instance; try { instance = (Inventor. Application)Marshal. Get. Active. Object(prog. Id); instance. Visible = true; return instance; } catch (COMException) { // No running instance, so proceed with creating a new instance } instance = (Inventor. Application)Activator. Create. Instance(inventor. Application. Type); instance. Visible = true; Autodesk 2012 26

Lab: Access the Application Object • Create an external Exe that tries to access the Inventor Application object at startup. If a running instance of Inventor is not found, then create a new instance using “Create. Instance” • Create some controls in order to: - Ask user for Height and Width values and set the Active. View to the entered values. - Ask user for the Application caption and set it. Autodesk 2012 27

Autodesk 2012 28
- Slides: 28