Hi,
I'm new to the API and coding in Inventor and just did the My First Plugin tutorial. What I'm trying to do is take a completely unconstrained rectangle in a sketch, constrain it (horizontal + 2x perpendicular), dimension it, add a diagonal construction line, and add a coincident to place the construction line's midpoint to the origin. The sketch is not created in the API. Its name also varies. I'm having trouble just accessing the lines through the API, and keep getting an error when I do Debug.Print to check if I'm accessing things correctly. Using the the VBA editor, I figured out the tree should be something like ThisApplication.ActiveDocument.ComponentDefinition.Sketches.Item 1.SketchLines.Item 1. Of course there are spaces in there so I know I'm doing something wrong. What am I missing?
Thanks
Solved! Go to Solution.
Solved by ekinsb. Go to Solution.
You're understanding the basic concept of the object model hierarchy and how to access a specific object but now just need to get the details of how to use it.
Here's what you've got so far:
ThisApplication.ActiveDocument.ComponentDefinition.Sketches.Item 1.SketchLines.Item 1
It's important to understand what this long statement is actually doing and it's useful to break it up, both to make your code easier to understand, but also to debug and it will help make your program run faster.
ThisApplication is a global property available in Inventor's VBA that returns the Application object. This is the top-level object in the hieararchy. The Application object supports many methods an properties. One of these properties is the ActiveDocument property which returns a Document object. This means that the portion of the statement shown below is getting a Document object. There are several types of documents in Inventor, (PartDocument, AssemblyDocument, DrawingDocument, etc.) and you'll actually get one of these specific types depending on which type is currently active.
ThisApplication.ActiveDocument
The PartDocument object supports a property called ComponentDefinition which returns a PartComponentDefinition object. Hopefully you can see now that each part of the long statement is calling a property on an object that was obtained from the previous call. An alternative to the code above is the code below which also iterates through all of the lines in the sketch and prints the length of each one.
Dim partDoc as PartDocument
Set partDoc = ThisApplcation.ActiveDocument
Dim partDef as PartComponentDefinition
Set partDef = partDoc.ComponentDefinition
' Get the first sketch in the part.
Dim sketch As PlanarSketch
Set sketch = partDef.Sketches.Item(1)
Dim lines As SketchLines
Set lines = sketch.SketchLines
' Iterate over all of the lines in the sketch.
Dim line As SketchLine
For Each line in lines
Debug.Print "Line length: " & line.Length
Next
The Sketches and SketchLines objects are collection objects which provide access to a set of similar objects. You can use the Item method like I do below to get a specific sketch. In this case it's getting the first one. In many cases you can also use a string to identify an item by name. For example I could use
Set sketch = partDef.Sketches.Item("MySketch")
To get the sketch named "MySketch", if it exists. You can also use the For Each statement to iterate over the entire contents of a collection which is what's demonstrated in the code above as it iterates through all of the lines in the sketch.
Hopefully this helps.