By Adam Nagy
There is a blog post on selecting multiple components interactively from an iLogic Rule, but it does not highlight the selected objects while the Rule is running in the Inventor versions I tested it in - something might have changed in the meantime. However, using a HighlightSet seems to work fine:
Dim oSet As HighlightSet ' Check to make sure the active document is a part. If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then MsgBox("A part document must be open.") Exit Sub End If ' Set a reference to the active part document. Dim oDoc As PartDocument oDoc = ThisApplication.ActiveDocument oSet = oDoc.CreateHighlightSet While True Dim oFace As Object oFace = ThisApplication.CommandManager.Pick( SelectionFilterEnum.kPartFaceFilter, "Select a face") ' If nothing gets selected then we're done If IsNothing(oFace) Then Exit While oSet.AddItem(oFace) End While oSet.Clear()
When you run an iLogic Rule it behaves as starting a command, which also means that the current selection will be cancelled. So we would need to store which entities were already selected inside our Rule and reselect them. In order to keep a list we would need to have a global static variable that could hold on to those entities. Even if we just kept them in a HighlightSet we would still need a global static object to keep a reference to the HighlightSet, because as soon as that goes out of scope it will be deleted and then highlighting will be gone.
In case of VB.NET the way to create a global static variable is to use the Shared keyword. It is possible to add global variables to an iLogic Rule by implementing the whole Rule class as shown in this article:
https://www.cadlinecommunity.co.uk/hc/en-us/articles/203491091-Inventor-2016-iLogic-Using-Global-Variables
Actually, because you can define the whole Rule class, you can also handle events in it if you want. That means you could even use the InteractionEvents class which enables you to do multi and window selections. However programmatically selecting entities inside a rule using SelectSet.Select or CommandManager.DoSelect does not seem to work well: the entities only get highlighted once the rule finished.
If we stick to HighlightSet then we could use it like this to do selection and re-selection of previously selected entities:
Class ThisRule ' Keep track of selected entities Shared oSelectedEnts As ObjectCollection Sub Main() Dim oSet As HighlightSet ' Check to make sure the active document is a part. If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then MsgBox("A part document must be open.") Exit Sub End If ' Initialize the entity collection ' If you want to keep track of the previously ' selected entitites then only initialize this variable ' if it has not been initialized before If oSelectedEnts Is Nothing Then oSelectedEnts = ThisApplication.TransientObjects.CreateObjectCollection() End If oDoc = ThisDoc.Document oSet = oDoc.CreateHighlightSet ' Show the previously selected entities For Each ent In oSelectedEnts oSet.AddItem(ent) Next While True Dim oFace As Object oFace = ThisApplication.CommandManager.Pick( SelectionFilterEnum.kPartFaceFilter, "Select a face") ' If nothing gets selected then we're done If IsNothing(oFace) Then Exit While oSet.AddItem(oFace) oSelectedEnts.Add(oFace) End While oSet.Clear() End Sub End Class