Batch export flat pattern dxf from iam

Batch export flat pattern dxf from iam

morrenengineering
Contributor Contributor
2,977 Views
10 Replies
Message 1 of 11

Batch export flat pattern dxf from iam

morrenengineering
Contributor
Contributor

hello,

 

i was trying to make a batch export from an open assembly to a folder. The file naming will nog be okay. It is a piece of code from an other forum member. The filename must be the same as the ipt name. for example, S0188.ipt must become a flat pattern S0188.dxf.

 

Can anyone help me?

 

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension



'Check that the active document is an assembly file
If oDoc.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If

'Get user input
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 <> vbYes Then Exit Sub

Dim oPath As String = ThisDoc.Path

'Get the DXF target folder path
Dim oFolder As String = 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  - - - - - - - - - - - -
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Dim oBOM As BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
oBOM = oDoc.ComponentDefinition.BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)
For Each oRow As BOMRow In oBOMView.BOMRows
Try
	Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
	Dim iDoc As Document = oCD.Document
	'SheetMetal parts only
	If iDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	Dim iName As String = iDoc.FullFileName

	'Check that model is saved
	If iName = vbNullString Then Continue For
	iDoc = ThisApplication.Documents.Open(iName)
	oCD = iDoc.ComponentDefinition
	
	Dim oItem As String = oRow.ItemNumber
	Try
		If Not oCD.HasFlatPattern Then
			oCD.Unfold()
		Else
			oCD.FlatPattern.Edit()
		End If
		Dim oCurFile As Document

		Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PROFILE"
    Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
   
    'find the postion of the last backslash in the path
    FNamePos = InStrRev(oCurFileName, "\", -1)  
    'get the file name with the file extension
    Name = Right(oCurFileName, Len(oCurFileName) - FNamePos)
    'get the file name (without extension)
    ShortName = Left(Name, Len(Name) - 4)

		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & ShortName & ".dxf")
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next

.  

0 Likes
Accepted solutions (1)
2,978 Views
10 Replies
Replies (10)
Message 2 of 11

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hello, here I have modified your rule. Delete some lines and modify the line that takes the name of the file, look at the lines in red.
I hope this is useful to you. Cheers

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

'Check that the active document is an assembly file
If oDoc.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If

'Get user input
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 <> vbYes Then Exit Sub

Dim oPath As String = ThisDoc.Path

'Get the DXF target folder path
Dim oFolder As String = 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  - - - - - - - - - - - -
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Dim oBOM As BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
oBOM = oDoc.ComponentDefinition.BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)
For Each oRow As BOMRow In oBOMView.BOMRows
Try
	Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
	Dim iDoc As Document = oCD.Document
	'SheetMetal parts only
	If iDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	Dim iName As String = iDoc.FullFileName

	'Check that model is saved
	If iName = vbNullString Then Continue For
	iDoc = ThisApplication.Documents.Open(iName)
	oCD = iDoc.ComponentDefinition
	
	Dim oItem As String = oRow.ItemNumber
	Try
		If Not oCD.HasFlatPattern Then
			oCD.Unfold()
		Else
			oCD.FlatPattern.Edit()
		End If

		Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PROFILE"
		Dim ShortName As String = Left(iDoc.DisplayName, (InStrRev(iDoc.DisplayName, ".", -1, vbTextCompare) - 1)) & ".dxf"

		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & ShortName)
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 3 of 11

b.mccarthy
Collaborator
Collaborator

Great code, thank you.

 

Is it possible to modify this slightly?

  • Add a line to use a custom export configuration *.ini file
  • Suppress the display of the unfold/refold operations

The workflow would be similar to that used in the part file: RMB on Flat Pattern > Save Copy As... > etc.

0 Likes
Message 4 of 11

KrishR330
Contributor
Contributor

I started using your iLogic Code with a slight variation in the code. It is working great for the Assemblies. However, not so successful when using on an iAssembly factory file. It goes on a loop even though it is done creating the DXFs for that member of the factory. I was wondering if there is a way to create the DXFs for all the factory members in one go. Please let me know if you can post a modified iLogic code.

 

Below is the modified code I am currently using:

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

'Check that the active document is an assembly file
If oDoc.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If

'Get user input
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 <> vbYes Then Exit Sub

Dim oPath As String = ThisDoc.Path

