Ilogic to export sheet metal flat patterns from within a drawing

Ilogic to export sheet metal flat patterns from within a drawing

Anonymous
Not applicable
3,362 Views
19 Replies
Message 1 of 20

Ilogic to export sheet metal flat patterns from within a drawing

Anonymous
Not applicable

We have all singing and dancing Ilogic models that update, export dxf files (from assembly environment), drawings, etc. But for non-standard products we still rely heavily on spread sheet driven models which allows us to manually customise without breaking anything.

 

The models can be quite messy depending on the designer but the end result is a parts list. If there are any parts within the parts list that require a dxf flat pattern it is manually assigned a dxf file name in a column in the parts list and left blank otherwise.

 

Within the drawing environment is there anyway we can use Ilogic to look at the parts list and if it sees a row that has a dxf file name assigned it will export the sheet metal flat pattern of that particular part to a designated location?

 

Thanks in advance.

0 Likes
3,363 Views
19 Replies
Replies (19)
Message 2 of 20

dgreatice
Collaborator
Collaborator

Hi,

 

did you find related post?

there are many post are solve. example:

https://forums.autodesk.com/t5/inventor-customization/ilogic-dxf/m-p/6330127#M63889

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 3 of 20

Anonymous
Not applicable

This link seems to run from the rule from the assembly file. I want the rule to be in the drawing with the parts list and for it to look at the parts list.

0 Likes
Message 4 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Use the following iLogic code to get Assembly Document from Drawing.

 

Dim referDoc As Document
referDoc = ThisDrawing.ModelDocument

If referDoc.DocumentType = kAssemblyDocumentObject Then
	'Use the code provided in the link (https://forums.autodesk.com/t5/inventor-customization/ilogic-dxf/m-p/6330127#M63889)
End If

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 20

Anonymous
Not applicable

This isn't working but I still don't think its the solution to what I'm after as thisseems to be looking at the assembly in the drawing and exporting all sheet metal parts it finds.

 

See the parts list attached. These parts are all sheet metal but I only want Ilogic to export the DXF flat pattern of the parts that only have a code assigned to them in the column DXF FILE NAME.

 

 

0 Likes
Message 6 of 20

MechMachineMan
Advisor
Advisor

Something like this is more along the lines of what you are looking for:

 

Sub Main()

'Pre-Checks
	If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
		MessageBox.Show("Please run this rule from the drawing document", "iLogic")
		Exit Sub
	End If

	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
		Exit Sub
	End If
'/Pre-Checks

'Output Config
	'oPath = ThisDoc.Path
	'oFolder = oPath & "\" & oAsmName & " DXF Files"
	oFolder = "C:\Users\Public\Public Documents\DXF Files"

	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
	
	oDxfFlagColumnIndex = 6
'/Output Config	

'Functionality
	Dim oDwgDoc As DrawingDocument
	oDwgDoc = ThisDoc.Document
	
	For Each oSheet As Sheet In oDwgDoc.Sheets
		If oSheet.PartsLists.Count > 0 Then
			For Each oPartsList As PartsList In oSheet.PartsLists
				For Each oPartsListRow In oPartsList.PartsListRows
					If oPartsListRow.Item(oDxfFlagColumnIndex).Value <> "" Then
						'oPartDoc = oPartsListRow.ReferencedRows.Item(1).Document
						oPartDoc = oPartsListRow.ReferencedRows.Item(1).BOMRow.ComponentDefinitions.Item(1).Document
						Call ExportDXFFromIPT(oPartDoc, oFolder)
					End If
				Next 'row
			Next 'partslist
		End If
	Next 'sheet
'Functionality

End Sub

Sub ExportDXFFromIPT(oPartDoc As Document, oDestinationFolder As String)
		
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	
	Try
		CustomName = Left(oPartDoc.DisplayName, Len(oPartDoc.DisplayName) -4)
		oDataMedium.FileName = oDestinationFolder & "\" & CustomName & ".dxf"

		Dim oCompDef As SheetMetalComponentDefinition
		oCompDef = oPartDoc.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_PR​OFILE"
		oCompDef.DataIO.WriteDataToFile(sOut,oDataMedium.FileName)
		oCompDef.FlatPattern.ExitEdit
	Catch
		MsgBox("DXF Not Created! Part is not a sheet metal type")
	End Try
