You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Autodesk Inventor插件:如何在OnUnSelect事件后执行后续子程序?

解决Autodesk Inventor插件UnSelect事件重选被覆盖的问题

核心思路

OnUnSelect事件执行完毕后,Inventor才会实际执行取消选择操作,因此直接在事件内调用选择方法会被后续系统操作覆盖。需通过延迟执行重选逻辑,让重选动作在Inventor完成取消操作后再运行。

具体实现方案

1. 记录待重选对象并触发延迟执行

在OnUnSelect事件中先保存需要重选的实体,再通过短延迟定时器触发后续重选操作:

Private WithEvents _eInteractEvents As InteractionEvents
Private WithEvents _eSelectEvents As SelectEvents
Private _entitiesToReselect As ObjectsEnumerator ' 存储要重选的对象

Private Sub _eSelectEvents_OnUnSelect(UnSelectedEntities As ObjectsEnumerator, SelectionDevice As SelectionDeviceEnum, ModelPosition As Point, ViewPosition As Point2d, View As View) Handles _eSelectEvents.OnUnSelect
    ' 保存需要重选的实体集合
    _entitiesToReselect = UnSelectedEntities
    
    ' 创建短延迟定时器,确保Inventor完成取消操作后再执行重选
    Dim delayTimer As New System.Windows.Forms.Timer()
    delayTimer.Interval = 10
    AddHandler delayTimer.Tick, AddressOf ExecuteReselect
    delayTimer.Start()
End Sub

2. 实现延迟重选逻辑

编写独立子程序处理重选,执行完毕后清理定时器:

Private Sub ExecuteReselect(sender As Object, e As EventArgs)
    Dim delayTimer As System.Windows.Forms.Timer = DirectCast(sender, System.Windows.Forms.Timer)
    delayTimer.Stop()
    delayTimer.Dispose()
    
    If _entitiesToReselect IsNot Nothing AndAlso _entitiesToReselect.Count > 0 Then
        Dim activeDoc As Document = ThisApplication.ActiveDocument
        Dim selectSet As SelectionSet = activeDoc.SelectSet
        
        ' 清空现有选择集(可选,根据需求调整)
        selectSet.Clear()
        ' 重新添加目标实体
        For Each entity As Object In _entitiesToReselect
            selectSet.Add(entity)
        Next
        
        _entitiesToReselect = Nothing
    End If
End Sub

3. 关键注意事项

  • 定时器Interval设为10ms即可,足够确保Inventor完成取消操作后再执行重选;
  • 需确保_entitiesToReselect的引用有效,避免对象被垃圾回收导致重选失败;
  • 多文档场景下,要确认ActiveDocument为当前操作的文档,避免选错目标文档。

内容的提问来源于stack exchange,提问作者Majjek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:45:05