Sheet Metal Flatsheets to DXF iLogic not using ini file specified. (or template)

jwitt1983
Enthusiast
Enthusiast

Sheet Metal Flatsheets to DXF iLogic not using ini file specified. (or template)

jwitt1983
Enthusiast
Enthusiast

Goal: Improve flat pattern output for the waterjet operator. (different colors for bend directions and mark features front or back)

I have been using the following code that I picked up from these forums (originally by Jhoel Forshav). I've only made slight modification to the original to add text below each flat pattern for part number, material, qty, etc... only problems have been incorrect layers per our standards, which we've lived with because of the amazing concatenation that this code performs.

In this code I've specified the ini file that I would like to use, however it is not actually using it. Will show example below. (Also, it's not utilizing the Acad template specified.. none of our layers/dimstyles/etc are in the output file.)

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 following 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)
'Use Template vvv?? Doesn't currently work
oDrawing = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, "C:\_CEIVault\Standards\Templates\CEI Drawing.dwg", 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


'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
'Set reference to SheetMetalComponentDefinition
	Dim oSDef As SheetMetalComponentDefinition = smPartDoc.ComponentDefinition
	Dim oStyle As SheetMetalStyle = oSDef.ActiveSheetMetalStyle
'MessageBox.Show("Active Style Name - " & oStyle.Name)
'smPartDoc.DisplayName = ThisDoc.FileName(False)
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


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


'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
'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
oView.Name = smPartDoc.DisplayName & vbNewLine & "Material: " & oStyle.Name _
& vbNewLine & "Metal Grade: " & 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
oX = oView.Left + oView.Width + 4
'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

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 _
& " - Description: " & smPartDoc.PropertySets.Item("Design Tracking Properties"). _
Item("Description").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 = "X:\COASTLINE BLOCK LIBRARY & STANDARDS\STANDARDS\INVENTOR TEMPLATES\ACAD2004 EXPORT.ini" 'Specify your INI file location here (eg C:\TEMP\DXF Export.ini)
oINI = "C:\_CEIVault\Standards\Templates\Inventor\DXF 2004 EXPORT.ini"

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

 

xy7EX26BM6.png5haJBtWznG.png

I am pointing to the same ini file in the Flat Pattern>Save Copy As, that is specified in the code:

jwitt1983_0-1673314218230.png

 

Can someone help me find a solution to this?

 

(Let me know if there is any information that I can give.)

 

0 Likes
Reply
494 Views
7 Replies
Replies (7)

A.Acheson
Mentor
Mentor

I believe the ini Autocad file is not available for use with export of a part. It is only for export of flat pattern from a drawing. I tested this before using ini file in inventor part and one from the drawing and none of them worked.

If you still want to proceed with export of flat pattern from a part you need to build out the string defining the format of the dxf file. See sample here

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

jwitt1983
Enthusiast
Enthusiast

I am confused by your message.

The code above exports all flat patterns from an assembly to a single DXF file. I don't fully understand how this works from my limited iLogic knowledge, but it appears that it is creating a base view of flatpattern for each SM part, while changing the position to not overlap. This is all done in the background but I think it is creating all this in a single drawing and then exporting using (from drawing)

DXFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)

instead of (from part)

oCompDef.DataIO.WriteDataToFile( sOut, sFname)

 I don't know the limitations of each, but I do not see where I can add the string for formatting using the export function DXFAddIn.SaveCopyAs.

 

0 Likes

A.Acheson
Mentor
Mentor

Apologies I did not see the export to drawing lines. Disregard the other option as your using the correct method for using an .ini file. Ensure it exists on disk and it should format dxf to that of .ini settings. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes

jwitt1983
Enthusiast
Enthusiast

jwitt1983_0-1673371086159.png

See screenshot of ini existence.

I've also tried a couple different ini files:

     saved configuration from drawing>Save Copy As>options dialog

     saved configuration from Part>right-click flat pattern>Save Copy As> dialog

Both still output with layers: Visible(Default), BendCenterLine, and no mark features visible as shown in my first post.

 

0 Likes

SevInventor
Advocate
Advocate

Hello Jeremy,

 

this one works with ini file:

 

Gelöst: Re: Exporting flat patterns from assembly to multiple DFX files - Autodesk Community - Inven...

 

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)
			Dim oDocu As Document = oDef.Document
			Dim oCompName As String = System.IO.Path.GetFileNameWithoutExtension(oDocu.FullFileName)
			Dim oDXFName As String = oPath & "\" & oCompName & ".dxf"
'			MsgBox(oDXFName)

				'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
					Try 
					oDef.Unfold()
					oDef.FlatPattern.ExitEdit()
					Catch
						End Try
				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
'				Try
				Dim oView As DrawingView = oSheet.DrawingViews.AddBaseView(smPartDoc, oTG.CreatePoint2d(0, 0), 1 , ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, "FlatPattern", , oBaseViewOptions)
'				Catch
'				Dim oView As DrawingView = oSheet.DrawingViews.AddBaseView(smPartDoc, oTG.CreatePoint2d(0, 0), 1 , ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, "FlatPattern", , oBaseViewOptions)
'				End Try
				oView.Name = smPartDoc.DisplayName
				oView.ShowLabel = True



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

				i += 1

				'Close the part
				smPartDoc.Close(True)


		'Save the DXF
		oINI = "X:\DXF importieren.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
	'Close the drawing
	SaveDXF(oDrawing, oDXFName, oINI)
	oView.Delete

	Next
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!"& oFilename, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
	End Try
	
End Sub

 

0 Likes

jwitt1983
Enthusiast
Enthusiast

Using the code you posted still gives me the wrong layers AND makes all separate files... The purpose of my posted code above is to create a single file with all flatsheets req'd for an assembly.

0 Likes

jwitt1983
Enthusiast
Enthusiast

I don't understand how making parts show up as a specific layer in a dwg doesn't work. Why can't I make Mark features show up as specific layers? This shouldn't be this hard to make happen.

0 Likes