Explode all sub-assemblies

Explode all sub-assemblies

rbertoletti
Enthusiast Enthusiast
1,163 Views
8 Replies
Message 1 of 9

Explode all sub-assemblies

rbertoletti
Enthusiast
Enthusiast

Hi guys,

i'm looking for a rule able to explode all sub assemblies in order to have only single parts in the "main assembly".

 

Thanks in advance.

 

0 Likes
Accepted solutions (1)
1,164 Views
8 Replies
Replies (8)
Message 2 of 9

Andrii_Humeniuk
Advisor
Advisor

Hi @rbertoletti . Please try this code. But it does not process patterns.

Sub main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
	Dim oPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oOccs As ComponentOccurrences = oDef.Occurrences
	ReturnStep :
	For Each oOcc As ComponentOccurrence In oOccs
		Dim oRefDoc As Document = oOcc.Definition.Document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject _
			Or Not oRefDoc.IsModifiable Then Continue For
		For Each oSubOcc As ComponentOccurrence In oOcc.SubOccurrences
		    Dim oSourceNode As BrowserNode
		    oSourceNode = oPane.GetBrowserNodeFromObject(oSubOcc)
		    Try : Call oPane.Reorder(oTargetNode, True, oSourceNode) : Catch : End Try
		Next
		oOcc.Delete()
	Next
	If HasSubAsm(oOccs) Then GoTo ReturnStep
End Sub

Private Function HasSubAsm(ByVal oOccs As ComponentOccurrences) As Boolean
	For i As Integer = 1 To oOccs.Count
		If oOccs(i).DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Return True
	Next i
	Return False
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 9

rbertoletti
Enthusiast
Enthusiast
I tested it, but seems that the code delete the sub-assemblies and not explode.
0 Likes
Message 4 of 9

Andrii_Humeniuk
Advisor
Advisor

Sorry, I made one small mistake. Try this code:

Sub main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
	Dim oPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oOccs As ComponentOccurrences = oDef.Occurrences
	ReturnStep :
	For Each oOcc As ComponentOccurrence In oOccs
		Dim oRefDoc As Document = oOcc.Definition.Document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject _
			Or Not oRefDoc.IsModifiable Then Continue For
		Dim oTargetNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)
		For Each oSubOcc As ComponentOccurrence In oOcc.SubOccurrences
		    Dim oSourceNode As BrowserNode
		    oSourceNode = oPane.GetBrowserNodeFromObject(oSubOcc)
		    Try : Call oPane.Reorder(oTargetNode, True, oSourceNode) : Catch : End Try
		Next
		oOcc.Delete()
	Next
	If HasSubAsm(oOccs) Then GoTo ReturnStep
End Sub

Private Function HasSubAsm(ByVal oOccs As ComponentOccurrences) As Boolean
	For i As Integer = 1 To oOccs.Count
		If oOccs(i).DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Return True
	Next i
	Return False
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 9

rbertoletti
Enthusiast
Enthusiast
Ok, now it works great! I've also found that if i have some components which has the same name the duplicated are deleted.
In order to avoid this is it possible to rename all the components inside the sub-assemblies by adding as prefix the sub-assembly name?
In this case i think no one of them will be deleted.

Thanks
0 Likes
Message 6 of 9

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Try this code:

 

Sub main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
	Dim oPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oOccs As ComponentOccurrences = oDef.Occurrences
	Dim oTM As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "Explode Assembly")
	ReturnStep :
	For Each oOcc As ComponentOccurrence In oOccs
		Dim oRefDoc As Document = oOcc.Definition.Document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject _
			Or Not oRefDoc.IsModifiable Then Continue For
		Dim oTargetNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)
		For Each oSubOcc As ComponentOccurrence In oOcc.SubOccurrences
			If oSubOcc.DefinitionDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
				Try : oSubOcc.Name = oOcc.Name & " - " & oSubOcc.Name :	Catch : End Try
			End If
		    Dim oSourceNode As BrowserNode
		    oSourceNode = oPane.GetBrowserNodeFromObject(oSubOcc)
		    Try : Call oPane.Reorder(oTargetNode, True, oSourceNode) : Catch : End Try
		Next
		oOcc.Delete()
	Next
	If HasSubAsm(oOccs) Then GoTo ReturnStep
	oTM.End()
End Sub

Private Function HasSubAsm(ByVal oOccs As ComponentOccurrences) As Boolean
	For i As Integer = 1 To oOccs.Count
		If oOccs(i).DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Return True
	Next i
	Return False
End Function

 

The new name of the component is written in line 15.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 7 of 9

dipranjan
Observer
Observer

please give code for reassembly of all explode sub assembly.

0 Likes
Message 8 of 9

chris
Advisor
Advisor

What is the use case for this?

0 Likes
Message 9 of 9

dipranjan
Observer
Observer

Dear Sir, 

I have huge assemblies. I want to explode the all-sub assembly. And again, assemble all assembly to show the clients as different part.

0 Likes