Delete unused derived component using iLogic

Delete unused derived component using iLogic

pdauphin
Enthusiast Enthusiast
435 Views
3 Replies
Message 1 of 4

Delete unused derived component using iLogic

pdauphin
Enthusiast
Enthusiast

Hello all,

 

Here is what I'm trying to do: I want to be able, using an iLogic rule, to delete the unused derived components in a part. Different "objects" are derived in different derived components. Sometimes it's parameters, blocks or sketches that for some reason are not used anymore in the part modeling. I would like to be able to "clean" the ipt file from these unnecessary links.

 

I figured out how to delete the derived components using the following code. The problem is that it deletes everything, all the derived components are deleted, even if they are used by something in the ipt file. There is a simple way of doing it manually by right clicking in the browser on the derived component and Inventor will list in the menu the "delete" command only if the component is not in use. I'm unable to find a property somewhere that flags whether or not it is "safe" to delete de derived component.

 

Untitled-1 copy.pngUntitled-2 copy.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Any help appreciated!

 

Thanks!

 

Dim Doc As PartDocument = ThisDoc.Document
Dim DerivedComps As DerivedPartComponents = Doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents
For Each DerivedComp As DerivedPartComponent In DerivedComps
	Try
		DerivedComp.Delete
	Catch
	End Try
Next

 

0 Likes
436 Views
3 Replies
Replies (3)
Message 2 of 4

lmc.engineering
Advocate
Advocate

I'm not sure there is a shorter way of achieving this, however, this is my method of handling derived components:

Sub Main()
Dim doc As Document = ThisApplication.ActiveDocument
If TypeOf doc Is PartDocument Then
	ClearDerived(doc)
End If
End Sub

Private Sub ClearDerived(doc As PartDocument)
	If doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Count < 1 Then
		Exit Sub
	End If

	Dim DComps As DerivedPartComponents = doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents

	For Each DComp As DerivedPartComponent In DComps
		Dim DPECount As Integer = 0
		Dim count As Integer = 0
		Dim DPDef As DerivedPartDefinition = DComp.Definition

		'SOLIDS
		Dim DSolids As DerivedPartEntities = DPDef.Solids
		Analyse(DSolids, DPECount)

		'SURFACES
		Dim DSurf As DerivedPartEntities = DPDef.Surfaces
		Analyse(DSurf, DPECount)

		'SketchBlocks
		Dim DSB As DerivedPartEntities = DPDef.SketchBlocks
		Purge(DSB, DPECount)

		'Sketches
		Dim DSketch As DerivedPartEntities = DPDef.Sketches
		Purge(DSketch, DPECount)

		'Sketches3D
		Dim DSketch3D As DerivedPartEntities = DPDef.Sketches3D
		Purge(DSketch3D, DPECount)

		'WorkFeatures
		Dim DWF As DerivedPartEntities = DPDef.WorkFeatures
		Purge(DWF, DPECount)

		'iMateDefinitions
		Dim DiMates As DerivedPartEntities = DPDef.iMateDefinitions
		Purge(DiMates, DPECount)

		'PARAMETERS
		Dim DParams As DerivedPartEntities = DPDef.Parameters
		Purge(DParams, DPECount)

		DComp.Definition = DPDef

		If DPECount = 0 Then
			DComp.Delete()
		End If
	Next
End Sub

Private Sub Analyse(entityObj As Object, ByRef DPECount As Integer)
	Dim DPE As DerivedPartEntity
	For Each DPE In entityObj
		Logger.Debug(DPE.ReferencedEntity.name)
		If DPE.IncludeEntity Then
			DPECount += 1
		End If
	Next
End Sub

Private Sub Purge(entityObj As Object, ByRef DPECount As Integer)
	Dim DPE As DerivedPartEntity
	For Each DPE In entityObj
		If DPE.IncludeEntity = True Then
			Try
				DPE.IncludeEntity = False
				If DPE.IncludeEntity = False Then
					Logger.Debug(DPE.ReferencedEntity.name)
				Else
					DPECount += 1
				End If
			Catch ex As Exception
				DPECount += 1
			End Try
		End If
	Next
End Sub

The Analyse Sub is checking if an entity is included, and if it is, increments the count to ensure it doesn't get deleted. The 'Purge' Sub clears out all unused entities from the derived part. However, you should be aware that if you are using a derived entity downstream, either deriving the current part in to further parts, or a workplane from the derive within the part has a constraint on it in an assembly, then this will not pick up on that relationship and will remove the entity regardless. If you do not want to purge the derived parts, then simply change all references of the 'Purge' sub to 'Analyse'. Trust that makes sense.

Regards

0 Likes
Message 3 of 4

pdauphin
Enthusiast
Enthusiast

Hello,

 

Thanks for the reply!

 

No wonder I was not able to figure it out. This is way more complicated than I expected.

 

I'll find the time to test tour solution in my models and I'll get back with the results.

 

Thank you so much!

0 Likes
Message 4 of 4

pdauphin
Enthusiast
Enthusiast

Hello,

 

So, I finally was able to spare some time and try the code you shared. Again, thank you! It works for most of the derived components that need to be deleted. The rule effectively deletes the derived components when no parameters are needed from them, seems to be the same thing for the solid bodies.

 

But for some reason, it looks like it's not picking up on the derived components where the only things included in the derive operation are sketch blocks. I'm pretty sure that a little tweak somewhere in the code will solve this.

 

Starting with what you shared, I should be able to figure it out.

 

Thanks again!

 

 

0 Likes