Custom C# batch crashes (performance issue?)

Valued Contributor
Valued Contributor

Hi,

 

I've recreated and heavily modified the Solid Edge batch application for my company, and it works for small assemblies (±20 parts). It reads the active assembly and recursively loops through all subassemblies to find all the model-documents. Then it will try to find all the corresponding drafts. The user can apply some checks to perform and then start the process. The program will then loop through all the model-documents, do the checks and save as STP. Then it will loop through all the drafts checking and saving them as PDF.

 

However, we would like to process much larger assemblies, but the process keeps crashing on random documents (last time it crashed on closing the draft). While the batch is processing, Solid Edge's memory usage keeps growing (380 MB idle, up to 2300 MB). I'm assuming it has to do with performance. I tried closing the document with Documents.CloseDocument, but this still didn't fix it.

 

Below is the code that processes the drafts (drafts are stored in a List of Tuple<string, string>; FullName and Name). The process for model-documents is somewhat the same, but the program breaks down on both of them.

 

                foreach (Tuple<string, string> draftraw in DraftsToProcess)
                {
                    draftindex++;

                    float perc = (((float)modelindex + (float)draftindex) / (float)totaldocs) * 100;
                    txtStatus.Text = "Processing drafts - " + draftindex + "/" + DraftsToProcess.Count + " (" + Math.Floor(perc) + "%)";
                    if (chkLogFile.Checked)
                    {
                        file.WriteLine("");
                        file.WriteLine(draftraw.Item2);
                    }

                    SolidEdgeDraft.DraftDocument draft;
                    SolidEdgeDocument refmodel;

                    try
                    {
                        draft = objApp.Documents.Open(draftraw.Item1);
                        refmodel = (SolidEdgeDocument)draft.ModelLinks.Item(1).ModelDocument;
                    }
                    catch (Exception)
                    {
                        if (chkLogFile.Checked)
                            file.WriteLine("Failed to open draft");

                        ErrorsDuringBatch.Add(draftraw);
                        continue;
                    }


                    // CHECK MODELLINK FOR DRAFT
                    if (refmodel.Name.Substring(0, refmodel.Name.Length - 4) != draft.Name.Substring(0, draft.Name.Length - 4))
                    {
                        draft.Close(false);
                        objApp.DoIdle();
                        if (chkLogFile.Checked)
                            file.WriteLine("Failed to process: Drafts links to wrong model");

                        ErrorsDuringBatch.Add(Tuple.Create(draft.FullName, draft.Name));
                        continue;
                    }
                    // CHECK AND RETRIEVE ISAH
                    string Isah = "";
                    if (chkISAH.Checked)
                        Isah = GetISAH(refmodel);

                    // UPDATE DRAFT
                    if(chkUpdateDFT.Checked)
                    {
                        for (int i = 0; i < draft.Sheets.Count; i++)
                        {
                            foreach(SolidEdgeDraft.Sheet sheet in draft.Sections.WorkingSection.Sheets)
                            {
                                foreach(SolidEdgeDraft.DrawingView view in sheet.DrawingViews)
                                {
                                    view.Update();
                                }
                            }
                        }

                        foreach(SolidEdgeDraft.PartsList list in draft.PartsLists)
                        {
                            if (list.IsUpToDate == false)
                                list.Update();
                        }
                    }

                    // CHECK REVISION AND DOCUMENT NUMBER
                    if (chkRevStatus.Checked && IsFileNameContourNumber(draft.Name))
                    {
                        if(CheckRevision(refmodel) == false)
                        {
                            draft.Close(false);
                            objApp.DoIdle();
                            if (chkLogFile.Checked)
                                file.WriteLine("Failed to process: Revision and/or document number are not correct");

                            ErrorsDuringBatch.Add(Tuple.Create(draft.FullName, draft.Name));
                            continue;
                        }
                    }

                    // CHECK PAPER SIZE (IDENTIFY > A3 SIZE)
                    string papersize = "";
                    if (chkPaperSize.Checked)
                    {
                        if(draft.ActiveSheet.Background.SheetSetup.SheetHeight > 0.297)
                            papersize = "_groot";
                    }

                    string newname = folder + @"\" + Isah + draft.Name.Substring(0, draft.Name.Length - 4) + papersize + ".pdf";
                    draft.SaveAs(newname);
                    objApp.DoIdle();

                    if (chkLogFile.Checked)
                        file.WriteLine("Successfully saved " + newname);

                    draft.Close(true);  // Last time it crashed here
                    objApp.DoIdle();

Is there some way to improve this (Can I clear the memory after every document, or set processed files to inactive or something)? I added the error message (not much of value but still)

___________________________
The truth is .... I am Dutch
VB.NET - C# - VS Ex 2015 - ST9