原文地址: http://modthemachine.typepad.com/my_weblog/2009/02/setting-the-weight-for-ipart-members.html
There was a question posted on the customization newsgroup that I spent a little time investigating and think the answer might be of general interest. The objective is to have a column in the iPart table that has the weight for that member. I wasn’t able to figure out a way to do this with built-in functionality in Inventor but it turned out it isn’t too difficult to write a program to accomplish the task. Although the program and description below is specific to the weight of a part, it could be modified to handle other part information that you want to associate with an iPart member.
Brian在欧特克Inventor论坛上看到一个帖子,提及如何为为Part表添加“重量”列。虽然API没有提供直接的方法,但实现并不难。当然,你想添加其它信息也是类似的。“重量”只是个样例。
First, let’s look at the iPart factory to see what needs to exist for the program to function correctly. The only thing special is that I created a custom iProperty called “Weight” and added that as one of the iProperties that I want to set from the table. It didn’t have to be an iProperty and could have been an “Other” value but using an iProperty provides the most flexibility for using the value elsewhere in Inventor. For example, in the the drawing. The picture below shows the iPart table after setting it up with this iProperty. The values for this new column are empty.
首先看看iPart 工厂。我们增加了一个名为“weight" (重量)的自定义iProperty,并加入到iPart表中。并不是必须iProperty,你也可以使用其它值。但iProperty较灵活。下图中,可以看到Weight列,尚未填写数值。
Now we need to assign the correct weight for each row to the weight column. If you had to this manually you would:
- Activate a member.
激活iPart中某行 - Run the iProperties command
进入iProoerties对话框 - Go to the “Physical” tab of the iProperties dialog.
切入到”物理“ 选项卡 - Click the “Update” button on the “Physical” tab.
点击”更新“ - Select and Copy the “Mass” field.
选择并拷贝”重量“ - Open the iPart table and paste the mass value into the table for the correct row.
打开iPart表,粘贴重量值到对应的格子 - Close the iPart table.
关闭iPart表 - Repeat steps 1 to 7 for every row in the table.
重复以上步骤,操作其它行
As you can see this is very tedious and prone to errors. Instead of manually going through these steps they can be automated using Inventor’s programming interface. Below is a VBA macro that will do the work.
相当麻烦!对吧。而用下面一个简单的宏,就可以自动搞定所有了。
‘ Get the active document. This assumes it is a part.
‘ 获取当前文档,假定是个零件
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
' Check that this part is an iPart factory.
’判断是否是iPart工厂
If Not oPartDoc.ComponentDefinition.IsiPartFactory Then
MsgBox "This part must be an iPart factory."
Exit Sub
End If
Dim oFactory As iPartFactory
Set oFactory = oPartDoc.ComponentDefinition.iPartFactory
' Check that there's a "Weight" column in the iPart table,
‘ 检查是否已经有”重量”列,并获取其序号
Dim iWeightColumnIndex As Long
iWeightColumnIndex = GetColumnIndex(oFactory, "Weight")
If iWeightColumnIndex = -1 Then
MsgBox "The column ""weight"" does not exist in the table."
Exit Sub
End If
' Iterate through the rows
' 遍历所有行
For Each oRow In oFactory.TableRows
' Make this the active row so the model will recompute.
’ 激活某行
oFactory.DefaultRow = oRow
' Get the weight.
‘获得重量值
Dim dWeight As Double
dWeight = oPartDoc.ComponentDefinition.MassProperties.Mass
' Convert it to current mass units defined by the document.
’按当前文档单位制转换
Dim strWeight As String
strWeight = oPartDoc.UnitsOfMeasure.GetStringFromValue( _
dWeight, kDefaultDisplayMassUnits)
' Set the row value for the weight column.
‘填写iPart表中对应的格子
oRow.Item(iWeightColumnIndex).Value = strWeight
Next
End Sub
’ Function that given a factory and the name or a column will return
’ the index number of the column, if it’s found in the factory’s
’ table. If the column is not found it returns –1. The comparison
’ of the name is done in a case insensitive way.
Private Function GetColumnIndex(ByVal Factory As iPartFactory, _
ByVal ColumnName As String) As Long
‘ Iterate through all of the columns looking for a
‘ match to the input name.
Dim i As Long
For i = 1 To Factory.TableColumns.Count
Dim oColumn As iPartTableColumn
Set oColumn = Factory.TableColumns.Item(i)
‘ Compare this column with the input name.
If LCase(oColumn.DisplayHeading) = LCase(ColumnName) Then
' A matching column was found so exit.
GetColumnIndex = i
Exit Function
End If
Next
' The column wasn't found so return -1.
GetColumnIndex = -1
End Function
The program is essentially performing the same steps that would be required to do this manually. It’s just able to do it much faster and without any mistakes. After running the macro on my factory the table now looks like this.
可见,代码的效率远大于手动的操作。执行后,可以看到iPart表的状态:
It’s also important to recognize that the weight is a snapshot of the weight at the time the macro was run. If the part is modified (for example, a new feature is added) or table values are edited that affect the part geometry, the weight will no longer be correct and the macro will need to be run again to update the weight column.
需要注意的是,重量将随着零件模型变化而变化,因此你需要在适当的时候及时更新iPart表,执行以上的代码。当然,也可以考虑在某些事件例如UserInputEventsSink.OnActivateCommand去做更新工作。