Place iAssembly factory in an Assembly document

By Augusto Goncalves

The following is the sample code which create the iAssemmbly factory. The sample code creates an iAssembly factory using the API method CreateFactory in the assembly file and adds a data from an Excel sheet.

The sample code expects the Arbor_Press assembly file is open before testing the code. Also, it requires references to Microsoft Excel Type Library.

Public Sub CreateiAssemblyFactory()

  Dim oDoc As AssemblyDocument

  oDoc = m_inventorApplication.ActiveDocument

 

  Dim odef As AssemblyComponentDefinition

  odef = oDoc.ComponentDefinition

 

  Dim oFactory As iAssemblyFactory

  oFactory = odef.CreateFactory

 

  Dim oWorkSheet As Microsoft.Office. _

    Interop.Excel.Worksheet

  oWorkSheet = oFactory.ExcelWorkSheet

 

  Dim oCells As Microsoft.Office. _

    Interop.Excel.Range

  oCells = oWorkSheet.Cells

 

  ‘ Columns…

  oCells.Item(1, 2) = "HANDLE CAP:1:Include/Exclude"

  oCells.Item(1, 3) = "HANDLE CAP:2:Include/Exclude"

  oCells.Item(1, 4) = "LEVER ARM:1:Include/Exclude"

  oCells.Item(1, 5) = "Arbor_Frame:1:Grounding Status"

  oCells.Item(1, 6) = "d75"

  oCells.Item(1, 7) = "d92"

 

  ‘ Row 1 values…

  oCells.Item(2, 1) = "Arbor_Press-01"

  oCells.Item(2, 2) = "Include"

  oCells.Item(2, 3) = "Include"

  oCells.Item(2, 4) = "Include"

  oCells.Item(2, 5) = "Grounded"

  oCells.Item(2, 6) = "0.0 in"


  oCells.Item(2, 7) =
"180.00000"

 

  ‘ Row 2 values…

  oCells.Item(3, 1) = "Arbor_Press-02"

  oCells.Item(3, 2) = "Include"

  oCells.Item(3, 3) = "Include"

  oCells.Item(3, 4) = "Include"

  oCells.Item(3, 5) = "Grounded"

  oCells.Item(3, 6) = "0.5 in"

  oCells.Item(3, 7) = "90.00000"

 

  ‘ Row 3 values…

  oCells.Item(4, 1) = "Arbor_Press-03"

  oCells.Item(4, 2) = "Exclude"

  oCells.Item(4, 3) = "Exclude"

  oCells.Item(4, 4) = "Exclude"

  oCells.Item(4, 5) = "Ungrounded"

  oCells.Item(4, 6) = "0.0 in"

  oCells.Item(4, 7) = "180.00000"

 

  Dim oWB As Microsoft.Office.Interop. _

    Excel.WorkBook

  oWB = oWorkSheet.Parent

 

  oWB.Save()

  oWB.Close()

End Sub

The following code shows how to add the occurrences of the created iAssembly factory in to the assembly. Using AddiAssemblyMember method where we can pass the assembly file and the number of rows and columns to add the iAssembly occurrences in the Assembly file.

Public Sub AddiAssemblyOccurrences()

  ‘ Open the factory document invisible.

  Dim oFactoryDoc As AssemblyDocument

  oFactoryDoc = m_inventorApplication. _

    Documents.Open( _

      "C:\folder\Arbor_Press.iam", False)

 

  ‘ Set a reference to the component definition.

  Dim oCompDef As AssemblyComponentDefinition

  oCompDef = oFactoryDoc.ComponentDefinition

 

  ‘ Make sure we have an iAssembly factory.

  If oCompDef.IsiAssemblyFactory = False Then

    MessageBox.Show("Chosen document is not a factory.", _

                    "Not a factory", _

                    MessageBoxButtons.OK, _

                    MessageBoxIcon.Exclamation)

    Exit Sub

  End If

 

  ‘ Set a reference to the factory.

  Dim oiAssyFactory As iAssemblyFactory

  oiAssyFactory = oCompDef.iAssemblyFactory

 

  ‘ Get the number of rows in the factory.

  Dim iNumRows As Integer

  iNumRows = oiAssyFactory.TableRows.Count

 

  ‘ Create a new assembly document

  Dim oDoc As AssemblyDocument

  oDoc = m_inventorApplication.ActiveDocument

 

  Dim oOccs As ComponentOccurrences

  oOccs = oDoc.ComponentDefinition.Occurrences

 

  Dim oPos As Matrix

  oPos = m_inventorApplication. _

    TransientGeometry.CreateMatrix

 

  Dim oStep As Double

  oStep = 0.0#

  Dim iRow As Long

 

  ‘ Add an occurrence for each member in the factory.

  For iRow = 1 To iNumRows

 

    oStep = oStep + 50

 

    ‘ Add a translation along X axis

    oPos.SetTranslation( _

      m_inventorApplication.TransientGeometry. _

      CreateVector(oStep, oStep, 0))

 

    Dim oOcc As ComponentOccurrence

    oOcc = oOccs.AddiAssemblyMember( _

      "C:\Folder\Arbor_Press.iam", _

      oPos, iRow)

  Next

End Sub


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading