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: 

Pick part in assembly drawing view (select part priority)

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Rob987654
2987 Views, 6 Replies

Pick part in assembly drawing view (select part priority)

I want to run an ilogic rule after an user select a part inside an assembly drawing view.

 

At the moment the rule works when selection priority is set to PartPriority (using shift RBM). 

 

Is it possible with vba/ilogic to give the user the possibility to select the part inside the assembly drawing view without selecting part priority first. After the user select the part, an  ilogic rule will be run.

 

Part of Code:

Set Test = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select Part")

 

Is there a way to replace KAssemblyOccurenceFilter with Part Priority that works inside drawing environment?

 

 

Labels (1)
6 REPLIES 6
Message 2 of 7
WCrihfield
in reply to: Rob987654

Not really, but you could select some geometry belonging to the part you want within the DrawingView, then get the document that geometry belongs to within the code after the selection.  Or you could navigate the BrowserNode for that view, to get the part you want, either manually or by code.  You could also probably pop-up an InputListBox with the list of all parts within the assembly, and select one that way.  If you want to select the part completely by code, instead of using the Pick method, you would have to specify something about the part you want, so it can find it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7
WCrihfield
in reply to: WCrihfield

FYI:  You can set the Part Priority selection preference by code but it just doesn't work with the Pick function.

Dim oDDoc As DrawingDocument = ThisDrawing.Document
oDDoc.SelectionPriority = SelectionPriorityEnum.kPartSelectionPriority

Same for the SelectionPreferences property of the DrawingDocument.

You can set it to select specific type of object, but the Pick function has it's own filter settings, which override these settings.

Dim oDDoc As DrawingDocument = ThisDrawing.Document
oDDoc.SelectionPreferences.Add(ObjectTypeEnum.kPartComponentDefinitionObject)

There are other ways of selecting things though.

 

Here's a 'somewhat' simple example of the method I mentioned before.  This iLogic rule will get the model document being represented within the specified view, then if it is an Assembly, it iterates through all its referenced documents, checking to see if they are Parts, then and adds their FullFileName and PartDocument object (key - value pair) to a Dictionary object for temporary storage.  When the loop finishes, it presents you with an InputListBox of the FullFileName's of those Parts.  You can select one, then the code gets the corresponding PartDocument.  Then I just added a MsgBox at the end to show you the FullFileName of the new PartDocument variable, to show that it worked.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Return
End If

Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Sheet = oDDoc.ActiveSheet
Dim oView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter,"Select a View.")
Dim oParts As New Dictionary(Of String, PartDocument)
If oView.ReferencedDocumentDescriptor.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	Dim oADoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
		If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
			Dim oPDoc As PartDocument = oRefDoc
			If Not oParts.ContainsKey(oRefDoc.FullFileName) Then
				oParts.Add(oPDoc.FullFileName, oPDoc)
			End If
		End If
	Next
End If
Dim oPartName As String = InputListBox("Select the part document you want.", oParts.Keys, " ", "PARTS")
'MsgBox("You chose:  " & oPartName)
Dim oPartDoc As PartDocument
oParts.TryGetValue(oPartName,oPartDoc) '<<< This line just set the value of the oPartDoc variable
MsgBox(oPartDoc.FullFileName)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 7
JhoelForshav
in reply to: WCrihfield

Hi @Rob987654 

Try this iLogic rule to pick a part in assembly drawing.

To show that an occurrence is selected it will return its name 🙂

Sub Main
	Dim oSelect As New clsSelect
	oSelect.SelectOccurrence(ThisApplication)
	Dim oOcc As ComponentOccurrence = oSelect.SelectedOcc
	oSelect = Nothing
	
	MsgBox(oOcc.Name)
