Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Deselect object on second selection

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
youann21700
230 Views, 5 Replies

Deselect object on second selection

Hi,

I have a rule that changes part material assignments from the assembly level (handy). I works by creating an object collection based on manual selections by the user, Ive added in a highlight set as well for usability. My only issue is that I would like something to be removed from the object collection and highlight set if it selected again (i.e. typical way you would un-select something).

Anyone got any hints? Ive attached my code below as well.

Dim oAsset As Asset
Dim oHighlight As Inventor.HighlightSet
oHighlight = ThisDoc.Document.CreateHighlightSet
oHighLight.Color = ThisApplication.TransientObjects.CreateColor(255,105,180)
Dim oAsset_Array As New ArrayList
For Each oAsset_Array_X In ThisApplication.ActiveMaterialLibrary.MaterialAssets
oAsset_Array.Add(oAsset_Array_X.DisplayName)
oAsset_Array.Sort()
Next
'present the user with the list to choose from
oAsset_Array_Selected = InputListBox("CHOOSE MATERIAL FROM ABOVE LIST", oAsset_Array, oAsset_Array.Item(1), "MATERIAL SELECTION", "LIST OF MATERIALS",600,0)
If oAsset_Array_Selected = "" Then
	Return 
End if

Dim comps As ObjectCollection
Dim comp As Object
comps = ThisApplication.TransientObjects.CreateObjectCollection

While True
	comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a component. Press ESC to end selection")

	' If nothing gets selected then we're done	
	If IsNothing(comp) Then Exit While
	comps.Add(comp) 
	Call oHighlight.AddItem(comp)
End While

' If there are selected components we can do something
For Each comp In comps
	Dim oDef As PartDocument
    oDef = comp.Definition.Document
    Dim oRenderStyle As String
    oRenderStyle = oAsset_Array_Selected
    oDef.ActiveMaterial.DisplayName = oRenderStyle
    iLogicVb.UpdateWhenDone = True		
Next

oHighlight.Clear

 

5 REPLIES 5
Message 2 of 6
WCrihfield
in reply to: youann21700

Hi @youann21700.  That 'deselect' process, when using HighlightSet can be tricky to get right.  The HighlightSet is also a bit odd to work with in certain situations (stuff can remain highlighted after done with code, when you do not want them to stay highlighted, and can be odd getting them back to normal).  You must either use its Clear or its Remove methods to remove stuff that is already in it, but sometimes you may need to update the View to see the results.  If working with an ObjectCollection, you can use that with its AddMultipleItems method for faster processing.  Below is a link to another similar forum post you may find helpful.

https://forums.autodesk.com/t5/inventor-programming-ilogic/highlightset-is-not-highlighted-after-sec... 

And is another that might be slightly helpful:

https://forums.autodesk.com/t5/inventor-programming-ilogic/display-doesn-t-update-selection-highligh... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

I recommend you to use selection API instead of simple Pick and highlight set. There is a full support for select, unselect and so on. Highlighting is included and you have much better control on selection process.

Message 4 of 6

This was a good suggestion, I was trying to avoid doing this since my programming skills are lacking but manged to get it working with some copy/pasting. I've attached my code below for anyone who comes across this in the future. Essentially it just allows you to select parts within an assembly and change their material at the part level, useful for complex assemblies.

Sub Main

	Dim oAsset_Array As New ArrayList
	For Each oAsset_Array_X In ThisApplication.ActiveMaterialLibrary.MaterialAssets
	oAsset_Array.Add(oAsset_Array_X.DisplayName)
	oAsset_Array.Sort()
	Next
	oAsset_Array_Selected = InputListBox("CHOOSE MATERIAL FROM ABOVE LIST", oAsset_Array, oAsset_Array.Item(48), "MATERIAL SELECTION", "LIST OF MATERIALS",600,0)
	If oAsset_Array_Selected = "" Then
		Return 
End If
	Dim oSelect As New clsSelect
	oSelect.WindowSelect(ThisApplication)
comps = oSelect
For Each comp As ComponentOccurrence In oSelect.SelectedObjects
	Dim oDef As PartDocument
    oDef = comp.Definition.Document
    Dim oRenderStyle As String
    oRenderStyle = oAsset_Array_Selected
    oDef.ActiveMaterial.DisplayName = oRenderStyle
    iLogicVb.UpdateWhenDone = True		
Next
End Sub

Class clsSelect
	Private WithEvents oInteractEvents As InteractionEvents
	Private WithEvents oSelectEvents As SelectEvents
	Private bTooltipEnabled As Boolean
	Private ThisApplication As Inventor.Application
	Public SelectedObjects As ObjectsEnumerator
	Private stillSelecting As Boolean = True

	Public Sub WindowSelect(oApp As Inventor.Application)
		ThisApplication = oApp
		oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents
		oInteractEvents.InteractionDisabled = False
		oSelectEvents = oInteractEvents.SelectEvents
		oSelectEvents.AddSelectionFilter(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter)
		oSelectEvents.WindowSelectEnabled = True
		bTooltipEnabled = ThisApplication.GeneralOptions.ShowCommandPromptTooltips
		ThisApplication.GeneralOptions.ShowCommandPromptTooltips = True
		oInteractEvents.StatusBarText = "Select components. Shift+Click to unselect. Esc to finish."
		oInteractEvents.Start()
		While stillSelecting
			ThisApplication.UserInterfaceManager.DoEvents()
		End While
	End Sub
	Private Sub oInteractEvents_OnTerminate() Handles oInteractEvents.OnTerminate
		ThisApplication.GeneralOptions.ShowCommandPromptTooltips = bTooltipEnabled
		oSelectEvents = Nothing
		oInteractEvents = Nothing
		stillSelecting = False
	End Sub
	Private Sub oSelectEvents_OnSelect(ByVal JustSelectedEntities As ObjectsEnumerator, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View) Handles oSelectEvents.OnSelect
		SelectedObjects = oSelectEvents.SelectedEntities
	End Sub
	Private Sub oSelectEvents_OnUnSelect(UnSelectedEntities As ObjectsEnumerator, SelectionDevice As SelectionDeviceEnum, ModelPosition As Point, ViewPosition As Point2d, View As View) Handles oSelectEvents.OnUnSelect
		SelectedObjects = oSelectEvents.SelectedEntities
	End Sub
End Class

 

Message 5 of 6

Please avoid to use DoEvents when possible. See this topic for more info.

Message 6 of 6

@youann21700 

 

This is the example I had on hand for this, in case it's of interest.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oList As New ArrayList
Dim oObj As New Object
Dim oCollection As ObjectCollection
oCollection = ThisApplication.TransientObjects.CreateObjectCollection

Dim oHLSet as HighlightSet = oADoc.CreateHighlightSet

While True
	oMsg = "Select parts (press ESC to continue) Count: " & oCollection.Count 

	Dim oOcc As ComponentOccurrence
	oOcc = ThisApplication.CommandManager.Pick(
	SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, oMsg)

	' If nothing gets selected then we're done	
	If IsNothing(oOcc) Then Exit While

	'set occurrence as object so it can be "removed by object"
	oObj = oOcc

	'check against list to prevent 
	'selecting same component twice
	If oList.Contains(oOcc.Name) = False Then
		oList.Add(oOcc.Name)
		oCollection.Add(oOcc)
		oHLSet.AddItem(oOcc)
	Else
		oList.Remove(oOcc.Name)
		oCollection.RemoveByObject(oObj)
		oHLSet.Clear
		oHLSet.AddMultipleItems(oCollection)
	End If
End While

'do stuff with collection here
'
'

'eventually clear HL set
oHLSet.Clear

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report