Apprentice App Stopped Working - Unable to call ComponentDefinition.Occurrences

Apprentice App Stopped Working - Unable to call ComponentDefinition.Occurrences

con_smith
Participant Participant
269 Views
2 Replies
Message 1 of 3

Apprentice App Stopped Working - Unable to call ComponentDefinition.Occurrences

con_smith
Participant
Participant

Hello,

 

I had an apprentice application that was working just fine, and then it stopped working out of nowhere. There have been no updates to Vault or Inventor. Really what this application does is just loop through the assembly structure and make a simple dictionary/BOM so I can use it later in the program. 

 

In the 'iterateBOM' loop, I search for the latest component file and create a new instance of apprentice. I do this because without this, I get a COM error. This COM error is persistent until I instantiated a new instance of Inventor in each loop. Odd behavior - I have seen others just pass occurrences.suboccurrences or use other methods. I am unable to explain it. 

 

What I have found is that it just simply cannot found any occurrences in apprenticeDocument.ComponentDefinition.Occurrences, so when it passes this into 'iterateBOM', an exception is thrown right at the line that says: foreach(ComponentOccurrence occurrence in occurrences) loop. 

 

 

public static Dictionary<string, BomItem> createBOM(Connection connection, WebServiceManager mWsMgr, ACW.File selectedContextFile, Dictionary<string, BomItem> BOM)
        {
            //ApprenticeServerDocument apprenticeDocument = InstantiateAndOpenApprentice(selectedContextFile, connection, mWsMgr);
            string mDocPath = AcquireFileForInventorAndGetPath(connection, mWsMgr, selectedContextFile);
            ApprenticeServerComponent newApprentice = new ApprenticeServerComponent();
            ApprenticeServerDocument apprenticeDocument = newApprentice.Open(mDocPath);

            iterateBOM(apprenticeDocument.ComponentDefinition.Occurrences, connection, mWsMgr, BOM);

            return BOM;
        }

public static void iterateBOM(ComponentOccurrences occurrences, Connection connection, WebServiceManager mWsMgr, Dictionary<string, BomItem> BOM)
        {
            foreach (ComponentOccurrence occurrence in occurrences)
            {
                if (!occurrence.Suppressed)
                {
                    if (occurrence.ReferencedDocumentDescriptor.ReferencedDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                    {
                        UpdateBomDictionary(occurrence, BOM);
                       
                        string fileOccurrencePath = occurrence.ReferencedDocumentDescriptor.FullDocumentName;
                        string[] filePathParts = fileOccurrencePath.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
                        string occurrenceName = filePathParts[filePathParts.Length - 1];
                        ACW.File[] occurrenceSearchResults = searchForFileNameInVault(mWsMgr, occurrenceName);
                        foreach (ACW.File resultsFile in occurrenceSearchResults)
                        {
                            if (resultsFile.Name.Contains(".iam") || resultsFile.Name.Contains(".ipt"))
                            {
                                if (!resultsFile.Name.Contains(".dwf"))
                                {
                                    string mDocPath = AcquireFileForInventorAndGetPath(connection, mWsMgr, resultsFile);
                                    ApprenticeServerComponent newApprentice = new ApprenticeServerComponent();
                                    ApprenticeServerDocument apprenticeDocument = newApprentice.Open(mDocPath);
                                    //ApprenticeServerDocument apprenticeDocument = InstantiateAndOpenApprentice(resultsFile, connection, mWsMgr);

                                    iterateBOM(apprenticeDocument.ComponentDefinition.Occurrences, connection, mWsMgr, BOM);
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(occurrence.Name, "Occurrence Part");
                        UpdateBomDictionary(occurrence, BOM);
                    }
                }
            }
        }

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

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

                newItem.FullFileName = occurrenceName;
                newItem.Quantity = 1;

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

public static string AcquireFileForInventorAndGetPath(Connection connection, WebServiceManager mWsMgr, ACW.File selectedContextFile)
        {
            VDF.Vault.Settings.AcquireFilesSettings mDownloadSettings = new VDF.Vault.Settings.AcquireFilesSettings(connection);
            mDownloadSettings.OrganizeFilesRelativeToCommonVaultRoot = true;
            mDownloadSettings.AddFileToAcquire(new VDF.Vault.Currency.Entities.FileIteration(connection, selectedContextFile), VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download);
            VDF.Vault.Results.AcquireFilesResults mDownLoadResult = connection.FileManager.AcquireFiles(mDownloadSettings);

            VDF.Vault.Results.FileAcquisitionResult fileAcquisitionResult = mDownLoadResult.FileResults.Where(n => n.File.EntityName == selectedContextFile.Name).FirstOrDefault();
            string mDocPath = fileAcquisitionResult.LocalPath.FullPath;
            string mExt = System.IO.Path.GetExtension(mDocPath);

            return mDocPath;
        }

 

 

I am really not sure what is wrong if it was working one moment, not working another. Does anything appear wrong in my code? Anything else I might be missing?

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

Michael.Navara
Advisor
Advisor
Accepted solution

Hi @con_smith 

It is hard to test your code. I just can to make a code review.

Try to modify your application to use just one instance of the ApprenticeServerComponent. Several years ago I try to make a tool for analyzing heap of local Inventor files using apprentice and I got in trouble with concurrent access to the files from different instances of apprentice.

At the second I don't understand why you acquire files from Vault one-by-one instead of single call. In AcquireFilesSettings you can specify you want to download top assembly and all children. (May be it is default behavior. I don't remember it). 

acquireFilesSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeChildren = true;
acquireFilesSettings.OptionsRelationshipGathering.FileRelationshipSettings.RecurseChildren = true;
acquireFilesSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeLibraryContents = true;

 

0 Likes
Message 3 of 3

con_smith
Participant
Participant

Hi @Michael.Navara  - Thank you very much for your input. I made both the changes you recommended and now it works again:

 

1. Only instantiated on instance of ApprenticeServerComponent and pass the variable between functions. 

2. Close the Apprentice session when done (I realized I was forgetting to do that). 

3. Changed AcquireFilesSettings so that it was grabbing all children files as well. 

 

Makes me wonder why it was working the first time....perhaps I cleared out the local directory it was downloading the files to (and all the old files the program was working with and referencing), and so it lost those file references? I was grabbing them one by one because I assumed it was quicker just to 'Get' only what I needed for the program to operate. 

 

Or perhaps I was just 'overloading' Apprentice Server with so many server instances?

 

To clarify, the solution was in the combination of your proposals, not one or the other. 

0 Likes