End Sub

--------------------------------------
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
0 Likes
Message 7 of 20

Anonymous
Not applicable

Now we're getting somewhere! This is great and what I'm after but still need assistance if you don't mind please?

 

1:  I can see the relevant column is chosen by "oDxfFlagColumnIndex = 6". Designers may add or takeaway extra columns at their will and so column 6 may not always be the one. Can this be chosen using the name of the column as this would remain static?

 

2: The name of the exported dxf file is currently the part name. We need it to be the code we have assigned to it in the column.

 

3: Until I go into each part and re-flat pattern it it seems to be not exporting it. Just trying to get to the bottom of this.

 

Thanks.

0 Likes
Message 8 of 20

MechMachineMan
Advisor
Advisor

Not sure about 3. Try some google-fu or browsing the forums for help with that one.

 

Sub Main()

'Pre-Checks
	If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
		MessageBox.Show("Please run this rule from the drawing document", "iLogic")
		Exit Sub
	End If

	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
		Exit Sub
	End If
'/Pre-Checks

'Output Config
	'oPath = ThisDoc.Path
	'oFolder = oPath & "\" & oAsmName & " DXF Files"
	oFolder = "C:\Users\Public\Public Documents\DXF Files"

	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
'/Output Config	

'Functionality
	Dim oDwgDoc As DrawingDocument
	oDwgDoc = ThisDoc.Document
	
	For Each oSheet As Sheet In oDwgDoc.Sheets
		If oSheet.PartsLists.Count > 0 Then
			For Each oPartsList As PartsList In oSheet.PartsLists
If oDXFCol = "" Then
oDXFCol = GetDXFColIndex(oPartsList)
If oDXFCol = "" Then: Msgbox("Col ID Issue, Aborting Sub!"): Exit Sub: End if
End if
For Each oPartsListRow In oPartsList.PartsListRows
oDestinationName = oPartsListRow.Item(oDxfFlagColumnIndex).Value If oDestinationName <> "" Then 'oPartDoc = oPartsListRow.ReferencedRows.Item(1).Document oPartDoc = oPartsListRow.ReferencedRows.Item(1).BOMRow.ComponentDefinitions.Item(1).Document Call ExportDXFFromIPT(oPartDoc, oFolder, oDestinationName) End If Next 'row Next 'partslist End If Next 'sheet 'Functionality End Sub
Function GetDXFColIndex(oPartsList As PartsList) As Integer For j = 1 to oPartsList.PartsListColumns.Count If oPartsList.oPartsListColumns(j).Title = "DXF" Then GetDXFColIndex = j Exit Function End if Next MsgBox("Column of appropriate title hardcoded in script not found. [See sub GetDXFColIndex]") End Function
Sub ExportDXFFromIPT(oPartDoc As Document, oDestinationFolder As String, oDestinationName) oDataMedium = ThisApplication.TransientObjects.CreateDataMedium Try 'CustomName = Left(oPartDoc.DisplayName, Len(oPartDoc.DisplayName) -4) oDataMedium.FileName = oDestinationFolder & "\" & oDestinationName & ".dxf" Dim oCompDef As SheetMetalComponentDefinition oCompDef = oPartDoc.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_PR​OFILE" oCompDef.DataIO.WriteDataToFile(sOut,oDataMedium.FileName) oCompDef.FlatPattern.ExitEdit Catch MsgBox("DXF Not Created! Part is not a sheet metal type") End Try End Sub 

 


--------------------------------------
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
0 Likes
Message 9 of 20

Frederick_Law
Mentor
Mentor

You can add a custom iProperty to mark the part/drawing as having flat layout.

You can check that iProperty in drawing instead of BOM.

Message 10 of 20

Anonymous
Not applicable

Thanks for this but I seem to get the below error?

 

