原文地址: 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:

  1. Activate a member.
    激活iPart中某行
  2. Run the iProperties command
    进入iProoerties对话框
  3. Go to the “Physical” tab of the iProperties dialog.
    切入到”物理“ 选项卡
  4. Click the “Update” button on the “Physical” tab.
    点击”更新“
  5. Select and Copy the “Mass” field.
    选择并拷贝”重量“
  6. Open the iPart table and paste the mass value into the table for the correct row.
    打开iPart表,粘贴重量值到对应的格子
  7. Close the iPart table.
    关闭iPart表
  8. 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.

相当麻烦!对吧。而用下面一个简单的宏,就可以自动搞定所有了。

Public Sub UpdateWeightOfiParts() 
    ‘ 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,

    ' and get its index in the 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 
    ' 遍历所有行

Dim oRow As iPartTableRow 
    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. 

’该函数用来获取重量列的序号。如果没有发现,则返回-1
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. 

   ‘ 没有发现,则返回-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去做更新工作。

为iPart表添加“重量”列相关推荐

  1. Mysql给一个大表加一列_MySQL 大表添加一列的实现

    问题参考自: https://www.zhihu.com/question/440231149 ,mysql中,一张表里有3亿数据,未分表,要求是在这个大表里添加一列数据.数据库不能停,并且还有增删改 ...

  2. 操作iPart表的行和列 -1

    API提供了访问iPart表,行,列的属性.但是没有提供直接修改行,列,或增加删除行列的方法. 实际上,每个iPart表都和一个后台的Excel表格关联,在用户界面里,右键iPart表,可以看到两个菜 ...

  3. php 添加表,关于php:如何向MYSQL表添加新列

    我正在尝试使用PHP将新列添加到我的MYSQL表中. 我不确定如何更改表以创建新列. 在我的评估表中 assessmentid | q1 | q2 | q3 | q4 | q5 假设我有一个带有文本框 ...

  4. oracle 表添加一列

    语法:alter table 表名 add 列名 列格式 [null/not null] 例子: alter table t_test_lll add createdate Date null; 注意 ...

  5. 操作iPart表的行和列 -2

    在上一篇文章, 我们学习了如何创建一个iPart Table,并设置了一些行和列.今天我们看看如何对现有的iPart Table进行操作.其实很简单,就是直接对Excel 表处理. 以下代码基于上次创 ...

  6. dax powerbi 生成表函数_Power BI应用技巧:如何为DAX建的表添加索引?

    ​ 来源于知识星球中一个星友的问题,使用DAX在PowerBI中新建了一个表,如何为这个表添加索引列呢? 假如数据模型中只有一张订单表,需要从订单表中提取客户表,可以直接使用VALUES函数提取一个不 ...

  7. 为现有的表添加自增列id并赋值

    为现有的表添加自增列id并赋值 表中已经有好多数据 可以用存储过程 Declare @Id int Declare @Name varchar(500) DECLARE TabA CURSOR FOR ...

  8. sqlyog表添加列_如何用数据透视表求差,而不是求和?

    如图 8‑43中上图所示为某商店2012年第一季度的商品销售明细表,如何利用数据透视表快速统计各商品销量与上个月的差异如图 8‑43中下图所示? 图8‑43销售报表 1.解决方案 创建数据透视表,设置 ...

  9. mysql新增列并同时增加数据_图解MySQL | [原理解析] MySQL 为表添加列 是怎么quot;立刻quot;完成的...

    在上一期图解 图解MySQL | MySQL DDL为什么成本高?中,我们介绍了: 传统情况下,为表添加列需要对表进行重建 腾讯团队为 MySQL 引入了 Instant Add Column 的方案 ...

最新文章

  1. 基于Reddsion分布式的锁实现
  2. HALCON学习之旅(七)
  3. Python--网络编程-----传输层tcp/udp协议
  4. 浏览器皮肤_和平精英返场皮肤投票时间是什么时候?投票地址入口介绍-手游资讯...
  5. 编程师代码G都喜欢的|细致场景森系插画手机壁纸
  6. 单片机ADC采样算法----限幅平均滤波法
  7. mysql的导入导出命令_mysql导入导出命令
  8. Solaris 简单命令
  9. 基于electron和ffmpeg下载rtmp直播流
  10. 【STC15】串行口1的相关寄存器解读
  11. Rocksdb 的compaction_filter和table_properties_collector 用法 及 其底层实现
  12. 试验设计[实验设计]
  13. matlab 张正友工具箱,TOOLBOX_calib0 张正友的标定工具箱,直接运行会有错误,这是经过修改的 首先 _gui,确 matlab 238万源代码下载- www.pudn.com...
  14. AMR 文件解析及编解码流程
  15. java nio oio_Netty NIO transport OIO transport
  16. 国内好用的免费DNS服务器
  17. 企业数字化进程中,商业智能 BI 如何降本增效
  18. 服务器里微信怎么多开,谈谈微信多开
  19. 关于5G系统天线的原理
  20. 【Unity入门】21.预制体

热门文章

  1. GeoServer入门学习:04-发布Shapfile地图数据
  2. “3亿”风暴席卷昆明 搜狗全国移动峰会即将开幕
  3. 工作五年,我该如何选择?
  4. Windows 更新安装错误 - 0x80070643
  5. Win10 安装VC++6.0
  6. 小程序与APP相融共生:两种服务形态的互补
  7. 售让链克业务后的迅雷 此身终于分明
  8. 苹果全球销量超越小米重回第二,荣耀回归国内手机市场第一梯队
  9. 当高铁遇到地面沉降 如何测量 怎么应对(转载)
  10. 快速部署边缘计算,需要考虑哪些问题?