Change member of Content Center part with iLogic

Change member of Content Center part with iLogic

benedictsweet
Contributor Contributor
602 Views
1 Reply
Message 1 of 2

Change member of Content Center part with iLogic

benedictsweet
Contributor
Contributor

Good morning, good afternoon and good evening all,

 

I'm currently wanting to implement some code in iLogic to replace content center parts in an assembly with other members of themselves (i.e. trying to automate the "Change Size" command with iLogic).

The following code is trying to do this by tapping into the API, but I've gotten stuck with it. Would appreciate any help and expertise.
____________________________________________________________________________

Sub Main()

    ' Example usage - Call the function to update the part

    UpdateContentCenterMember("Fixtures", "Legs", "Marine Leg 180", "SAA")

End Sub

Sub UpdateContentCenterMember(Category As String, Group As String, FamilyName As String, FinishType As String)

    ' Get application and active document

    Dim oApp As Inventor.Application

    oApp = ThisApplication

    Dim oMainAsm As AssemblyDocument

    oMainAsm = oApp.ActiveDocument

    Dim oAsmDef As AssemblyComponentDefinition

    oAsmDef = oMainAsm.ComponentDefinition

 

    ' Get all leaf occurrences (parts without sub-occurrences)

    Dim leafOccurrences As List(Of ComponentOccurrence)

    leafOccurrences = GetLeafOccurrences(oAsmDef.Occurrences)

 

    Dim oContentCenter As ContentCenter

    oContentCenter = oApp.ContentCenter

 

    ' Iterate over all leaf occurrences and check if they are Content Center parts

    For Each oOcc As ComponentOccurrence In leafOccurrences

        If oOcc.Definition.IsContentMember Then

            Dim oDef As PartComponentDefinition

            oDef = oOcc.Definition

            Dim oDoc As PartDocument

            oDoc = oDef.Document

 

            ' Reference to the Content Library Component Properties

            Dim oPropSets As PropertySets

            oPropSets = oDoc.PropertySets

            Dim oProps As PropertySet

            oProps = oPropSets.Item("Content Library Component Properties")

 

            ' Get FamilyId

            Dim oProp As Inventor.Property

            oProp = oProps.Item("FamilyId")

            Dim FamilyId As String

            FamilyId = oProp.Value

            Debug.Print("FamilyId: " + FamilyId)

 

            ' Reference to the ContentFamily

            Dim oFamily As ContentFamily

            oFamily = oContentCenter.GetContentObject("v3#" + FamilyId + "#")

            Debug.Print("Content Family DisplayName: " + oFamily.DisplayName)

 

            ' Get current MemberId (row) for the part

            oProp = oProps.Item("MemberId")

            Dim MemberId As String

            MemberId = oProp.Value

            Debug.Print("Current MemberId (row): " + MemberId)

 

            ' Find column for PARTNUMBER

            Dim nCol As Integer

            nCol = -1

            For i = 1 To oFamily.TableColumns.Count

                If oFamily.TableColumns.Item(i).InternalName = "PARTNUMBER" Then

                    nCol = i

                    Exit For

                End If

            Next

 

            If nCol < 0 Then

                MessageBox.Show("Error: PARTNUMBER column not found!", "Error")

                Exit Sub

            End If

 

            ' Get table row for the current member

            Dim oRow As ContentTableRow

            oRow = oFamily.TableRows.Item(MemberId)

            Dim partNumber As String

            partNumber = oRow.Item(nCol).Value

            Debug.Print("Member PartNumber: " & partNumber)

 

            ' Select a new member row (this could be dynamically set based on a condition)

            Dim NewRow As Integer

           NewRow = 3  ' <--- Example: Set dynamically based on your logic

 

            ' Create a new member based on the selected row

            Dim Error1 As MemberManagerErrorsEnum

            Dim strErrorMessage As String

            Dim strContentPartFileName As String

 

            ' Correct the method call

            Dim RefreshEnum As ContentMemberRefreshEnum

            RefreshEnum = ContentMemberRefreshEnum.kUseDefaultRefreshSetting ' Correct enum member

 

            strContentPartFileName = oFamily.CreateMember(NewRow, Error1, strErrorMessage, RefreshEnum, False, "", Nothing, Nothing)

 

            If strErrorMessage <> "" Then

                MessageBox.Show("Error creating member: " & strErrorMessage, "Error")

                Exit Sub

            End If

 

            ' Replace occurrence with the new part

            oOcc.Replace(strContentPartFileName, False)

            Beep

 

            ' Optional: Set the finish type (if it exists as a parameter)

            Dim oFinish As Inventor.Property

            On Error Resume Next

            oFinish = oDoc.PropertySets.Item("Design Tracking Properties").Item("Finish")

            On Error GoTo 0

 

            If Not oFinish Is Nothing Then

                oFinish.Value = FinishType

                Debug.Print("Finish updated to: " & FinishType)

            End If

        End If

    Next

End Sub

 

' Function to retrieve all leaf occurrences in the assembly

Function GetLeafOccurrences(Occurrences As ComponentOccurrences) As List(Of ComponentOccurrence)

    Dim leafOccurrences As New List(Of ComponentOccurrence)

 

    For Each oOccurrence As ComponentOccurrence In Occurrences

        If oOccurrence.SubOccurrences.Count = 0 Then ' If no children, it's a leaf

            leafOccurrences.Add(oOccurrence)

        Else

            ' Recursively get leaf occurrences

            leafOccurrences.AddRange(GetLeafOccurrences(oOccurrence.SubOccurrences))

        End If

    Next

 

    Return leafOccurrences

End Function

0 Likes
603 Views
1 Reply
Reply (1)
Message 2 of 2

Curtis_W
Consultant
Consultant

@benedictsweet you can use the ilogic AddContentCenterPart function to do this.

 

Here I have a CC part that has been renamed in the browser to be Cap Screw_3

Curtis_Waguespack_3-1740671501854.png

 

In the iLogic rule, I find the CC component and right click and choose Capture Current State () as shown.

 

I used "by Designation" in this example

Curtis_Waguespack_1-1740671398015.png

 

That gives you the code to add the component to the assembly. Then you can modify it to add the logic needed to add a specific size based on a condition.

 

In my example, I just looked at the User Parameters of that component and used Capture Current State on the TPU parameter, to provide the logic.

Curtis_Waguespack_4-1740671745122.png

 

This example creates a rule to toggle between a #6 or a #10 by looking at the current Thread Per Unit ( TPU) value

Dim Designation As String
If Parameter("Cap Screw_3", "TPU") = 32 Then
	Designation = "No. 10 - 24 UNC - 1/2"
Else
	Designation = "No. 6 - 32 UNC - 1/2"
End If

Components.AddContentCenterPart("Cap Screw_3", "Fasteners:Bolts:Socket Head", "Hexagon Socket Head Cap Screw - Inch", Designation)

 

I think you can work this into your example.

 

Hope that helps,

Curtis

 

 

 

EESignature

0 Likes