About the Authors

July 2015

April 2012

April 27, 2012

This is a continuation of the getting started tutorial that’s discussing the creation of a new command to draw a slot. In this installment I discuss drawing geometry in a sketch.

When creating sketch geometry using the API, there are a couple of concepts that are important to understand that aren’t very obvious when you create sketches interactively. Let’s look at what you really have when you draw two lines in a sketch. You can see from the picture below it just looks like two lines and nothing else.

If you know what to look for, you’ll also notice something else. If you move the mouse over the end of the lines, a point is displayed, as shown below. At first you might think this is just a handle being displayed to indicate that you can drag the end of the line. It’s not a handle, it’s a real sketch point that’s being displayed.

In fact, to better see these are real points you can select a point and using the context menu, change the display style of the point to a center point. With this display style it remains visible all the time and not just when the cursor is over it. The picture below shows all three of the points being displayed with a center point style.

The thing to learn from this is that all sketch geometry is always associated to sketch points. Sketch geometry doesn’t exist on its own. The position of sketch geometry is controlled by the position of the points. It’s like the geometry is fastened to the points and if the points move the geometry attached to them also moves.

Another very important concept is that the two lines are connected by the fact that they share the same point. Both lines are attached to the same point. That’s how Inventor determines connected curves in a profile. We’ll see an example of this later in this post.

Below is a picture of the two lines after placing a fillet. The geometry consists of two lines and an arc. The position of each of the lines is still defined by two sketch points and the arc shares the sketch points where it connects to the lines. In addition, the arc has an additional sketch point that defines its center.

When you interactively draw a sketch, Inventor automatically creates these points behind the scenes. It also automatically deletes them when the associated geometry is deleted. When using the API it’s possible to have it automatically create these points too, but without understanding these concepts of how geometry is connected to points and each other by sharing points it’s easy to get results you weren’t expecting. When interactively drawing a sketch, it’s natural to get the correct result because Inventor infers connections based on where you click the mouse. When using the API, none of this inference can take place because you’re not moving the mouse over the geometry.

The code below creates a sketch line on an existing sketch. You can paste this into the bottom of the sub from the previous post in this series.

' Get a reference to the TransientGeometry object. Dim tg As Inventor.TransientGeometry = invApp.TransientGeometry

' Get a reference to the SketchLines collection. Dim lines As Inventor.SketchLines = sketch.SketchLines

The first thing this does is get a reference to the TransientGeometry object. This is a utility object in the API that lets you create various types of geometry. However, the geometry created is not ever displayed or saved by Inventor, which is why they are called transient. These objects are simply the mathematical definition of the geometry and are not visible entities. I’m using it in this case to create two coordinate points to define the end points of the line. The CreatePoint2d method creates a transient 2d point. The Point2d object is a simple API object that wraps the x-y coordinates of a point in 2d space. In Inventor’s API, instead of passing two values to define x-y coordinates a Point2d object is used instead. There’s also a Point object which wraps x-y-z coordinates for a 3d point. When working with a sketch you want to make sure you’re using the CreatePoint2d method and not the CreatePoint method. It’s a common mistake.

The next thing the program above does is get the SketchLines collection. In a previous post I briefly discussed collection objects. The SketchLines object lets you access all of the lines on the sketch and it supports add methods to let you create new lines. This examples uses the AddByTwoPoints method to create a new sketch line.

The final section of the code above creates the new line. A variable is declared with the type of SketchLine and the newly created line is assigned to the variable. The AddByTwoPoints method takes to points as input. These can either be coordinate points (defined by a Point2d object as in the example above) or they can be existing SketchPoint objects, which you’ll see demonstrated below.

Another thing that’s important to understand are the units that Inventor uses. Internally, Inventor is consistent in the units it uses. Lengths are always defined in centimeters and angles are always defined in radians. It doesn’t matter what units the user has specified as their document units. The API is consistent with the Inventor internal units. When you create or query geometry, any length or coordinate values will always be in centimeters and any angles will always be in radians. The example above creates a 4 centimeter long line starting from the origin and going in the sketch X direction. We’ll see in a later post how to deal with user defined units.

Below is some code that creates a second and third line to create a triangle.

The resulting geometry is shown below on the left. If I try to create an extrusion using this sketch I can’t select the triangle, but only the individual lines. That’s because each line has it’s own unique pair of sketch points for its end points, instead of sharing a single point between two lines. This becomes obvious if I drag any of the end points, the triangle pulls apart, as shown in the picture on the right.

