Thursday, April 28, 2011

Using iLogic to Trim a File Name to Populate iProperties

Issue:
You name your files with long file names consisting of a part number and some other information, and you’d like to extract that information from the file name to automatically populate the iProperties. For instance you have a file name made up of a part number and a revision number such as:
34-0987 Rev 2.ipt



Solution:
Combining the part number and revision number in the file name is actually the convention I use every day at the office. We employ a very structured part numbering/file naming system that allows us to keep an organized file system. Having the revision number in the file name removes all guess work involved with rolling component revision levels in assemblies. 

I use a simple iLogic rule in my files to extract the information from the file names and write it to the part number and revision number iProperties. The trick is using VB text string manipulation functions to strip off the information not needed.

For instance this line of code uses the VB Left function to count from the left of the string and keep only the first 7 characters for the iLogic ThisDoc.FileName(False) function:

iProperties.Value("Project", "Part Number") = (Left(ThisDoc.FileName(False),7))

The False option of the FileName function specifies the file name without the extension (34-0987 Rev 2.ipt becomes just 34-0987 Rev 2).
The VB Left function syntax is:  Left (string, length)

So this line returns: 34-0987 to the Part Number iProperty.

This next line returns the revision number by starting at the left of the file name and keeping only one character.

iProperties.Value("Project", "Revision Number") = (Right(ThisDoc.FileName(False),1))

The VB Right function syntax is:  Right (string, length)

So this line returns: 2 to the Revision Number iProperty. 
 
But what if some of our revision numbers vary in the number of characters? 
For instance: 34-0987 Rev 10 and/or 34-0987 Rev 0.3

In that case we’d use the Mid function such as this:
iProperties.Value("Project", "Revision Number") = (Mid(ThisDoc.FileName(False),13))

This line starts at the 13th character from the left and keeps everything from there on. 

So it returns: 
10 (where 34-0987 Rev 10 is the part number)
0.3 (where 34-0987 Rev 0.3 is the part number)
2 (where 34-0987 Rev 2 is the part number)

The VB Mid function syntax used here is:  Mid (string, start)

If we had a long file name such as 34-0987, 4x4 Steel Bracket, Rev 5.ipt we could use the Mid function to selectively write part of the file name to the Description iProperties by specifying not only the start but also the length of the text string to select. In this case starting at character 10 and  selecting the next 17 characters. 

iProperties.Value("Project", "Description") = (Mid(ThisDoc.FileName(False),10,17))

This returns: 4x4 Steel Bracket as the description. This illustrates the use of the Mid function with a Length, but of course to use that line effectively, your description would need to always remain the same number of characters. However, you might be able to use the Len function to get the character count of the string and create a function in the code to calculate the number of characters remaining once the file name and Rev number are subtracted, see the link below for more on the Len function and other VB String Manipulation tools.

The VB Mid function syntax for specifying the length also is:  Mid (string, start, length)
You can find more information on VB Text String Manipulation here:
http://msdn.microsoft.com/en-us/library/e3s99sd8%28v=vs.80%29.aspx

Thanks to Quinn for the subject of this topic.