在使用Inventor进行设计的过程中,很多时候设计文件的命名需要遵循本公司的项目命名规则,以便进行项目管理和文档标准化管理。那么在Inventor中,除了复制粘贴外,如何方便地进行文档的命名呢?
实际上,Inventor 中提供了和文档命名有关的API,我们可以侦听相关的事件,在Inventor弹出文档命名的对话框前,根据既定的命名规则生成一个字符串,然后做为默认的文件名填写到相应的对话框中。
下面用一个简单的示例来演示它如何工作(程序见文后)。这个示例仅仅是根据命名规则提供一个默认的文件名,用户可以检查和修改这个名字,然后手动保存。要注意的是,该示例仅用于演示,并没有经过大量的测试,请大家务必根据实际情况修改和测试后使用。
设置步骤:
1.启动Inventor,切换到“工具”选项卡,点击 “VBA 编辑器”
2.在VBA编辑器窗口,右键点击项目浏览器中的ApplicationProject,然后选择Insert -> UserForm。默认生成的Form的名字是“UserForm1”。
3.右键点击form窗口,然后选择“View Code”。在代码窗口,清除默认生成的代码,然后把文后的代码复制粘贴过来。 (注意,如果上一步中生成的名字不是“UserForum1”,可以通过搜索替换的方式把代码中的“UserForum1”替换为实际的名字。)
4.在项目浏览器中展开Modules节点,右键点击Module1并选择 “View Code”。把下面的代码粘贴到代码窗口。同样的,根据实际情况替换Form的名字。
Public Sub FileNaming()
UserForm1.Show
End Sub
5.保存并关闭VBA编辑器。
使用方法:
1.当需要开始辅助文件命名时,提前运行该VBA程序。“工具”->”宏”, 运行 “FileNaming”。也可以把该程序自定义到常用的工具栏上。
2.界面说明:
Do Nothing – 不进行事件侦听,保留Inventor默认的行为。
Handle Event – 开始侦听文件命名事件,根据规则提供默认文件名。
Use Context – 勾选该项时,可以指定响应特定的命令。具体的命令过滤需要在代码中设定。本示例中勾选该选项时,不处理标准的零件和装配保存,仅处理一些特殊的模块,比如管路文件的保存。
Name Prefix – 字符串,指定命名规则的前缀。
Starting Number – 数字,指定流水号的起始数字,本示例会忽略数字的前导0。默认的增量为1,可以在代码中修改。
Start – 点击该按钮开始工作。
3.任何时候,如果需要重新开始设定命名规则,只需要重复步骤1,重新启动该程序即可。
VBA Code:
Option Explicit
' Creates a user form with the following controls:
' Button named cmdFirePopulate
' Radio/Option buttons named: optDoNothing, optHandle, optCancel
' Checkbox named chkUseContext
' Textbox
Private WithEvents oFileUIEvents As FileUIEvents
Private iIndex As Integer
Private WithEvents cmdFirePopulate As CommandButton
' Calls the PopulateFileMetadata method which will trigger the OnPopulateFileMetadata event
Private Sub cmdFirePopulate_Click()
Dim oTM As TransientObjects: Set oTM = ThisApplication.TransientObjects
Dim MetaDatas As ObjectCollection
Set MetaDatas = oTM.CreateObjectCollection
Dim Formulae As String: Formulae = "<formula>My formulae</formula>"
Dim context As NameValueMap
Set context = ThisApplication.TransientObjects.CreateNameValueMap
Dim handling As HandlingCodeEnum: handling = kEventNotHandled
Call oFileUIEvents.PopulateFileMetadata(MetaDatas, Formulae, context, handling)
iIndex = Val(Me!txtNameSN.Text)
UserForm1.Hide
End Sub
' Handles the OnPopulateFileMetadata event
Private Sub oFileUIEvents_OnPopulateFileMetadata(ByVal FileMetadataObjects As ObjectsEnumerator, ByVal Formulae As String, ByVal context As NameValueMap, HandlingCode As HandlingCodeEnum)
'Context Filter Sample
'Only response to specific command
'For Tube & Pipe command, Context.Count=0
If (Me!chkUseContext And context.Count > 0) Then
Exit Sub
End If
If Me!optHandle Then
HandlingCode = kEventHandled
Dim oEachMetaData As FileMetadata
For Each oEachMetaData In FileMetadataObjects
oEachMetaData.DisplayName = Me!txtNamePrefix.Text + CStr(iIndex)
oEachMetaData.FileName = Me!txtNamePrefix.Text + CStr(iIndex)
oEachMetaData.FileNameOverridden = False
iIndex = iIndex + 1
Next
ElseIf Me!optCancel Then
HandlingCode = kEventCanceled
End If
End Sub
Private Sub UserForm_Initialize()
InitControls
Set oFileUIEvents = ThisApplication.FileUIEvents
Me!optnothing.Value = True
Me!chkUseContext.Value = False
End Sub
Private Sub InitControls()
Me.Width = 220: Me.Height = 150: Me.StartUpPosition = 1
' Button named cmdFirePopulate
Set cmdFirePopulate = Me.Controls.Add("Forms.CommandButton.1", "cmdFirePopulate")
cmdFirePopulate.Caption = "Start "
cmdFirePopulate.Left = 12: cmdFirePopulate.Top = 100
cmdFirePopulate.AutoSize = True
' Radio/Option buttons named: optDoNothing, optHandle, optCancel
Dim oOpt As MSForms.OptionButton
Set oOpt = Me.Controls.Add("Forms.OptionButton.1", "optNothing")
oOpt.Caption = "Do Nothing"
oOpt.Left = 12: oOpt.Top = 12
oOpt.AutoSize = True
Set oOpt = Me.Controls.Add("Forms.OptionButton.1", "optHandle")
oOpt.Caption = "Handle Event"
oOpt.Left = 12: oOpt.Top = 30
oOpt.AutoSize = True
Set oOpt = Me.Controls.Add("Forms.OptionButton.1", "optCancel")
oOpt.Caption = "Cancel Event"
oOpt.Left = 12: oOpt.Top = 48
oOpt.Visible = False
oOpt.AutoSize = True
' Checkbox named chkUseContext
Dim oChk As MSForms.CheckBox
Set oChk = Me.Controls.Add("Forms.Checkbox.1", "chkUseContext")
oChk.Caption = "Use Context"
oChk.Left = 12: oChk.Top = 70
oChk.AutoSize = True
'oChk.Visible = False
' Label named labNamePrefix
Dim oLabPerfix As MSForms.Label
Set oLabPerfix = Me.Controls.Add("Forms.Label.1", "labNamePrefix")
oLabPerfix.Caption = "Name Prefix:"
oLabPerfix.Left = 100: oLabPerfix.Top = 12: oLabPerfix.Width = 100
oLabPerfix.AutoSize = False
' Textbox named txtNamePrefix
Dim oTxtPrefix As MSForms.TextBox
Set oTxtPrefix = Me.Controls.Add("Forms.Textbox.1", "txtNamePrefix")
oTxtPrefix.Left = 100: oTxtPrefix.Top = 22: oTxtPrefix.Width = 100
oTxtPrefix.Text = "Name Prefix-"
oTxtPrefix.AutoSize = False
' Label named labNameIndex
Dim oLabIndex As MSForms.Label
Set oLabIndex = Me.Controls.Add("Forms.Label.1", "labNameIndex")
oLabIndex.Caption = "Starting Number:"
oLabIndex.Left = 100: oLabIndex.Top = 58: oLabIndex.Width = 100
oLabIndex.AutoSize = False
' Textbox named txtNameSN
Dim oTxtSN As MSForms.TextBox
Set oTxtSN = Me.Controls.Add("Forms.Textbox.1", "txtNameSN")
oTxtSN.Left = 100: oTxtSN.Top = 70: oTxtSN.Width = 100
oTxtSN.Text = "Please input number"
oTxtSN.AutoSize = False
End Sub