subOccurrence cannot be used in traverse assembly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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?