Full code now reads:
To replace title block and border, and place sketch symbols for all drawings in a folder
Batch Update
Sub Main()
' On Error GoTo errtrap
'setup to read in the log file
Dim oFile As String = "Text file for report.txt"'must already exist
Dim oReader As System.IO.StreamReader = New System.IO.StreamReader(oFile)
Dim DoneList As String
Dim line As String
'read in log file
Do
line = oReader.ReadLine()
DoneList = DoneList & line
Loop Until line Is Nothing
'close log for reader
oReader.Close()
Dim Folder As New IO.DirectoryInfo("Folder location containing all drawings to be updated")'Folder in which drawings to be updated are located
Dim FileList As New List(Of String)'creates a list of files
For Each File As IO.FileInfo In Folder.GetFiles("*.idw", IO.SearchOption.AllDirectories)'runs below "If" loop on every file with ".idw" file extension
If File.FullName.Contains("OldVersions") = False Then'exludes all files that have "OldVersions" in their filepath from the next line
'to exclude any files with a specific string of characters from being processed, remove the ' from the next 5 lines & change "Your Text here" to you character string
' If File.FullName.Contains("Your Text here") = False Then
' If File.FullName.Contains("OldVersions") = False Then
' FileList.Add(File.FullName)
' End If
' End if
FileList.Add(File.FullName)'adds file to the list to be processed
End If
Next
'display files found by search, exit if 0
If FileList.Count > 0 Then
MsgBox(FileList.Count & " files were found")
Else
MsgBox("No more files were found. ")
Exit Sub
End If
'Shows how many files have been found and will be processed - currently approx 2m30s per file
'MessageBox.Show(FileList.Count & " files were found", "File count", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
Dim oDoc1 As Document'variable
Dim oAppend As System.IO.StreamWriter
oAppend = IO.File.AppendText(oFile)
' Try
'
' Catch
' GoTo Close
'End Try
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'change these to false if you need to open and update from model.
oOptions.Value("DeferUpdates") = True
oOptions.Value("FastOpen") = True
For Each DocName As String In FileList'iterates through every file in the filelist
Try
'oDoc1 = ThisApplication.Documents.Open(DocName, True)'opens file
oDoc1 = ThisApplication.Documents.OpenWithOptions(DocName, oOptions, True)
Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument'variable to allow iteration through all sheets in each drawing
ThisApplication.UserInterfaceManager.DoEvents'wait until all processes have finished
'Dim Path As String = ThisDoc.PathAndFileName(False)'gets the path of the active document
'Dim oDrawing As Inventor.DrawingDocument = ThisApplication.ActiveDocument'variable
Sheet_Count = oDoc1.Sheets.Count 'variable - counts number of sheets in opened drawing
Dim SheetNumber As Integer = 1'sets sheetnumber variable to equal 1 so below external rule is run starting on sheet 1 - passed through to "Batch - Update title block and border" rule
For Each oSheet As Sheet In oDDoc.Sheets'iterates through every sheet in the open drawing
ThisApplication.UserInterfaceManager.DoEvents'wait until all processes have finished
Dim iLogicAuto = iLogicVb.Automation'sets up iLogic Automation
Dim oArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap'creates set of arguments to be passed to external rule
oArgs.Add("SheetNumber", SheetNumber)'adds SheetNumber variable to argumetns to be passed through
iLogicAuto.RunExternalRuleWithArguments(oDoc1, "Batch - Update title block and border", oArgs)'call for "Batch - Update title block and border" to be run from oDoc1 and passes through oArgs
SheetNumber = SheetNumber + 1'increments SheetNumber by one to active next sheet in next loop
Next 'loops the above until all pages have been processed
'record that the changes were made
oAppend.WriteLine(DateTime.Now.ToString("s") & DocName)
oDoc1.Save2(False)'saves drawing
oDoc1.Close(True)'closes drawing
'nextfile :
Catch
oAppend.WriteLine(DateTime.Now.ToString("s") & DocName & " < an error occured")
oDoc1.Close(True)
End Try
Next'moves to the next file in filelist
'close the writer when done
close:
oAppend.Close()
MessageBox.Show("Done! " & FileList.Count & " files processed. " & vbCrLf & "See " & oFile & " for log", "Drawing update complete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
'MessageBox.Show("Done!" & FileList.Count & " files were processed" & +vbCrLf+ & "See " & oFile " for log", "Rule complete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
Exit Sub
' errtrap:
' Select Case Err.Number
' Case Else
' oAppend.WriteLine(DateTime.Now.ToString("s") & DocName & " < an error occured")
' oDoc1.Close(True)
'
' Resume nextfile
' End Select
'
End Sub
Batch - Update Title Block and Border
Sub Main()
Dim SheetNumber As Integer = RuleArguments("SheetNumber")
'MessageBox.Show(SheetNumber, "SheetNumber Value", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
Resize(SheetNumber)
UpdateStyles()
ChangeStyle()
ChangeBorder()
ChangeTitleBlock()
AddOADimsTable(SheetNumber)
AddRevTable()
End Sub
Sub Resize(SheetNumber)
Dim oDrawingDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oDrawing As DrawingDocument = ThisApplication.ActiveDocument 'variable
Sheet_Count = oDrawing.Sheets.Count 'variable
Sheet_Size = ActiveSheet.Size
'MessageBox.Show("1", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
oDrawingDoc.Sheets(SheetNumber).Activate 'activates sheet
'MessageBox.Show("2", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
ThisApplication.UserInterfaceManager.DoEvents
'i = MessageBox.Show(Sheet_Size, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
If Sheet_Size = "A2"'checks sheet size
'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A2 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Else If Sheet_Size = "A3"'
'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A3 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Else If Sheet_Size = "A4"
ThisApplication.ActiveDocument.ActiveSheet.Orientation = kLandscapePageOrientation
ActiveSheet.ChangeSize("A3", moveBorderItems := True) '
Sheet_Size = ActiveSheet.Size
'i = MessageBox.Show("Page resized!", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In A4 branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Else
'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In custom branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
ThisApplication.ActiveDocument.ActiveSheet.Orientation = kLandscapePageOrientation
ActiveSheet.ChangeSize("A2", moveBorderItems := True) 'sets sheet to A2
Sheet_Size = ActiveSheet.Size
'i = MessageBox.Show("Page resized!", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
'i = MessageBox.Show("Sheet size is " & Sheet_Size & vbCrLf & "In custom branch", "Page size", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End If
End Sub
Sub UpdateStyles()
OpenDoc = ThisDoc.Document
oStyles = OpenDoc.StylesManager.Styles
For Each oStyle As Style In oStyles
If Not oStyle.UpToDate Then
oStyle.UpdateFromGlobal
End If
Next
End Sub
Sub ChangeStyle()
Dim oDrawingDoc As DrawingDocument = ThisDoc.Document
'the styles collection
Dim oStandardDesignStyles As DrawingStandardStylesEnumerator = oDrawingDoc.StylesManager.StandardStyles
'find the right style and set it then exit
Dim oStandardStyle As DrawingStandardStyle
For Each oStandardStyle In oStandardDesignStyles
If oStandardStyle.Name = "Style Name here" Then
oDrawingDoc.StylesManager.ActiveStandardStyle = oStandardStyle
Exit Sub
End If
Next
End Sub
Sub ChangeBorder()
ThisDrawing.ResourceFileName = "Full filepath of template drawing with Border.idw" 'document to copy title block and border from
Sheet_Size = ActiveSheet.Size
ActiveSheet.Border = "Border name here"'applies border to corresponding page size
End Sub
Sub ChangeTitleBlock()
ThisDrawing.ResourceFileName = "Full filepath of template drawing with title blocks.idw" 'document to copy title block and border from
Sheet_Size = ActiveSheet.Size
ActiveSheet.SetTitleBlock("Title block name here", "PLACEHOLDER - CHANGE ME", "PLACEHOLDER - CHANGE ME")'applies title block that corresponds to page size
End Sub
Sub AddOADimsTable(SheetNumber)
Dim oDrawingDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Sheet_Count = oDrawingDoc.Sheets.Count 'variable
oSymbolDrawingName = "Sketch symbols.idw"
'This is the name of the symbol to place
oSymbolName = "OVERALL DIMENSION TABLE"
Dim oSymbol As SketchedSymbol
Sheet_Size = ActiveSheet.Size 'gets active sheet size
Dim oPosition As Point2d
'define the drawing document
oDrawingDoc = ThisApplication.ActiveDocument
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
oSketchSymLib = oDrawingDoc.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item(oSymbolDrawingName)
Dim oSymDef As SketchedSymbolDefinition
'place the symbol in the Drawing Resources folder
oSymDef = oDrawingDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, oSymbolName, True)
oDrawingDoc.Sheets(SheetNumber).Activate 'activates sheet
Sheet_Size = ActiveSheet.Size
If Sheet_Size = "A2"
oPosition= ThisApplication.TransientGeometry.CreatePoint2d(45.65, 2.5)
oSymbol = oDrawingDoc.ActiveSheet.SketchedSymbols.Add(oSymDef, oPosition, 0, 1, )
Else If Sheet_Size = "A3"
oPosition= ThisApplication.TransientGeometry.CreatePoint2d(28.25, 2.5)
oSymbol = oDrawingDoc.ActiveSheet.SketchedSymbols.Add(oSymDef, oPosition, 0, 1, )
Else
'i = MessageBox.Show("Page size should be A2 or A3", "Incorrect page size!", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End If
End Sub
Sub AddRevTable()
Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim Sheet_Size
Dim X_Pos
Dim Y_Pos
Dim oRTBs As RevisionTables = oDrawDoc.ActiveSheet.RevisionTables
Sheet_Size = ActiveSheet.Size
If Sheet_Size = "A2"
X_Pos = 47.4
Y_Pos = 41
Else If Sheet_Size = "A3"
X_Pos = 30
Y_Pos = 28.7
End If
Dim oLocation As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos, Y_Pos)
'add a new table
Dim oRTB As RevisionTable
oRTB = oRTBs.Add2(oLocation, True, True, False, "00")
End Sub