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.

Wednesday, April 27, 2011

Bring Back the Classic Right Click Menu in Autodesk Inventor 2012

Issue:
You've tried to use the new Marking Menus in Inventor 2012, but find that they are too slow, too busy, or too distracting to use comfortably. You'd prefer to use the old style right click context menu found in earlier releases of Inventor. Is there a way to set the interface back to the old style?


Solution:
Fortunately, Autodesk has provided an option to disable the marking menu and use the classic old style right click context menu. To find this option go to the Tools tab and click the Customize button.



Then select the Marking Menu tab and check the Use Classic Context Menu check box:


This allows you to work as you have in the past, using the old style right click menu.



Update:
Inventor 2017 R2 allows you to turn off the Marking Menu and Mini Toolbars by going to the View tab > User Interface button as shown:




Previous to Inventor 2017 R2:

Of course now you're probably going to want to know how to disable the mini toolbars and use just the classic style dialog boxes.

Unfortunately, Autodesk has not provided the ability to turn off the mini toolbars, so we're stuck with the current strange Jekyll and Hyde interface, which is cluttered, distracting and in many cases incomplete.

You might take a moment to go to the Feature Request link and request that Autodesk fix this in the next release, so that there is an option to use just the classic dialog boxes. The more requests they get on this, the more likely it is to be changed, so do your part and let them know what you think about it.

Monday, April 25, 2011

Using Excel and iLogic to Retrieve Part Numbers From a Drawing Log

Issue:
Because you don't use Vault and don't use an MRP or ERP system you utilize a simple spreadsheet as a part number log to record part numbers and descriptions. This works well, but you'd like to use iLogic to read the spreadsheet and copy in the information you've already entered, rather than having to re-type it or copy/paste it.


Solution:
You can use the GoExcel.Open function in iLogic to do this. I use this approach at home for a simple and effective drawing log. It allows me to maintain consistency between the drawing log and my models. Having the information in the spreadsheet allows me to search and index past designs quickly. Here's an example:

This spreadsheet contains just two columns. One for the Part Number and one for the Description.

 

The first thing needed in the iLogic rule is a line to locate the spreadsheet, such as:


GoExcel.Open("J:\My Drawing Log.xls", "Sheet1")

When the iLogic rule is triggered, it follows the GoExcel address. I then want to tell it what infromation to read from the sheet. I'll add a For/Next function to it, to look at Column A and find the first empty cell in that column.  In the example above this would be cell 8A.


'index row 2 through 10000
For rowPN = 2 To 10000
'find first empty cell in column A
 If (GoExcel.CellValue("A" & rowPN) = "") Then
'create a variable for the cell value that is one row less than the empty cell row
    lastPN = GoExcel.CellValue("A" & rowPN - 1)
    lastDesc = GoExcel.CellValue("B" & rowPN- 1)
         Exit For
 End If
Next

In the snippet above the variable called lastPN reads the value of the cell that is one less the first empty cell in Column A. Then a variable called lastDesc is set to the value in the same row but for column B.

So in this case iLogic would read in the Part Number: 09-0805 and the Description: hardware, latch, cam action

In order to write these values to the model I'll use this bit of code:

'check to see if Part Number is the same as the file name or blank
If iProperties.Value("Project", "Part Number") = ThisDoc.FileName(False) Or iProperties.Value("Project", "Part Number") = "" Then
'set iProperty to value read in from excel
iProperties.Value("Project", "Part Number")  =  lastPN
'set iProperty to value read in from excel
iProperties.Value("Project", "Description")  =  lastDesc
Else
End If

This checks the information in spreadsheet against the iProperties of the model and then writes the values of the target cells to them if the part number is the same as the file name (less the file extension) or if the part number is empty.

To add a new part number to my drawing log, I would simply fill in the Part# and Description for the next row, and then save the spreadsheet:



Now when the rule is run, this new Part Number and Description are looked up and written to the model file iProperties.

Next, I've added a confirmation dialog box with Yes/No buttons to the end of the rule to allow me to accept or reject the returned values (in case I run the rule without remembering to save the spreadsheet after adding a new row, etc.)



'show results and ask user to confirm results
question = MessageBox.Show("PN: " & lastPN & vbLf _
& "Description: " & lastDesc & vbLf _
& "Is this correct?", "iLogic from Excel", MessageBoxButtons.YesNo )

'if answer is no
If question = vbNo Then
'clear these iProperties
iProperties.Value("Project", "Part Number")  =  ""
iProperties.Value("Project", "Description")  =  ""
'run rule again
Goto StartRule
'if answer is yes exit rule
Else End If

If the information is incorrect I click No and the Part Number and Description iProperties are cleared and then the rule is run from the beginning again. If the information is correct, then the rule exits.

Note that if an empty cell is found further up the list, the iLogic rule will stop at it and read in the row above it. In the example below a cell in column A has been cleared and the spreadsheet saved.


Now the rule will return the part number and description for row 4, since cell 5A is empty. I often use this to update existing models by inserting a blank row below the changed part number and then running the rule in the corresponding model. Then I simply remove the empty row when done.

Here is the code for the complete rule:


'---------start of iLogic code---------

StartRule:
'read excel file
GoExcel.Open("J:\My Drawing Log.xls", "Sheet1")

'index row 2 through 10000
For rowPN = 2 To 10000
'find first empty cell in column A
 If (GoExcel.CellValue("A" & rowPN) = "") Then