Error on Line 71 : All parameters must be explicitly typed if any of them are explicitly typed

0 Likes
Message 11 of 20

Anonymous
Not applicable

Hi Pineapple...

 

We do already set Iproperties value within parts so they are identified as sheet metal and require to be dxf'd but that's on models specifically setup to be illogic driven from scratch.

 

However, this is a workaround for non-illogic models where we have hundreds of parts that aren't given any Iproperty values.

0 Likes
Message 12 of 20

MechMachineMan
Advisor
Advisor

Have you (a) identified line 71? and (b) Have you tried applying what the error trace says (the plain english of it) to the line to try to find out the error?

 

Probably not, but here's how:

 

(a)

 

Easy way to find what line it is on:

1. Copy the text of the code.

2. Go to diffchecker.com

3. Paste the code.

4. Use the line index on the side to find the line.

 

In this case, the line giving issues is:

 

Sub ExportDXFFromIPT(oPartDoc As Document, oDestinationFolder As String, oDestinationName)

 

Good, line found.

 

(b)

 

Now, the error trace says:

 

All parameters must be explicitly typed if any of them are explicitly typed.

 

(from google:)

parameter: a numerical or other measurable factor forming one of a set that defines a system or sets the conditions of its operation.

explicitly: in a clear and detailed manner, leaving no room for confusion or doubt.

typed: classified in a certain manner.

 

A rough conversion to plain English tell us?

"One of these things is not like the other one, and these things involve parameters and explicitly written types"

 

What does this mean?

Well, look for patterns.

If you look in the brackets, you see writing followed by a comma repeated.

 

Our section that includes the pattern:

oPartDoc As Document, oDestinationFolder As String, oDestinationName

Notice how one of the segments is not like the other ones?

(Big hint: "As _________")

 

Hmm, well what follows the 'as' you might ask?

That's where you need to understand the available programming language you use, then analyze where that variable is used and apply the necessary typing.

 

A good place to start is with the basic variable types for the programming language (easy link found through google: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/data-type-summary)

 

In this case, you would luck out pretty quick and find out that "STRING" is your answer.

 

Now you should be able to put this all together and have some working code.

 

 


--------------------------------------
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
0 Likes
Message 13 of 20

MechMachineMan
Advisor
Advisor

Also, as using iProperties is a more accessible and robust method, you could have considered your workflow from the beginning.

 

Step 1:

What does the ideal workflow look like, and how do we apply this 2 legacy files?

 

Step 2:

What is the code to apply the necessary conversion to the legacy files to make the act functionally like the current files?

 

Step 3:

What is the code we need to continue are workflow to solve the original problem?

 

 

In this case;

- Push the info from the PartsList into the iProperties for the parts, and clear the "static" status of the cell in the parts list.

- Iterate through the BOM rows, or master doc in order to create the dxfs.

 

This legacy method that forces us to use the partslist is not only inefficient, but it FORCES us to have to use the PartsLists to gather the relevant information, which is just adding another layer of complexity to the problem.


--------------------------------------
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
0 Likes
Message 14 of 20

Frederick_Law
Mentor
Mentor

@Anonymous wrote:

Hi Pineapple...

 

However, this is a workaround for non-illogic models where we have hundreds of parts that aren't given any Iproperty values.

Then add that iProperty to all models without iLogic.

Write code (VBA macro or addin) to go through all files and check if there is flat pattern in it and change iProperty accordingly.

You can try the code in with 10 files with and without flat pattern first to see if it work.

You can then run it at the end of the day to work on all files.

 

I"m not sure if iLogic can do the following.  I only code with VBA and addin.

Code a event trigger on Before File Save or After File Open to check file if there is flat pattern and add iProperty.

 

This way you don't need a workaround.

0 Likes
Message 15 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Without iPropeties, I tried some iLogic code to find sheet metal path and export the same to DXF as shown below. 

 

Can you please provide sample drawing and related files? So that, we can understand structure of partslist and assembly.

 

Please make sure that files are non confidential.

 

Sub Main() 
Dim oDoc As DrawingDocument oDoc = ThisApplication.ActiveDocument Dim oSheet As Inventor.Sheet oSheet = oDoc.Sheets.Item(1) 'Assumed that only one partslist exists in drawing sheet Dim oPartsList As PartsList oPartsList = oSheet.PartsLists.Item(1) Dim oPartListRow As PartsListRow For Each oPartListRow In oPartsList.PartsListRows If Not oPartListRow.Item(6).Value = "" Then 'Assumed that 6th column is "DXF FILE NAME" Dim referDoc As Document referDoc = oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument If referDoc.DocumentType = kAssemblyDocumentObject Then Dim assyDoc As AssemblyDocument assyDoc = referDoc Dim occName As String 'Assumed that 2nd columne is "PART NAME". This name is used for finding the sheet metal document path occName = oPartListRow.Item(2).Value 'Assumed that Occurrence name and PART NAME are similar iptPathName = getOccPath(assyDoc, occName) If Not iptPathName = "" Then ExportDXF(iptPathName) End If End If End If Next End Sub Public Function getOccPath(ByVal assyDoc As AssemblyDocument, ByVal occName As String) As String Dim assyDef As AssemblyComponentDefinition assyDef = assyDoc.ComponentDefinition occName = occName + ":1" Dim occPath As String occPath = "" Dim occ As ComponentOccurrence For Each occ In assyDef.Occurrences.AllLeafOccurrences If occ.Name = occName Then occPath = occ.ReferencedDocumentDescriptor.FullDocumentName getOccPath = occPath End If Next End Function Public Sub ExportDXF(ByVal iptPathName As String) Dim oSheetDoc As PartDocument oSheetDoc = ThisApplication.Documents.Open(iptPathName, True) If TypeOf oSheetDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Dim oCompDef As SheetMetalComponentDefinition oCompDef = oSheetDoc.ComponentDefinition 'Desire location to save DXF file oFolder = "C:\Temp" oFileName = Left(oSheetDoc.DisplayName, Len(oSheetDoc.DisplayName) - 4) Try oDataMedium = ThisApplication.TransientObjects.CreateDataMedium oDataMedium.FileName = oFolder & "\" & oFileName & ".dxf" 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_PR?OFILE" oCompDef.DataIO.WriteDataToFile( sOut, oDataMedium.FileName) oCompDef.FlatPattern.ExitEdit MessageBox.Show("DXF file saved at " + oDataMedium.FileName, "Inventor") Catch End Try Else MessageBox.Show ("Unable to export to DXF. PartDocument is not sheet metal", "Inventor") End If oSheetDoc.Close End Sub

 

 

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 16 of 20

Anonymous
Not applicable

At this time the method MechMachine is assisting with is the way I wish to go. There are better ways and for other models we do use better ways but this is more appropriate to our current situation until we spend time re-evaluating our spread sheet models.

 

I am not a programmer (hopefully in the future I can dedicate time to understanding it properly) and spend my time cutting and pasting code from other peoples generosity or existing found solutions.

 

My obstacle for closing this as a working solution now is a new error as below. I'm currently trying to solve this but keep hitting a brick wall with my lack of understanding.

 

Public member 'oPartsListColumns' on type 'PartsList' not found

0 Likes
Message 17 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Can you please provide sample drawing and related files (Non confidential files )? So that, we can understand structure of partslist and assembly.

 

With sample drawing files, feasible solution can be arrived.

 

Thanks and regards,

 

 

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 18 of 20

MechMachineMan
Advisor
Advisor

Step 1:

    Read below link.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/variables/h...

 

Step 2:

    Familiarize yourself with the object model & overview

    http://help.autodesk.com/view/INVNTOR/2018/ENU/?guid=GUID-4939ABD1-A15E-473E-9376-D8208EC029EB

 

Step 3:

    Use the API resources to find the relevant member that it should be:

    http://help.autodesk.com/view/INVNTOR/2018/ENU/?guid=GUID-CC993480-55C0-4C1C-9B2F-E522B879E990

 

 

