Thank you, @johnsonshiue , for your reply. How exactly can this be setup? and does it work with ilogic code, that I have provided in the question?
'check that the active document is an assembly file
'--> first check if the is an assembly before you declare it to an AssemblyDocument
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If
'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
'--> oAsmDoc.DisplayName can show the extension depending on windows settings.
'--> i guess that you try to remove the extensin here.
'--> if you do it in this way your colega, with different windows settings, will have an unexpexted resilt.
'Dim oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) - 4)
Dim oAsmName = oAsmDoc.DisplayName.Replace(".iam", "")
'get user input
Dim RUsure = MessageBox.Show(
"This will create a DXF file for all of the asembly components that are sheet metal." _
& vbLf & "This rule expects that the part file is saved." _
& vbLf & " " _
& vbLf & "Are you sure you want to create DXF for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic - Batch Output DXFs ", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Return
' Else <-- this is not needed.
End If
Dim oPath = ThisDoc.Path
'--> it seems that you use oDataMedium only for the file path.
'--> you might want to have a look at the k.i.s.s. design principl
'--> https://en.wikipedia.org/wiki/KISS_principle
'--> i think you wont need it.
'Dim oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
'--> oContext is declared her but is never used...
'Dim oContext = ThisApplication.TransientObjects.CreateTranslationContext
'oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'--> oOptions is declared her but is never used...
'Dim oOptions = ThisApplication.TransientObjects.CreateNameValueMap
'get DXF target folder path
Dim oFolder = oPath & "\" & oAsmName & " DXF Files"
'Check for the DXF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If
'- - - - - - - - - - - - -'- - - - - - - - - - - - -Component - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
'work the the drawing files for the referenced models'this expects that the model has been saved
For Each oRefDoc As Document In oRefDocs
'--> this seem to replase the extension of the filename. Then you try to check with this
'--> if it is saved (and probaly if its an part and not an assembly)
'--> you can better use the Document.Dirty property. this will be false if the file is not saved.
'--> also if the file exists but was not saved.
'--> for checking if its a part we can use the Document.DocumentType.
'--> last i did not see if you checked if it is realy a sheet metal part.
'--> we can do that with the Document.SubType
'Dim iptPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "ipt"
'check that model is saved
'If (System.IO.File.Exists(iptPathName)) Then
'--> check if its a part:
If (oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
Continue For '--> this will make the program go to the next oRefDoc
End If
'--> check if its a Sheetmetal part:
If (oRefDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
Continue For '--> this will make the program go to the next oRefDoc
End If
'--> check if its saved:
If (oRefDoc.Dirty = True) Then
'--> TODO ask the user if he/she wants to save?
'--> oRefDoc.Save()
Continue For '--> this will make the program go to the next oRefDoc
End If
Dim oDrawDoc As PartDocument = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
Dim oFileName = oRefDoc.DisplayName.Replace(".ipt", "")
Dim oCompDef As SheetMetalComponentDefinition = oDrawDoc.ComponentDefinition
Try
'Set the DXF target file name
'--> you need to declare the CustomName before the try/cath or you will only be able to use its value in try/cath statement
'--> i also declared the other values that you asked for. (with there default values so i dont have to do it in the cath block)
Dim customPropertiesSet = oRefDoc.PropertySets.Item("Inventor User Defined Properties")
Dim customName As String = "XXX"
Try
customName = customPropertiesSet.Item("PF_PRT_ZNR").Value
Catch
'--> TODO create a msgbox to want the user that the name can be starnge
'customName = "XXX" 'Wert, wenn iPropertie PF_PRT_ZNR nicht existiert
End Try
Dim designTrackingPropertiesSet = oRefDoc.PropertySets.Item("Design Tracking Properties")
Dim description As String = designTrackingPropertiesSet.Item("Description").Value
Dim partnumber As String = designTrackingPropertiesSet.Item("Part Number").Value
Dim StockNumber As String = designTrackingPropertiesSet.Item("Stock Number").Value
Dim iQTY As String = customPropertiesSet.Item("iQTY").Value
' Dim Material As String = oCompDef.ActiveSheetMetalStyle.Material.Name
Dim Mat2 As String = designTrackingPropertiesSet.Item("Material").Value
'oDataMedium.FileName = oFolder & "\" & CustomName & " " & oFileName & ".dxf"
Dim newFileName As String =
oFolder & "\" & partnumber & " " & Mat2 & " " & StockNumber & " " & iQTY & "vnt.dxf"
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold()
Else
oCompDef.FlatPattern.Edit()
End If
Dim sOut As String
'sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=0&OuterProfileLayerColor=0;0;0&InteriorProfilesLayer=0&InteriorProfilesLayerColor=0;0;0&InvisibleLayers=IV_ARC_CENTERS;IV_TANGENT;IV_ROLL;IV_ROLL_TANGENT;IV_ALTREP_BACK;IV_ALTREP_FRONT;IV_FEATURE_PROFILES_DOWN;IV_FEATURE_PROFILES;IV_TOOL_CENTER_DOWN;IV_BEND;IV_BEND_DOWN"
sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=0&OuterProfileLayerColor=0;0;0&InteriorProfilesLayer=0&InteriorProfilesLayerColor=0;0;0&InvisibleLayers=IV_ARC_CENTERS;IV_TANGENT;IV_ROLL;IV_ROLL_TANGENT;IV_ALTREP_BACK;IV_ALTREP_FRONT;IV_FEATURE_PROFILES_DOWN;IV_FEATURE_PROFILES;IV_TOOL_CENTER_DOWN;" _
+ "IV_BEND;IV_BEND_DOWN"
'+ "IV_BEND=1;IV_BEND_DOWN=1"
oCompDef.DataIO.WriteDataToFile(sOut, newFileName)
'oCompDef.DataIO.WriteDataToFile(sOut, oDataMedium.FileName)
oCompDef.FlatPattern.ExitEdit()
Catch
End Try
oDrawDoc.Close()
'Else
'End If
Next
Full code bellow