Convert Solid Parts Assembly to Sheet Metal Parts in Preparation for Nesting

Convert Solid Parts Assembly to Sheet Metal Parts in Preparation for Nesting

andrewringler
Explorer Explorer
2,953 Views
4 Replies
Message 1 of 5

Convert Solid Parts Assembly to Sheet Metal Parts in Preparation for Nesting

andrewringler
Explorer
Explorer

We have a model we created in Fusion containing 2000+ individual solid parts. All the parts are different shapes, but they all have the same thickness, 1.2cm. Our final build contains 8 different materials. We are going to cut all of the parts out of sheets (8 different material types), all 1.2cm thick. Our plan is to send our model into Autodesk TruNest which will efficiently nest all of the parts onto the correct material and add etching labels to each part so that we can find them for assembly.

 

TruNest accepts Inventor files as input, and expects them to be sheet metal flat parts (I think). We exported our Fusion file to Inventor (using Autodesk Cloud). Then the following script is meant convert each part from a solid part to a sheet metal part, ready to be nested by TruNest. It uses a naming convention on the part name to assign each part to a material (since the material is lost exporting to Inventor from Fusion).

 

I am still having an issue with the script. The line oSheetMetalCompDef.Unfold or perhaps the line oSheetMetalCompDef.FlatPattern.ExitEdit causes Inventor to crash. Perhaps if Inventor can't fold, then ExitEdit crashes? I am hard-coding the thickness settings, I am thinking that maybe if I calculate the thickness of the part like fredrik.carlsson did in his rule, based on the distance between planes that maybe that would be more robust against rounding errors? Is it typical for Inventor to crash doing something in the API, I guess the Try/Catch blocks don't prevent crashing…?

 

Any thoughts on the crashing or our workflow would be appreciated. Thanks. Andrew

 

' ConvertToSheetMetal
' an iLogic rule that converts parts matching a naming scheme 1w21-09a-a:1 
' to Sheet Metal, Unfolds them and prepares for layout in Autodesk TruNest or Autodesk Nesting Utility
'
' Use case: we have an assembly of 2000+ solid parts of uniform thickness created in Fusion that we actually
' want to layout and cut from sheets. So, we export from Fusion to Inventor using Autodesk Cloud. Then we
' run this script, which re-assigns a material to each part, converts it to sheet metal, then unfolds read
' to be packed onto sheets with a nesting program.
'
' This is an Inventor Rule | AKA iLogic Rule
' To run this rule, goto Manage > iLogic Browser
' double click iam main assembly (in the rule window) to expand children
' click plus icon or right click in iLogic Browser and then select add New Rule
' give the rule a name, paste this file into the window, click Save and Run
' after the rule is run you might need to double click on any part to get Inventor to display updates
'
' References:
' loop through all parts of an assembly https://www.cadlinecommunity.co.uk/hc/en-us/articles/202782862-Inventor-2016-ILogic-to-update-parameters-in-every-part-of-an-Assembly
' various ways of getting on parts of the model https://forums.autodesk.com/t5/inventor-forum/ilogic-assembly-component-occurrence-definition/td-p/3639162
'
'
'
'
'
Class ThisRule

Dim updatedParts As Integer = 0
Dim updated_a As Integer = 0
Dim updated_b As Integer = 0
Dim updated_c As Integer = 0
Dim updated_d As Integer = 0
Dim updated_e As Integer = 0
Dim updated_f As Integer = 0
Dim updated_g As Integer = 0
Dim updated_h As Integer = 0
Dim errors As Integer = 0

Dim oAsmDoc As AssemblyDocument 

' This thickness seems to be in cm
Const SHEET_METAL_THICKNESS As Double = 1.2

Sub Main()
	oAsmDoc = ThisApplication.ActiveDocument  
	Call Iterate(oAsmDoc.ComponentDefinition.Occurrences, 1)
	
	' updated the parts, is this required?
	oAsmDoc.Update

	' All done, give some feedback
	MsgBox("Updated " & updatedParts & " parts To sheet metal:" & vbLf & updated_a & " 'a' parts" & vbLf & updated_b & " 'b' parts" & vbLf & updated_c & " 'c' parts" & vbLf & updated_d & " 'd' parts" & vbLf & updated_e & " 'e' parts" & vbLf & updated_f & " 'f' parts" & vbLf & updated_g & " 'g' parts" & vbLf & updated_h & " 'h' parts" & vbLf & vbLf & errors & " Errors")
End Sub 

Private Sub Iterate(Occurrences As ComponentOccurrences, Level As Integer) 
	'Iterate through Assembly
	Dim oOcc As ComponentOccurrence 
	For Each oOcc In Occurrences 
		
		'Find Parts in the Assembly
		Dim CadlinePart As String
		CadlinePart = oOcc.Name

		Try 
			' Look for parts of the form: 1w21-09a-a:1 (the :1 is automatically added by Autodesk Fusion)
			If CadlinePart.Length = 12
				Dim materialChar As Char
				materialChar = CadlinePart.Chars(9)
				
		    ' Grab this part's document
		    Dim invPart As Document
  			invPart = oOcc.Definition.Document
				
				' Assign this part a material based on the part name
				'https://forums.autodesk.com/t5/Inventor-customization/material-change-Using-Inventor-api/td-p/4319281
				Dim componentDefinition As ComponentDefinition
				componentDefinition = oOcc.Definition

				' Actual materials don't matter since we'll remap them to different materials in the nesting program
				Select Case materialChar
				Case "a"c
					componentDefinition.Material.Name = "ABS Plastic"
					updated_a += 1
				Case "b"c
					componentDefinition.Material.Name = "Acetal Resin, Black"
					updated_b += 1
				Case "c"c
					componentDefinition.Material.Name = "Acetal Resin, White"
					updated_c += 1
				Case "d"c
					componentDefinition.Material.Name = "Aluminum 6061"
					updated_d += 1
				Case "e"c
					componentDefinition.Material.Name = "Aluminum 6061-AHC"
					updated_e += 1
				Case "f"c
					componentDefinition.Material.Name = "Aluminum 6061, Welded"
					updated_f += 1
				Case "g"c
					componentDefinition.Material.Name = "Brass, Soft Yellow"
					updated_g += 1
				Case "h"c
					componentDefinition.Material.Name = "Brass, Soft Yellow, Welded"
					updated_h += 1
				End Select				
				
				' Change to a Sheet Metal Subtype
				' https://forums.autodesk.com/t5/inventor-forum/batch-convert-regular-parts-to-sheet-metal-parts/td-p/6477451
				Try
					invPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"

					' Grab ComponentDefinition as SheetMetalComponentDefinition, now that we have changed its type
					Dim oSheetMetalCompDef As SheetMetalComponentDefinition
					oSheetMetalCompDef = invPart.ComponentDefinition
					
					' Override the thickness for the document, otherwise we can't actually set the thickness
					' https://forums.autodesk.com/t5/inventor-forum/sheet-metal-controlled-through-ilogic/td-p/3795810
					oSheetMetalCompDef.UseSheetMetalStyleThickness = False
					
					' Change the thickness of this sheet metal part to match the thickness of the part
					' this is so when we ask Inventor to Create a Flat Pattern / Unpack it just grabs a single face
					' IE we don't want to unfold anything we are just cutting a solid part of uniform thickness out of a sheet
					Dim oThicknessParam As Parameter
					oThicknessParam = oSheetMetalCompDef.Thickness
					oThicknessParam.Value = SHEET_METAL_THICKNESS
					
					Trace.WriteLine("Inventor: about to unfold part=" &CadlinePart, "error")
					Try
						' Create Flat Pattern AKA Unfold
						If (Not oSheetMetalCompDef.HasFlatPattern) Then
						  ' the following commands are crashing Inventor
    					'oSheetMetalCompDef.Unfold
							'oSheetMetalCompDef.FlatPattern.ExitEdit
							
							' Do I need any of these?
							'invPart.Save()
							'invPart.Close()
							'oAsmDoc.Activate()
	    				End If					
					Catch
						Trace.WriteLine("Inventor: unable to unfold part for part=" &CadlinePart, "error")
						errors += 1
					End Try
					
					updatedParts += 1
				Catch
					Trace.WriteLine("Inventor: unable to change part to sheet metal for part=" &CadlinePart, "error")
					errors += 1
				End Try
								
				'Trace.WriteLine("Inventor: " & CadlinePart & " material=" & materialChar & ", " & componentDefinition.Material.Name)
				'Write iProps to Parts, could be usefull to add comments to part to record what we did to it
				'iProperties.Value(CadlinePart, "Summary", "Comments") = "Hello World"
			
			End If
		Catch
			Trace.WriteLine("Inventor: unknown error for part=" &CadlinePart, "error")
			errors += 1
		End Try
		        
		'Run through the sub assemblies 
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call Iterate(oOcc.SubOccurrences, Level + 1) 
		End If 
	Next	
