Option Explicit on AddReference "System.Drawing" Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms Imports Microsoft.VisualBasic.FileSystem Private Sub Main Dim sSavePath As String = "C:\Temp\pth.txt" ' <--------------------------------- Speicherpfad ggf. anpassen --------------------------------------------------- Dim oApp As Inventor.Application = ThisApplication If Not oApp.ActiveDocumentType = DocumentTypeEnum.kDrawingDocumentObject Then MsgBox("Funktion nur in Zeichnungen verfügbar",MsgBoxStyle.Exclamation, "Export") Exit Sub End If Dim oDrawDoc As DrawingDocument = oApp.ActiveDocument Dim sNamePN As String = oDrawDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Part Number").Value Dim sNameRE As String= oDrawDoc.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}").Item("Revision Number").Value Dim sNameBE As String= oDrawDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Description").Value Dim sNameDA As String If Not Format(oDrawDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Creation Time").Value,"dd-MM-yyyy") = "" Then sNameDA = Format(oDrawDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Creation Time").Value,"dd-MM-yyyy") Dim sFolder As String If System.IO.File.Exists(sSavePath) Then Dim MyComputer As New Microsoft.VisualBasic.Devices.Computer sFolder = MyComputer.FileSystem.ReadAllText(sSavePath) If Not System.IO.Directory.Exists(sFolder) Then sFolder = "" End If End If Dim oMyForm As New WinForm(sSavePath, sFolder, sNamePN, sNameRE, sNameBE, sNameDA) Dim oResult As DialogResult = oMyForm.ShowDialog If oResult=DialogResult.OK Then sFolder = oMyForm.Controls.Item("TBPath").Text Dim sName as String = oMyForm.Controls.Item("TBName").Text Dim cBoxPDF As CheckBox = oMyForm.Controls("cboxPDF") Dim bPDF As Boolean = cBoxPDF.Checked Dim cBoxSTP As CheckBox = oMyForm.Controls("cboxSTP") Dim bSTP As Boolean = cBoxSTP.Checked Export(oDrawDoc, sName, sFolder, bPDF, bSTP) MsgBox("Verarbeitung beendet",MsgBoxStyle.Information,"Export") Else 'User canceled out, nothing more to do End If End Sub Private Sub Export(ByVal oDrawDoc As DrawingDocument, ByVal sName As String, ByVal sFolder As String, ByVal bPDF As Boolean, bSTP As Boolean) Dim oApp As Inventor.Application = ThisApplication Dim oDoc As Document = ThisDrawing.ModelDocument Dim strName As String = sFolder & "\" & sName If bSTP = True Then Dim oCurrentView As Inventor.View = oApp.ActiveView Dim oView As Inventor.View Dim bNewView As Boolean = False If oDoc.Views.Count = 0 Then oView = oDoc.Views.Add bNewView=True Else oView = oDoc.Views(1) End If oView.Activate STP_Export(oDoc, strName) If bNewView = True Then oView.Close End If ocurrentview.Activate End If If bPDF = True Then PDF_Export(oDrawDoc, strName) End If End Sub Private Sub PDF_Export(ByVal oDrawDoc As DrawingDocument, ByVal strname As String) Dim PDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}") If PDFAddIn Is Nothing Then Exit Sub PDFAddIn.Activate Dim oPDFContext As TranslationContext= ThisApplication.TransientObjects.CreateTranslationContext Dim oPDFOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap oPDFContext.Type = IOMechanismEnum.kFileBrowseIOMechanism oPDFOptions.Value("All_Color_AS_Black") = 1 oPDFOptions.Value("Remove_Line_Weights") = 0 oPDFOptions.Value("Vector_Resolution") = 400 oPDFOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets Dim oDataMediumPDF As DataMedium = ThisApplication.TransientObjects.CreateDataMedium oDataMediumPDF.FileName = strname & ".pdf" PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oDataMediumPDF) End Sub Private Sub STP_Export(ByVal oDoc As Document, ByVal strname As String) Dim oSTPAddin As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}") If oSTPAddin Is Nothing Then Exit Sub oSTPAddin.Activate Dim oSTPContext As TranslationContext= ThisApplication.TransientObjects.CreateTranslationContext Dim oSTPOptions As NameValueMap= ThisApplication.TransientObjects.CreateNameValueMap oSTPOptions.Value("ApplicationProtocolType") = 3 'oSTPOptions.Value("Author") = "" 'oSTPOptions.Value("Authorization") = "" 'oSTPOptions.Value("Description") = "" 'oSTPOptions.Value("Organization") = "" oSTPContext.Type = IOMechanismEnum.kFileBrowseIOMechanism Dim oDataMediumSTP As DataMedium = ThisApplication.TransientObjects.CreateDataMedium oDataMediumSTP.FileName = strname & ".stp" oSTPAddin.SaveCopyAs(oDoc, oSTPContext, oSTPOptions, oDataMediumSTP) End Sub Public Class WinForm Inherits System.Windows.Forms.Form 'declare any thing here that you want to use/access throughout all Subs & Functions Public oLargerFont As System.Drawing.Font = New Font("Arial", 10) Private myName As String Private oCheckBoxPN As New System.Windows.Forms.CheckBox Private oCheckBoxRE As New System.Windows.Forms.CheckBox Private oCheckBoxBE As New System.Windows.Forms.CheckBox Private oCheckBoxDA As New System.Windows.Forms.CheckBox Private oTextBoxFolder As New System.Windows.Forms.TextBox Private oTextBoxName As New System.Windows.Forms.TextBox Private myNamePN As String Private myNameRE As String Private myNameBE As String Private myNameDA As String Private mySavePath As String Public Sub New(ByVal sSavePath As String, ByVal sFolder As String, ByVal sNamePN As String, ByVal sNameRE As String, ByVal sNameBE As String, ByVal sNameDA As String) 'creates the new instance Dim myFolder As String = sFolder mySavePath = sSavePath myNamePN = sNamePN myNameRE = sNameRE myNameBE = sNameBE myNameDA = sNameDA Dim myName As String = myNamePN & "_" & myNameRE & "_" & myNameBE & "_" & myNameDA Dim oForm As Form oForm = Me With oForm .FormBorderStyle = FormBorderStyle.FixedToolWindow .StartPosition = FormStartPosition.CenterScreen .Width = 525 .Height = 350 .TopMost = True '.Font = oLargerFont .Text = "STEP/PDF-Export" .Name = "STEP/PDF-Export" .ShowInTaskbar = False End With Dim oLabelFolder As New Label With oLabelFolder .Name = "lblFolder" .Text = "Speicherpfad" .Top = 25 .Left = 25 .Height = 25 .Width = 350 End With oForm.Controls.Add(oLabelFolder) 'Dim oTextBoxFolder As New System.Windows.Forms.TextBox With oTextBoxFolder .Name="TBPath" .ReadOnly = True If sFolder = String.Empty Then .Text = "Ablagepfad auswählen..." Else .Text = sFolder End If .Top = oLabelFolder.Top + oLabelFolder.Height + 10 .Left = 25 .Width = 350 .Height = 25 .TabStop=False End With oForm.Controls.Add(oTextBoxFolder) Dim oButtonBF As New Button() With oButtonBF .Text = "Browse..." .Top = oLabelFolder.Top + oLabelFolder.Height + 10 .Left = oTextBoxFolder.Left + oTextBoxFolder.Width + 25 .Enabled = True .AutoSize = True .TabIndex=0 End With oForm.Controls.Add(oButtonBF) AddHandler oButtonBF.Click, AddressOf oButtonBF_Click ' Bauteilnummer ' Revisionsindex ' Benennung ' Datum 'Dim oCheckBoxPN As New System.Windows.Forms.CheckBox() With oCheckBoxPN .AutoSize=True .Name = "cboxPN" .Text ="Bauteilnummer" .Top = oTextBoxFolder.Top + oTextBoxFolder.Height + 25 .Left = 25 .Checked = True .TabIndex=1 End With oForm.Controls.Add(oCheckBoxPN) AddHandler oCheckBoxPN.CheckedChanged, AddressOf oCheckBox_CheckChanged 'Dim oCheckBoxRE As New System.Windows.Forms.CheckBox() With oCheckBoxRE .AutoSize=True .Name = "cboxRE" .Text ="Revision" .Top = oCheckBoxPN.Top .Left = oCheckBoxPN.Left + oCheckBoxPN.Width + 5 .Checked = True .TabIndex=2 End With oForm.Controls.Add(oCheckBoxRE) AddHandler oCheckBoxRE.CheckedChanged, AddressOf oCheckBox_CheckChanged 'Dim oCheckBoxBE As New System.Windows.Forms.CheckBox() With oCheckBoxBE .AutoSize=True .Name = "cboxBE" .Text ="Benennung" .Top = oCheckBoxPN.Top .Left = oCheckBoxRE.Left + oCheckBoxRE.Width +5 .Checked = True .TabIndex=3 End With oForm.Controls.Add(oCheckBoxBE) AddHandler oCheckBoxBE.CheckedChanged, AddressOf oCheckBox_CheckChanged 'Dim oCheckBoxDA As New System.Windows.Forms.CheckBox() With oCheckBoxDA .AutoSize=True .Name = "cboxDA" .Text ="Datum" .Top = oCheckBoxPN.Top .Left = oCheckBoxBE.Left + oCheckBoxBE.Width+5 .Checked = True .TabIndex=4 End With oForm.Controls.Add(oCheckBoxDA) AddHandler oCheckBoxDA.CheckedChanged, AddressOf oCheckBox_CheckChanged Dim oLabelName As New Label With oLabelName .Name = "lblName" .Text = "Dateiname (ohne Endung)" .Top = oCheckBoxPN.Top + oCheckBoxPN.Height + 25 .Left = 25 .Height = 25 .Width = 350 End With oForm.Controls.Add(oLabelName) 'Dim oTextBoxName As New System.Windows.Forms.TextBox With oTextBoxName .Name="TBName" .Text = myName .Top = oLabelName.Top + oLabelName.Height + 10 .Left = 25 .Width = 350 .Height = 25 .TabIndex=5 End With oForm.Controls.Add(oTextBoxName) Dim oCheckBoxPDF As New System.Windows.Forms.CheckBox() With oCheckBoxPDF .Name = "cboxPDF" .Text ="PDF" .Top = oTextBoxName.Top + oTextBoxName.Height + 25 .Left = 25 .Checked = True .TabIndex=6 End With oForm.Controls.Add(oCheckBoxPDF) Dim oCheckBoxSTP As New System.Windows.Forms.CheckBox() With oCheckBoxSTP .Name = "cboxSTP" .Text ="STP" .Top = oTextBoxName.Top + oTextBoxName.Height + 25 .Left = oCheckBoxPDF.Left + oCheckBoxPDF.Width .Checked = True .TabIndex=7 End With oForm.Controls.Add(oCheckBoxSTP) Dim oButtonCancel As New Button() With oButtonCancel .DialogResult=DialogResult.Cancel .Text = "Schließen" .Top = oTextBoxName.Top + oTextBoxName.Height + 25 .Left = oButtonBF.Left .Enabled = True .AutoSize = True .TabIndex=8 End With oForm.Controls.Add(oButtonCancel) AddHandler oButtonCancel.Click, AddressOf oButtonCancel_Click Dim oButtonOK As New Button() With oButtonOK .DialogResult=DialogResult.OK .Text = "Export" .Top = oTextBoxName.Top + oTextBoxName.Height + 25 .Left = oButtonCancel.Left - oButtonOK.Width - 25 .Enabled = True .AutoSize = True .TabIndex=4 End With oForm.Controls.Add(oButtonOK) AddHandler oButtonOK.Click, AddressOf oButtonOK_Click oForm.AcceptButton = oButtonOK oForm.CancelButton = oButtonCancel 'This is the end of the main Sub that defines the Form End Sub Private Sub WinForm_FormClosing(ByVal oSender As Object, ByVal oFormCloseEvents As FormClosingEventArgs) Handles Me.FormClosing If System.IO.Directory.Exists(Me.Controls("TBPath").Text) Then 'save folder in file Dim MyComputer As New Microsoft.VisualBasic.Devices.Computer MyComputer.FileSystem.WriteAllText(mySavePath,Me.Controls("TBPath").Text,False) End If End Sub Private Sub oButtonBF_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) Dim oFolderBrowser As New FolderBrowserDialog Dim oResult As DialogResult = oFolderBrowser.ShowDialog If oResult=DialogResult.OK Then Me.Controls("TBPath").Text = oFolderBrowser.SelectedPath End If End Sub Private Sub oButtonOK_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) If IsValidFileName(Me.Controls("TBName").Text) = False Then Exit Sub End If Me.close End Sub Private Sub oButtonCancel_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) Me.close End Sub Private Sub oCheckBox_CheckChanged(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) Dim sNewName As New List(Of String) If oCheckBoxPN.checked = True Then sNewName.Add(myNamePN) If oCheckBoxRE.checked = True Then sNewName.Add(myNameRE) If oCheckBoxBE.checked = True Then sNewName.Add(myNameBE) If oCheckBoxDA.checked = True Then sNewName.Add(myNameDA) oTextBoxName.Text = String.Join("_",sNewName) End Sub Private Function IsValidFileName(ByVal Name As String) As Boolean ' Determines if the name is Nothing. If Name Is Nothing Then MsgBox("Das Feld Dateiname darf nicht leer sein.", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Export") Return False End If ' Determines if the name is empty. If Name = "" Then MsgBox("Das Feld Dateiname darf nicht leer sein.", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Export") Return False End If ' Determines if there are bad characters in the name. Dim BadChars() As Char = System.IO.Path.GetInvalidFileNameChars For Each BadChar As Char In BadChars If InStr(Name, BadChar) > 0 Then MsgBox("Der Dateiname enthält mindestens" & vbCrLf & "ein ungültiges Zeichen.",MsgBoxStyle.Exclamation,"Export") Return False End If Next ' The name passes basic validation. Return True End Function End Class