Here is two approaches to doing the translation to DXF,SAT and DXF in one operation. Rule 1 just process everything in sequence. Rule 2 uses a form built within VB.NET environment by code so has more of a manual approach rather than using Visual studio to build out the form.
Rule 1 Process All
Option Explicit On
Sub Main
ProcessRefDocuments(ThisDoc.Document)
End Sub
Sub ProcessRefDocuments(oDoc As Document)
Dim oDXFDirectory As String
Dim oStepDirectory As String
Dim oStpFullFileName As String
Dim oSatDirectory As String
Dim oSatFullFileName As String
Dim oDxfFullFileName As String
Dim oAsmPN As String = oDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
oDXFDirectory = CreateFolder(oDoc, " DXF Files")
oStepDirectory = CreateFolder(oDoc, "STEP Files")
oStpFullFileName = oStepDirectory & "\" & oAsmPN & ".stp"
SaveAs(oDoc, oStpFullFileName)
oSatDirectory = CreateFolder(oDoc, " SAT Files")
oSatFullFileName = oSatDirectory & "\" & oAsmPN & ".sat"
SaveAs(oDoc,oSatFullFileName)
'Look at the files referenced by the assembly and work the referenced models.
For Each oRefDoc As Document In oDoc.AllReferencedDocuments
If oRefDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
Dim oModelPN As String = oRefDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
oStpFullFileName = oStepDirectory & "\" & oModelPN & ".stp"
oStpFullFileName = Nothing
SaveAs(oRefDoc, oStpFullFileName)
oSatFullFileName = Nothing
oSatFullFileName = oSatDirectory & "\" & oModelPN & ".sat"
SaveAs(oRefDoc, oSatFullFileName)
'FilterSheetmetal Parts.
If oRefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
oDxfFullFileName = Nothing
oDxfFullFileName = oDXFDirectory & "\" & oModelPN & ".dxf"
CreateDXF(oRefDoc ,oDxfFullFileName)
End If
Next
MessageBox.Show("Batch Processing Rule Finished!","iLogic")
End Sub
Function CreateFolder(oDoc As Document, oFolderName As String ) As String
Dim oPath As String = IO.Path.GetDirectoryName(oDoc.FullFileName)
Dim oAsmName As String = IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
'Get Target folder path.
Dim oFolder As String = oPath & "\" & oAsmName & " " & oFolderName
'Check For the Step folder And create it If it does Not exist.
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If
Return oFolder
End Function
Sub SaveAs(oDoc,oFullFileName)
Try
oDoc.SaveAs(oFullFileName, True)
Catch ex As Exception
'Logger.Info("Error Saving As " & oFullFileName)
End Try
End Sub
Sub CreateDXF(oDoc As PartDocument, oFullFileName As String)
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
oDataMedium.FileName = oFullFileName
oDoc = ThisApplication.Documents.Open(oDoc.FullFileName, False)
Try
Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2010&OuterProfileLayer=IV_OUTER_PROFILE&OuterProfileLayerColor=0;0;255"& _
"&TangentLayer=IV_TANGENT&TangentLayerColor=255;0;0&BendUpLayer=IV_BEND&BendUpLayerColor=255;255;0"& _
"&BendDownLayer=IV_BEND_DOWN&BendDownLayerColor=255;128;0&InteriorProfilesLayer=IV_INTERIOR_PROFILES"& _
"&InteriorProfilesLayerColor=255;0;0&MergeProfilesIntoPolyline=True"
'MessageBox.Show("Message", "Title")
oCompDef.DataIO.WriteDataToFile(sOut, oDataMedium.FileName)
oCompDef.FlatPattern.ExitEdit
Catch
End Try
oDoc.Close
End Sub
Rule 2 Give Form to user for Translation Options
Option Explicit On
AddReference "System.Drawing"
Imports System.Windows.Forms
Imports System.Drawing
Public Class WinForm
Inherits System.Windows.Forms.Form
Public ThisApplication As Inventor.Application = GetObject(,"Inventor.Application")
Public oArial10 As System.Drawing.Font = New Font("Arial", 10)
Public oArial12Bold As System.Drawing.Font = New Font("Arial", 10, FontStyle.Bold)
Dim Operation As New List(Of String)
Public Sub New()
Dim oForm As System.Windows.Forms.Form = Me
With oForm
.FormBorderStyle = FormBorderStyle.FixedToolWindow
.StartPosition = FormStartPosition.CenterScreen
.Width = 300
.Height = 300
.TopMost = True
.Font = oArial10
.Text = "Assembly File Processing"
.Name = "Form Title"
.ShowInTaskbar = False
End With
Dim oMainLabel As New System.Windows.Forms.Label
With oMainLabel
.Text = "Pick As Required!"
.Top = 10
.Left = 25
.Height = 20
.Width = 300
End With
oForm.Controls.Add(oMainLabel)' Add to the form.
'[Check Box1
Dim CheckBox1 As New System.Windows.Forms.CheckBox'Create and initialize a CheckBox.
With CheckBox1
.Text = "STEP"
.Top = oMainLabel.Bottom
.Left = 25
.Width = 20
.Height = 25
.Name = "oCheckBox1"
.Appearance = Appearance.Normal ' Make the check box control appear as a toggle button.
.AutoCheck = True 'Turn off the update of the display on the click of the control.
End With
oForm.Controls.Add(CheckBox1)' Add the check box control to the form.
AddHandler CheckBox1.Click, AddressOf checkBox1_Click
Dim NameBox1 As New System.Windows.Forms.TextBox
With NameBox1
.Top = CheckBox1.Top
.Left = CheckBox1.Right +25
.Width = 200
.Height = 25
.Name = "oNameBox1"
.Text = "STEP"
.Enabled = False
End With
oForm.Controls.Add(NameBox1)
']
'[Check Box2
Dim CheckBox2 As New System.Windows.Forms.CheckBox'Create and initialize a CheckBox.
With CheckBox2
.Text = "DXF"
.Top = CheckBox1.Bottom
.Left = 25
.Width = 20
.Height = 25
.Name = "oCheckBox2"
.Appearance = Appearance.Normal ' Make the check box control appear as a toggle button.
.AutoCheck = True 'Turn off the update of the display on the click of the control.
End With
oForm.Controls.Add(CheckBox2)' Add the check box control to the form.
AddHandler CheckBox2.Click, AddressOf checkBox2_Click
Dim NameBox2 As New System.Windows.Forms.TextBox
With NameBox2
.Top = CheckBox2.Top
.Left = CheckBox2.Right +25
.Width = 200
.Height = 25
.Name = "oNameBox2"
.Text = "DXF"
.Enabled = False
End With
oForm.Controls.Add(NameBox2)
']
'[Check Box3
Dim CheckBox3 As New System.Windows.Forms.CheckBox'Create and initialize a CheckBox.
With CheckBox3
.Text = "SAT"
.Top = CheckBox2.Bottom
.Left = 25
.Width = 20
.Height = 25
.Name = "oCheckBox3"
.Appearance = Appearance.Normal ' Make the check box control appear as a toggle button.
.AutoCheck = True 'Turn off the update of the display on the click of the control.
End With
oForm.Controls.Add(CheckBox3)' Add the check box control to the form.
AddHandler CheckBox3.Click, AddressOf checkBox3_Click
Dim NameBox3 As New System.Windows.Forms.TextBox
With NameBox3
.Top = CheckBox3.Top
.Left = CheckBox3.Right +25
.Width = 200
.Height = 25
.Name = "oNameBox3"
.Text = "SAT"
.Enabled = False
End With
oForm.Controls.Add(NameBox3)
']
'[Check Box4
Dim CheckBox4 As New System.Windows.Forms.CheckBox'Create and initialize a CheckBox.
With CheckBox4
.Text = "PROCESS ALL"
.Top = CheckBox3.Bottom
.Left = 25
.Width = 20
.Height = 25
.Name = "oCheckBox4"
.Appearance = Appearance.Normal ' Make the check box control appear as a toggle button.
.AutoCheck = True 'Turn off the update of the display on the click of the control.
End With
oForm.Controls.Add(CheckBox4)' Add the check box control to the form.
AddHandler CheckBox4.Click, AddressOf checkBox4_Click
Dim NameBox4 As New System.Windows.Forms.TextBox
With NameBox4
.Top = CheckBox4.Top
.Left = CheckBox4.Right +25
.Width = 200
.Height = 25
.Name = "oNameBox4"
.Text = "PROCESS ALL"
.Enabled = False
End With
oForm.Controls.Add(NameBox4)
']
Dim oStartButton As New System.Windows.Forms.Button
With oStartButton
.Text = "START"
.Top = CheckBox4.Bottom + 10
.Left = 25
.Height = 25
.Width = 75
.Enabled = True
.Name = "StartBox"
End With
oForm.AcceptButton = oStartButton
oForm.Controls.Add(oStartButton)
AddHandler oStartButton.Click, AddressOf oStartButton_Click
Dim oCancelButton As New System.Windows.Forms.Button
With oCancelButton
.Text = "CANCEL"
.Top = oStartButton.Top
.Left = oStartButton.Right + 10
.Height = 25
.Width = 75
.Enabled = True
.Name = "CancelBox"
End With
oForm.AcceptButton = oCancelButton
oForm.Controls.Add(oCancelButton)
AddHandler oCancelButton.Click, AddressOf oCancelButton_Click
End Sub
Private Sub checkBox1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
Dim oBox1 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox1")
If oBox1.Checked = True
Operation.add(oBox1.Text)
Else
Operation.Remove(oBox1.Text)
End If
End Sub
Private Sub checkBox2_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
Dim oBox2 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox2")
If oBox2.Checked = True
Operation.add(oBox2.Text)
Else
Operation.Remove(oBox2.Text)
End If
End Sub
Private Sub checkBox3_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
Dim oBox3 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox3")
If oBox3.Checked = True
Operation.add(oBox3.Text)
Else
Operation.Remove(oBox3.Text)
End If
End Sub
Private Sub checkBox4_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
Dim oBox1 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox1")
Dim oBox2 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox2")
Dim oBox3 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox3")
Dim oBox4 As System.Windows.Forms.CheckBox = Me.Controls.Item("oCheckBox4")
If oBox4.Checked = True
oBox1.Checked = True
oBox2.Checked = True
oBox3.Checked = True
Operation.add(oBox4.Text)
Else
oBox1.Checked = False
oBox2.Checked = False
oBox3.Checked = False
Operation.Remove(oBox3.Text)
End If
End Sub
Private Sub oStartButton_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
Me.Close
ProcessRefDocuments(ThisApplication.ActiveDocument)
End Sub
Private Sub oCancelButton_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
Me.Close
End Sub
Sub ProcessRefDocuments(oDoc As Document)
Dim oDXFDirectory As String
Dim oStepDirectory As String
Dim oStpFullFileName As String
Dim oSatDirectory As String
Dim oSatFullFileName As String
Dim oDxfFullFileName As String
Dim oAsmPN As String = oDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
If Operation.Contains("DXF")Or Operation.Contains("PROCESS ALL") Then
oDXFDirectory = CreateFolder(oDoc, " DXF Files")
End If
If Operation.Contains ("STEP") Or Operation.Contains("PROCESS ALL") Then'Or Operation.Contains("PROCESS ALL")
oStepDirectory = CreateFolder(oDoc, "STEP Files")
oStpFullFileName = oStepDirectory & "\" & oAsmPN & ".stp"
SaveAs(oDoc, oStpFullFileName)
End If
If Operation.Contains ("SAT") Or Operation.Contains("PROCESS ALL") Then
oSatDirectory = CreateFolder(oDoc, " SAT Files")
oSatFullFileName = oSatDirectory & "\" & oAsmPN & ".sat"
SaveAs(oDoc,oSatFullFileName)
End If
'Look at the files referenced by the assembly and work the referenced models.
For Each oRefDoc As Document In oDoc.AllReferencedDocuments
If oRefDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
Dim oModelPN As String = oRefDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
If Operation.Contains("STEP") Or Operation.Contains("PROCESS ALL")Then
oStpFullFileName = oStepDirectory & "\" & oModelPN & ".stp"
oStpFullFileName = Nothing
SaveAs(oRefDoc, oStpFullFileName)
End If
If Operation.Contains ("SAT") Or Operation.Contains("PROCESS ALL") Then
oSatFullFileName = Nothing
oSatFullFileName = oSatDirectory & "\" & oModelPN & ".sat"
SaveAs(oRefDoc, oSatFullFileName)
End If
If Operation.Contains("DXF") Or Operation.Contains("PROCESS ALL")Then
'FilterSheetmetal Parts.
If oRefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
oDxfFullFileName = Nothing
oDxfFullFileName = oDXFDirectory & "\" & oModelPN & ".dxf"
CreateDXF(oRefDoc ,oDxfFullFileName)
End If
End If
Next
MessageBox.Show("Batch Processing Rule Finished!","iLogic")
End Sub
Function CreateFolder(oDoc As Document, oFolderName As String ) As String
Dim oPath As String = IO.Path.GetDirectoryName(oDoc.FullFileName)
Dim oAsmName As String = IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
'Get Target folder path.
Dim oFolder As String = oPath & "\" & oAsmName & " " & oFolderName
'Check For the Step folder And create it If it does Not exist.
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If
Return oFolder
End Function
Sub SaveAs(oDoc,oFullFileName)
Try
oDoc.SaveAs(oFullFileName, True)
Catch ex As Exception
'Logger.Info("Error Saving As " & oFullFileName)
End Try
End Sub
Sub CreateDXF(oDoc As PartDocument, oFullFileName As String)
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
oDataMedium.FileName = oFullFileName
oDoc = ThisApplication.Documents.Open(oDoc.FullFileName, False)
Try
Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2010&OuterProfileLayer=IV_OUTER_PROFILE&OuterProfileLayerColor=0;0;255"& _
"&TangentLayer=IV_TANGENT&TangentLayerColor=255;0;0&BendUpLayer=IV_BEND&BendUpLayerColor=255;255;0"& _
"&BendDownLayer=IV_BEND_DOWN&BendDownLayerColor=255;128;0&InteriorProfilesLayer=IV_INTERIOR_PROFILES"& _
"&InteriorProfilesLayerColor=255;0;0&MergeProfilesIntoPolyline=True"
'MessageBox.Show("Message", "Title")
oCompDef.DataIO.WriteDataToFile(sOut, oDataMedium.FileName)
oCompDef.FlatPattern.ExitEdit
Catch
End Try
oDoc.Close
End Sub
End Class
Public Class RunMyForm
Private Sub Main
Dim oMyForm As New WinForm
oMyForm.Show
End Sub
End Class
If this solved a problem, please click (accept) as solution.
Or if this helped you, please, click (like)
Regards
Alan