Tuesday, February 28, 2012

iLogic to Rename All Solid Bodies

Issue:
You've created a multi-body solid model with the intent to use the Make Components tool to write out each solid as a new part file. This works fine, but you find yourself wishing there was an easier way to tag each solid with a new name based on a prefix and a series number. For instance, you'd like to name each solid in a part file called 45-723 as such: 45-723_01, 45-723_02, 45-723_03, etc.



Issue:
You can use this iLogic rule to quickly rename the solid bodies for you. In this rule we first look for a custom iProperty named "Prefix" and create it if it is not present. Then we write the part number to the custom iProperty if it is found to be empty. Then the rule uses the value of the custom iProperty named "Prefix" in an input box to query the user for the prefix:



The user can change the prefix at this point or accept the existing one. When we press the OK button the rule renames each solid body using the prefix and an increment number, adding a leading zero for each number less than 10.

Thanks to Barbara Han for the original code I used for this rule.



'------- start of ilogic ------
'check for custom iProperty and add it if not found
Dim prefix As String = "Prefix"
customPropertySet = ThisDoc.Document.PropertySets.Item _
("Inventor User Defined Properties")

Try
         prop= customPropertySet.Item(prefix)
Catch
      ' Assume error means not found
            customPropertySet.Add("", prefix)
End Try

'write the part number to the Prefix iProperty if it is empty
if iProperties.Value("Custom", "Prefix") = "" Then
iProperties.Value("Custom", "Prefix") = iProperties.Value("Project", "Part Number") & "_"
else
end if

'check that this active document is a part file   
Dim partDoc As PartDocument
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If

'define the active document
partDoc = ThisApplication.ActiveDocument
Dim solid As SurfaceBody
Dim i As Integer

'get input from user
prefix = InputBox("Enter a prefix for the solid body names", "iLogic", iProperties.Value("Custom", "Prefix"))

'write input back to custom iProperty
iProperties.Value("Custom", "Prefix") = prefix
i = 1
'rename all solid bodies incrementing suffix
For Each solid In partDoc.ComponentDefinition.SurfaceBodies
solid.Name = prefix + IIf(i < 10, "0" + CStr(i), CStr(i))
i = i + 1
Next
------- end of ilogic ------

Thursday, January 19, 2012

Set Your Drawing View Labels to Use the Model Part Number

Issue:
You'd prefer your view labels to use the model part number (or some other file property) rather than the default "View1", "View2", and so on. Re-naming the view labels manually is tedious and error prone. 



Solution:
You can change the view label default to use an iProperty from the model using these steps:
  • Open a drawing file
  • Go to the Manage tab
  • Click the Styles and Standards button
  • Click the Standard to work with from the top of the tree on the left pane
  • Activate the View Preferences tab in the right pane
  • Click the Edit button for Display field
  • Set the Type drop down to Properties-Model
  • Set the Properties drop down to Part Number (or something else)
  • Click the Add Text Parameter button to add this property to your view label

You can click the image below to enlarge it and see the picks and clicks.

Click to Enlarge



Here is a bit of iLogic Code to set the view name and browser node to the model part number also:


'start of ilogic code
Dim oApp As Application: oApp = ThisApplication
Dim oDoc As DrawingDocument:  oDoc = oApp.ActiveDocument

Dim oSheets As Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView

oSheets = oDoc.Sheets

For Each oSheet In oSheets
oViews = oSheet.DrawingViews
                For Each oView In oViews
                oModelName = _
                oView.ReferencedDocumentDescriptor.ReferencedDocument.DisplayName
                oPartNumber = iProperties.Value(oModelName, "Project", "Part Number")
            oView.Name = oPartNumber
                Next
Next
'end of ilogic code



Update:
By request here is a rule that adds a custom iProperty to the view label with a dash separator.



'start of ilogic code
Dim oDoc As DrawingDocument:  oDoc = ThisDoc.Document

Dim oSheets As Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView

oSheets = oDoc.Sheets

For Each oSheet In oSheets
oViews = oSheet.DrawingViews
                For Each oView In oViews
                'capture the current view label
                ViewLabel = oView.Name
                oModelName = _
                oView.ReferencedDocumentDescriptor.ReferencedDocument.DisplayName
                Try
            o_iProp = iProperties.Value(oModelName, "Custom", "My_iProp")        
            'add the the iProperty to the current view label, with a dash separator
                oView.Name = ViewLabel & " - " & o_iProp
            Catch
            'do nothing if error
                End Try
            Next
Next

'end of ilogic code


Thursday, January 5, 2012

Creating a Basic iLogic Rule with an Event Trigger



Issue:
You've found some iLogic code online, but you're not sure how to create a simple rule or have it trigger on a save event. You've found the iLogic tutorials on line, but don't have the time to go through them right now. You really just want to know how to create a simple rule and get it working.

Solution:
Here are some basics to get you going, using some simple example code to update the creation date and author iProperties, as described at this link.

Here is the iLogic Code you'll use:

'set Author iproperty to match the system user name
iProperties.Value("Summary", "Author" ) = ThisApplication.GeneralOptions.UserName
'set the Creattion Date iproperty to the current date
iProperties.Value("Project", "Creation Date" ) = Now
'update the file
iLogicVb.UpdateWhenDone = True



To use this code first create an iLogic Rule:
  1. On the ribbon, click Manage tab > iLogic panel > Add Rule button.
  2. Name the new rule Name-Date.
  3. In the rule text area of the Edit Rule dialog box, simply paste in the code from above.
  4. Click OK to save this new rule.
Next you need to have a couple of text fields that read the iProperties (you'd likely create these text objects in your title block, but for now just create some simple text on your drawing to test this first).
  1. On the ribbon, click Annotate tab > Text panel > Text button.
  2. Click on screen to place the text
  3. In the Format Text dialog box select the Type pull down and choose Properties - Model from the list.
  4. Then select the Property pull down and choose Creation Date from the list.
  5. Then click the Add Text Parameter button (looks like and X with an arrow) to "push" the field to the text box.  
  6. Click OK to create the text.
  7. Repeat these steps for the Author iProperty
Next set the rule to trigger when the file is saved.
  1. On the ribbon, click Manage tab > iLogic panel > Event Triggers button. A dialog box displays the list of available events.
  2. Click the Before Save Document event in the list.
  3. Click Select Rules, or right-click and choose Select Rules from the context menu.
  4. Place a check mark next to the Name-Date rule.
  5. Click OK.
  6. Click OK to close the event list.
Now whenever this file is saved the date and name text fields are updated.
 
Note:
You might also be interested in this post from the guys at the Being Inventive blog:
http://beinginventive.typepad.com/being-inventive/2011/07/plot-date-stamp-in-title-block.html

And this article from Paul Munford:
http://www.augi.com/library/playing-by-the-ilogic-rules 


And this article from Steve Bedder:
http://autodeskmfg.typepad.com/blog/2012/01/working-with-external-ilogic-rules.html