Traverse an Assembly Using Custom Job - Inventor Server

Traverse an Assembly Using Custom Job - Inventor Server

con_smith
Participant Participant
122 Views
0 Replies
Message 1 of 1

Traverse an Assembly Using Custom Job - Inventor Server

con_smith
Participant
Participant

Hello, 

 

Perhaps an easy one, but I am struggling to find documentation on how to use Inventor Server instead of Apprentice. I am trying to use apprentice to traverse and assembly, but it just is not working. I receive COMException when trying to call occurrences.Name;

 

string mDocPath = AcquireFileForInventorAndGetPath(connection, mWsMgr, selectedContextFile);
ApprenticeServerComponent apprenticeServer = new ApprenticeServerComponent();
ApprenticeServerDocument apprenticeDocument = apprenticeServer.Open(mDocPath);

IterateBOM(apprenticeDocument.ComponentDefinition.Occurrences, connection, mWsMgr, BOM, bomLevel, parentName);

 public static ErrorHandler IterateBOM(ComponentOccurrences asssemblyOccurrences, Connection connection, WebServiceManager mWsMgr, Dictionary<string, BomItem> BOM, int bomLevel, string parentName)
 {
     bomLevel++;

     foreach (ComponentOccurrence occurrence in asssemblyOccurrences)
     {
         string occurrenceNameForError = occurrence.Name;
         try
         {
             if (!occurrence.Suppressed)
             {
                 UpdateBomDictionary(occurrence, BOM, bomLevel, parentName, "Assembly");

                 string newParentName = occurrence.Name;

                 MessageBox.Show(newParentName, "Message", MessageBoxButtons.OK);

                 ProcessAllSubOcc(occurrence.SubOccurrences, connection, mWsMgr, BOM, bomLevel, newParentName);

               

             }
         }
         catch (Exception ex)
         {
             ErrorHandler iterateBOMError = new ErrorHandler() { FailureOccurred = true, Message = $"{ex.ToString()} --- Document {occurrenceNameForError} has caused a failure." };
             return iterateBOMError;
         }
     }
     ErrorHandler iterateBOMSuccess = new ErrorHandler() { FailureOccurred = false, Message = "Success" };
     return iterateBOMSuccess;
 }

        public static void ProcessAllSubOcc(ComponentOccurrencesEnumerator subOccurrences, Connection connection, WebServiceManager mWsMgr, Dictionary<string, BomItem> BOM, int bomLevel, string parentName)
        {
            bomLevel++;

            foreach (ComponentOccurrence occurrence in subOccurrences)
            {
                if (!occurrence.Suppressed)
                {
                    UpdateBomDictionary(occurrence, BOM, bomLevel, parentName, "Assembly");

                    string newParentName = occurrence.Name;

                    ProcessAllSubOcc(occurrence.SubOccurrences, connection, mWsMgr, BOM, bomLevel, newParentName);                

                }
            }
        }

        public static void UpdateBomDictionary(ComponentOccurrence occurrence, Dictionary<string, BomItem> BOM, int bomLevel, string parentName, string documentType)
        {
            //string occurrencePath = occurrence.ReferencedDocumentDescriptor.FullDocumentName;
            //string[] filePathParts = occurrencePath.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
            //string occurrenceName = filePathParts[filePathParts.Length - 1];
            string occurrenceName = occurrence.Name;

            if (BOM.ContainsKey(occurrenceName) == false)
            {
                BomItem newItem = new BomItem();

                newItem.FullFileName = occurrenceName;
                newItem.Quantity = 1;
                newItem.BOMLevel = bomLevel;
                newItem.BOMParent = parentName;
                //newItem.AssemblyOrPart = documentType;

                BOM.Add(occurrenceName, newItem);
            }
            else
            {
                BOM[occurrenceName].Quantity += 1;
            }
        }

 

I have seen others do this successfully using Inventor, but I am struggling to find how to instantiate a session of Inventor similar to how I do above with Apprentice. Any tips or links?

0 Likes
123 Views
0 Replies
Replies (0)