By Barbara Han
Issue
Is it possible to change the default filename before poping up the dialog of the standard Create Component" command? I want to give all new files a unique filename.
Solution
The "CommandManager.PostPrivateEvent" method enable you send the desired file name to the "Create Component" dialog, as demonstrated by below sample code:
Thisapplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, "MyPart.ipt")
Thisapplication.CommandManager.ControlDefinitions.Item("AssemblyCreateComponentCmd").Execute()
The following is a use case. This VB.NET has a form and a button on the form, clicking the button calls above code to issue "Create Component" command.
Imports Inventor
Imports System.Windows.Forms
Public Class Form1
Private Thisapplication As Application
Private m_quitInventor As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Me.Visible = False
Dim oControlDef As ControlDefinition
Thisapplication.CommandManager.PostPrivateEvent(
PrivateEventTypeEnum.kFileNameEvent, "MyPart.ipt")
oControlDef = Thisapplication.CommandManager.ControlDefinitions.Item(
"AssemblyCreateComponentCmd")
oControlDef.Execute()
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Try
' Try to get an active instance of Inventor
Try
Thisapplication = System.Runtime.InteropServices.Marshal.
GetActiveObject("Inventor.Application")
Catch ex As Exception
End Try
' If not active, create a new Inventor session
If Thisapplication Is Nothing Then
Dim inventorAppType As Type = System.Type.GetTypeFromProgID(
"Inventor.Application")
Thisapplication = System.Activator.CreateInstance(inventorAppType)
m_quitInventor = True
End If
Catch ex As Exception
MessageBox.Show("Unable to get Inventor", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error)
End Try
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not Thisapplication Is Nothing AndAlso m_quitInventor = True Then
Thisapplication.Quit()
End If
Thisapplication = Nothing
End Sub
End Class