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: 

referencing all occurences of specific part in assembly

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
op_thorsager
301 Views, 3 Replies

referencing all occurences of specific part in assembly

Hi, i'm trying to reference the same part file multiple times using a "for each" loop. this is my code

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccName As String = "ExCross"
Dim oOcc As ComponentOccurrence = oADoc.ComponentDefinition.Occurrences.ItemByName(oOccName)

For Each oOccPart As ComponentOccurrence In oOcc.OccurrencePath
'	Dim refDoc As PartDocument = oOcc.Definition.Document

If InsDepthBuild >= 21 Then
	CrossDepthBuild = 12
	CF = CrossDepthBuild
	
	oOcc.Visible = True
'	Component.Visible("ExCross") = True
'	Component.Visible("ExCross:1") = True
Else If InsDepthBuild <= 21 Then
	CrossDepthBuild = InsDepthBuild
	CF = CrossDepthBuild
	oOcc.Visible = False
	
End If	
Next

 i want oOcc to reference all occurences of my part called "ExCross" instead of having to write in every instance of said file (i.e. ExCross:1, ExCross:2....) since this specific part is driven by a pattern, i thought it would be easier to just reference the actual part file, then have it count every instance of said part(no matter the trailing instance number) then have it turn invisible if set value is lower than 21, and have it turn visible again if set value is higher than 21. 

 

am i approaching this wrong, or am i forgetting something? as the code is right now it will only show/hide one instance of the part instead of all of them.

3 REPLIES 3
Message 2 of 4
WCrihfield
in reply to: op_thorsager

Hi @op_thorsager.  There are a couple/few ways of doing what I think you are trying to do.  One way is to loop through all components, while checking if the Name of the component 'StartsWith' the name you are specifying.  Below is a quick example of that route.

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
For Each oOcc As ComponentOccurrence In oOccs
	If oOcc.Name.StartsWith("ExCross") Then
		'do something with oOcc object here
	End If
Next

Another way to get all of the components that reference a specific document is to use the following lines of code...keeping in mind how I am defining the 'oOccs' variable above.

Dim oPDoc As Document = ThisApplication.Documents.ItemByName("PartFullDocumentName")
Dim oAllPartRefOccs = oOccs.AllReferencedOccurrences(oPDoc)

However, I am not sure about the other variables you are using in your code, such as 'CrossDepthBuild' & 'InsDepthBuild'.  If those are 'local' parameters of the main assembly, that might be OK, but if those are supposed to be representing parameter values in the referenced part, that would be a problem.  If they do not represent local parameters, then I do not know where their Types or Values have been set.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4
op_thorsager
in reply to: WCrihfield

Thank you! will have to check the code tomorrow, but since it you i'm almost 100% certain it works as i want it to. the InsDepth build and the other variables are local parameters in the main assembly, which changes other parameters within some parts related to the for each loop. they work as intended, and so far i've had no issues with them 🙂

Message 4 of 4
op_thorsager
in reply to: WCrihfield

everything works as expected, after tweaking when the code you posted does specific things, to ensure that the parameters which was in the original code would work with the for each. the one you sent would delete all component occurences of the entire assembly for some reason (im guessing it was due to the parameters) i patched it up and got it working as intended with the following code:

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences


For Each oOcc As ComponentOccurrence In oOccs
If InsDepthBuild >= 22 Then
	If oOcc.Name.StartsWith("ExCross") Then
			oOcc.Visible = True
		End If
	CrossDepthBuild = 12
	CF = CrossDepthBuild
	'adjusts depth of visible/invisible component to flush with insdepthbuild 
	ExCrossDepth = InsDepthBuild - CrossDepthBuild
	Else If InsDepthBuild <= 21 Then
			If oOcc.Name.StartsWith("ExCross") Then
					oOcc.Visible = False
				end if
	CrossDepthBuild = InsDepthBuild
	CF = CrossDepthBuild

	End If
Next

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

Post to forums  

Autodesk Design & Make Report