subOccurrence cannot be used in traverse assembly

subOccurrence cannot be used in traverse assembly

con_smith
Participant Participant
135 Views
1 Reply
Message 1 of 2

subOccurrence cannot be used in traverse assembly

con_smith
Participant
Participant

I am trying to traverse an assembly but receive the following error when occurrence.SubOccurrences is called:

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

        public static (ErrorHandler, Dictionary<string, BomItem>) createBOM(Connection connection, WebServiceManager mWsMgr, ACW.File selectedContextFile, Dictionary<string, BomItem> BOM, InventorServer mInv)
        {
            try
            {
                string mDocPath = AcquireFileForInventorAndGetPath(connection, mWsMgr, selectedContextFile);

                Inventor.Document contextFileDocument = mInv.Documents.Open(mDocPath);
                string parentName = contextFileDocument.GetFileNameWithoutExtensionInventorDocument();

                AssemblyDocument assemblyDocument = (AssemblyDocument)contextFileDocument;

                int bomLevel = 0;

                ErrorHandler iterateBOMError = IterateBOM(assemblyDocument.ComponentDefinition.Occurrences, connection, mWsMgr, BOM, bomLevel, parentName);

                contextFileDocument.Close(true);

                return (iterateBOMError, BOM);
            }
            catch (Exception ex)
            {
                ErrorHandler createBOMError = new ErrorHandler() { FailureOccurred = true, Message = ex.ToString()" };
                return (createBOMError, null);
            }
        }

      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)
                  {
                      if (occurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                      {
                          try
                          {
                              string occurrenceName = occurrence.GetFileNameWithExtensionOccurrence();
                              string newParentName = occurrence.GetFileNameWithoutExtensionOccurrence();

                              
                              UpdateBomDictionary(occurrenceName, BOM, bomLevel, parentName, "Assembly");

                              ProcessAllSubOcc(occurrence.SubOccurrences, connection, mWsMgr, BOM, bomLevel, newParentName);
                          }
                          catch
                          {
                              Exception ex = new Exception(occurrenceNameForError);
                              throw ex;
                          }
                      }
                      else
                      {
                          try
                          {
                              string occurrenceName = occurrence.GetFileNameWithExtensionOccurrence();

                              UpdateBomDictionary(occurrenceName, BOM, bomLevel, parentName, "Part");
                          }
                          catch
                          {
                              Exception ex = new Exception(occurrenceNameForError);
                              throw ex;
                          }
                      }              
                  }
              }
              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)
           {
               string occurrenceNameForError = occurrence.Name;

               if (!occurrence.Suppressed)
               {
                   if (occurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                   {
                       try
                       {
                           string occurrenceName = occurrence.GetFileNameWithExtensionOccurrence();
                           string newParentName = occurrence.GetFileNameWithoutExtensionOccurrence();

                           UpdateBomDictionary(occurrenceName, BOM, bomLevel, parentName, "Assembly");

                           ProcessAllSubOcc(occurrence.SubOccurrences, connection, mWsMgr, BOM, bomLevel, newParentName);
                       }
                       catch
                       {
                           Exception ex = new Exception(occurrenceNameForError); 
                           throw ex;

                       }
                   }
                   else
                   {
                       try
                       {
                           string occurrenceName = occurrence.GetFileNameWithExtensionOccurrence();

                           UpdateBomDictionary(occurrenceName, BOM, bomLevel, parentName, "Part");
                       }
                       catch
                       {
                           Exception ex = new Exception(occurrenceNameForError);
                           throw ex;
                       }
                   }
               }
           }
       }

 

The program calls CreateBOM and starts from there. 

 

I have definitely isolated the issue to when ProcessAllSubOcc(occurrence.SubOccurrences, ....). The thing is it fails on a very simple assembly that most certainly has suboccurrences: 

 

- Assembly X (Normal BOM Structure)

- - Part Y

- - Part Z 

 

I didn't include UpdateBOMDictionary, because it just adds some properties to a C# dict for use later in the program.

 

Perhaps also worth noting - I found for my application traversing the assembly in the manner shown was the most effective (I tried a few other ways to obtain the information as well). 

 

I am a bit at a loss because the error is not descriptive at all. Looking at this code, do you see anything wrong? Perhaps are there additional debug steps I should take to root-cause this issue? 

0 Likes
136 Views
1 Reply
Reply (1)
Message 2 of 2

Markus.Koechl
Autodesk
Autodesk

Your question relates to Inventor API rather than Vault, which we discuss in this forum. Anyway, the ComponentOccurrence Object of the Inventor API does not provide the methods that you call to fill the occurrenceName or newParentName variables.
Note: Your approach is at risk of failing for weldments, as they may contain an internal subassembly node without a referenced file. I'd recommend validating the suboccurrences.count in any case.



Markus Koechl

Solutions Engineer PDM, Autodesk Central Europe
0 Likes