Export Flatpattern to DXF from Assembly. Filenaming.

Export Flatpattern to DXF from Assembly. Filenaming.

Jesper_S
Collaborator Collaborator
3,161 Views
10 Replies
Message 1 of 11

Export Flatpattern to DXF from Assembly. Filenaming.

Jesper_S
Collaborator
Collaborator

Hi.

 

I got an working code for this already but I am wondering if its possible to make some changes.

 

Instead of getting the filename for the .DXF from the .ipt itself, i would like to get the filename from 

the Main Assembly and if its possible, itemnumber from the BOM for the part that's exported.

 

Example. Main Assembly name is ABCDEFG and i got a sheetmetal part with Itemnumber 8.

I want the filename to be  "ABCDEFG-8"

 

This code will be run when the design is finished and approved for manufacturing so there will be no renumbering of the BOM.

 

Running IV 2015.

 

Present Code.

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument

oAsmDoc = ThisApplication.ActiveDocument

oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)



'check that the active document is an assembly file
If ThisApplication.ActiveDocument.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 = vbNo Then

	Return
	
	Else
	
End If

oPath = ThisDoc.Path

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

oContext = ThisApplication.TransientObjects.CreateTranslationContext

oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

oOptions = ThisApplication.TransientObjects.CreateNameValueMap

'get DXF target folder path
oFolder = 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  - - - - - - - - - - - -
'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator

oRefDocs = oAsmDoc.AllReferencedDocuments

Dim oRefDoc As Document


'work the the drawing files for the referenced models
'this expects that the model has been saved
		For Each oRefDoc In oRefDocs
			
			iptPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "ipt"

    		'check that model is saved
			If(System.IO.File.Exists(iptPathName)) Then
			
                Dim oDrawDoc As PartDocument
				
                oDrawDoc = ThisApplication.Documents.Open(iptPathName, True)
				
            	oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName))
				
				Try
				
                	'Set the DXF target file name
                	oDataMedium.FileName = oFolder & "\" & oFileName & ".dxf"
				
					Dim oCompDef As SheetMetalComponentDefinition
				
					oCompDef = oDrawDoc.ComponentDefinition
					
					If oCompDef.HasFlatPattern = False Then
					
						oCompDef.Unfold
						
					Else
					
   						oCompDef.FlatPattern.Edit
						
					End If

					Dim sOut As String
				
					sOut = "FLAT PATTERN DXF?AcadVersion=2004&OuterProfileLayer=IV_OUTER_PROFILE"

					
					oCompDef.DataIO.WriteDataToFile( sOut, oDataMedium.FileName)
				
					'just for check its works coretcly
					'i=MessageBox.Show(oDataMedium.FileName, "Title",MessageBoxButtons.OKCancel)
					
					'MessageBox.Show(i,"title",MessageBoxButtons.OK)
				
					'If i=2 Then
				
						'Exit Sub
				
					'End If

					oCompDef.FlatPattern.ExitEdit
				
				Catch
				
				End Try
                
				oDrawDoc.Close
				
			Else
					
			End If
			
		Next

If more info is needed, ask me.

 

Thanks in advance.


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Accepted solutions (2)
3,162 Views
10 Replies
Replies (10)
Message 2 of 11

Owner2229
Advisor
Advisor

Something like this?

 

'Define the active document as an assembly file
Dim oDoc As Document = ThisApplication.ActiveDocument

'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 oAsmName As String = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
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  - - - - - - - - - - - -

Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
oBOM.StructuredViewEnabled = True
'You'll have to play with this to get the BOM you want. Try 1, 2, 3 or "Structured"
Dim oBOMView As BOMView = oBOM.BOMViews.Item(2)
For Each oRow As BOMRow In oBOMView.BOMRows
	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"
		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & oAsmName & "-" & oItem & ".dxf")
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Next
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 11

Jesper_S
Collaborator
Collaborator

Hi.

 

Thanks for answering.

 

Got this when i tried the code.

 

Error in rule: Rule3, in document: 66430-300.iam

Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

 

Forum copy error or something else?


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Message 4 of 11

Owner2229
Advisor
Advisor
Accepted solution

I'm not sure what might be causing the issue, but you can try it with some error handling:

Red highlighted is what I've changed.

Also you can try to place this "MsgBox(1)" in the code (and move it) to see how far can the rule get without crashing and this way find the line with the error.

 

'Define the active document as an assembly file
Dim oDoc As Document = ThisApplication.ActiveDocument

'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 oAsmName As String = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
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  - - - - - - - - - - - -

Dim oBOM As BOM = oDoc.ComponentDefinition.BOM
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" oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & oAsmName & "-" & oItem & ".dxf") oCD.FlatPattern.ExitEdit() Catch ex As Exception MsgBox(ex.Message) End Try iDoc.Close(True) Catch End Try Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 5 of 11

Jesper_S
Collaborator
Collaborator

The line marked as red. If i move the messagebox above it, the message appears.

 

 

'Define the active document as an assembly file
Dim oDoc As Document = ThisApplication.ActiveDocument

'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 oAsmName As String = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
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  - - - - - - - - - - - -

Dim oBOM As BOM = 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"
		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & oAsmName & "-" & oItem & ".dxf")
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next

 


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Message 6 of 11

Jesper_S
Collaborator
Collaborator
Accepted solution
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

'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 oAsmName As String = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
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"
		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & oAsmName & "-" & oItem & ".dxf")
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next

Now it works. I changed the parts in red.

 

Thansk alot for your help.


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Message 7 of 11

Owner2229
Advisor
Advisor

That doesn't make any sense... But if it works ¯\_(ツ)_/¯

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 8 of 11

MechMachineMan
Advisor
Advisor

I think I've run into cases before when it seems to bug out and the functions won't take without having the ComponentDefinition completely declared (ie; "As AssemblyComponentDefinition" following an "As AssemblyDocument"), but that restarting inventor would fix it as well, so it was a session based thing. Maybe something similar here?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 9 of 11

Owner2229
Advisor
Advisor

Might be as I've tested the code and it worked for me (as I've posted it).

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 10 of 11

Jesper_S
Collaborator
Collaborator

Hi.

 

Correct, I restarted Inventor and @Owner2229 :s code works just fine.


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Message 11 of 11

cadman777
Advisor
Advisor

Dear Owner,

Thanx for posting this 'code'.

I want to write my own, but maybe can use yours and adapt it to my needs.

So would you be so kind as to explain where you get the BOM 'Item Number', and where you insert it into the file name?

Also, how would you change that to be the 'Stock Number' instead of the 'Item Number'?

FYI:
I don't know VB nor do I know iLogic.

I actually HATE programming, but by force of need, I have to learn it AGAIN.

I just want to know what these things mean.

I need a book that defines the variables and the syntax, and also a book to look up the various elements (iProps, Params, etc.), b/c I do not know them 'by heart'.

Thing is, my experience w/'programming' is AutoLISP, which I used 'back in the day' to write small 'programs' for AutoCAD 'routines'.

Any help would be much appreciated.

Thanx!

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes