﻿' Hromadny export plechu do DXF, viz tez:
' https://www.cadforum.cz/cz/export-rozvinu-plechove-soucasti-do-dxf-parametry-ilogic-tip12391

Imports Inventor
Imports System.Drawing
Imports System.IO
Imports System.IO.DirectoryInfo
Imports System.String
Imports System.Threading.Tasks
Class ThisRule
	
Dim sDXFsubFolderName As String = "ExportyDXF"

'set to false if need to output dxf in part locations
Dim oneOutputFolder As Boolean = True

Dim topAssemblyFolder As String	
Dim colectionSheetMetalParts As New List(Of String)

Sub Main
	Dim selectedDocument As Document = ThisApplication.ActiveDocument
	topAssemblyFolder = System.IO.Path.GetDirectoryName(selectedDocument.FullFileName)
	
	Dim oRefDoc As Document
	For Each oRefDoc In selectedDocument.AllReferencedDocuments
		If oRefDoc.ComponentDefinition.Type = ObjectTypeEnum.kSheetMetalComponentDefinitionObject Then
			colectionSheetMetalParts.Add(oRefDoc.FullFileName)
		
		End If
	Next
	GenerateUnfoldsDXF(colectionSheetMetalParts)
End Sub

Function GenerateUnfoldsDXF(colectionSheetMetalParts)
	
	For Each sPath In colectionSheetMetalParts
		Dim oSheetMetalPart As PartDocument
		
		'Here set True or False, if the file must be visible or not
		oSheetMetalPart = ThisApplication.Documents.Open(sPath, False)
		
		Dim oSM_CompDef As SheetMetalComponentDefinition
		oSM_CompDef = oSheetMetalPart.ComponentDefinition

		oSheetMetalPart.Update

		Dim unsuccessfullUnfold = False
		If oSM_CompDef.HasFlatPattern = False Then
			Try
				oSM_CompDef.Unfold()
			Catch
				MsgBox("Nepovedlo se rozvinout " & oSM_CompDef.DisplayName)
				unsuccessfullUnfold = True
			End Try
		End If
		
		oSheetMetalPart.Save
		
		If unsuccessfullUnfold = False
			' Create the DXF file.
			WriteSheetMetalDXF(oSheetMetalPart)
		End If
		oSheetMetalPart.Save()
		oSheetMetalPart.Close()
	Next
End Function

Function WriteSheetMetalDXF(oDoc As PartDocument)
    Dim oDataIO As DataIO
    oDataIO = oDoc.ComponentDefinition.DataIO
	Dim dxfOutFolder As String
	
	If oneOutputFolder Then
		dxfOutFolder = System.IO.Path.Combine(topAssemblyFolder, sDXFsubFolderName)
		If Not System.IO.Directory.Exists(dxfOutFolder) Then
			Dim dirSec = Nothing
			System.IO.Directory.CreateDirectory(dxfOutFolder, dirSec)
		End If
	Else
		dxfOutFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(oDoc.FullFileName), sDXFsubFolderName)
		If Not System.IO.Directory.Exists(dxfOutFolder) Then
			Dim dirSec = Nothing
			System.IO.Directory.CreateDirectory(dxfOutFolder, dirSec)
		End If
	End If
	
	Dim sDxfOutPath As String 
	sDxfOutPath = dxfOutFolder & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName) & ".dxf"
	Dim sOut As String

	sOut = "FLAT PATTERN DXF?AcadVersion=R12&OuterProfileLayer=P&InteriorProfilesLayer=P&FeatureProfilesUpLayer=G&FeatureProfilesDownLayer=RED&OuterProfileLayerColor=0;255;255&InteriorProfilesLayerColor=0;255;255&FeatureProfilesUpLayerColor=255;0;255&FeatureProfilesDownLayerColor=255;0;0&OuterProfileLayerLineWeight=0,05&InteriorProfilesLayerLineWeight=0,05&FeatureProfilesUpLayerLineWeight=0,05&InvisibleLayers=IV_TANGENT;IV_ARC_CENTERS;IV_BEND;IV_BEND;IV_BEND_DOWN;IV_TOOL_CENTER;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN;IV_FEATURE_PROFILES;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_UNCONSUMED_SKETCHES;IV_ROLL_TANGENT;IV_ROLL"

	oDataIO.WriteDataToFile (sOut, sDxfOutPath)
End Function

End Class
