Insert specific size fastener from Content Center using VB.NET

Insert specific size fastener from Content Center using VB.NET

J_Dumont
Advocate Advocate
497 Views
9 Replies
Message 1 of 10

Insert specific size fastener from Content Center using VB.NET

J_Dumont
Advocate
Advocate

Hello,

 

I am trying to insert a fastener from Content Center, and the conditions will determine the size that is needed.

I was able to use the code in the API help to place all members of a family but I need to narrow that down.

 

 

    Public Sub InsertPEMNuts()
        'Dim oAsmDoc As AssemblyDocument
        'oAsmDoc = ThisApplication.ActiveDocument

        Dim oContentCenter As ContentCenter
        oContentCenter = g_inventorApplication.ContentCenter

        Dim oContentNode As ContentTreeViewNode
        oContentNode = oContentCenter.TreeViewTopNode.ChildNodes.Item("Fasteners").ChildNodes.Item("Bolts").ChildNodes.Item("Hex Head")

        Dim oFamily As ContentFamily
        For Each oFamily In oContentNode.Families
            If oFamily.DisplayName = "ISO 4015" Then
                Exit For
            End If
        Next

        ' Create a NameValueMap to specify parameters (e.g., length, diameter)
        Dim oParams As NameValueMap
        oParams = g_inventorApplication.TransientObjects.CreateNameValueMap
        oParams.Add("NND", 5) ' Set length to 50 mm
        oParams.Add("NLG", 35)


        ' Create the member
        Dim failureReason As MemberManagerErrorsEnum
        Dim failureMessage As String
        Dim oMemberDoc As Document
        oMemberDoc = oFamily.CreateMember(oParams, failureReason, failureMessage, ContentMemberRefreshEnum.kRefreshOutOfDateParts)

        ' Insert the member into the assembly
        Dim oMatrix As Matrix
        oMatrix = g_inventorApplication.TransientGeometry.CreateMatrix

        Dim oOcc As ComponentOccurrence
        oOcc = oDoc.ComponentDefinition.Occurrences.Add(oMemberDoc.FullFileName, oMatrix)
    End Sub

 

 

It seems that I need to specify the row. I don't know how to determine the row from the information that I have.

 

J_Dumont_0-1739882482021.png

Here's my error.

J_Dumont_1-1739882519064.png

 

 

 

 

0 Likes
Accepted solutions (1)
498 Views
9 Replies
Replies (9)
Message 2 of 10

jjstr8
Collaborator
Collaborator

You're correct that you need to specify a row (or ContentIdentifier string) when calling .CreateMember. I'm not seeing anything built in to narrow it down to your NND & NLG values through the ContentFamily type. It looks like you'll have to loop through oFamily.TableRows and look for a match. Fortunately, you don't have to loop through all columns. The indexer in TableRow does takes a column name string, i.e. oFamily.TableRows(1).Item("NND") will return the column with its InternalName  = "NND". 

0 Likes
Message 3 of 10

J_Dumont
Advocate
Advocate

Hi @jjstr8 

 

Thanks for the reply. I will only need two separate families and just a few sizes of each, so I can use the row method. In this example, I determined which row I needed, and it failed. Any help would be greatly appreciated.

 

Here is updated code.

    Public Sub InsertPEMNuts()
        Dim oAsmDoc As AssemblyDocument
        oAsmDoc = g_inventorApplication.ActiveDocument

        Dim oContentCenter As ContentCenter
        oContentCenter = g_inventorApplication.ContentCenter

        Dim oContentNode As ContentTreeViewNode
        oContentNode = oContentCenter.TreeViewTopNode.ChildNodes.Item("Sheet Metal").ChildNodes.Item("Nuts")

        Dim oFamily As ContentFamily
        For Each oFamily In oContentNode.Families
            If oFamily.DisplayName = "PEM Self-Clinching Nuts S,SS,CLS,CLSS - Inch" Then
                Exit For
            Else
                MsgBox("Family not found")
            End If
        Next

        If Not oFamily Is Nothing Then
            Dim failureReason As MemberManagerErrorsEnum
            Dim failureMessage As String
            Dim memberFilename As String
            memberFilename = oFamily.CreateMember(14, failureReason, failureMessage, ContentMemberRefreshEnum.kDoNotRefreshOutOfDateParts)
            ' Place the part into the assembly.
            Dim transMatrix As Matrix
            transMatrix = g_inventorApplication.TransientGeometry.CreateMatrix
            'transMatrix.Cell(2, 4) = 0
            Dim Occ As ComponentOccurrence
            Occ = oAsmDoc.Occurrences.Add(memberFilename, transMatrix)
        End If

Here is the error

J_Dumont_0-1739961870710.png

 

 

 

0 Likes
Message 4 of 10

JBerns
Advisor
Advisor

@J_Dumont,

 

I too wanted to learn how to insert Content Center parts using VB.NET.

 

Could you provide an example of the NND and NLG you want to place?

 

I found this source code, which originally inserted all the parts of a family.

Place Content Center Parts API Sample 

 

I converted the code to a subroutine that receives arguments similar to the iLogic command. Currently, it supports a single column filter. In my case, I know StockNumber is unique.

 

Private Sub Main()
	Break
	Call PlaceFromContentCenter("Fasteners", "Washers", "Plain", "NMC Flat Washer - Inch", "Stock Number", "030-0254")
End Sub

''' <summary>
''' Place a component from Content Center using VB.NET
''' </summary>
''' <param name="oNode1">Category</param>
''' <param name="oNode2">Subcategory</param>
''' <param name="oNode3">SubSubCategory</param>
''' <param name="oFamilyName">Family name</param>
''' <param name="oColName">Column name</param>
''' <param name="oValue">Value to find</param>
Public Sub PlaceFromContentCenter(oNode1 As String, oNode2 As String, oNode3 As String, oFamilyName As String, oColName As String, oValue As String)
	Break
	logger.Trace("Entering PFCC")
    Dim asmDoc As AssemblyDocument = ThisApplication.Documents.Add(kAssemblyDocumentObject)
    
    Dim asmDef As AssemblyComponentDefinition = asmDoc.ComponentDefinition
    
    ' Get the node in the content browser based on the names of the nodes in the hierarchy.
'    Dim hexHeadNode As ContentTreeViewNode = ThisApplication.ContentCenter.TreeViewTopNode.ChildNodes.Item("Fasteners").ChildNodes.Item("Washers").ChildNodes.Item("Plain")
    Dim hexHeadNode As ContentTreeViewNode = ThisApplication.ContentCenter.TreeViewTopNode.ChildNodes.Item(oNode1).ChildNodes.Item(oNode2).ChildNodes.Item(oNode3)
    
    ' Find a specific family.  In this case it's using the display name, but any family
    ' characteristic could be searched for.
    Dim family As ContentFamily
    Dim checkFamily As ContentFamily
    For Each checkFamily In hexHeadNode.Families
'        If checkFamily.DisplayName = "NMC Flat Washer - Inch" Then
        If checkFamily.DisplayName = oFamilyName Then
            family = checkFamily
            Exit For
        End If
    Next
    
    Dim i As Integer = 0
    If Not family Is Nothing Then

        ' Place one instance of each member.
        Dim offset As Double
        offset = 0
        Dim row As ContentTableRow
		
		' Get column
		Dim oSNcol As Integer = 0		
		For i = 1 To family.TableColumns.Count
'			If family.TableColumns.Item(i).DisplayHeading = "Stock Number" Then
			If family.TableColumns.Item(i).DisplayHeading = oColName Then
				oSNcol = i
				Exit For
			End If
		Next i
		
		' create member if value is found
        For Each row In family.TableRows
            ' Create the member (part file) from the table.
            Dim failureReason As MemberManagerErrorsEnum
            Dim failureMessage As String
            Dim memberFilename As String
					
			If oSNcol <> 0 Then
'				If row.Item(oSNcol).Value = "030-0254" Then
				If row.Item(oSNcol).Value = oValue Then
					memberFilename = family.CreateMember(row, failureReason, failureMessage, kRefreshOutOfDateParts)
				Else
					memberFilename = Nothing
				End If
			End If
            
			Break
			
            ' Place the part into the assembly.
			If Not IsNothing(memberFilename) AndAlso memberFilename.Length <> 0 Then
				Dim transMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
				transMatrix.Cell(2, 4) = offset
				Dim Occ As ComponentOccurrence = asmDef.Occurrences.Add(memberFilename, transMatrix)
            
	            ' Compute the position for the next placement based on the size of the part just placed.
	            Dim minY As Double
	            Dim maxY As Double
	            minY = Occ.RangeBox.MinPoint.Y
	            maxY = Occ.RangeBox.MaxPoint.Y
	            offset = offset + ((maxY - minY) * 1.1)
			End If
				
        Next
    End If
End Sub

 

In the example above, I am searching for a plain flat washer having a specific stock number. Since the code originally placed multiple parts, you could remove a large portion of the code in lines 75-86 that increments the insertion point. For a single part, your would just need line 78.

 

I left commented lines in the code that represent the hard-coded search for that specific washer.

 

All that code above can make one appreciate how much work is going on inside the iLogic 'Components.AddContentCenterPart'

 

Dim Washer1 = Components.AddContentCenterPart(
   "Washer1",
   "Fasteners:Washers:Plain",
   "NMC Flat Washer - Inch",
   { "StockNumber", "030-0254" },
   position := Nothing, grounded := False,
   visible := True, appearance := Nothing)

 

Let us know if this is helpful. Please mark as 'Accepted' if this answers your question.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 5 of 10

jjstr8
Collaborator
Collaborator

Sorry, I should have specified that CreateMember is looking for a ContentIdentifier or the TableRow object, not just the index. Instead of 14, try oFamily.TableRows(14) or oFamily.TableRows(14).ContentIdentifier 

0 Likes
Message 6 of 10

J_Dumont
Advocate
Advocate

Thanks for input.

This is what happens.

 

oFamily.TableRows(14) fails here.

J_Dumont_0-1739977707056.png

oFamily.TableRows(14).ContentIdentifier fails here

J_Dumont_1-1739977734725.png

 

 

 

0 Likes
Message 7 of 10

J_Dumont
Advocate
Advocate

Hi @JBerns 

This looks great, thank you.

I'm working on getting the rows to work because I think that might be the simplest approach.

 

For PEM nuts, I would need to search for "Thread_Code" and "Shank_Code."

 

J_Dumont_2-1739978054750.png

 

 

0 Likes
Message 8 of 10

jjstr8
Collaborator
Collaborator
Accepted solution

Sorry, I was mistaken about using .ContentIdentifier and your earlier row number. It should take a TableRow(14), 14, or TableRow(14).InternalName. Regardless, where you add an occurrence, it needs to be to the ComponentDefinition, not the Document object.

0 Likes
Message 9 of 10

J_Dumont
Advocate
Advocate

It was an issue with the component definition.

Below is the code that is working.

Thank you both so much for replying and helping me with this issue.

 

 

    Public Sub InsertPEMNuts()
        Dim asmDef As AssemblyComponentDefinition
        asmDef = oDoc.ComponentDefinition

        ' Get the node in the content browser based on the names of the nodes in the hierarchy.
        Dim hexHeadNode As ContentTreeViewNode
        hexHeadNode = g_inventorApplication.ContentCenter.TreeViewTopNode.ChildNodes.Item("Sheet Metal").ChildNodes.Item("Nuts")

        ' Find a specific family.
        Dim family As ContentFamily
        Dim checkFamily As ContentFamily
        For Each checkFamily In hexHeadNode.Families
            If checkFamily.DisplayName = "PEM Self-Clinching Nuts S,SS,CLS,CLSS - Inch" Then
                family = checkFamily
                Exit For
            End If
        Next

        If Not family Is Nothing Then
            ' Place one instance of each member.
            Dim offset As Double
            offset = 0
            'Dim row As ContentTableRow
            ' Create the member (part file) from the table.
            Dim failureReason As MemberManagerErrorsEnum
            Dim failureMessage As String
            Dim memberFilename As String
            memberFilename = family.CreateMember(14, failureReason, failureMessage, ContentMemberRefreshEnum.kDoNotRefreshOutOfDateParts)

            ' Place the part into the assembly.
            Dim transMatrix As Matrix
            transMatrix = g_inventorApplication.TransientGeometry.CreateMatrix
            transMatrix.Cell(2, 4) = offset
            Dim Occ As ComponentOccurrence
            Occ = asmDef.Occurrences.Add(memberFilename, transMatrix)
        Else
            MsgBox("Content Center Family not Found for PEM Nuts.")
        End If
    End Sub

 

0 Likes
Message 10 of 10

JBerns
Advisor
Advisor

@J_Dumont,

You're welcome. 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes