AddReference "System.Drawing" Imports System.Drawing Imports System.Windows.Forms Public Class RunMyForm Private Sub Main 'this create a new instance of the form above Dim oMyForm As New WinForm(ThisApplication) 'this visibly launches the form oMyForm.Show End Sub End Class Public Class WinForm Inherits System.Windows.Forms.Form 'declare any thing here that you want to use/access throughout all Subs & Functions Private _InvApp As Inventor.Application Private _Form As System.Windows.Forms.Form Private _NameBox As System.Windows.Forms.TextBox Private _DataTypeBox As System.Windows.Forms.ComboBox Private _ValueBox As System.Windows.Forms.TextBox Private WithEvents _BrowseButton As System.Windows.Forms.Button Private _FolderBox As System.Windows.Forms.TextBox Private _FileTypesBox As System.Windows.Forms.ComboBox Private WithEvents _StartButton As System.Windows.Forms.Button Private WithEvents _CancelButton As System.Windows.Forms.Button Private oArial10 As System.Drawing.Font = New Font("Arial", 10) Private oArial12Bold As System.Drawing.Font = New Font("Arial", 10, FontStyle.Bold) Private oArial12BoldUnderLined As System.Drawing.Font = New Font("Arial", 12, FontStyle.Underline) Public Sub New(InventorApp As Inventor.Application) 'creates the new instance _InvApp = InventorApp Initialize() End Sub Private Sub Initialize() _Form = Me With _Form .SuspendLayout .FormBorderStyle = FormBorderStyle.FixedToolWindow .StartPosition = FormStartPosition.CenterScreen .Width = 750 .Height = 400 .TopMost = True .Font = oArial10 .Text = "Ensure This iProperty Exists In All Inventor Files In Chosen Directory." .Name = "iProperty Batch Publisher" .ShowInTaskbar = False End With Dim oMainLabel As New System.Windows.Forms.Label With oMainLabel .Text = "Enter iProperty Data, Select Folder, Select File Types, Then Click Start." .Font = oArial12BoldUnderLined .Top = 10 .Left = 25 .Height = 20 .Width = 600 .TabStop = False End With _Form.Controls.Add(oMainLabel) Dim oNameBoxLabel As New System.Windows.Forms.Label With oNameBoxLabel .Text = "Name" .Font = oArial12Bold .Top = oMainLabel.Bottom + 10 .Left = 25 .Height = 20 .Width = 150 .TabStop = False End With _Form.Controls.Add(oNameBoxLabel) _NameBox = New System.Windows.Forms.TextBox With _NameBox .Text = "" .Top = oNameBoxLabel.Bottom + 1 .Left = 25 .Width = 200 .Height = 25 .Name = "NameBox" .TabIndex = 1 '.PlaceholderText = "iProperty's Name" End With _Form.Controls.Add(_NameBox) Dim oDataTypeBoxLabel As New System.Windows.Forms.Label With oDataTypeBoxLabel .Text = "Value's Data Type" .Font = oArial12Bold .Top = _NameBox.Bottom + 10 .Left = 25 .Height = 20 .Width = 250 .TabStop = False End With _Form.Controls.Add(oDataTypeBoxLabel) _DataTypeBox = New ComboBox() Dim oDataTypes() As String = {"Text (String)","Number (Double)","True or False (Boolean)","Date"} With _DataTypeBox .DropDownStyle = ComboBoxStyle.DropDownList .Items.Clear() .Items.AddRange(oDataTypes) .SelectedIndex = 1 .Top = oDataTypeBoxLabel.Bottom + 1 .Left = 25 .Width = 150 .Name = "DataTypeBox" .TabIndex = 2 End With _Form.Controls.Add(_DataTypeBox) Dim oValueBoxLabel As New System.Windows.Forms.Label With oValueBoxLabel .Text = "Value" .Font = oArial12Bold .Top = _DataTypeBox.Bottom + 10 .Left = 25 .Height = 20 .Width = 150 .TabStop = False End With _Form.Controls.Add(oValueBoxLabel) _ValueBox = New System.Windows.Forms.TextBox With _ValueBox .Text = "" .Top = oValueBoxLabel.Bottom + 1 .Left = 25 .Width = 350 .Height = 25 .Name = "ValueBox" .TabIndex = 3 End With _Form.Controls.Add(_ValueBox) _BrowseButton = New Button() With _BrowseButton .Text = "BROWSE FOR FOLDER" .Top = _ValueBox.Bottom + 10 .Left = 25 .Height = 25 .Width = 175 .Enabled = True .Name = "BrowseButton" .TabIndex = 4 End With _Form.Controls.Add(_BrowseButton) _FolderBox = New System.Windows.Forms.TextBox With _FolderBox .Text = "" .Top = _BrowseButton.Top .Left = _BrowseButton.Right + 10 .Width = 500 .Height = 25 .ReadOnly = True 'just to show the user the full path of the selected folder, not for them to manually edit .Name = "FolderBox" '.TabStop = False End With _Form.Controls.Add(_FolderBox) Dim oFileTypesBoxLabel As New System.Windows.Forms.Label With oFileTypesBoxLabel .Text = "Choose File Types To Target" .Font = oArial12Bold .Top = _BrowseButton.Bottom + 10 .Left = 25 .Height = 20 .Width = 250 .TabStop = False End With _Form.Controls.Add(oFileTypesBoxLabel) 'ask the user which file types they want to process (Drawings, Parts, Assemblies, Parts & Assemblies, All Three) _FileTypesBox = New ComboBox() Dim oFileTypeChoices() As String = {"Drawings Only","Parts Only","Assemblies Only","Parts & Assemblies Only","Drawings & Parts & Assemblies"} With _FileTypesBox .DropDownStyle = ComboBoxStyle.DropDownList .Items.Clear() .Items.AddRange(oFileTypeChoices) .SelectedIndex = 4 .Top = oFileTypesBoxLabel.Bottom + 1 .Left = 25 .Width = 225 .Name = "FileTypesBox" .TabIndex = 5 End With _Form.Controls.Add(_FileTypesBox) _StartButton = New Button() With _StartButton .Text = "START" .Top = _FileTypesBox.Bottom + 10 .Left = 25 .Height = 25 .Width = 75 .Enabled = True .Name = "StartBox" .TabIndex = 6 End With _Form.AcceptButton = _StartButton _Form.Controls.Add(_StartButton) _CancelButton = New Button With _CancelButton .Text = "CANCEL" .Top = _StartButton.Top .Left = _StartButton.Right + 10 .Height = 25 .Width = 75 .Enabled = True .Name = "CancelButton" .TabIndex = 7 End With _Form.CancelButton = _CancelButton _Form.Controls.Add(_CancelButton) _Form.ResumeLayout() 'This is the end of the main Sub that defines physical features of the Form End Sub '<<<< THESE OTHER SUBS/FUNCTIONS WITHIN THIS CLASS, INSTRUCT WHAT TO DO WHEN EVENTS HAPPEN >>>>>>> Private Sub _BrowseButton_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) Handles _BrowseButton.Click 'MsgBox("You just clicked the [" & oSender.Text & "] button.", vbOKOnly + vbInformation, "EVENT FEEDBACK") Dim oPath As String = ChooseFolder If oPath = "" Then MsgBox("No folder was chosen. Exiting.", , "") Exit Sub End If 'put path data into _FolderBox Dim oPathBox As System.Windows.Forms.TextBox = Me.Controls.Item("FolderBox") oPathBox.Text = oPath End Sub Private Function ChooseFolder() As String Dim oFolder As String = "" Dim oPlaceHolder As String = "Selection" Dim oDirSepChar As Char = System.IO.Path.DirectorySeparatorChar Dim oOpenDlg As New System.Windows.Forms.OpenFileDialog oOpenDlg.Title = "Select A Folder." oOpenDlg.InitialDirectory = _InvApp.DesignProjectManager.ActiveDesignProject.WorkspacePath oOpenDlg.Filter = "" oOpenDlg.Multiselect = False oOpenDlg.RestoreDirectory = False oOpenDlg.CheckFileExists = False oOpenDlg.CheckPathExists = True oOpenDlg.ValidateNames = False oOpenDlg.FileName = oPlaceHolder oOpenDlg.Filter = "" oOpenDlg.AddExtension = False oOpenDlg.DefaultExt = "" Dim oResult = oOpenDlg.ShowDialog If oResult = vbOK Then If oOpenDlg.FileName <> vbNullString Then oFolder = oOpenDlg.FileName Else MsgBox("No file was selected. Exiting.", vbExclamation, "FILE NOT SELECTED") Exit Function End If ElseIf oResult = vbCancel Then MsgBox("The dialog was Canceled. Exiting.", vbInformation, "CANCELED") Exit Function End If 'count the number of characters from the end, until it finds the directory separator character Dim oPos As Integer = InStr(StrReverse(oFolder), oDirSepChar) 'check if the directory separator character was at the end, if so, perfect, if not, eliminate end text If oPos <> 1 Then 'the directory seperator character was not at the end, so... oFolder = Strings.Left(oFolder, InStrRev(oFolder, oDirSepChar)) End If Return oFolder End Function Private Sub _StartButton_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) Handles _StartButton.Click Dim oPropName As String = Me.Controls.Item("NameBox").Text If oPropName = "" Then MsgBox("No Property Name was specified. Exiting.", , "") Exit Sub End If 'could specify data type by Enum or List of available types to choose from (drop-down list) (InputListBox or Form input) Dim oValDataType As String = Me.Controls.Item("DataTypeBox").Text Dim oPropVal As String = Me.Controls.Item("ValueBox").Text If oPropVal = "" Then oValAns = MsgBox("No Property Value was specified." & vbCrLf & _ "Is this how you want it?", vbYesNo + vbQuestion, "") If oValAns = vbNo Then Exit Sub End If 'check the input property Data Type first, then try to create the property with the value converted to that Type ''"Text (String)" ; "Number (Double)" ; "True or False (Boolean)" ; "Date" Dim oVal As Object If oValDataType = "Text (String)" Then 'no Type conversion needed oVal = oPropVal ElseIf oValDataType = "Number (Double)" Then oVal = CDbl(oPropVal) ElseIf oValDataType = "True or False (Boolean)" Then oVal = CBool(oPropVal) ElseIf oValDataType = "Date" Then oVal = CDate(oPropVal) End If Dim oTargetFolder As String = Me.Controls.Item("FolderBox").Text If oTargetFolder = "" Then MsgBox("No folder chosen. Exiting.", , "") Exit Sub End If '<<<< Determine which file types they want to target here >>>> Dim oFilters As New List(Of String) 'get what we selected in the form Dim oSelectedFileTypes As String = Me.Controls.Item("FileTypesBox").Text 'look that value up in our dictionary, to get the array of file extensions needed for them Dim oFileTypeDict As New Dictionary(Of String, String()) From {{"Drawings Only", {"*.idw" }}, {"Parts Only", {"*.ipt" }}, {"Assemblies Only", {"*.iam" }}, {"Parts & Assemblies Only", {"*.ipt", "*.iam" }}, {"Drawings & Parts & Assemblies", {"*.idw", "*.ipt", "*.iam" }}} oFilters.AddRange(oFileTypeDict.Item(oSelectedFileTypes)) 'use the GetFiles once for each filter, and add the results to a New List(Of String) using its AddRange() Dim oFileNames As New List(Of String) For Each oFilter As String In oFilters Dim oFiles() As String = System.IO.Directory.GetFiles(oTargetFolder, oFilter, IO.SearchOption.TopDirectoryOnly) oFileNames.AddRange(oFiles) Next 'get an array of fine names that match the search criteria within that directory (without searching in sub-directories) 'should either return one string, or may return no string if not found (oFileNames.Length = 0) If oFileNames.Count = 0 Then 'no files found matching search criteria 'tell user the file name is available, and ask if they want to save it there with that name. MsgBox("No files match the search criteria in the selected folder. Exiting.", , "") Exit Sub End If 'start checking each file for the specified iProperty 'try checking for iProperty without opening file ' -if it is there, check if Value is the same, if Value is the same, skip to next file (Continue For) ' -if it isn't there, we will likely have to open the document before we can add the new iProperty For Each oFileName As String In oFileNames Dim oExt As String = System.IO.Path.GetExtension(oFileName) 'includes the dot (.) Dim oOpenOptions As NameValueMap = _InvApp.TransientObjects.CreateNameValueMap oOpenOptions.Add("FileVersionOption", FileVersionEnum.kOpenCurrentVersion)' use for All document types oOpenOptions.Add("SkipAllUnresolvedFiles", True) 'used for All document types 'must check document type before specifying these options (document type specific) If oExt = ".idw" Then oOpenOptions.Add("DeferUpdates", True) 'only used for Drawings ElseIf oExt = ".iam" Then oOpenOptions.Add("ExpressModeBehavior", "OpenExpress") 'only used for Assemblies End If Dim oDoc As Document Try 'oDoc = _InvApp.Documents.OpenWithOptions(oFileName, oOpenOptions, False) oDoc = _InvApp.Documents.Open(oFileName, False) Catch MsgBox("Error while attempting to open the following file:" & vbCrLf & _ oFileName & vbCrLf & _ "Skipping to the next file.", , "") Continue For End Try Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties") Dim oCProp As Inventor.Property 'this is where you check if the Property already exists or not Try oCProp = oCProps.Item(oPropName) oCProp.Value = oPropVal Catch oCProp = oCProps.Add(oVal, oPropName) Catch MsgBox("Error while trying to update or create property in the following file:" & vbCrLf & oFileName & vbCrLf & "Skipping to next file.",,"") End Try If oDoc.Dirty Then oDoc.Save2(False) End If oDoc.Close(True) 'True = SkipSave (close silently) Next MsgBox("All files have been processed.",,"") End Sub Private Sub _CancelButton_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs) Handles _CancelButton.Click Me.Close End Sub End Class