Quite often I am faced with a challenge to control something new with code. If you read my iLogic: Where do I go from here blog post, then you at least know a little about using the Help file to look up methods and properties for objects.
In that post I had you search for table in the Programming/API Help and then choose CustomTable Object from the search results. I never gave you any additional help on how to choose that search term. In the case of the General Table it was pretty straightforward. But what if it's not?
The method shown in this post will help you understand what the API calls objects, which in turn will assist you in searching for it.
First, create the object in Inventor using the standard manual process. In this example, I'll be looking at the General Table again so I've created a blank General Table in a drawing.
Next, fire up the ol' VBA Editor, create a new procedure, and enter the code below:
Public Sub TestObj()
Dim obj As Object
Set obj = ThisApplication.ActiveDocument.SelectSet(1)
Now we have two different ways we can see the type of the object. We can either set a breakpoint at the Set obj line and look at the Locals window or we can set a Watch on the obj expression.
I suggest turning on your Debug toolbar if you don't have it on already.
- Run Sub
- Toggle Breakpoint
- Locals Window
- Watch Window
To set a breakpoint:
Ensure your Locals window is visible in the VBA Editor. If it is not visible use the Locals Window button on the Debug toolbar to turn it on.
Place your cursor anywhere in the End Sub line and select the Toggle Breakpoint tool on the Debug toolbar. You'll see a red dot in the left column of the Editor window. You can also click this area to toggle a breakpoint. Once the rule gets to this line, it will break and allow you to control when and how it moves forward. We are setting it here on the End Sub so that the obj object will already be set.
To set a watch:
Ensure your Watch window is visible in the VBA Editor. If it is not visible use the Watch Window button on the Debug toolbar to turn it on.
Select the obj text in the procedure. Right mouse click and choose Add Watch... from the contextual menu. I'd suggest changing the Watch Type to Break When Value Changes. This will break the code when the value for obj changes.
Back in Inventor, select the entity that you created (the General Table in my case) and then run the TestObj procedure by clicking on the Run Sub button on the Debug toolbar.
If you've set a breakpoint, then the rule will break at the End Sub line. Take a look at the Locals window, find the obj object, and look in the Type column. In my case where I selected the General Table, I see Object/CustomTable.
If you've set a watch, you'll need to look at the Watches window. The expression that you are watching should show up along with it's Value and Type. In my case where I selected the General Table, I see Object/CustomTable.
Now I know the API considers this a CustomTable so I can go to my Programming/API Help, search for CustomTable, and read all about it. Be sure to stop your code by clicking the Reset button on the Debug toolbar!
In addition, you can also use this method to determine if something can be controlled by code or if it is supported by the API. If the Type is just Object or Empty or if you see Nothing or <Out of context> in the Value column, that means that type of object is not supported by the API at this time.
Thanks and Happy Coding!
About the AuthorFollow on Linkedin More Content by Randall Mabery