This is a difficult question.
First of all, there is no way to select multiple documents that you want to export. Here I solved this by opening each file. if you don't select a face. (press escape) then it will skip the part.
Maybe a bigger problem there is no API for exporting faces. There is a workaround. You can execute commands as you clicked on them. this is unstable but might work for you. This is what the rule below does. (I also left the other 2 ways of exporting documents in the code for educational purposes)
Sub Main()
Dim doc As AssemblyDocument = ThisDoc.Document
Dim bom As BOM = doc.ComponentDefinition.BOM
bom.PartsOnlyViewEnabled = True
Dim bomView As BOMView = bom.BOMViews.Item("Parts Only")
For Each row As BOMRow In bomView.BOMRows
Try
Dim refDoc As Document = row.ComponentOccurrences.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
If (refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then
Continue For
End If
Dim openDoc = ThisApplication.Documents.Open(refDoc.FullFileName)
Dim newFileName As String = createFileName(openDoc, row.TotalQuantity)
If (openDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
Dim defSheetMetal As SheetMetalComponentDefinition = openDoc.ComponentDefinition
If defSheetMetal.HasFlatPattern = False Then
defSheetMetal.Unfold()
Else
defSheetMetal.FlatPattern.Edit()
End If
exportFace(openDoc, newFileName)
defSheetMetal.FlatPattern.ExitEdit()
Else
exportFace(openDoc, newFileName)
End If
openDoc.Close()
Catch ex As Exception
Try
MsgBox("Problem with file: " &
row.ComponentOccurrences.Item(1)._DisplayName &
"Message: " & ex.Message)
Catch
End Try
End Try
Next
End Sub
Function createFileName(doc As PartDocument, quantity As String) As String
Dim dirName As String = IO.Path.GetDirectoryName(doc.FullFileName)
Dim fileNameWithoutExtension As String = IO.Path.GetFileNameWithoutExtension(doc.FullFileName)
Dim propSet As PropertySet = doc.PropertySets.Item("Inventor User Defined Properties")
Dim MATERIAL_SIFRA = GetPropValue(propSet, "MATERIAL_SIFRA")
Dim DEB = GetPropValue(propSet, "DEB")
Return String.Format("{0}\{1}_{2}_{3}mm_{4}x.dxf",
dirName, fileNameWithoutExtension, MATERIAL_SIFRA, DEB, quantity)
End Function
Function GetPropValue(propSet As PropertySet, name As String) As String
Try
Return propSet.Item(name).Value
Catch ex As Exception
Return "~Unknown~"
End Try
End Function
Sub exportFace(doc As PartDocument, newFileName As String)
Dim face As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select a face to export. Esc to skip")
If face Is Nothing Then Return
' Many options but does not work for faces
'Dim def As PartComponentDefinition = doc.ComponentDefinition
'Dim sketch = def.Sketches.Add(face, True)
'Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2010&InvisibleLayers=BendUpLayer;BendDownLayer"
'sOut = ""
'sketch.DataIO.WriteDataToFile(sOut, newFileName)
'sketch.Delete()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Many options but in a seperate file. Does not work with faces
'Dim DXFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
'oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
' Dim strIniFile = "C:\temp\DxfExport.ini" 'this is a pre-configed ini file
' oOptions.Value("Export_Acad_IniFile") = strIniFile
' oDataMedium.FileName = newFileName
' DXFAddIn.SaveCopyAs(face, oContext, oOptions, oDataMedium)
'End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' no options posible but works for faces.
Dim oCtrlDef As ButtonDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
doc.SelectSet.Select(Face)
ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, newFileName)
oCtrlDef.Execute()
End Sub
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog: hjalte.nl - github.com