The code below is a variation of the previous code but creates a set of connected lines. The difference is that instead of defining new coordinates for the end of each line it uses the EndSketchPoint and StartSketchPoint properties of the SketchLine object to get the sketch points from the existing lines.

' Draw a second line from the end of the previous line to (4,3).Dim line2 As Inventor.SketchLine line2 = lines.AddByTwoPoints(line1.EndSketchPoint, _ tg.CreatePoint2d(4, 3))

' Draw a line connecting the last point to the first point. Dim line3 As Inventor.SketchLine line3 = lines.AddByTwoPoints(line2.EndSketchPoint, _ line1.StartSketchPoint)

The approach to creating sketch geometry that I’ve had the best personal success with is to create the sketch points first and then use those as input when creating the various sketch entities. It’s easier for me to visualize the points I need if I map it out before hand and having the map helps to eliminate mistakes in my code. For example, below is an example of what I sketched up to help me visualize the points that will be needed to create the slot. Remember that arcs and circles require a sketch point at their center. I’ve numbered the points starting with zero. Those numbers will correspond to indices in an array in my program.

Below is the code that demonstrates this approach. It begins by creating the sketch points and then it uses those points to create the sketch geometry. It becomes a dot-to-dot exercise to draw the geometry. This examples illustrates lines and arcs, but all of sketch geometry uses the same concepts. You’ll just be using different collection objects and different add methods within those collections.

' Get a reference to the SketchPoints collection.Dim points As Inventor.SketchPoints = sketch.SketchPoints

' Get a reference to the SketchLines collection. Dim lines As Inventor.SketchLines = sketch.SketchLines

' Get a reference to the SketchArcs collection.Dim arcs As Inventor.SketchArcs = sketch.SketchArcs

The result after running the program is shown below, which is what’s expected. Because the entities share sketch points at the connections, Inventor sees this as being closed so it can be used as input to create an extrusion.

However, if you try and edit the sketch by dragging the points it may not behave as you expect, as shown in the picture below. This is because there aren’t any constraints to define the behavior. Interactively, constraints are inferred during the sketching process by how you move your mouse, which can’t happen when using the API. I’ll discuss adding constraints in an upcoming post.

Note: One issue that I just discovered while writing this post is that the AddByCenterStartEndPoint method is not associating the input center sketch point with the new arc, but is instead creating a new point at that position. This is not expected behavior and I’ll discuss how you can work around this issue in a later post. Just know that when you see the extra center points that you didn’t do anything wrong.

April 25, 2012

Autodesk supports several Developer Center web pages that contain general information about programming the various Autodesk products that support an API. With each major release these sites are updated, and occasionally there are updates in between releases. The update to Inventor’s Developer Center site for Inventor 2013 is now available. See http://www.autodesk.com/developinventor. You’ll also see links on the left-hand side of the page to the pages of the other Autodesk products. These sites aren’t meant to have all the information you’ll ever need but are intended to help get you started programming a product by providing a general overview of the technology and links to existing resources.

April 20, 2012

In this post, on our slow journey towards creating a command that creates a slot shape in a sketch, I’m going to discuss the main principles when working with sketches.

You can create a sketch in a part or assembly but I’m only going to address parts for now. A sketch is basically a 2D coordinate system positioned in 3D space. The API has a special type of object for this type of sketch called a PlanarSketch since it’s defining a plane in 3D space. There is also a DrawingSketch which is slightly different since a drawing is already 2D. Typically, sketches are created by referencing an existing planar entity, either a planar face or a work plane. The sketch is associatively related to the entity so that if the entity moves the sketch moves with it.

When you pick a face or work plane, Inventor has some logic that it uses to determine the orientation and origin of the 2D coordinate system that’s defined during the creation of the sketch. You typically don’t care about this orientation because the default is usually what you expect and the main thing it affects is the horizontal and vertical directions. The origin doesn’t matter since you dimension the sketch geometry relative to other geometry in the model so the actual x-y coordinates of the sketch geometry is immaterial. The picture below shows a selected face on a part where I want to create a sketch.

After creating the sketch it is possible to see and even edit the coordinate system that was defined for this sketch. You can do this using the “Edit Coordinate System” command that’s accessed through the context menu for the sketch. A 2D triad is displayed indicating the origin and the direction of the x and y axes. While in this command you can edit these to reorient the sketch on the plane.

The yellow lines you see in the picture above are sketch lines that were automatically created by projecting the edges of the selected face onto the sketch. This is the default behavior of Inventor but is controlled using the setting in the Application Options dialog as shown below.

To create a sketch using the API you need to use the collection object for sketches. Remember from the discussion in the previous post that collection objects provide access to existing objects of a certain type and also support methods to create new objects of that type. Below is a simplified view of the object hierarchy to get to the PlanarSketches object. We’ve already discussed how to create a PartDocument object but most of the data that we think of as part specific data, (features, work features, parameters, imates, etc.) are part of the PartComponentDefinition object. There is always a one-to-one relationship between a PartDocument and a PartComponentDefinition object. Don’t get too caught up in why there is a PartComponentDefinition object but instead just remember that for part information you’ll need to take that one extra step to get the PartComponentDefinition from the PartDocument.

Below is some code that creates a new part document and creates a new sketch on the X-Y work plane of the part.

The code above uses the ComponentDefinition property of the PartDocument object to get the PartComponentDefinition object associated with the part. In most cases the name of the property matches the name of the object is returns but that’s not always the case as in this example.

Next it gets the X-Y work plane from the part. It does this by getting the WorkPlanes collection object from the PartComponentDefinition and gets a work plane from it using the Item property. The WorkPlanes collection object contains all of the work planes in a part and also supports several Add methods to create new work planes. All parts will always have at least three work planes, three work axes, and one work point. Below is a picture of the browser for a new, empty part. You can see the base work features that exist in every part. When accessing these through the API they are returned by the API collection objects in the same order they’re shown in the browser. For example, getting WorkPlanes.Item(1) will return the YZ Plane. Getting WorkAxes.item(2) will return the Y axis. and getting WorkPoints.Item(1) will always return the origin work point. The user cannot delete or reorder these base work features so this is guaranteed to always be true. In this case I’m getting item 3 from the WorkPlanes collection, which is the XY Plane.

The last section of code, shown below, creates a new sketch. There are a couple of things that I want to point out here. There are several things happening on the line where the sketch gets created. First, the Sketches property of the PartComponentDefinition is called. This returns a PlanarSketches object. Next, the Add method of the PlanarSketches object is called. This line could have been broken up into multiple lines by first getting the PlanarSketches object and then calling the Add method. Most languages support the ability to combine these calls into a single line to allow for less code. Just be aware that there are several calls being made on that single line, which can sometimes make problems a little more difficult to debug because if that line fails it’s difficult to know which call failed.

The Add method to create a new sketch is fairly simple. It has two arguments. The first is the planar object you want to create the sketch on. This can be a work plane, like in this example, or it can be a planar face. The second argument indicates if the edges of the face should be copied into the sketch. This is the equivalent of the “Auto Project” option discussed above. The API ignores the application setting for the auto project and allows you to control it explicitly when you create a sketch. This setting is only used when you’re creating a sketch on a face and is ignored when a work plane is used. In fact the second argument is defined to be an optional argument so I could have left it out. It defaults to False if you don’t specify it.

The work plane created is the same as if you had interactively created it in Inventor, in that the origin and orientation of the plane is automatically inferred by Inventor. The PlanarSketches collection also supports an AddWithOrientation method that lets you specify the origin and orientation when you create it. In practice this is rarely used.

If you run the code above it will create a new part and you’ll see a new empty sketch in the part. Each time you run the program you get a new part with a sketch. Not too exciting yet, but in the next post we’ll look at drawing geometry on the sketch. Then it gets a lot more fun .

April 16, 2012

Here’s another post in the getting series where we’re looking at the creation of a new sketch command to create slot shapes. I’ll be adding each of the posts in this series to the Getting Started Tutorial category.

This post is more discussion than coding so you will have some background to follow what’s coming next. First, I want to go back to the previous program and look at that code in a bit more detail. The code below is from the previous post with the error handling removed and one of the lines has been broken into two to make it easier to follow what’s happening

Line 2 declares a variable to be of type Inventor.Application. The “Inventor” portion of the declaration is specifying the library and the “Application” portion is the name of the object. The Inventor API is exposed as objects. You’re already used to this concept since that’s also how Visual Basic works. For example, when you place a button control on a form, that results in the creation of a button object that you can access in your program. The button has properties that you can read and write to manipulate the button. It also has methods you can call and it has events that notify you when certain things happen to the button, like when it is clicked.

Inventor objects have the same concept but instead of representing something on a form, they represent something within Inventor. For example, for every extrude feature in a part there is an ExtrudeFeature object that you can access and use in a program. Inventor is a large and fairly complicated application and the API reflects that. Inventor 2013 has 1365 objects which have a total of 20,462 properties, methods, and events. Don’t let these big numbers discourage you though because you don’t need to understand all of it be able to use it.

These objects are accessed through something known as the object model or object hierarchy. A useful tool in working with the API is the object model chart, which is shown below. You can download a printable pdf version of the chart here.

Below is a snapshot of the upper-left corner of the chart that includes the few objects that are used in the small program discussed here. You can see in the chart that the top-level object is the Application object. This object represents the Inventor application and has functionality that lets you manipulate the entire application; things like resizing the main window and accessing application options. However, the most important thing the Application object does is support properties that return other objects. This is how you gain access to the other objects in the hierarchy. For example, the Application object supports the Documents property which returns the Documents object. This is illustrated in line 7 in the code sample above and that relationship can be seen in the chart below.

You can also view all of the objects and their associated methods, properties, and events from within Visual Basic using the Object Browser. To access the Object Browser in Visual Basic Express 2010, press F2. The Object Browser is shown below, with the Inventor library selected.

Below is the Object Browser with the Documents object selected in the list on the left and the Item property selected on the right. The Object Browser provides a convenient way to see the objects and what functionality they support. You can see in the picture below that the Item property is read-only and has a single argument called Index which is an Integer. You can also see that it returns an Inventor.Document object. (You can ignore the _ character in front of Document. It has something to do with how the API was implemented internally and I don’t understand it either. )

Tip: You can expose additional capabilities in the Visual Basic Express 2010 interface by changing your settings to “Expert Settings” in the “Tools” menu, as shown below. After doing this the “Object Browser” command will be displayed in the “View” menu.

Line 6, in the sample above, declares a variable to be of type Documents. The Documents object is a special type of object known as a collection object. Collection objects are used extensively throughout the API and are an important concept to understand.

Collection objects are utility objects used in the API. They don’t represent specific objects within Inventor but instead provide access to a group of related objects and in many cases also provide the ability to create new objects. The Documents object illustrates both of these.

The Documents object provides access to all of the documents that are currently open in Inventor. All collection objects support the properties Count and Item. The Count property tells you how many items are in the collection and the Item property lets you access the items within the collection. If you add the following to the end of the previous program it will display a message box telling you how many documents are currently open.

You use the Item property of a collection to get a specific object from the collection. This is somewhat similar to an array but the first item has in index of 1. Some collections will also let you specify the name of the object you want. The code below uses both the Count and the Item properties of the Documents object to iterate over all of the open documents and display the name of each one in the output window. (You may need to turn on the display of the output window from the menu “View –> Other Windows –> Output” or “Ctrl + Alt + O”.)

' Iterate over the open documentsFor i As Integer = 1 To docs.Count Debug.Print("Doc Name: " & docs.Item(i).DisplayName) Next

If you’re iterating through a collection a more efficient technique for is to use the For Next statement. Code using a For Each vs. the For To will typically run faster and is easier to read. This is demonstrated below.

' Iterate over the open documents For Each doc As Inventor.Document In docs Debug.Print("Doc: " & doc.DisplayName) Next

The Item property of the Documents collection does not support indexing by name. You can determine which Item properties do support this by looking at the description of the property in the API help. The Documents object supports a separate property for named lookup called ItemByName where instead of providing the index number you supply the filename of the document. If you specify an out of range number or a name that doesn’t exist the call will fail.

Adding objects to a collection is done by using the Add methods of the collection. This was demonstrated in the original program above in line 11 where the Add method is called. Some collections won’t have any add methods and others will have several. The arguments to the add methods will also vary greatly depending on what is being created. It’s takes much different information to create a new document versus creating a new extrude feature.

We’ll look at practical applications of collection objects and using them to create new objects in the upcoming posts. In the next post I’ll talk about creating sketches.

April 11, 2012

DevCamps are held every other year where software developers like you can learn to get the most from your time and effort working with Autodesk platform technologies. The two industry specific Developer Camps include several tracks catering to the learning needs of beginner and expert software developers – as well as a business track for start-ups looking to develop and leverage their relationship with Autodesk. Come to Camp and get two days of face to face learning and help from Autodesk software engineers – the same Autodesk software engineers developing and supporting the Autodesk technology you work with every day.

You can also join us for a third day at “DevLab” where you can work on your hardest coding challenges with an Autodesk software engineer looking over your shoulder giving you immediate help and advice. Bring your laptop and work on your code as you’d normally do in your own office – the difference is that you have a team of DevTech experts (the same people who answer your questions through DevHelp Online) available to review your code if you’d like, answer your questions, and make suggestions.

Cloud and Mobile Classes

New for this year’s DevCamps are several classes on helping you get started developing apps based on Cloud and Mobile technologies. Learn how to build your first Cloud app integrated with your favorite Autodesk product – and how you can make it available “everywhere” through browser and mobile user interfaces. Have you been considering building your first app for the iPhone, iPad or Android device but holding back because of fear of lost hours thrashing while learning the new technology? DevCamp includes classes that will show you how to quickly and easily create your first “Hello World” app for iOS and Android - with a design and graphics twist.

Class schedule, June 13-14 (in Portland Oregon)

You can attend beginner and expert classes on developing solutions with Autodesk® Inventor® (including Inventor ETO), Autodesk® Vault, Autodesk® Navisworks, Autodesk PLM 360, and several more Autodesk manufacturing technologies. Hear from Autodesk’s Manufacturing Leadership including Executive Vice President Robert “Buzz” Kross. Learn about new technologies Autodesk is working on from the head of our “Fusion” Product Management Team Kevin Schneider.

April 08, 2012

If you’ve gone through the lessons in the book I suggested earlier, or any of the online Microsoft tutorials that are available you should be familiar with what projects are and how to create them. I won’t go into a lot of detail but will go through the steps below.

Here are the steps to creating an exe program that will access and control Inventor using the API.

In the New Project dialog select “Windows Forms Application” as the type of project and enter “DrawShape” for the project name.

The project you’ve just created is a standard Windows application and doesn’t have any knowledge about Inventor’s API. In order to access Inventor’s API you need to make your project aware of the API and what it supports. You do this by referencing Inventor’s Interop library. The interop completely describes the API in a .Net friendly way.

To add the reference choose the “Add Reference…” command from the Project menu, as shown below.

In the Add Reference dialog choose the Browse tab and browse to the directory below (for whatever version of Inventor you’re using).

C:\Program Files\Autodesk\Inventor 2013\bin\Public Assemblies

Select “Autodesk.Inventor.Interop.dll”, as shown below and click OK.

You can view the references a project has by using the Project window and selecting the “Show All Files” button highlighted in red below. All of the references will be displayed in the “References” folder.

I’ll talk more about this in my next post, but just so we end this post with your program doing something somewhat useful, add a button to your form, (I called mine btnRun), and add the code below into the Click event of the button. (Sorry about the weird line continuations but the width of a line in this blog interface is very limited.)

Now when you run your application and click the button the program will connect to Inventor if it’s running or display a warning message if it’s not. If it is running it will create a new part document. Each time you click the button a new part will be created. In the next post we’ll look at this code in more detail and discuss the basics of working with documents.

April 04, 2012

Before actually beginning to write any code I wanted to spend some time discussing what language to use when programming Inventor. I wrote a post on this a while ago that still applies but my opinion has changed a bit since then. In that post I recommended that you begin with Inventor’s VBA. VBA still does have a few advantages but I think the advantages some of the other languages have now outweighs the advantages VBA has.

If you’re a fairly new programmer, I recommend Visual Basic. The latest version is Visual Basic 2010. It comes as one of several languages in Visual Studio 2010. The Professional version of Visual Studio, which is the least expensive full version, is approx. $500 and it probably has everything you’ll ever need. You can also download Visual Basic 2010 Express from Microsoft, which is a trimmed down version and is free. This is the version that’s used for the book I recommended in my previous post. There are a couple of things missing from this version that you’ll wish you had when you begin some more advanced Inventor development but we’ll look at some workarounds later when we get to those topics. For now the free Express edition has more than enough capabilities to begin learning Inventor’s API and is a great way to get started.

If you’re an old C++ programmer then you’ll likely prefer C# over Visual Basic. I believe Visual Basic is easier to use for a new programmer but C# is very similar so it really is a personal preference rather than a question of which one is better. You can also use Visual C++, but it is much harder, especially for a new programmer, to use than either Visual Basic or C#. For this series of posts on getting started with the API, I’ll stick primarily with Visual Basic, but will also do some coverage of C# when there are differences. I’m not going to spend any time on C++ but plan to write a separate post covering using C++ with Inventor.