Inventor 2014 - iLogic – Set Sheet Metal Rule in Assembly

Zen Admin
Zen Admin

by Luke Davenport

Hello comrades – Topic of the day today is – how do you use iLogic to control the active sheet metal rule for an entire assembly of parts all at once?

Well there is a simple line of code in the standard iLogic snippets that goes as follows:

SheetMetal.SetActiveStyle("styleName")

This is a lovely addition and works a treat when run in a single part file. However in this case, what I’m looking at is the ability to have a simple drop down list on a form in your assembly file, which allows you to select from a list of sheet metal rules that you have created. Once you select from the list the rule then finds any sheet metal parts within that assembly and changes the active sheet metal rule for them.

Well there’s certainly some information on the forums for this, but I haven’t found anyone producing a nice rule with some helpful error checking, so here we go. The code pasted below should work for assemblies with mixed sheet metal and non-sheet metal parts, and will give you a heads up if it comes across any errors (such as if you try to run the code in a part file instead of in an assembly – although I like to think all my blog readers are way too smart for that right? yes?)

Check out this video that shows the whole process

 

 

Ps. As always, make sure that when you paste this code into an iLogic rule you remove any line spaces that it creates – if you have a line gap on some of the lines it’ll throw up an error.

 

'Start of iLogic code

 

'Check whether open document is an assembly and exit rule if not

oDoc = ThisDoc.ModelDocument

If oDoc.DocumentType = kPartDocumentObject Then

MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "iLogic")

Return

End If

 

'Define assembly

Dim oAsmCompDef As AssemblyComponentDefinition

oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

 

'Check ActiveRule parameter exists

Try

    If Parameter("ActiveRule") Is Nothing Then

    End If

Catch

MessageBox.Show("You need to create a multivalue parameter called 'ActiveRule' in this assembly" & _

" for this code to work", "iLogic",MessageBoxButtons.Ok,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1)

Return 'Exit rule

End Try

 

'Iterate through the occurrences in assembly

i = 0

Dim ErrorList As New List (Of String)

Dim oOccurrence As ComponentOccurrence

For Each oOccurrence In oAsmCompDef.Occurrences

    Try

    'check it is a sheet metal component

    If oOccurrence.Definition.Type = 150995200 Then

    i = 1

    Dim oSheetMetalCompDef As SheetMetalComponentDefinition

    oSheetMetalCompDef = oOccurrence.Definition

    'change active sheet metal rule

    oSheetMetalCompDef.SheetMetalStyles.Item(ActiveRule).Activate

    End If

    Catch

    'error checking

    ErrorList.Add(oOccurrence.Name)

    End Try

Next

 

'Inform user which parts have failed to update if there are any errors

Dim ErrorListNew As String = String.Join(vbLf, ErrorList)

If Not String.IsNullOrEmpty(ErrorListNew) Then

MessageBox.Show("Failed to update the following sheet metal parts: " & vbLf & vbLf & ErrorListNew & "." & vbLf & vbLf & _

"Please check there is a sheet metal rule called '" & ActiveRule & "' available in these parts" & _

" and spelled correctly!" , "Error!",MessageBoxButtons.Ok,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1)

End If

 

'Inform user if no sheet metal components are present in the assembly

If i = 0 Then

MessageBox.Show("No Sheet Metal Components are in the Current Assembly", "Title")

End If

 

'End of iLogic code

 

 

 

Comments from our old site

16 Dec 2013 18:31

atotiace said:

This works if you want to change all of the sheet metal style uniformly. But what if you would like to change each sheet metal part in the assembly to different styles?

17 Dec 2013 07:37

LukeDavenport said:

Hi atotiace,

How would this work from an interface point of view? You could certainly add a quick modification to the code to throw up an input box (with a list of available sheet metal styles) for each sheet metal component found in the assembly, but in my mind that might be frustrating from a user point of view (to wade through dozens of choices when you might only want to change one or two). A nice method might be to prompt the user to select a sheet metal component and allow instant selection from a list. Perhaps I'll do a blog on this. Thanks for your comment. Luke

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.