'create a variable for the cell value that is one row less than 
'the empty cell row
    lastPN = GoExcel.CellValue("A" & rowPN - 1)
      lastDesc = GoExcel.CellValue("B" & rowPN- 1)
         Exit For
 End If
Next

'check to see if Part Number is the same as the file name or blank
If iProperties.Value("Project", "Part Number") = ThisDoc.FileName(False) _  
Or iProperties.Value("Project", "Part Number") = "" Then
'set iProperty to value read in from excel
iProperties.Value("Project", "Part Number")  =  lastPN
'set iProperty to value read in from excel
iProperties.Value("Project", "Description")  =  lastDesc
Else
End If

'show results and ask user to confirm results
question = MessageBox.Show("PN: " & lastPN & vbLf _
&"Description: " & lastDesc & vbLf _
& "Is this correct?", "iLogic from Excel", MessageBoxButtons.YesNo )

'if answer is no
If question = vbNo Then
'clear these iProperties
iProperties.Value("Project", "Part Number")  =  ""
iProperties.Value("Project", "Description")  =  ""
'run rule again
Goto StartRule
'if answer is yes exit rule
Else End If


'---------end of iLogic code---------



Thursday, April 21, 2011

Using a Spreadsheet Table in a Drawing Sheet

Issue:
You have a MS Excel file (.xls or .xlsx) that you'd like to place in a drawing. You've tried to use the Insert Object tool, but the resulting OLE (Object Linked Entity) table is of poor quality and doesn't print well.  Is there a better way?
Example of the poor results you get using the Insert Object method.


This might not be the best way to insert the table.



Solution:
Indeed there is a better way than using the Insert Object tool. You can use the General Table tool to do this. Unfortunately, many Inventor users seem to overlook this method. Let's have a look:

Click the General Table button (found on the Annotate tab of the drawing environment):


Click the Browse button to locate the spreadsheet file you want to use:



Once you have located the file you want to use click the OK button:



This will place the Spreadsheet on the drawing sheet as a table:



You'll see the link in the browser as a 3rd Party node, and a sheet node for the table:


To edit the spreadsheet you can right-click the 3rd Party reference and choose Edit:
(note too the Change Source option that allows you to swap out the link of the original spreadsheet with a link to a new spreadsheet)


In Excel you can change cell values as you normally would, then Save and Close the spreadsheet:



Back in Inventor, you can right-click on the table and choose Update:


This will pull the new values into the table:


You can format the table by right-clicking it and choosing Edit, in order to change the Table header, etc.

Note: A few things have been pointed out to me since originally creating this post: 
  • OLE object table will print ok, but it just doesn't display on the page very well. 
  • If you save drawings as PDF OLE's will still display poorly. 
  • If you have merged cells in your table then the OLE option might be best, since the General Table option doesn't honor the merged cells.

Tuesday, April 19, 2011

Using Inventor iLogic to Create E-Mail Notifications with Outlook

Issue:
When you've made a design change, you'd like to use iLogic to create an email notification with the current file as an attachment to be sent to your client.



Solution:
Here is a sample code that will do the following tasks:

  • Time Stamp the current file
  • Fill out the status iProperties for the current file
  • Save the current file
  • Create a new email using Outlook that is addressed to the client's email
  • Add a basic message
  • Add the current file to the email as an attachment
  • Display the email (but don't send it)


'------- start of iLogic code -------------------


 ' Start Outlook.
 ' If it is already running, you'll use the same instance...
   Dim oOApp
   oOApp = CreateObject("Outlook.Application")
   
' Log on. Doesn't hurt if you are already running and logged on...
   Dim olNs
   olNs = oOApp.GetNamespace("MAPI")
   olNs.Logon

‘get the Inventor user name from the Inventor Options
myName= ThisApplication.GeneralOptions.UserName

'get current time
oTime = Now.ToShortTimeString

'set status tag to For Review and time stamp
iProperties.Value("Status", "Status") = "For Review @ " oTime

'set the Design State to Pending
iProperties.Value("Status", "Design State") = "Pending"

'set the Checked By to user name from the Inventor Options
iProperties.Value("Status", "Checked By") = myName

'set the Checked Date to the current date
iProperties.Value("Status", "Checked Date") = Now

'save the file
ThisDoc.Save

'send email using outlook
Dim oOMail

oOMail = oOApp.CreateItem(olMailItem)

With oOMail
            'replace with your client's email address
            .To = "myclient@domain.com"
            .Subject = "Please Review: " & ThisDoc.FileName
          .Body = "Please review " & ThisDoc.FileName & " and let me know if you see any issues."
            .Body = .Body & "Thanks, " & myname
            .Display

            'Add attachments to the message.
            objOutlookAttach = .Attachments.Add(ThisDoc.PathAndFileName(True))

End With
'------- end of iLogic code -------------------



The end result is an email waiting for you to hit the send button:


 Here are the results of the Status iProperties that are being set:



 To send an email automatically from iLogic, you can add .Send as in this example:

'------- Start of iLogic code -------------------

'send email using outlook
Dim oOApp
Dim oOMail

oOApp = CreateObject("Outlook.Application")
oOMail = oOApp.CreateItem(olMailItem)

With oOMail
.To = "name@domain.com"
.Subject = "Some Subject Line Here"
.Body = "Some Text Here"
'Add attachments to the message
objOutlookAttach = .Attachments.Add(ThisDoc.PathAndFileName(True))
.Send
End With
'------- end of iLogic code -------------------


Update: 5-27-2014
  For more on this topic, see this link for Clinton Brown's blog article "iLogic- Email your Part Files with EOP Rolled Up"