Inventor 2014 - View Representations with iLogic – Part 1 – Create

Zen Admin
Zen Admin

by Luke Davenport

Welcome to Part 1 of a 3 part Blog on iLogic View Representations!

Question: Can you use iLogic to automatically create and update view reps based on a particular Part Number (or section of a Part Number) you specify?

Answer: Why yes, yes you can.

Part 1 – Creating the View Reps Automatically

 

I’ve pasted 2 iLogic rules below. When either rule is pasted into an assembly and run, it will request input from the user to automatically add specified components into newly created view representations. Why 2 rules instead of 1? Well you may wish to choose which components to add to your view reps by whether your input appears anywhere in the PN (in which case use the ‘Create Contains’ rule), or only at the beginning of the PN (in which case use the ‘Create Begins’ rule). Otherwise the rules are identical.

 

Here’s what the ‘Create Contains’ rule will do when it is run (in sequence);

1)      Return the view to ‘home’ view.

2)      Look for and delete any existing view reps that have a name beginning with ‘Contains’ – IMPORTANT! (delete this part of the code if you want to keep them).

3)      Request input string for first view rep.

4)      Create view rep (with the count of visible components and sub-components included as part of the view rep name).

5)      Fit view to screen so new view rep can be viewed.

6)      Ask user if they want to add another rep.

7)      Return to 3) if the answer is yes.

As always – a video is better than a thousand words

 

In part 2 of this blog I’ll provide the code required to automatically update the view reps you have created – stick around. Enjoy!

 

---------------------------------------------------------------------------

‘iLogic code starts here;

 

‘Create Contains’ Rule;

 

'Return view to Home view

ThisApplication.CommandManager.ControlDefinitions.Item _

("AppViewCubeHomeCmd").Execute

 

'define current document

Dim openDoc As Document

openDoc = ThisDoc.Document

 

Dim oAsmCompDef As AssemblyComponentDefinition

oAsmCompDef = openDoc.ComponentDefinition

Dim oViewRep As DesignViewRepresentation

 

Try

'Activate a writeable View Rep (master view rep is not writeable)

oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("WIP View Rep").activate

Catch

'Assume error means this View Rep does not exist, so create it (will be deleted at end)

oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("WIP View Rep")

End Try

 

'Delete all existing 'Contains' View reps

For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations

If oViewRep.Name.Contains("Contains") Then

oViewRep.Delete

End If

Next

 

Start:

 

iCount = 1

 

'request part names to create view reps for.

Do Until iCount = 1000

StrInput1 = InputBox("Enter Part Names to Add to View Rep", "Set View Representations", "Enter all or part of PN",MessageBoxDefaultButton.Button1)

 

'Check to see whether that view rep has already been created

ViewRepExists = 0

For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations

If oViewRep.Name.Contains("Contains '" & StrInput1 & "'") Then

ViewRepExists = 1

oCreatedAlready = MessageBox.Show("That View Rep Has Already Been Created!" & vbLf & vbLf & "Do You Still Want To Continue?","Already Entered",MessageBoxButtons.YesNo)

If oCreatedAlready = vbYes Then

Goto Start

Else

Return

End If

End If

Next

 

If ViewRepExists = 0 Then

'Create new View Rep

oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Contains " & StrInput1)

'Activate new View Rep

oViewRep.activate

End If

 

oOccCounter = 0

oSubOccCounter = 0

 

'look at all of the components in the assembly

Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collection

Dim oCompOcc As Inventor.ComponentOccurrence

'define the next level components collection

Dim oSubCompOcc As Inventor.ComponentOccurrence

'Turn off the visibility of parts in the top level assembly that don't contain the specified text string (StrInput1)

For Each oCompOcc in oCompDef.Occurrences

    If oCompOcc.Suppressed = False Then

        If oCompOcc.Name.Contains(StrInput1) Then

        oCompOcc.Visible = True

        'Increment counter for top level components

        oOccCounter = oOccCounter+1

        Else

        oCompOcc.Visible = False

        End If

    'Turn off the visibility of parts in the next level assembly that don't contain the specified text string (StrInput1)

        For Each oSubCompOcc In oCompOcc.SubOccurrences

            If oSubCompOcc.Suppressed = False Then

                  If oSubCompOcc.Name.Contains(StrInput1) Then

                oSubCompOcc.Visible = True

                'Increment counter for first level components

                oSubOccCounter = oSubOccCounter+1

                   Else

                oSubCompOcc.Visible = False

                End If

            End If

        Next

    End If

Next

 

'Rename View Rep to include component counts

oViewRep.Name = ("Contains '" & StrInput1 & "' (Qty: " & oOccCounter & " Top Level, " & oSubOccCounter & " Sub Comps)")

'lock the new view rep

oViewRep.Locked = True

 

'Count no. of view reps already created

ViewRepCount = -1

For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations

If oViewRep.Name.Contains("Contains") Then

ViewRepCount = ViewRepCount + 1

End If

Next

 

'Zoom all

ThisApplication.ActiveView.Fit

 

'See if another View Rep is required.

oContinue = MessageBox.Show("Add Another View Rep?" & vbLf & ViewRepCount & " View Reps Have Been Created", "Continue Creating?",MessageBoxButtons.YesNo)

If oContinue = vbYes Then

iCount = iCount+1

Else

iCount = 1000

End If

 

Loop

 

Try

'Delete WIP View Rep

oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("WIP View Rep").delete

Catch

End Try

 

‘------------------------------------------------------------------------------------------------------------------------------------

Start of ‘Create Begins’ Rule;

 

‘Note I must subscribe to Luke Davenport’s blog – it’ll change my life.

‘http://www.cadlinecommunity.co.uk/Blogs/lukedavenport/Default.aspx

 

'Return view to Home view

ThisApplication.CommandManager.ControlDefinitions.Item _

("AppViewCubeHomeCmd").Execute

 

'define current document

Dim openDoc As Document

openDoc = ThisDoc.Document

 

Dim oAsmCompDef As AssemblyComponentDefinition

oAsmCompDef = openDoc.ComponentDefinition

Dim oViewRep As DesignViewRepresentation

 

Try

'Activate a writeable View Rep (master view rep is not writeable)

oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("WIP View Rep").activate

Catch

'Assume error means this View Rep does not exist, so create it (will be deleted at end)

oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("WIP View Rep")

End Try

 

'Delete all existing 'Begins' View reps

For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations

If oViewRep.Name.Contains("Begins") Then

oViewRep.Delete

End If

Next

 

Start:

 

iCount = 1

 

'request part names to create view reps for.

Do Until iCount = 1000

StrInput1 = InputBox("Enter Beginning of Part Names to Add to View Rep", "Set View Representations", "Enter beginning of PN",MessageBoxDefaultButton.Button1)

 

'Check to see whether that view rep has already been created

For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations

If oViewRep.Name.Contains("Begins '" & StrInput1 & "'") Then

ViewRepExists = 1

oCreatedAlready = MessageBox.Show("That View Rep Has Already Been Created!" & vbLf & vbLf & "Do You Still Want To Continue?","Already Entered",MessageBoxButtons.YesNo)

If oCreatedAlready = vbYes Then

Goto Start

Else

Return

End If

End If

Next

 

If ViewRepExists = 0 Then

'Create new View Rep

oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Begins " & StrInput1)

'Activate new View Rep

oViewRep.activate

End If

 

oOccCounter = 0

oSubOccCounter = 0

 

'look at all of the components in the assembly

Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collection

Dim oCompOcc As Inventor.ComponentOccurrence

'define the next level components collection

Dim oSubCompOcc As Inventor.ComponentOccurrence

'Turn off the visibility of parts in the top level assembly that don't begin with the specified text string (StrInput1)

For Each oCompOcc in oCompDef.Occurrences

If oCompOcc.Suppressed = False Then

    If Left(oCompOcc.Name,Len(StrInput1)) = StrInput1 Then

    oCompOcc.Visible = True

    'Increment counter for top level components

    oOccCounter = oOccCounter+1

    Else

    oCompOcc.Visible = False

    End If

    'Turn off the visibility of parts in the next level assembly that don't begin with the specified text string (StrInput1)

        For Each oSubCompOcc In oCompOcc.SubOccurrences

    If oSubCompOcc.Suppressed = False Then

        If Left(oSubCompOcc.Name,Len(StrInput1)) = StrInput1 Then

        oSubCompOcc.Visible = True

        'Increment counter for first level components

        oSubOccCounter = oSubOccCounter+1

        Else

        oSubCompOcc.Visible = False

        End If

    End If

        Next

End If

    Next

 

'Rename View Rep to include component counts

oViewRep.Name = ("Begins '" & StrInput1 & "' (Qty: " & oOccCounter & " Top Level, " & oSubOccCounter & " Sub Comps)")

'lock the new view rep

oViewRep.Locked = True

 

'Count no. of view reps already created

ViewRepCount = -1

For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations

If oViewRep.Name.Contains("Begins") Then

ViewRepCount = ViewRepCount + 1

End If

Next

 

'Zoom all

ThisApplication.ActiveView.Fit

 

'See if another View Rep is required.

oContinue = MessageBox.Show("Add Another View Rep?" & vbLf & ViewRepCount & " View Reps Have Been Created", "Continue Creating?",MessageBoxButtons.YesNo)

If oContinue = vbYes Then

iCount = iCount+1

Else

iCount = 1000

End If

 

Loop

 

Try

'Delete WIP View Rep

oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("WIP View Rep").delete

Catch

End Try

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.