Loop through BOM in assembly and get iProperties using Apprentice

Loop through BOM in assembly and get iProperties using Apprentice

Anonymous
Not applicable
2,206 Views
2 Replies
Message 1 of 3

Loop through BOM in assembly and get iProperties using Apprentice

Anonymous
Not applicable

Hello, 

I would like to ask if it is possible to loop through the assembly file using Apprentice Server and export information from BOM (part number, description, . I read everywhere that people can access BOM through Apprentice Server but I never found a working solution how to do it. 

I would need Part number, Description, Category, item QTY, Mass, G_L (custom parameter for unit length), Full file Path. 

 

If I reference : 

Dim oApprentice As ApprenticeServerComponent

    oApprentice = New ApprenticeServerComponent

 

    ' Open a document.

    Dim oDoc As ApprenticeServerDocument

    Dim oBom as inventor.bom

    oDoc = oApprentice.Open("filename.iam")

    oBom  = oDoc.componentdefinition.bom     <- This property/method is not available in Apprentice

 

Can you please help me how to do it correctly. 

 

Thank you. 

0 Likes
Accepted solutions (1)
2,207 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor
Accepted solution

Hi,

in the documentation i could only find: "Apprentice provides access to file references, assembly structure, B-Rep, geometry, attributes, render styles, and document properties." In the ApprenticeServerDocument object i could also not find any reference to a BOM object (like in the full Inventor api).

But it is possible to acces the assembly structure. therefor you could write your own BOM/List object. something like this:

 

c# (console application)

class Program
    {
        private static Dictionary<string,BomItem> BOM = new Dictionary<string,BomItem>();

        static void Main(string[] args)
        {
            string fileName = @"C:\temp\Assembly2.iam";
            ApprenticeServerComponent apprentice = new ApprenticeServerComponent();
            ApprenticeServerDocument apprenticeDoc = apprentice.Open(fileName);

            createPartsOnlyBom(apprenticeDoc);

            foreach (KeyValuePair<string, BomItem> item in BOM)
            {
                BomItem b = item.Value;
                Console.WriteLine("{0}|{1}|{2}",b.qty, b.fullFileName,b.partNumber);
            }
            Console.ReadKey();
        }

        public static void createPartsOnlyBom(ApprenticeServerDocument doc)
        {
            foreach (ComponentOccurrence occ in doc.ComponentDefinition.Occurrences)
            {
                if (occ.ReferencedDocumentDescriptor.ReferencedDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    createPartsOnlyBom(occ.ReferencedDocumentDescriptor.ReferencedDocument);
                }
                else
                {
                    if (occ.BOMStructure == BOMStructureEnum.kNormalBOMStructure ||
                    occ.BOMStructure == BOMStructureEnum.kPhantomBOMStructure)
                    {
                        string fulldocName = occ.ReferencedDocumentDescriptor.FullDocumentName;
                        if (BOM.ContainsKey(fulldocName) == false)
                        {
                            ApprenticeServerDocument docOcc = occ.ReferencedDocumentDescriptor.ReferencedDocument;
                            
                            BomItem newItem = new BomItem();
                            newItem.fullFileName = fulldocName;
                            newItem.qty = 1;
                            newItem.partNumber = docOcc.PropertySets["Design Tracking Properties"]["Part number"].Value;
                            BOM.Add(fulldocName, newItem);
                        }
                        else
                        {
                            BOM[fulldocName].qty += 1;
                        }
                    }
                }
            }
        }
    }

    public class BomItem
    {
        public string fullFileName;
        public int qty;
        public string partNumber;
    }

 

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 3

Anonymous
Not applicable

Thank you very much for your help. I have already used a different solution, i.e. to export BOM to Excel with full file path and then load it into my program, where I reference each file path in the assembly and I use InventorView activeX component to display parts and subassemblies within my program. As I need to do it only once, it is no big deal. But when I have time, I will try your solution too.

0 Likes