Model explosion

Model explosion

rbertoletti
Enthusiast Enthusiast
214 Views
2 Replies
Message 1 of 3

Model explosion

rbertoletti
Enthusiast
Enthusiast

I've a code for explode an assembly but actually all the Patterns are deleted. I'd like to not delete the patterns but if I remove the delete command the rule keep working in loop.

 

 

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

How can i fix it?

 

0 Likes
215 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @rbertoletti.  I do not know yet if I can help with this task, because I am still not sure I understand your intent here.  What exactly do you mean by "explosion".  It looks like you are trying to eliminate all components in your assembly that represent an assembly, but somehow still keep the sub components of those assemblies within the main assembly.  Does that sound accurate?  If not please explain in more detail about what you are trying to do, and also explaining why you are trying to do that sometimes helps us understand better.  As far as patterns are concerned, we can check if a component is being used in a pattern by checking if its ComponentOccurrence.IsPatternElement property is True, and can get that pattern element object itself using the similar ComponentOccurrence.PatternElement property, which can help lead us to its parent OccurrencePattern.  Those can be complicated to deal with, and can be nested inside of other patterns, so they can also be difficult to navigate.  We do have the ability to make individual pattern elements 'Independent ', but only the elements the pattern creates, not the input elements used to create the pattern.

 

Edit:  By the way, I do not know if you are familiar with the term 'Promote', but that sounds similar to what you are trying to achieve here.  And below is a link to another forum post discussing this topic that you might find interesting/useful.

https://forums.autodesk.com/t5/inventor-forum/promote-components-without-affecting-the-subassemblies... 

WCrihfield_0-1701876748630.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

Andrii_Humeniuk
Advisor
Advisor

Hi @rbertoletti . Try this code, but check that patterns are not related to subassemblies.

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As AssemblyDocument = oInvApp.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 = oInvApp.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 oOccPatt As OccurrencePattern In oOcc.Definition.OccurrencePatterns
		    Dim oSourceNode As BrowserNode
		    oSourceNode = oPane.GetBrowserNodeFromObject(oOccPatt)
		   Try : Call oPane.Reorder(oTargetNode, True, oSourceNode): Catch : End Try		
		Next
		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
	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

 

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