The various add methods of the ComponentOccurrences objects let you place new parts or assemblies into an assembly, but in all cases you have to specify the position as part of the placement. There are times when you don’t know where it should be positioned and want to give the user the ability to position it. This can be done using a little known capability of the API.
The Place Component command has a special API behavior. When the command is invoked, it typically displays the dialog to allow you to choose which part or assembly to place and then puts you in a mode where that part or assembly is attached to the cursor and each click creates a new occurrence. The special behavior of the command is that it can bypass the dialog and go directly to the placement. It’s possible through the API to post the filename to a queue and when the command is executed it checks to see if there is a filename in the queue and if there is it uses it and skips the dialog.
The VBA sample below demonstrates this. It uses the PostPrivateEvent of the CommandManager object to post the filename to the private event queue. It then executes the Place Component command. Calling Execute on a control definition is exactly the same as executing the command in the user interface. In fact if you comment out the last line in the program below and run it and then run the Place Component command in the user interface it will still skip the dialog step of the command. Once the filename is read from the queue, the queue is cleared so the next time the Place Component command is run it will run in the normal way and display the file dialog.
Public Sub PlacePartInteractive()
' Define the filename to place.
Dim filename As String
filename = "C:\Temp\PartTest_2016.ipt"
' Post the filename to the private event queue.
Dim cmdMgr As CommandManager
Set cmdMgr = ThisApplication.CommandManager
Call cmdMgr.PostPrivateEvent(kFileNameEvent, filename)
' Execute the "Place Component" command.
There is a downside to using this technique. With the typical Add methods, the occurrence is created and then passed back to your program where you now have control and can continue whatever processing you need to do. By executing a standard command you've essentially turned control of Inventor back to the user. If you're finished with whatever you need to do then this works great because you're done and don't need to get back control. Otherwise it gets more complicated because you'll also need to watch events to catch when new occurrences are placed and do whatever processing might be needed. I'll save that discussion for another time.