End Sub

End Class

 

0 Likes
Accepted solutions (1)
2,954 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor

I didn't look in detail at your code because I suspect none of it is necessary.  I don't know for sure but I would be very surprised if TruNest will only work on sheet metal flat patterns.  There are plenty of other parts that people would want to next that aren't sheet metal.  On the front page of the product description it says, "Work with composites, wood, plastics, glass, sheet metal, cloth, and leather."  There may be limitations with the initial Inventor integration but it's worth asking before you do a lot of work that's not needed. 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 5

andrewringler
Explorer
Explorer

 

Brian,

 

> but it's worth asking before you do a lot of work that's not needed

Yes, we did ask a TruNest expert, who said importing from Inventor must be a flat sheet metal part. Perhaps there are different workflows someone in the forum could recommend, but this is the path recommended to us.

 

>TruNest…works with composites, wood, plastics, glass, sheet metal, cloth, and leather

It is my understanding that the term “sheet metal part” within Inventor is a catch-all term for any part of constant thickness which you want to cut out of any sheet-like material, plastic, cloth, glass, etc… It is my understanding that modeling as a “flat pattern sheet metal part” is the best/only way to create parts destined for cutting out of sheets of any material type when using Inventor.

 

0 Likes
Message 4 of 5

BrianEkins
Mentor
Mentor

It looks like you're correct about the new nesting functionality requiring a sheet metal part.  They're taking advantage of the flat pattern of the model and the fact that it's always in a known orientation.  I can understand why they would want to do that but it's too bad because not everything is logically a sheet metal part.

 

Anyway, I did a quick test in VBA and found the following to work.  It can be easily converted to VB.NET, (which is what iLogic uses).  If the thickness of the Fusion parts is correct, then hard-coding the thickness when setting it in Inventor should be ok.  The main difference in mine is how it's handling the part document.  I think there is some weird behavior with the active document and this seems to work around that.

 

Public Sub CreateSMTest()
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
    Dim occ As ComponentOccurrence
    For Each occ In asmDoc.ComponentDefinition.Occurrences
        Dim partDoc As PartDocument
        Set partDoc = occ.Definition.Document
        
        ' Make the part document visible.
        partDoc.Views.Add
        
        ' Convert it to a sheet metal type.
        partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
        
        Dim smDef As SheetMetalComponentDefinition
        Set smDef = partDoc.ComponentDefinition
        
        ' Unfold the part.  This will activate the flat pattern environment.
        smDef.Unfold
        
        ' Exit the flat pattern environment.
        smDef.FlatPattern.ExitEdit
        
        ' Close and save the document.  This will close the view but it will
        ' actually remain open because the assembly is still referencing it.
        Call partDoc.Close(True)
    Next
End Sub
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 5

andrewringler
Explorer
Explorer
Accepted solution

OK. Perfect, thanks Brian! I integrated your changes and now the script is working. It converts parts to flat pattern sheet metal ready for nesting. Thickness is hardcoded.

 

' ConvertToSheetMetal
' an iLogic rule that converts parts matching a naming scheme 1w21-09a-a:1 
' to Sheet Metal, Unfolds them and prepares for layout in Autodesk TruNest or Autodesk Nesting Utility
'
' Use case: we have an assembly of 2000+ solid parts of uniform thickness created in Fusion that we actually
' want to layout and cut from sheets. So, we export from Fusion to Inventor using Autodesk Cloud. Then we
' run this script, which re-assigns a material to each part, converts it to sheet metal, then unfolds read
' to be packed onto sheets with a nesting program.
'
' This is an Inventor Rule | AKA iLogic Rule
' To run this rule, goto Manage > iLogic Browser
' double click iam main assembly (in the rule window) to expand children
' click plus icon or right click in iLogic Browser and then select add New Rule
' give the rule a name, paste this file into the window, click Save and Run
' after the rule is run you might need to double click on any part to get Inventor to display updates
'
' References:
' loop through all parts of an assembly https://www.cadlinecommunity.co.uk/hc/en-us/articles/202782862-Inventor-2016-ILogic-to-update-parameters-in-every-part-of-an-Assembly
' various ways of getting access to parts of the model https://forums.autodesk.com/t5/inventor-forum/ilogic-assembly-component-occurrence-definition/td-p/3639162
'
' Written by Andrew Ringler public@andrewringler.com, https://andrewringler.com/ 2018
' for New American Public Art, http://www.newamericanpublicart.com/kempelens-owls
'
' This code is licensed under Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
'
Class ThisRule

    Dim updatedParts As Integer = 0
    Dim updated_a As Integer = 0
    Dim updated_b As Integer = 0
    Dim updated_c As Integer = 0
    Dim updated_d As Integer = 0
    Dim updated_e As Integer = 0
    Dim updated_f As Integer = 0
    Dim updated_g As Integer = 0
    Dim updated_h As Integer = 0
    Dim errors As Integer = 0

    Dim oAsmDoc As AssemblyDocument

    ' This thickness seems to be in cm
    Const SHEET_METAL_THICKNESS As Double = 1.2
	
	' Main gets run when we Run the rule
    Sub Main()
        oAsmDoc = ThisApplication.ActiveDocument
        Call Iterate(oAsmDoc.ComponentDefinition.Occurrences, 1)

        ' updated the parts, is this required?
        oAsmDoc.Update

        ' All done, give some feedback
        MsgBox("Updated " & updatedParts & " parts To sheet metal:" & vbLf & updated_a & " 'a' parts" & vbLf & updated_b & " 'b' parts" & vbLf & updated_c & " 'c' parts" & vbLf & updated_d & " 'd' parts" & vbLf & updated_e & " 'e' parts" & vbLf & updated_f & " 'f' parts" & vbLf & updated_g & " 'g' parts" & vbLf & updated_h & " 'h' parts" & vbLf & vbLf & errors & " Errors")
    End Sub

	' Loop through all components in Assembly
    Private Sub Iterate(Occurrences As ComponentOccurrences, Level As Integer)
        'Iterate through Assembly
        Dim oOcc As ComponentOccurrence
        For Each oOcc In Occurrences

            'Find Parts in the Assembly
            Dim CadlinePart As String
            CadlinePart = oOcc.Name

            Try
                ' Look for parts of the form: 1w21-09a-a:1 (the :1 is automatically added by Autodesk Fusion)
                If CadlinePart.Length = 12 Then
					' Pull off 9th character, which is the material IE 1w21-09a-a:1 would be a
                    Dim materialChar As Char
                    materialChar = CadlinePart.Chars(9)

                    ' Grab this part's document
                    Dim invPart As PartDocument
                    invPart = oOcc.Definition.Document

                    ' Various updates to visible document, thanks to Brian Ekins
                    'https://forums.autodesk.com/t5/inventor-customization/convert-solid-parts-assembly-to-sheet-metal-parts-in-preparation/td-p/8013539

                    ' Make the part document visible.
                    invPart.Views.Add

                    ' Assign this part a material based on the part name
                    'https://forums.autodesk.com/t5/Inventor-customization/material-change-Using-Inventor-api/td-p/4319281
                    Dim componentDefinition As ComponentDefinition
                    componentDefinition = oOcc.Definition

                    ' Actual materials don't matter since we'll remap them to different materials in the nesting program
                    Select Case materialChar
                        Case "a"c
							' here we have grabbed the first material listed in the Inventor GUI
                            componentDefinition.Material.Name = "ABS Plastic"
							' track how many of material a we have updated, so this 
							' can appear in the summary message at the end
                            updated_a += 1
                        Case "b"c
							' here we have grabbed the second material listed in the Inventor GUI
                            componentDefinition.Material.Name = "Acetal Resin, Black"
                            updated_b += 1
                        Case "c"c
                            componentDefinition.Material.Name = "Acetal Resin, White"
                            updated_c += 1
                        Case "d"c
                            componentDefinition.Material.Name = "Aluminum 6061"
                            updated_d += 1
                        Case "e"c
                            componentDefinition.Material.Name = "Aluminum 6061-AHC"
                            updated_e += 1
                        Case "f"c
                            componentDefinition.Material.Name = "Aluminum 6061, Welded"
                            updated_f += 1
                        Case "g"c
                            componentDefinition.Material.Name = "Brass, Soft Yellow"
                            updated_g += 1
                        Case "h"c
                            componentDefinition.Material.Name = "Brass, Soft Yellow, Welded"
                            updated_h += 1
                    End Select

                    Try
	                    ' Change to a Sheet Metal Subtype
	                    ' https://forums.autodesk.com/t5/inventor-forum/batch-convert-regular-parts-to-sheet-metal-parts/td-p/6477451
                        invPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"

                        ' Grab ComponentDefinition as SheetMetalComponentDefinition, now that we have changed its type
                        Dim oSheetMetalCompDef As SheetMetalComponentDefinition
                        oSheetMetalCompDef = invPart.ComponentDefinition

                        ' Override the thickness for the document, otherwise we can't actually set the thickness
                        ' https://forums.autodesk.com/t5/inventor-forum/sheet-metal-controlled-through-ilogic/td-p/3795810
                        oSheetMetalCompDef.UseSheetMetalStyleThickness = False

                        ' Change the thickness of this sheet metal part to match the thickness of the part
                        ' this is so when we ask Inventor to Create a Flat Pattern / Unpack it just grabs a single face
                        ' IE we don't want to unfold anything we are just cutting a solid part of uniform thickness out of a sheet
                        Dim oThicknessParam As Parameter
                        oThicknessParam = oSheetMetalCompDef.Thickness
                        oThicknessParam.Value = SHEET_METAL_THICKNESS

						' Traces show up in DebugView
						' Google Windows DebugView to download this free Windows Program to display these logs
                        Trace.WriteLine("Inventor: about to unfold part=" & CadlinePart)
                        Try
                            ' Create Flat Pattern AKA Unfold
                            If (Not oSheetMetalCompDef.HasFlatPattern) Then
                                ' Unfold the part.  This will activate the flat pattern environment.
                                oSheetMetalCompDef.Unfold

                                ' Exit the flat pattern environment.
                                oSheetMetalCompDef.FlatPattern.ExitEdit

                                ' Close and save the document.  This will close the view but it will
                                ' actually remain open because the assembly is still referencing it.
                                Call invPart.Close(True)
                            End If
                        Catch e As Exception
                            Trace.WriteLine("Inventor: unable to unfold part for part=" & CadlinePart, e.Message)
                            errors += 1
                        End Try

                        updatedParts += 1
                    Catch e As Exception
                        Trace.WriteLine("Inventor: unable to change part to sheet metal for part=" & CadlinePart, e.Message)
                        errors += 1
                    End Try

                    'Trace.WriteLine("Inventor: " & CadlinePart & " material=" & materialChar & ", " & componentDefinition.Material.Name)
                    'Write iProps to Parts, could be usefull to add comments to part to record what we did to it
                    'iProperties.Value(CadlinePart, "Summary", "Comments") = "Hello World"

                End If
            Catch e As Exception
                Trace.WriteLine("Inventor: unknown error for part=" & CadlinePart, e.Message)
                errors += 1
            End Try

            'Run through the sub assemblies 
            If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
                Call Iterate(oOcc.SubOccurrences, Level + 1)
            End If
        Next
    End Sub

End Class