My First Plug-in - Inventor API .NET 开发从0开始-课程5

有一阵子没继续这个课程系列的翻译了。不知前面几个课程是否已经熟练掌握,今天来看看如何为对象添加属性。原文地址:

Lesson 5: Adding Attributes

课程相关代码工程:

 lesson5_vb-net.zip (zip - 26Kb)

lesson5_c-sharp.zip (zip - 40Kb)


  1. 打开课程4的工程,进入Form1的代码窗口。如果你还在运行或调试中,可关闭窗口即停止程序。在Form1.vb里有个Button1_Click函数,是按钮1被按下时的事件,其中对选择到的对象进行隐藏(可见性)操作。现在注释掉设置可见性的代码,在行首加上一个单引号即可,如下绿色部分。
           For Each obj In selSet
                compOcc = obj
                Debug.Print(compOcc.Name)
                'compOcc.Visible = False
           Next

  2. 要想给对象添加属性,需要访问到AttributeSets。看如下代码,它获取到前面选择到对象的AttributeSets 。AttributeSets 是AttributeSet的集合,而AttributeSet是Attribute的集合。即,对象可以有多个属性集合,每个集合里是具体的属性。
    Dim attbSets As AttributeSets = compOcc.AttributeSets

  3. 添加一个新的属性集合AttributeSet,可先利用NameIsUsed方法检查一下是否已经存在同名的属性集合,例如:
    '为 ComponentOccurrence添加属性集合
    If Not attbSets.NameIsUsed("myPartGroup") Then
     
    End If

  4. 接着,如果没有同名的,则可以添加该属性集合,本例中名为“myPartGroup”
    Dim attbSet As AttributeSet = attbSets.Add("myPartGroup")

  5. 最后,添加具体的属性Attribute。AttributeSet的方法Add添加属性,第一个参数是属性名,第二个是属性类型,第三个是属性值。例如,添加一个名为“PartGroup1”的字串,字串内容是“Group1”。
    Dim attb As Inventor.Attribute = _
                 attbSet.Add( _
                   "PartGroup1", ValueTypeEnum.kStringType, "Group1")

  6. 编译工程,看看有无什么错误


 如何调试中查看变量

  1. 开发程序最重要的能力就是会调试。它可以帮助查找到程序错误的原因,从而解决问题或优化代码。调试一般会设置断点,即程序运行时会在此处停下来等你查看。断点添加可以把光标放在该代码行,按下F9,或直接用鼠标在行首处点一下即可。



  2. 启动调试, 按下F5。



  3. 本课程测试,我们需要先在Inventor里选择一些装配组件,然后点击程序的按钮。这时,程序停在断点处,并高亮显示。等待你下一步的命令或在此查看你关心的变量。




  4. 要查看某个变量,将光标停在变量处,然后右键菜单里选择【添加监视】。



    监视窗口就会出现,并列出变量的相关数据:




  5. 要继续执行程序,则按下F5或F10(单步执行)



  6. 要停止调试,关闭程序窗口或点击 【调试】>>【停止调试】即可


再添加一个按钮,用AttributeManager 找到组件

  1. 为了进一步熟悉属性,在程序里再添加一个按钮。或许你忘记如何添加了,别着急,双击Form1,出现对话框编辑界面,打开工具条窗口,找到按钮这种控件,拖进对话框即可。
  2. 按钮添加后缺省有显示的字串,例如“button2”, 为了让程序更易懂,可设置有含义的名字。点中按钮,右键菜单里选择【属性】,则【属性】窗口打开。其中【文本】就是设置按钮显示字串。

     

  3. 同样步骤,修改另外一个按钮的显示字串。

  4. 双击新添加的按钮,程序自动增加按钮点击事件。

  5. 在事件中,判断当前激活Inventor的文档是否是装配
       If _invApp.Documents.Count = 0 Then
          MsgBox("Need to open an Assembly document")
          Return
        End If
     
        If _invApp.ActiveDocument.DocumentType <> _
             DocumentTypeEnum.kAssemblyDocumentObject Then
          MsgBox("Need to have an Assembly document active")
          Return
        End If
     
        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocumen

  6. 接着获取文档的AttributeManager 。顾名思义,这个对象用来管理文档里所有属性, 还提供方法通过属性名查找对象。如下,代码寻找哪些对象有名为PartGroup1并且字串值为“Group1”属性。

    Try
         Dim attbMan As AttributeManager = asmDoc.AttributeManager
     
          Dim objsCol As ObjectCollection
          objsCol = _
            attbMan.FindObjects("myPartGroup", "PartGroup1", "Group1")
     
         Catch ex As Exception
              MsgBox("Problem hiding component")
    End Try

  7. 找到的对象可能有多个,所以返回的是对象集合ObjectCollection。依次遍历就能得到每个对象。代码样例对找到的对象控制其可见性。
    Dim compOcc As ComponentOccurrence
    For Each obj As Object In objsCol
        compOcc = obj
     ' Toggle the visibility of the Component Occurrence
       compOcc.Visible = Not compOcc.Visible
    Next

  8.  编译工程看看有无错误。可以直接测试或进行调试。选择装配的组件,然后点击按钮2,则包含该属性的组件将被隐藏。