Again, feel free to ask if you have any more questions! Seems like you're catching on quick though.


--------------------------------------
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
0 Likes
Message 19 of 20

Anonymous
Not applicable

This is a very basic model but it shows what I'm trying to do. Thanks.

0 Likes
Message 20 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

After downloading drawing sample, a small improvement to previous iLogic code and extended to work for drawing which is having presentation document. Code looks like below.

 

Sub Main() 

    Dim oDoc As DrawingDocument 
    oDoc = ThisApplication.ActiveDocument 
    
    Dim oSheet As Inventor.Sheet
    oSheet = oDoc.Sheets.Item(1)
    
    'Assumed that only one partslist exists in drawing sheet
    Dim oPartsList As PartsList
    oPartsList = oSheet.PartsLists.Item(1)
    
    Dim oPartListRow As PartsListRow
    For Each oPartListRow In oPartsList.PartsListRows
        If  Not oPartListRow.Item(6).Value = "" Then 'Assumed that 6th column is "DXF FILE NAME"
            Dim referDoc As Document
            referDoc = oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
            
            If referDoc.DocumentType = kAssemblyDocumentObject Then
				
                Dim assyDoc As AssemblyDocument
                assyDoc = referDoc
                
				Export_Through_AssyDoc(assyDoc,oPartListRow)
				
			Else If referDoc.DocumentType = kPresentationDocumentObject Then
				Dim oPresDoc As PresentationDocument
				oPresDoc = referDoc
				
				Dim assyDoc As AssemblyDocument
                assyDoc = referDoc.ReferencedDocumentDescriptors.Item(1).ReferencedDocument
				
				Export_Through_AssyDoc(assyDoc, oPartListRow)
            End If
        End If
    Next
    
End Sub

Public Sub Export_Through_AssyDoc(ByVal assyDoc As AssemblyDocument, ByVal oPartListRow As PartsListRow )
	Dim occName As String
    'Assumed that 2nd columne is "PART NAME". This name is used for finding the sheet metal document path
	occName = oPartListRow.Item(2).Value 'Assumed that Occurrence name and PART NAME are similar
    iptPathName = getOccPath(assyDoc, occName)
	If Not iptPathName = "" Then
		ExportDXF(iptPathName)
	End If	
End Sub

Public Function getOccPath(ByVal assyDoc As AssemblyDocument, ByVal occName As String) As String
    Dim assyDef As AssemblyComponentDefinition
    assyDef = assyDoc.ComponentDefinition
    
    occName = occName + ":1"
    
    Dim occPath As String
    occPath = ""
    
    Dim occ As ComponentOccurrence
    For Each occ In assyDef.Occurrences.AllLeafOccurrences
        If occ.Name = occName Then
            occPath = occ.ReferencedDocumentDescriptor.FullDocumentName
            getOccPath = occPath
        End If
    Next
    
End Function

Public Sub ExportDXF(ByVal iptPathName As String)

	Dim oSheetDoc As PartDocument
	oSheetDoc = ThisApplication.Documents.Open(iptPathName, True)
	
	If TypeOf oSheetDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
		Dim oCompDef As SheetMetalComponentDefinition
		oCompDef = oSheetDoc.ComponentDefinition	

		'Desire location to save DXF file
		oFolder = "C:\Temp"
		oFileName = Left(oSheetDoc.DisplayName, Len(oSheetDoc.DisplayName) - 4)
		
		Try

			oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
			oDataMedium.FileName = oFolder & "\" & oFileName & ".dxf"	

			
			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_PR?OFILE"
			oCompDef.DataIO.WriteDataToFile( sOut, oDataMedium.FileName)
			
			oCompDef.FlatPattern.ExitEdit
			
			MessageBox.Show("DXF file saved at " + oDataMedium.FileName, "Inventor")
		Catch
		End Try
	Else 		
		MessageBox.Show ("Unable to export to DXF. PartDocument is not sheet metal", "Inventor")		
	End If
	
	oSheetDoc.Close
	
End Sub

Please feel free to contact if there is any queries.

 

If problem is solved, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes