Save all sheetmetal parts from assembly to DXF

Save all sheetmetal parts from assembly to DXF

Anonymous
Not applicable
3,806 Views
15 Replies
Message 1 of 16

Save all sheetmetal parts from assembly to DXF

Anonymous
Not applicable

Hello all,

 

I found a code to Save all sheetmetal parts from assembly to DXF. I make some changes to apear below the drawing parts, Material and thickness. But I whant to put the QTY also? Can I help me to make the necessary changes in the code?

 

Thanks in advance 

 

Code:

 

Sub Main
'iLogic Code by Jhoel Forshav - originally posted at https://clintbrown.co.uk/ilogic-export-all-flat-patterns-to-one-dxf
'Check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("This rule can only run from an Assembly file", "DXF-creator", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'Dim the active document as AssemblyDocument
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
'Make sure the assembly is saved
If oDoc.FullFileName = ""
MessageBox.Show("Please save the Assembly before running this rule.", "DXF-creator", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
'Get the assembly filename without extension
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
'Get the assembly filepath
Dim oPath As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

'Get the parts only BOM.
Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
'Make sure Parts Only is activated
oBOM.PartsOnlyViewEnabled = True
'Parts only will be last BomView (difficult to get by name since it's different depending on your language)
Dim oBOMview As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)


'Set a reference to the TransientGeometry object
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
'oX and oY will be used to create points for view placement
Dim oX As Double = 0
Dim oY As Double = 0
'Create the Baseview options to place flatpattern-views
Dim oBaseViewOptions As NameValueMap
oBaseViewOptions = ThisApplication.TransientObjects.CreateNameValueMap
oBaseViewOptions.Add("SheetMetalFoldedModel", False)
'Set a variable for the drawing document
Dim oDrawing As DrawingDocument
'Create a String to return a message if any SM-parts are not saved
Dim unsavedSmParts As String = ""

Dim i As Integer = 1
Dim oInfo As String = ""

'Traverse Parts Only BOM
For Each oRow As BOMRow In oBOMview.BOMRows
Try
'Get the component definition for the part
Dim oDef As ComponentDefinition = oRow.ComponentDefinitions(1)
'Check if the part is SheetMetal
If TypeOf (oDef) Is SheetMetalComponentDefinition
'Set a reference to the partdocument
Dim smPartDoc As PartDocument = oDef.Document
'Check if the part is saved
If smPartDoc.FullFileName = "" Then
If unsavedSmParts = "" Then unsavedSmParts = "The fallowing SM-documents were not saved and therefore " & _
"no drawingviews were created:" & vbCrLf
unsavedSmParts = unsavedSmParts & vbCrLf & oDef.Document.DisplayName
Continue For
End If
'Create flatpattern if it doesn't already exist
If Not oDef.HasFlatPattern
oDef.Unfold()
oDef.FlatPattern.ExitEdit()
End If
'Create the drawing if it doesn't already exist
If oDrawing Is Nothing
oDrawing = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, _
, False)
'Set the drawings length units to the same as the assemblys length units
oDrawing.UnitsOfMeasure.LengthUnits = oDoc.UnitsOfMeasure.LengthUnits
End If

'Set a reference to the drawing sheet
Dim oSheet As Sheet = oDrawing.ActiveSheet


'Create the flatpattern view
Dim oView As DrawingView = oSheet.DrawingViews.AddBaseView(smPartDoc, oTG.CreatePoint2d(oX, oY), 1 _
, ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, _
"FlatPattern", , oBaseViewOptions)

oView.Name = smPartDoc.DisplayName & vbNewLine & "Espessura: " & smPartDoc.ComponentDefinition.Thickness.Expression _
& vbNewLine & "Material: " & smPartDoc.PropertySets("Design Tracking Properties")("Material").Value
oView.ShowLabel = True

'Set the position with our oX and oY
oView.Position = oTG.CreatePoint2d(oView.Position.X + oView.Width / 2, oView.Position.Y)
'Move oX to place the next view to the right of this one
oX = oView.Left + oView.Width + 1
'Remove the bend lines of the view
RemoveBendLines(oView, oDef.FlatPattern)'You could comment out this line to keep bend lines


oInfo = oInfo & "Here" & If (i = 1, "", vbCrLf) & i & ". " & smPartDoc.PropertySets.Item("Design Tracking Properties"). _
Item("Part Number").Value

i += 1

'Close the part
smPartDoc.Close(True)
End If
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
Next
If oDrawing IsNot Nothing
'Create the save location string for the DXF
Dim oDXFName As String = oPath & "\" & oAsmName & "_FlatPatterns.dxf"
'Save the DXF
oINI = "C:\Users\LF\Desktop\Configuracoes Inventor LF\config Inventor\exportdxf.ini" 'Specify your INI file location here (eg C:\TEMP\DXF Export.ini)
If oINI = "" Then
MessageBox.Show("You need to specify an INI file location in the code - Look for oINI and set the path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
SaveDXF(oDrawing, oDXFName, oINI)
'Create the save location string for the information txt
Dim oInfoName As String = oPath & "\" & oAsmName & "_FlatPatterns.txt"
'Create the txt
CreateTXT(oInfo, oInfoName)
End If
'Close the drawing
oDrawing.Close

 

'return information about any unsaved parts
If unsavedSmParts <> "" Then _
MessageBox.Show(unsavedSmParts, "Some parts were not saved", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
'Update the assembly (could be dirty if any flatpatterns were created)
oDoc.Update
End Sub

Sub SaveDXF(oDrawing As DrawingDocument, oFileName As String, oIniFile As String)
'Set a reference to the DFX translator
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Create translation context
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'Create options for the translation
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'Create a DataMedium object
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Set the options (which .ini-file to use)
If DXFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
oOptions.Value("Export_Acad_IniFile") = oIniFile
End If
'Set the filename property of the DataMedium object
oDataMedium.FileName = oFileName
Try
'Try to save the DXF
DXFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
MessageBox.Show("Dxf saved to: " & oFileName, "DXF SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch
MessageBox.Show("Couldn't save dxf!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Sub RemoveBendLines(oView As DrawingView, oFlattPattern As FlatPattern)
'Get all the bend edges from the FlatPattern
Dim oBendEdgesUp As Edges = oFlattPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendUpFlatPatternEdge)
Dim oBendEdgesDown As Edges = oFlattPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendDownFlatPatternEdge)

For Each oEdge As Edge In oBendEdgesUp
'Get the curves representing these edges in the drawing view
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
'Set visibility to false
oSegment.Visible = False
Next
Next
Next
For Each oEdge As Edge In oBendEdgesDown
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
oSegment.Visible = False
Next
Next
Next
End Sub

Sub CreateTXT(oText As String, oFileName As String)
Dim oTxtWriter As System.IO.StreamWriter = System.IO.File.CreateText(oFileName)
oTxtWriter.WriteLine(oText)
oTxtWriter.Close()
End Sub

 

0 Likes
Accepted solutions (2)
3,807 Views
15 Replies
Replies (15)
Message 2 of 16

Anonymous
Not applicable

.

0 Likes
Message 3 of 16

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Anonymous 

I'm glad you found my code 😄

Try this to get the Qty as well.

Sub Main
'iLogic Code by Jhoel Forshav - originally posted at https://clintbrown.co.uk/ilogic-export-all-flat-patterns-to-one-dxf
'Check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("This rule can only run from an Assembly file", "DXF-creator", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'Dim the active document as AssemblyDocument
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
'Make sure the assembly is saved
If oDoc.FullFileName = ""
MessageBox.Show("Please save the Assembly before running this rule.", "DXF-creator", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
'Get the assembly filename without extension
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
'Get the assembly filepath
Dim oPath As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

'Get the parts only BOM.
Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
'Make sure Parts Only is activated
oBOM.PartsOnlyViewEnabled = True
'Parts only will be last BomView (difficult to get by name since it's different depending on your language)
Dim oBOMview As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)


'Set a reference to the TransientGeometry object
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
'oX and oY will be used to create points for view placement
Dim oX As Double = 0
Dim oY As Double = 0
'Create the Baseview options to place flatpattern-views
Dim oBaseViewOptions As NameValueMap
oBaseViewOptions = ThisApplication.TransientObjects.CreateNameValueMap
oBaseViewOptions.Add("SheetMetalFoldedModel", False)
'Set a variable for the drawing document
Dim oDrawing As DrawingDocument
'Create a String to return a message if any SM-parts are not saved
Dim unsavedSmParts As String = ""

Dim i As Integer = 1
Dim oInfo As String = ""

'Traverse Parts Only BOM
For Each oRow As BOMRow In oBOMview.BOMRows
Try
'Get the component definition for the part
Dim oDef As ComponentDefinition = oRow.ComponentDefinitions(1)
'Check if the part is SheetMetal
If TypeOf (oDef) Is SheetMetalComponentDefinition
'Set a reference to the partdocument
Dim smPartDoc As PartDocument = oDef.Document
'Check if the part is saved
If smPartDoc.FullFileName = "" Then
If unsavedSmParts = "" Then unsavedSmParts = "The fallowing SM-documents were not saved and therefore " & _
"no drawingviews were created:" & vbCrLf
unsavedSmParts = unsavedSmParts & vbCrLf & oDef.Document.DisplayName
Continue For
End If
'Create flatpattern if it doesn't already exist
If Not oDef.HasFlatPattern
oDef.Unfold()
oDef.FlatPattern.ExitEdit()
End If
'Create the drawing if it doesn't already exist
If oDrawing Is Nothing
oDrawing = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, _
, False)
'Set the drawings length units to the same as the assemblys length units
oDrawing.UnitsOfMeasure.LengthUnits = oDoc.UnitsOfMeasure.LengthUnits
End If

'Set a reference to the drawing sheet
Dim oSheet As Sheet = oDrawing.ActiveSheet


'Create the flatpattern view
Dim oView As DrawingView = oSheet.DrawingViews.AddBaseView(smPartDoc, oTG.CreatePoint2d(oX, oY), 1 _
, ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, _
"FlatPattern", , oBaseViewOptions)

oView.Name = smPartDoc.DisplayName & vbNewLine & "Espessura: " & smPartDoc.ComponentDefinition.Thickness.Expression _
& vbNewLine & "Material: " & smPartDoc.PropertySets("Design Tracking Properties")("Material").Value & _
vbCrLf & "Quantity: " & oDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(smPartDoc).Count
oView.ShowLabel = True

'Set the position with our oX and oY
oView.Position = oTG.CreatePoint2d(oView.Position.X + oView.Width / 2, oView.Position.Y)
'Move oX to place the next view to the right of this one
oX = oView.Left + oView.Width + 1
'Remove the bend lines of the view
RemoveBendLines(oView, oDef.FlatPattern)'You could comment out this line to keep bend lines


oInfo = oInfo & "Here" & If (i = 1, "", vbCrLf) & i & ". " & smPartDoc.PropertySets.Item("Design Tracking Properties"). _
Item("Part Number").Value

i += 1

'Close the part
smPartDoc.Close(True)
End If
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
Next
If oDrawing IsNot Nothing
'Create the save location string for the DXF
Dim oDXFName As String = oPath & "\" & oAsmName & "_FlatPatterns.dxf"
'Save the DXF
oINI = "C:\Users\LF\Desktop\Configuracoes Inventor LF\config Inventor\exportdxf.ini" 'Specify your INI file location here (eg C:\TEMP\DXF Export.ini)

If oINI = "" Then
MessageBox.Show("You need to specify an INI file location in the code - Look for oINI and set the path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
SaveDXF(oDrawing, oDXFName, oINI)
'Create the save location string for the information txt
Dim oInfoName As String = oPath & "\" & oAsmName & "_FlatPatterns.txt"
'Create the txt
CreateTXT(oInfo, oInfoName)
End If
'Close the drawing
oDrawing.Close

 

'return information about any unsaved parts
If unsavedSmParts <> "" Then _
MessageBox.Show(unsavedSmParts, "Some parts were not saved", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
'Update the assembly (could be dirty if any flatpatterns were created)
oDoc.Update
End Sub

Sub SaveDXF(oDrawing As DrawingDocument, oFileName As String, oIniFile As String)
'Set a reference to the DFX translator
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Create translation context
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'Create options for the translation
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'Create a DataMedium object
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Set the options (which .ini-file to use)
If DXFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
oOptions.Value("Export_Acad_IniFile") = oIniFile
End If
'Set the filename property of the DataMedium object
oDataMedium.FileName = oFileName
Try
'Try to save the DXF
DXFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
MessageBox.Show("Dxf saved to: " & oFileName, "DXF SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch
MessageBox.Show("Couldn't save dxf!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Sub RemoveBendLines(oView As DrawingView, oFlattPattern As FlatPattern)
'Get all the bend edges from the FlatPattern
Dim oBendEdgesUp As Edges = oFlattPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendUpFlatPatternEdge)
Dim oBendEdgesDown As Edges = oFlattPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendDownFlatPatternEdge)

For Each oEdge As Edge In oBendEdgesUp
'Get the curves representing these edges in the drawing view
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
'Set visibility to false
oSegment.Visible = False
Next
Next
Next
For Each oEdge As Edge In oBendEdgesDown
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
oSegment.Visible = False
Next
Next
Next
End Sub

Sub CreateTXT(oText As String, oFileName As String)
Dim oTxtWriter As System.IO.StreamWriter = System.IO.File.CreateText(oFileName)
oTxtWriter.WriteLine(oText)
oTxtWriter.Close()
End Sub
Message 4 of 16

Anonymous
Not applicable

First of all, thanks for your contribution and your site, help me a lot.

The code works perfectly.

 
 

Thanks, one more time.

Message 5 of 16

Anonymous
Not applicable

Thanks @JhoelForshav   can the rule write that quantity material and thickness  in the txt file

0 Likes
Message 6 of 16

JhoelForshav
Mentor
Mentor
Accepted solution

Sure, @Anonymous 

This should do it 🙂

Sub Main
'iLogic Code by Jhoel Forshav - originally posted at https://clintbrown.co.uk/ilogic-export-all-flat-patterns-to-one-dxf
'Check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("This rule can only run from an Assembly file", "DXF-creator", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'Dim the active document as AssemblyDocument
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
'Make sure the assembly is saved
If oDoc.FullFileName = ""
MessageBox.Show("Please save the Assembly before running this rule.", "DXF-creator", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
'Get the assembly filename without extension
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
'Get the assembly filepath
Dim oPath As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

'Get the parts only BOM.
Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
'Make sure Parts Only is activated
oBOM.PartsOnlyViewEnabled = True
'Parts only will be last BomView (difficult to get by name since it's different depending on your language)
Dim oBOMview As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)


'Set a reference to the TransientGeometry object
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
'oX and oY will be used to create points for view placement
Dim oX As Double = 0
Dim oY As Double = 0
'Create the Baseview options to place flatpattern-views
Dim oBaseViewOptions As NameValueMap
oBaseViewOptions = ThisApplication.TransientObjects.CreateNameValueMap
oBaseViewOptions.Add("SheetMetalFoldedModel", False)
'Set a variable for the drawing document
Dim oDrawing As DrawingDocument
'Create a String to return a message if any SM-parts are not saved
Dim unsavedSmParts As String = ""

Dim i As Integer = 1
Dim oInfo As String = ""

'Traverse Parts Only BOM
For Each oRow As BOMRow In oBOMview.BOMRows
Try
'Get the component definition for the part
Dim oDef As ComponentDefinition = oRow.ComponentDefinitions(1)
'Check if the part is SheetMetal
If TypeOf (oDef) Is SheetMetalComponentDefinition
'Set a reference to the partdocument
Dim smPartDoc As PartDocument = oDef.Document
'Check if the part is saved
If smPartDoc.FullFileName = "" Then
If unsavedSmParts = "" Then unsavedSmParts = "The fallowing SM-documents were not saved and therefore " & _
"no drawingviews were created:" & vbCrLf
unsavedSmParts = unsavedSmParts & vbCrLf & oDef.Document.DisplayName
Continue For
End If
'Create flatpattern if it doesn't already exist
If Not oDef.HasFlatPattern
oDef.Unfold()
oDef.FlatPattern.ExitEdit()
End If
'Create the drawing if it doesn't already exist
If oDrawing Is Nothing
oDrawing = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, _
, False)
'Set the drawings length units to the same as the assemblys length units
oDrawing.UnitsOfMeasure.LengthUnits = oDoc.UnitsOfMeasure.LengthUnits
End If

'Set a reference to the drawing sheet
Dim oSheet As Sheet = oDrawing.ActiveSheet


'Create the flatpattern view
Dim oView As DrawingView = oSheet.DrawingViews.AddBaseView(smPartDoc, oTG.CreatePoint2d(oX, oY), 1 _
, ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, _
"FlatPattern", , oBaseViewOptions)

oView.Name = smPartDoc.DisplayName & vbNewLine & "Thickness: " & smPartDoc.ComponentDefinition.Thickness.Expression _
& vbNewLine & "Material: " & smPartDoc.PropertySets("Design Tracking Properties")("Material").Value & _
vbCrLf & "Quantity: " & oDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(smPartDoc).Count
oView.ShowLabel = True

'Set the position with our oX and oY
oView.Position = oTG.CreatePoint2d(oView.Position.X + oView.Width / 2, oView.Position.Y)
'Move oX to place the next view to the right of this one
oX = oView.Left + oView.Width + 1
'Remove the bend lines of the view
RemoveBendLines(oView, oDef.FlatPattern)'You could comment out this line to keep bend lines


oInfo = oInfo & If (i = 1, "", vbCrLf) & i & ". " & smPartDoc.PropertySets.Item("Design Tracking Properties"). _
Item("Part Number").Value & " - Thickness: " & oDef.Thickness.Expression & " - Material: " _
& smPartDoc.PropertySets("Design Tracking Properties")("Material").Value & " - Quantity: " & _
oDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(smPartDoc).Count

i += 1

'Close the part
smPartDoc.Close(True)
End If
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
Next
If oDrawing IsNot Nothing
'Create the save location string for the DXF
Dim oDXFName As String = oPath & "\" & oAsmName & "_FlatPatterns.dxf"
'Save the DXF
oINI = "C:\TEMP\INI.ini" 'Specify your INI file location here (eg C:\TEMP\DXF Export.ini)

If oINI = "" Then
MessageBox.Show("You need to specify an INI file location in the code - Look for oINI and set the path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
SaveDXF(oDrawing, oDXFName, oINI)
'Create the save location string for the information txt
Dim oInfoName As String = oPath & "\" & oAsmName & "_FlatPatterns.txt"
'Create the txt
CreateTXT(oInfo, oInfoName)
End If
'Close the drawing
oDrawing.Close

 

'return information about any unsaved parts
If unsavedSmParts <> "" Then _
MessageBox.Show(unsavedSmParts, "Some parts were not saved", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
'Update the assembly (could be dirty if any flatpatterns were created)
oDoc.Update
End Sub

Sub SaveDXF(oDrawing As DrawingDocument, oFileName As String, oIniFile As String)
'Set a reference to the DFX translator
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Create translation context
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'Create options for the translation
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'Create a DataMedium object
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Set the options (which .ini-file to use)
If DXFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
oOptions.Value("Export_Acad_IniFile") = oIniFile
End If
'Set the filename property of the DataMedium object
oDataMedium.FileName = oFileName
Try
'Try to save the DXF
DXFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
MessageBox.Show("Dxf saved to: " & oFileName, "DXF SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch
MessageBox.Show("Couldn't save dxf!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Sub RemoveBendLines(oView As DrawingView, oFlattPattern As FlatPattern)
'Get all the bend edges from the FlatPattern
Dim oBendEdgesUp As Edges = oFlattPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendUpFlatPatternEdge)
Dim oBendEdgesDown As Edges = oFlattPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendDownFlatPatternEdge)

For Each oEdge As Edge In oBendEdgesUp
'Get the curves representing these edges in the drawing view
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
'Set visibility to false
oSegment.Visible = False
Next
Next
Next
For Each oEdge As Edge In oBendEdgesDown
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
oSegment.Visible = False
Next
Next
Next
End Sub

Sub CreateTXT(oText As String, oFileName As String)
Dim oTxtWriter As System.IO.StreamWriter = System.IO.File.CreateText(oFileName)
oTxtWriter.WriteLine(oText)
oTxtWriter.Close()
End Sub
Message 7 of 16

Anonymous
Not applicable

@JhoelForshav Thanks a lot

0 Likes
Message 8 of 16

timw
Explorer
Explorer

Can you attach part number to the dxf file parts?  

0 Likes
Message 9 of 16

jacob.ellerbrock
Advocate
Advocate

Was trying to use your code, (wonderful concept by the way) and I'm running into the Couldn't save dxf! error. Any ideas on how I fix this? I'm currently just testing it on a small assembly with 4 sheet metal parts (no formed parts yet). I'm running Inventor 2020. I tried diagnosing it myself, but I am severely lacking in ilogic knowledge. Any advice is greatly appreciated.

0 Likes
Message 10 of 16

Nedeias
Participant
Participant

 

Hello, 
You need to specify an INI file location in the code first!

If you have not "extracted" the .INI file jet, follow the instructions in the link below.

iLogic: Export All Flat Patterns To One DXF – Clint Brown


 

0 Likes
Message 11 of 16

StuartRoodt
Contributor
Contributor

Hey guys, I've stumbled across this thread while trying to figure out how to remove/turn off the bend lines on views in an IDW. Bend lines currently come through in profile drawings and we usually have a separate drawing for the folding of any profiled parts; the goal is to only have the information for cutting on the profile drawing.

 

This is what I've managed to get so far from reusing parts of Jhoel's code above, but it seems to struggle accessing the flat pattern. Looks like lines 6 and 15/16 are causing issues. Any ideas on where I've gone wrong?

Thanks in advance!

 

Dim oDrawDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Sheet
Dim oView As DrawingView
Dim oViews As DrawingViews
Dim oDef As ComponentDefinition
Dim oFlatPattern As FlatPattern

   oSheet = oDrawDoc.ActiveSheet
   oViews = oSheet.DrawingViews

   For Each oView In oViews
	   ''Remove the bend lines of the view
'RemoveBendLines(oView, oDef.FlatPattern)'You could comment out this line to keep bend lines
	   'Get all the bend edges from the FlatPattern
Dim oBendEdgesUp As Edges = oFlatPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendUpFlatPatternEdge)
Dim oBendEdgesDown As Edges = oFlatPattern.GetEdgesOfType(FlatPatternEdgeTypeEnum.kBendDownFlatPatternEdge)

For Each oEdge As Edge In oBendEdgesUp
'Get the curves representing these edges in the drawing view
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
'Set visibility to false
oSegment.Visible = False
Next
Next
Next
For Each oEdge As Edge In oBendEdgesDown
For Each oCurve As DrawingCurve In oView.DrawingCurves(oEdge)
For Each oSegment As DrawingCurveSegment In oCurve.Segments
oSegment.Visible = False
Next
Next
Next
	   
   Next

 

 

0 Likes
Message 12 of 16

jacob.ellerbrock
Advocate
Advocate

@StuartRoodt You may want to start a new post and reference this one. I don't think you'll get much help on a solved forum.

0 Likes
Message 13 of 16

Felipe.BarbosaM3A2P
Explorer
Explorer

Olá @JhoelForshav :). 

 

I have a question.
Do You have maybe a solution how to change the code so that all components could be separately saved as DXF?
Thanks in advance  😊.

Message 14 of 16

LeanderTorres
Advocate
Advocate
Anny succes with this, I am looking for a sollution as well.
0 Likes
Message 15 of 16

LeanderTorres
Advocate
Advocate

I asked for a solution and got one if you are stil interested.

Solved: Re: ilogic batch create flat pattern to individual dxf - Autodesk Community - Inventor

0 Likes
Message 16 of 16

Apollo_Tamworth
Contributor
Contributor

Hi @JhoelForshav,

The code is brilliant—thanks for sharing!

However, I encountered an issue when setting my DXF flat pattern configuration file. It doesn’t seem to follow the INI file.

Is there a specific reason for this? The file is stored on the server, and I have full access to all the files there.

Interestingly, when I create a DXF file directly from a sheet metal part, the configuration is applied correctly and works perfectly.

What do you think might be causing this?

0 Likes