再深入学习按钮中的代码

如前所述,可以通过API为对象添加属性或根据属性追查找到到对象。很多Inventor对象都有AttributeSets ,即能添加属性。另外,顺便提及,在定义代码变量时,尽量让其有一定含义,例如本例中attbSets代表AttributeSets 。这样在程序分析和调试中会很有帮助。尤其别人来看你的代码,不会因为一些不知所云的变量名而弄晕。

Dim attbSets As AttributeSets = compOcc.AttributeSets


对象可以有多个AttributeSet ,但必须不同名,API也不允许你添加同名的AttributeSet 。所以我们建议可先用NameIsUsed查看。如果有同名,则弹出消息其实,若没有,则添加。

' Add the attributes to the ComponentOccurrence
If Not attbSets.NameIsUsed("myPartGroup") Then
 
End If

属性集合的添加使用AttributeSets.Add() 。提供一个字串作为参数。显然,起一个有含义的名字尤为重要,否则当属性集合很多时,自己都弄不清属性是用来存放哪一类信息了

     Dim attbSet As AttributeSet = attbSets.Add("myPartGroup")

具体的属性Attribute是通过AttributeSet.Add方法,参数前面已经解释了。同理,在一个属性集合里,每个属性的名字也必须不一样。

Dim attb As Inventor.Attribute = _
             attbSet.Add( _
               "PartGroup1", ValueTypeEnum.kStringType, "Group1")
Visual Studio 提供的智能提示,可以帮助你在编写代码时知道方法的参数类型 .这里需要多提示一点:AttributeSets.Add() 第二个参数是Inventor.ValueTypeEnum,是数据枚举值,目前有这几种类型:
  kIntegerType = 14593
  kDoubleType = 14594
  kStringType = 14595
  kByteArrayType = 14596




再深入学习按钮2中的代码


按钮2寻找有特定属性的对象,通过AttributeManager的FindObjects方法。这里我们使用了Try-Catch。这样有错误的话,可以捕获。

Try
     Dim attbMan As AttributeManager = asmDoc.AttributeManager
 
      Dim objsCol As ObjectCollection
      objsCol = _
        attbMan.FindObjects("myPartGroup", "PartGroup1", "Group1")
 
     Catch ex As Exception
          MsgBox("Problem hiding component")
End Try

The FindObjects() method populated an ObjectCollection with each of the objects it found with your custom attribute attached. FindObjects() took three arguments to locate objects (Inventor entities) with attributes. Although you used all three arguments in your project – to look for attributes with a specific AttributeSetName, AttributeName and AttributeValue – they were all not required: in other situations you might have chosen to look only for attributes belonging to a set with a particular name, for instance.  In this example you passed in “myPartGroup” (as the AttributeSetName), “PartGroup1” (as the AttributeName) and “Group1” (as the AttributeValue). IntelliSense, as we see below, helps us understand the meaning of the arguments to the FindObjects() method. Notice the square brackets, which indicate that each of the arguments is optional.

FindObjects有三个参数:属性集合名,属性名,属性值。我们的代码全部都提供了。其实这三个参数可以非常灵活。当编写代码

时,Visual Studio会提示这三个参数是可选的,即你可以不用设置它们。例如,如果只设置属性值,则该方法只寻找那些拥有该属性

值的对象,而不管是什么属性集合或属性。其实前面的代码,无非是最苛刻的查找条件,必须三个参数都满足。你可以根据具体的需

求灵活使用

最后所做的事情很简答了,遍历找到的对象集合,依次设置其可见性,隐藏起来。这里用到了我们熟知的For - Next语句,即循环体。 

Dim compOcc As ComponentOccurrence
For Each obj As Object In objsCol
     compOcc = obj
     ' Toggle the visibility of the Component Occurrence
     compOcc.Visible = Not compOcc.Visible
Next



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值