Copy sheet metal parts to new assembly

Copy sheet metal parts to new assembly

augustin_roegiersTXZKR
Explorer Explorer
577 Views
5 Replies
Message 1 of 6

Copy sheet metal parts to new assembly

augustin_roegiersTXZKR
Explorer
Explorer

Hi,

 

At my company we have a category in the BOM called 'Cost Center' which can take the values of 'Turning', 'Milling' or 'Sheet Metal'. I would like to only copy the parts which are designated as 'Sheet Metal' to a new assembly file (or even better, to directly export this new assembly as a step file). I would llike to do this using an Ilogic rule or a VBA macro.

 

I am a total novice on programming in VBA, however I do have some experience in Python and Java. I already tried my luck with chatgpt, which failed. 

 

Now my question is: would it be doable to implement this and which are some resources I can base myself on?

Are there build in functions to do this which maybe I am missing?

 

Thanks in advance for your guidance!

 

Kind regards,  

0 Likes
Accepted solutions (1)
578 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @augustin_roegiersTXZKR.  The process you are asking about does sound possible to do by code, but also sounds a bit odd.  There is a 'standard' iProperty called "Cost Center", and we can see it on the 'Project' tab of the iProperties dialog, just above 'Estimated Cost'.  Is that the same property that you are using to determine which parts are for 'Turning', 'Milling', & 'Sheet Metal'?  I assume that an assembly will be open when you would start this type of process, then the code will look down through all levels of the assembly, checking each part's iProperty value for "Sheet Metal", and if found, try to copy it to a new assembly.  If you have the same sheet metal part in the original assembly 3 times, do you want it added to the 'new' assembly 3 times, or just once.  I do not understand the purpose of the other new assembly that you want to copy them to, or why you would want to export it that way (no constraints, just a bunch of models dropped into it at origin location).  Maybe explaining the purpose of this will make it easier to understand, and clarify the design intent of the code solution.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

augustin_roegiersTXZKR
Explorer
Explorer

Hi @WCrihfield!

 

Thank you for your reply! Yes that is indeed the iProperty I'm referring to. We use an external supplier which has automated software to create sheet metal parts, that is why indeed I want a new assembly with only the sheet metal parts and preferrably also the correct amount (so like you suggested 3 times in the active assembly implies 3 copies in the sheet metal assembly file). Just dropping them at the origin is okay. 

 

In any case thank you for your help already!

 

Kind regards,

Augustin 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @augustin_roegiersTXZKR.  I created an iLogic rule that you may be able to use for this task, but I have not tested it out yet.  One detail I am not sure about yet, is if the 'new' assembly needs to be saved to disk 'before' we can export it to a STEP file.  I did leave some code in there for saving the new assembly document, just in case that step is necessary.  And I also left some code in there for closing the new assembly, and deleting its file after exporting it as a STEP file, just in case that is something you may need/want to do also, but I left those lines of code commented out for now, to be safe.  Do not be intimidated by the code being broken up into multiple separate routines.  This is partially necessary for a truly 'recursive' process (a process that can call itself to run again, to step down another level deeper).  And it also helps keep the code processes modular (copyable and usable in other situations).

 

I left several comments in the code, but if you have questions about some of this stuff, feel free to ask them.  And of course be cautious with any new code like this, and try it out on test files before trying it on production files to start with, just to be safe, because I have not tested the final code myself yet.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	If oOccs.Count = 0 Then Return 'if there were no components, then exit rule (nothing to do)
		
	'specify the assembly template file to use when creating the new assembly
	Dim sAsmTemplate As String = "C:\Temp\MyAssemblyTemplate.iam"
	'create the new assembly document here
	Dim oNewAsmDoc As AssemblyDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, sAsmTemplate, False)
	'set the value of the 'shared' variable for the components collection of this new assembly
	oNewOccs = oNewAsmDoc.ComponentDefinition.Occurrences
	
	'run our custom recursive Sub routine here, which will process all levels of components in the original assembly
	RecurseComponents(oOccs, AddressOf ProcessComponent)
	
	'formulate full path and file name of the new STEP file we want to create
	Dim sPath As String = System.IO.Path.GetDirectoryName(oADoc.FullFileName) 'no directory separator character at end
	Dim sDirSep As Char = System.IO.Path.DirectorySeparatorChar
	Dim sName As String = System.IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)
	Dim sSTEP_FullFileName As String = sPath & sDirSep & sName & " - Sheet Metal Parts.stp"
	
	'the new assembly may need to be 'saved' before it can be exported, not sure
	Dim sNewAssemblyFile As String = sPath & sDirSep & sName & " - Sheet Metal Parts.iam"
	oNewAsmDoc.SaveAs(sNewAssemblyFile, False)
	
	'call custom Sub routine to export the 'New' assembly as STEP file
	ExportAsSTEP(oNewAsmDoc, sSTEP_FullFileName)
	'we could now close the new assembly, and delete the new assembly file
	'oNewAsmDoc.Close(True) 'True = skip save
	'System.IO.File.Delete(sNewAssemblyFile) 'to delete the new
	MsgBox("This rule's work has finished.", vbInformation, "Job Is Done!")
End Sub

'variable declared between other routines, so that it will be 'shared' by other routines
Dim oNewOccs As ComponentOccurrences

Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
	If oComps Is Nothing OrElse oComps.Count = 0 Then Return
	For Each oComp As ComponentOccurrence In oComps
		ComponentProcess(oComp)
		If oComp.Suppressed = False AndAlso _
			oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			RecurseComponents(oComp.Definition.Occurrences, ComponentProcess)
		End If
	Next
End Sub

Sub ProcessComponent(oComp As ComponentOccurrence)
	If oComp Is Nothing OrElse oComp.Suppressed Then Return
	'check if this component represents a sheet metal part, if not, return to calling routine
	If Not TypeOf oComp.Definition Is SheetMetalComponentDefinition Then Return
	Dim oCompDoc As Document = oComp.Definition.Document
	'check the 'Cost Center' iProperty value
	If oCompDoc.PropertySets.Item(3).Item(4).Value = "Sheet Metal" Then
		Dim sFDN As String = oComp.ReferencedDocumentDescriptor.FullDocumentName
		Dim oPos As Inventor.Matrix = ThisApplication.TransientGeometry.CreateMatrix
		Dim oNewOcc As ComponentOccurrence = Nothing
		Try : oNewOccs.Add(sFDN, oPos)
		Catch : Logger.Error("Error adding new component for:" & vbCrLf & oFDN & vbCrLf & "to new assembly.")
		End Try
	End If
End Sub

Sub ExportAsSTEP(oDoc As Inventor.Document, sNewFullFileName As String)
	Dim sSTEP_ClientID As String = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}"
	Dim oSTEP As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById(sSTEP_ClientID)
	'create needed variables for translator
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oContext As TranslationContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	Dim oOptions As NameValueMap = oTO.CreateNameValueMap
	Dim oDataMedium As DataMedium = oTO.CreateDataMedium
	'check if this file already exists, if so, let user know, and ask about overwriting it
	If System.IO.File.Exists(sNewFullFileName) Then
		Dim oAns As MsgBoxResult = MsgBox("A STEP file with this name already exists." & vbCrLf &
		 sNewFullFileName & vbCrLf & _
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "STEP FILE EXISTS")
		If oAns = MsgBoxResult.No Then Exit Sub
	End If
	'proceed to use the supplied new full file name, if passed that test
	oDataMedium.FileName = sNewFullFileName
	'set options for export now
	If oSTEP.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
		' Set application protocol.
		 ' 2 = AP 203 - Configuration Controlled Design
		 ' 3 = AP 214 - Automotive Design
		 oOptions.Value("ApplicationProtocolType") = 3
		 oOptions.Value("IncludeSketches") = True
		 oOptions.Value("export_fit_tolerance") = .000393701  'minimum
		 oOptions.Value("Author") = ThisApplication.GeneralOptions.UserName
		 'oOptions.Value("Authorization") = ""
		'oOptions.Value("Description") = oDoc.PropertySets.Item(3).Item(14).Value 'Description iProperty
		'oOptions.Value("Organization") = oDoc.PropertySets.Item(2).Item(3).Value 'Company iProperty
		Try
			 oSTEP.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
		Catch
			Logger.Error("Error exporting to following STEP file:" & vbCrLf & sNewFullFileName)
		End Try
	End If
End Sub

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

augustin_roegiersTXZKR
Explorer
Explorer

Hi @WCrihfield,

 

Thank you! This code does exactly what I was looking for! Thank you for the extensive comments in the code, it really helped me to understand how it works!

 

Kind regards,

Augustin

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Hi @augustin_roegiersTXZKR.  Glad I was able to help.

I noticed a small detail on Line 63 in the code I posted that you may want to correct, but it is not a big deal.  In that line, shown below:

Catch : Logger.Error("Error adding new component for:" & vbCrLf & oFDN & vbCrLf & "to new assembly.")

...I am using the wrong variable (oFDN).  That should be "sFDN", not "oFDN".  The sFDN variable is what I declared in Line 59, and is what I am using in Line 62, but I must have changed it at some point, and forgot to change it in that one place where the previous spelling was used.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes