OK. Then, are you saying that you are not exporting a 'true' FlatPattern object, but just a flat part from the 'folded model' mode, that simply does not have any bends or flanges in it yet? It took me a while to create an alternate code example that you can review, but when I typed this up, I had a regular FlatPattern object in mind, not just a regular part with no bends or folds in it yet.
Sub Main
oInvApp = ThisApplication
sPrimaryMSName = oInvApp.LanguageTools.CurrentPrimaryModelStateString
Dim oDoc As Inventor.Document = ThisDoc.Document
Dim sMainDWG As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".dwg")
ExportFlatPatternToDWG(oDoc, sMainDWG)
For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
Dim sRefDWG As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".dwg")
ExportFlatPatternToDWG(oRefDoc, sRefDWG)
Next oRefDoc
End Sub
Dim oInvApp As Inventor.Application
Dim sPrimaryMSName As String
Sub ExportFlatPatternToDWG(oDoc As Inventor.Document, sDWG_FullFileName As String)
If (oDoc Is Nothing) OrElse (Not TypeOf oDoc Is PartDocument) Then Return
Dim oPDoc As PartDocument = oDoc
If (Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition) Then Return
Dim sMS_Name As String = oPDoc.ModelStateName
If (Not String.IsNullOrEmpty(sMS_Name)) AndAlso (sMS_Name <> sPrimaryMSName) Then
Dim sFDN As String = oInvApp.FileManager.GetFullDocumentName(oPDoc.FullFileName, sPrimaryMSName)
oPDoc = oInvApp.Documents.Open(sFDN, False)
End If
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
Dim oFP As FlatPattern = Nothing
If oSMDef.HasFlatPattern Then
oFP = oSMDef.FlatPattern
Else
Try
oSMDef.Unfold
oFP = oSMDef.FlatPattern
oFP.ExitEdit
Catch
End Try
End If
If oFP Is Nothing Then Return
'<<< CHANGE THIS FORMAT STRING AS NEEDED >>>
Dim sFormat As String = "FLAT PATTERN DWG?AcadVersion=2004&Outerprofilelayer=0&Interiorprofileslayer=0&BendLayer=6&BendDownLayer=6&InvisibleLayers=IV_TANGENT;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_BEND;IV_BEND_DOWN;IV_FEATURE_PROFILES_DOWN&MergeProfilesIntoPolyline=TRUE&SimplifySplines=TRUE&REBASEGEOMETRY=TRUE"
'&BendLayer=SCRIBE&BendDownLayer=SCRIBE
Try
oFP.DataIO.WriteDataToFile(sFormat, sDWG_FullFileName)
Logger.Error("Following DWG Was Created:" & vbCrLf & oPDoc.sDWG_FullFileName)
Catch
Logger.Error("Error Exporting Following Part To DWG!" _
& vbCrLf & oPDoc.FullDocumentName)
End Try
End Sub
But if we are just exporting the regular part, we can use a slightly simpler version of practically the same code.
Sub Main
oInvApp = ThisApplication
sPrimaryMSName = oInvApp.LanguageTools.CurrentPrimaryModelStateString
Dim oDoc As Inventor.Document = ThisDoc.Document
Dim sMainDWG As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".dwg")
ExportFlatPatternToDWG(oDoc, sMainDWG)
For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
Dim sRefDWG As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".dwg")
ExportFlatPatternToDWG(oRefDoc, sRefDWG)
Next oRefDoc
End Sub
Dim oInvApp As Inventor.Application
Dim sPrimaryMSName As String
Sub ExportFlatPatternToDWG(oDoc As Inventor.Document, sDWG_FullFileName As String)
If (oDoc Is Nothing) OrElse (Not TypeOf oDoc Is PartDocument) Then Return
Dim oPDoc As PartDocument = oDoc
If (Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition) Then Return
Dim sMS_Name As String = oPDoc.ModelStateName
If (Not String.IsNullOrEmpty(sMS_Name)) AndAlso (sMS_Name <> sPrimaryMSName) Then
Dim sFDN As String = oInvApp.FileManager.GetFullDocumentName(oPDoc.FullFileName, sPrimaryMSName)
oPDoc = oInvApp.Documents.Open(sFDN, False)
End If
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
'<<< CHANGE THIS FORMAT STRING AS NEEDED >>>
Dim sFormat As String = "FLAT PATTERN DWG?AcadVersion=2004&Outerprofilelayer=0&Interiorprofileslayer=0&BendLayer=6&BendDownLayer=6&InvisibleLayers=IV_TANGENT;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_BEND;IV_BEND_DOWN;IV_FEATURE_PROFILES_DOWN&MergeProfilesIntoPolyline=TRUE&SimplifySplines=TRUE&REBASEGEOMETRY=TRUE"
'&BendLayer=SCRIBE&BendDownLayer=SCRIBE
Try
oSMDef.DataIO.WriteDataToFile(sFormat, sDWG_FullFileName)
Logger.Error("Following DWG Was Created:" & vbCrLf & oPDoc.sDWG_FullFileName)
Catch
Logger.Error("Error Exporting Following Part To DWG!" _
& vbCrLf & oPDoc.FullDocumentName)
End Try
End Sub
Both of these examples can be ran from any type of document. If the current document is a sheet metal part, it will attempt to export it to DWG. If it is an assembly or drawing, those usually have referenced documents, and it will iterate through those, and attempt to export any sheet metal parts it finds to DWG. I copied the 'Format' String from your attached code, so I hope that is OK. It is just saving those DWG files in the same location as the part file, and with the same name as the part file, so you may have to change that, if needed. But this is just example code for review and testing. You can change it however you want, or copy parts of it out into your other code, if you want.
Wesley Crihfield

(Not an Autodesk Employee)