'Get the DXF target folder path
Dim oFolder As String = oPath & "\" & " 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  - - - - - - - - - - - -
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Dim oBOM As BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
oBOM = oDoc.ComponentDefinition.BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)
For Each oRow As BOMRow In oBOMView.BOMRows
Try
	Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
	Dim iDoc As Document = oCD.Document
	'SheetMetal parts only
	If iDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	Dim iName As String = iDoc.FullFileName

	'Check that model is saved
	If iName = vbNullString Then Continue For
	iDoc = ThisApplication.Documents.Open(iName)
	oCD = iDoc.ComponentDefinition
	
	Dim oItem As String = oRow.ItemNumber
	Try
		If Not oCD.HasFlatPattern Then
			oCD.Unfold()
		Else
			oCD.FlatPattern.Edit()
		End If

		Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PROFILE&InvisibleLayers=IV_TANGENT&BendDownLayerLineType=37644&BendUpLayerLineType=37644"
				
		Dim ShortName As String = Left(iDoc.DisplayName, (InStrRev(iDoc.DisplayName, ".", -1, vbTextCompare) - 1)) & ".dxf"

		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & ShortName)
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next

 

0 Likes
Message 5 of 11

Kris.kaczmarek
Explorer
Explorer

Is there a way to turn off all layers but the outside and inside profiles?

0 Likes
Message 6 of 11

Anonymous
Not applicable

Hello

 

I started use your macro. That's greate!

I try to modify code to use Iproperties Part Number as a name of dxf file, but parts and assemblies have the same  property. Finaly I have only one dxf file with number of the assembly as a name.

How to use the numbers of the parts?

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

'Check that the active document is an assembly file
If oDoc.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Proszę, uruchom tą regułę w pliku złożenia.", "iLogic")
	Exit Sub
End If

'Get user input
RUsure = MessageBox.Show ( _
"Wszystkie części blachowe w tym złożeniu zostaną wyeksportowane do plików DXF jako rozwinięcia." _
& vbLf & "Ta reguła wymaga aby wszystkie pliki części zostały zapisane." _
& vbLf & " " _
& vbLf & "Czy jesteś pewien, że chcesz zapisać wszystkie rozwinięcia jako pliki DXF?" _
& vbLf & "To może chwilę potrwać.", "iLogic  - Exportowanie DXF-ów", MessageBoxButtons.YesNo)

If RUsure <> vbYes Then Exit Sub

Dim oPath As String = ThisDoc.Path

'Get the DXF target folder path
Dim oFolder As String = 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  - - - - - - - - - - - -
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Dim oBOM As BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
oBOM = oDoc.ComponentDefinition.BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)
For Each oRow As BOMRow In oBOMView.BOMRows
Try
	Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
	Dim iDoc As Document = oCD.Document
	'SheetMetal parts only
	If iDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	Dim iName As String = iDoc.FullFileName

	'Check that model is saved
	If iName = vbNullString Then Continue For
	iDoc = ThisApplication.Documents.Open(iName)
	oCD = iDoc.ComponentDefinition
	
	Dim oItem As String = oRow.ItemNumber
	Try
		If Not oCD.HasFlatPattern Then
			oCD.Unfold()
		Else
			oCD.FlatPattern.Edit()
		End If

		Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PROFILE"
		'Dim ShortName As String = Left(iDoc.DisplayName, (InStrRev(iDoc.DisplayName, ".", -1, vbTextCompare) - 1)) & ".dxf"
		Dim ShortName As String =iProperties.Value("Project", "Part Number") & ".dxf"
		'iProperties.Value("part1:1", "Project", "Part Number")
		
		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & ShortName)
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next

 

0 Likes
Message 7 of 11

A.Acheson
Mentor
Mentor

Where you are getting the part number at the moment is incorrect, it is trying to take the part number from the assembly document where the rule is running  and not the part document. 


No Part Document specified

 

 

Dim ShortName As String =iProperties.Value("Project", "Part Number") & ".dxf"
		'iProperties.Value("part1:1", "Project", "Part Number")

 

 

Document specified. Use this to get the part doc part number 

 

Dim ShortName As String =iProperties.Value(iDoc.DisplayName,"Project", "Part Number") & ".dxf"

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 8 of 11

Anonymous
Not applicable
Great!
That works! Thanks a lot!
0 Likes
Message 9 of 11

aji.sugiyantoro
Contributor
Contributor

Hi Sergio
This is great code. but it still use Master Model to create dxf. And in inventor 2022 has model state.
It is possible it modify to create dxf file's the model state that i choose ?

 

@Anonymous.D.Suarez

Message 10 of 11

denis.semeniuc
Advocate
Advocate

Great Works, is possible to including export for all sub assembly???

0 Likes
Message 11 of 11

pl.sivakumar
Advocate
Advocate

Great code, thank you.

0 Likes