<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic API VB.net Counting Parts in assembly in Inventor Programming - iLogic, Macros, AddIns &amp; Apprentice</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/api-vb-net-counting-parts-in-assembly/m-p/11594787#M145994</link>
    <description>&lt;P&gt;Hello Guys Im passing a old Ilogic rule to VB.net, the rule was courtesy of Mega Jerk.&lt;/P&gt;&lt;P&gt;The code is to Run in Assembly and count instaces of the same part and punt the instaces number in a parameter&lt;/P&gt;&lt;P&gt;to have the total count of parts in the drawing.&lt;/P&gt;&lt;P&gt;But Im running into an error, and havent figured out how to solve it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any help would be appreciated&lt;/P&gt;&lt;P&gt;Here is The code&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Public Class DATOS_FRM


    Private _inventor As Inventor.Application
    Public InventorApp As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
    Public assemblyDoc As AssemblyDocument = InventorApp.ActiveDocument

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ThisApplication As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
        Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument


        Dim openDoc As Document
        Dim FNamePos As Long
        Dim docFName As String
        Dim assemblyDef As AssemblyComponentDefinition
        Dim partQty As ComponentOccurrencesEnumerator
        Dim docFile As PartDocument


        If oAsmDoc.DocumentType &amp;lt;&amp;gt; DocumentTypeEnum.kAssemblyDocumentObject Then
            Exit Sub
        Else

            'MsgBox("You must have a valid Assembly document open before using this code!", "File Type Mismatch!")
        End If

        For Each docFile In oAsmDoc.AllReferencedDocuments


                FNamePos = InStrRev(docFile.FullFileName, "\", -1)

                docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)

                If docFile.IsModifiable = True Then

                    oAsmDoc = openDoc
                    assemblyDef = oAsmDoc.ComponentDefinition
                    partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)

                    Try
                        If partQty.Count &amp;lt;&amp;gt; assemblyDef.iProperties.Value(docFName, "Project", "Authority") Then
                            assemblyDef.iProperties.Value(docFName, "Project", "Authority") = partQty.Count

                        End If
                    Catch
                        assemblyDef.iProperties.Value(docFName, "Project", "Authority") = partQty.Count
                    End Try
                End If
            Next

    End Sub
End Class&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jromo_0-1670040314836.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1147725i7388965F3976EEC7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jromo_0-1670040314836.png" alt="jromo_0-1670040314836.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 03 Dec 2022 04:05:23 GMT</pubDate>
    <dc:creator>j.romo</dc:creator>
    <dc:date>2022-12-03T04:05:23Z</dc:date>
    <item>
      <title>API VB.net Counting Parts in assembly</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/api-vb-net-counting-parts-in-assembly/m-p/11594787#M145994</link>
      <description>&lt;P&gt;Hello Guys Im passing a old Ilogic rule to VB.net, the rule was courtesy of Mega Jerk.&lt;/P&gt;&lt;P&gt;The code is to Run in Assembly and count instaces of the same part and punt the instaces number in a parameter&lt;/P&gt;&lt;P&gt;to have the total count of parts in the drawing.&lt;/P&gt;&lt;P&gt;But Im running into an error, and havent figured out how to solve it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any help would be appreciated&lt;/P&gt;&lt;P&gt;Here is The code&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Public Class DATOS_FRM


    Private _inventor As Inventor.Application
    Public InventorApp As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
    Public assemblyDoc As AssemblyDocument = InventorApp.ActiveDocument

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ThisApplication As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
        Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument


        Dim openDoc As Document
        Dim FNamePos As Long
        Dim docFName As String
        Dim assemblyDef As AssemblyComponentDefinition
        Dim partQty As ComponentOccurrencesEnumerator
        Dim docFile As PartDocument


        If oAsmDoc.DocumentType &amp;lt;&amp;gt; DocumentTypeEnum.kAssemblyDocumentObject Then
            Exit Sub
        Else

            'MsgBox("You must have a valid Assembly document open before using this code!", "File Type Mismatch!")
        End If

        For Each docFile In oAsmDoc.AllReferencedDocuments


                FNamePos = InStrRev(docFile.FullFileName, "\", -1)

                docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)

                If docFile.IsModifiable = True Then

                    oAsmDoc = openDoc
                    assemblyDef = oAsmDoc.ComponentDefinition
                    partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)

                    Try
                        If partQty.Count &amp;lt;&amp;gt; assemblyDef.iProperties.Value(docFName, "Project", "Authority") Then
                            assemblyDef.iProperties.Value(docFName, "Project", "Authority") = partQty.Count

                        End If
                    Catch
                        assemblyDef.iProperties.Value(docFName, "Project", "Authority") = partQty.Count
                    End Try
                End If
            Next

    End Sub
End Class&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jromo_0-1670040314836.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1147725i7388965F3976EEC7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jromo_0-1670040314836.png" alt="jromo_0-1670040314836.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Dec 2022 04:05:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/api-vb-net-counting-parts-in-assembly/m-p/11594787#M145994</guid>
      <dc:creator>j.romo</dc:creator>
      <dc:date>2022-12-03T04:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: API VB.net Counting Parts in assembly</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/api-vb-net-counting-parts-in-assembly/m-p/11594870#M145995</link>
      <description>&lt;P&gt;A few things I see.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is incorrect, this is an API object mixed with an ilogic function for iproperties.&amp;nbsp; You will need to stick with the API versions a list is here in this &lt;A href="https://modthemachine.typepad.com/my_weblog/iproperties/" target="_blank" rel="noopener"&gt;article&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;assemblyDef.iProperties.Value(docFName, "Project", "Authority") &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Doc file is declared as a part Document but when looping through all referenced documents you will come across both assembly documents and part documents.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;      &lt;SPAN&gt;Dim&lt;/SPAN&gt; &lt;SPAN&gt;docFile&lt;/SPAN&gt; &lt;SPAN&gt;As&lt;/SPAN&gt; &lt;SPAN&gt;PartDocument&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;So you will need to be more generic at the beginning then use a filter like&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;If&lt;/SPAN&gt; &lt;SPAN&gt;docFile&lt;/SPAN&gt;.&lt;SPAN&gt;DocumentType&lt;/SPAN&gt; = &lt;SPAN&gt;DocumentTypeEnum&lt;/SPAN&gt;.&lt;SPAN&gt;kAssemblyDocumentObject&lt;/SPAN&gt; &lt;SPAN&gt;Then&lt;BR /&gt;End If&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;A few more questions:&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where in the drawing do you want the number put? You could try a different approach and extract the part qty either from the partslist object or the BOM Object. Both of which should give you total qty's once you filter by the target part.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want to stay with the assembly, are you putting the qty in a parameter of the Main assembly or an iProperty? What is your search criteria for the&amp;nbsp; parts&amp;nbsp; to&amp;nbsp; be counted?&lt;/P&gt;</description>
      <pubDate>Sat, 03 Dec 2022 05:48:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/api-vb-net-counting-parts-in-assembly/m-p/11594870#M145995</guid>
      <dc:creator>A.Acheson</dc:creator>
      <dc:date>2022-12-03T05:48:50Z</dc:date>
    </item>
  </channel>
</rss>

