Monday, August 15, 2011

iLogic: Replace Revision Level Component

Issue:
When you replace revised components in your assembly using the Replace or Replace All tools, you find yourself needing to browse through a lot of folders. You'd like to create an iLogic rule to look at the same folder path as the file being replaced.

Solution:
Here is a sample rule that uses the folder path and file name of a selected component to look for the next revision level. The input box defaults to 1+ the selected revision level, but allows the user to input any number. A Yes/No message allows the user to see exactly what file is to replace the selected one. If the file is not found a message is displayed.

I set this rule up as an External Rule, and run the rule while editing a top level assembly.

 

If I've forgotten to pre-select a file to replace I am greeted with a message telling me I must pre-select a component:


When a file is pre-selected and the rule is run,  I am greeted with an input box asking for the revision level to replace the selected file with. The input defaults to 1+ the value of the selected file's revision level:


Next a message displays the file path and name of the selected file and the revised file that will replace it:


When I click Yes, the replacement is made.



In the event that I enter a revision level that creates a path to a file that does not exist, I receive a message telling me about it:




Although I've used the file path and name to get the revision level in this example, I could have used the revision iProperty to pull in the revision level as well.

Here is the example rule:




'----start of ilogic code-------
'get currently selected component
Dim oOccurrence as ComponentOccurrence
Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  MessageBox.Show("Please select a component before running this rule.", "iLogic")
  Return
End Try

Dim doc As Document
Dim CurFileName As String
Dim NewFileName As String

'set the selected item
oOccurrence = ThisApplication.ActiveDocument.SelectSet.Item(1)

'get the selected item document occurrence name
doc = oOccurrence.Definition.Document

'get the path and file name of the selected item
CurFileName = doc.FullFileName

'defines backslash as the subdirectory separator
Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar

'set array for each folder name found between backslashes in the source directory path
Dim oPathItems() As Object = CurFileName.Split(strCharSep)

'parse the path of the file, to get the folder names
'Example: N:\12-3456-78\Design Outputs\Rev #
Dim strDrive As String = oPathItems(0)‘returns N:
Dim strPN As String = oPathItems(1)'returns: 12-3456-78
Dim strOutputs As String = oPathItems(2)'returns: Design Outputs
Dim strOrigRevNum As String = oPathItems(3) 'returns: Rev #
Dim intOrigRevNum As Integer = Mid(strOrigRevNum,5) 'returns #
Dim strExt As String = LCase(System.IO.Path.GetExtension(CurFileName)) 'File Extension

‘get new Revision Number
'default input is 1+ the existing revison number, but any number can be input
Dim strRevNum As String = InputBox("Enter New Rev Number", “iLogic", intOrigRevNum +1)
if strRevNum = “” Then
Return
Else
End if

‘compile new name
NewFileName = strDrive & “\” & strPN & “\” &strOutputs & “\Rev ” _
& strRevNum & “\” & strPN & “ Rev “ & strRevNum & strExt

'check to see if the new revision level file exists
If(Not My.Computer.FileSystem.FileExists(NewFileName)) Then
MessageBox.Show(NewFileName & vbLF & “ does not exist.”, "File Does Not Exist")
Return
Else
End if

'provide feedback of the file replacement and ask if the user is sure
if MessageBox.Show(“This will replace: “ & vbLF & _
CurFileName & vbLF & _
  with: “ & vbLF & _
NewFileName & vbLF & _
  Are you sure?”, _
"iLogic", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button1) = vbYes Then

‘Replace all occurences, True = Replace All
oOccurrence.Replace (NewFileName, True)
Else
End if
'----end of ilogic code-------