End Sub


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

	Public Sub SelectOccurrence(oApp As Inventor.Application)
		ThisApplication = oApp
		oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents
		oInteractEvents.InteractionDisabled = False
		oSelectEvents = oInteractEvents.SelectEvents
		oSelectEvents.AddSelectionFilter(SelectionFilterEnum.kDrawingCurveSegmentFilter)
		oSelectEvents.WindowSelectEnabled = False
		bTooltipEnabled = ThisApplication.GeneralOptions.ShowCommandPromptTooltips
		ThisApplication.GeneralOptions.ShowCommandPromptTooltips = True
		oInteractEvents.StatusBarText = "Select component"
		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_OnPreselect(ByRef PreSelectEntity As Object, ByRef DoHighlight As Boolean, ByRef MorePreSelectEntities As ObjectCollection, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Inventor.Point2d, ByVal View As Inventor.View)  Handles oSelectEvents.OnPreselect
		Try
			Dim oCurves As DrawingCurvesEnumerator = DirectCast(PreSelectEntity, DrawingCurveSegment).Parent.Parent.DrawingCurves(DirectCast(PreSelectEntity, DrawingCurveSegment).Parent.ModelGeometry.Parent.Parent)
			Dim oCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
			For Each oCurve As DrawingCurve In oCurves
				For Each oSeg As DrawingCurveSegment In oCurve.Segments
					If oSeg IsNot PreSelectEntity Then oCol.Add(oSeg)
				Next
			Next
			MorePreSelectEntities = oCol
			DoHighlight = True
		Catch
			DoHighlight = False
		End Try

	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
		SelectedOcc = DirectCast(JustSelectedEntities.Item(1), DrawingCurveSegment).Parent.ModelGeometry.Parent.Parent
		ThisApplication.CommandManager.StopActiveCommand
	End Sub
End Class

 

Message 5 of 7
Rob987654
in reply to: JhoelForshav

Thanks for the response. 

The code posted by @JhoelForshav is in my situation the perfect solution. 

Message 6 of 7

Hi @JhoelForshav,

 

Do you think it is possible to rewrite your code so that once you select a part, it will select all occurrences of that part in the drawing? 

My end goal is to have all occurrences in the drawing selected in the browser and then right-click -> Properties -> and change color. This can be time-consuming to do manually if you have many views.

 

Thanks in advance!

 

 

Message 7 of 7


@JhoelForshav this link needs to be updated: iLogic – Occurrence Selection Filter In Drawings – Clint Brown

 

with this:

 

 

 

 

Sub Main

	'iLogic Code by Jhoel Forshav - originally posted at https://clintbrown.co.uk/ilogic-occurrence-selection-filter-in-drawings/
	Dim oSelect As New SelectClass
	Dim oOcc As ComponentOccurrence = oSelect.SelectOccurrence(ThisApplication)
	oSelect = Nothing
	If IsNothing(oOcc) Then Exit Sub

	Try
	'MessageBox.Show("test1")
	MessageBox.Show("You have picked: " & ("Part: " & oOcc.Name & " / " & "in Assembly > " & oOcc.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.ParentOccurrence.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.ParentOccurrence.ParentOccurrence.ParentOccurrence.Name & "(.iam)"), "Pick part", MessageBoxButtons.OK, MessageBoxIcon.Information)
	Catch
		Try
		'MessageBox.Show("test2")
		MessageBox.Show("You have picked: " & ("Part: " & oOcc.Name & " / " & "in Assembly > " & oOcc.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.ParentOccurrence.ParentOccurrence.Name & "(.iam)"), "Pick part", MessageBoxButtons.OK, MessageBoxIcon.Information)
		Catch
			Try
			'MessageBox.Show("test3")
			MessageBox.Show("You have picked: " & ("Part: " & oOcc.Name & " / " & "in Assembly > " & oOcc.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.ParentOccurrence.Name & "(.iam)"), "Pick part", MessageBoxButtons.OK, MessageBoxIcon.Information)
			Catch
				Try
				'MessageBox.Show("test4")
				MessageBox.Show("You have picked: " & ("Part: " & oOcc.Name & " / " & "in Assembly > " & oOcc.ParentOccurrence.Name & "(.iam)" & " > " & oOcc.ParentOccurrence.ParentOccurrence.Name & "(.iam)"), "Pick part", MessageBoxButtons.OK, MessageBoxIcon.Information)
				Catch
					Try
					'MessageBox.Show("test5")
					MessageBox.Show("You have picked: " & ("Part: " & oOcc.Name & " / " & "in Assembly > " & oOcc.ParentOccurrence.Name & "(.iam)"), "Pick part", MessageBoxButtons.OK, MessageBoxIcon.Information)
					Catch
						Try
						'MessageBox.Show("test6")
						MessageBox.Show("You have picked: " & "Part: " & oOcc.Name & " in this current Assembly", "Pick part", MessageBoxButtons.OK, MessageBoxIcon.Information)
						Catch
							Try
							MessageBox.Show("unkown error")
							Catch
							End Try
						End Try
					End Try
				End Try
			End Try
		End Try
	End Try
End Sub

Class SelectClass

	Private WithEvents oInteractEvents As InteractionEvents
	Private WithEvents oSelectEvents As SelectEvents
	Private bTooltipEnabled As Boolean
	Private ThisApplication As Inventor.Application
	Private SelectedOcc As ComponentOccurrence
	Private stillSelecting As Boolean = True

	Public Function SelectOccurrence(oApp As Inventor.Application)
		ThisApplication = oApp
		oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents
		oInteractEvents.InteractionDisabled = False
		oSelectEvents = oInteractEvents.SelectEvents
		oSelectEvents.AddSelectionFilter(SelectionFilterEnum.kAllEntitiesFilter)'kDrawingCurveSegmentFilter
		oSelectEvents.WindowSelectEnabled = False
		bTooltipEnabled = ThisApplication.GeneralOptions.ShowCommandPromptTooltips
		ThisApplication.GeneralOptions.ShowCommandPromptTooltips = True
		oInteractEvents.StatusBarText = "Pick part occurrence."
		oInteractEvents.Start()
		AppActivate(ThisApplication.Caption)
		While stillSelecting
			ThisApplication.UserInterfaceManager.DoEvents()
		End While
		Return SelectedOcc
	End Function

	Private Sub oInteractEvents_OnTerminate() Handles oInteractEvents.OnTerminate
		ThisApplication.GeneralOptions.ShowCommandPromptTooltips = bTooltipEnabled
		oSelectEvents = Nothing
		oInteractEvents = Nothing
		stillSelecting = False
	End Sub

	Private Sub oSelectEvents_OnPreselect(ByRef PreSelectEntity As Object, ByRef DoHighlight As Boolean, _
		ByRef MorePreSelectEntities As ObjectCollection, ByVal SelectionDevice As SelectionDeviceEnum, _
		ByVal ModelPosition As Point, ByVal ViewPosition As Inventor.Point2d, _
		ByVal View As Inventor.View) Handles oSelectEvents.OnPreselect
		Try
			Dim oCurves As DrawingCurvesEnumerator = DirectCast(PreSelectEntity, DrawingCurveSegment) _
			.Parent.Parent.DrawingCurves(DirectCast(PreSelectEntity, DrawingCurveSegment) _
			.Parent.ModelGeometry.Parent.Parent)
			Dim oCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
			For Each oCurve As DrawingCurve In oCurves
				For Each oSeg As DrawingCurveSegment In oCurve.Segments
					If oSeg IsNot PreSelectEntity Then oCol.Add(oSeg)
				Next
			Next
			MorePreSelectEntities = oCol
			DoHighlight = True
		Catch
			DoHighlight = False
		End Try

	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 Inventor.View) Handles oSelectEvents.OnSelect
		SelectedOcc = DirectCast(JustSelectedEntities.Item(1), DrawingCurveSegment) _
		.Parent.ModelGeometry.Parent.Parent
		ThisApplication.CommandManager.StopActiveCommand
	End Sub

End Class

 

 

 

 


this allows to be explained the different assemblies that are linked to that part. If you can make this easier please do. I dont know how. 

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

Post to forums  

Autodesk Design & Make Report