IfcDoc.FormEdit.backgroundWorkerValidate_DoWork C# (CSharp) Method

backgroundWorkerValidate_DoWork() private method

private backgroundWorkerValidate_DoWork ( object sender, DoWorkEventArgs e ) : void
sender object
e DoWorkEventArgs
return void
        private void backgroundWorkerValidate_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this.m_filterviews == null)
                return;

            // reset state
            foreach(DocTemplateDefinition template in this.m_project.Templates)
            {
                TemplateReset(template);
            }

            // count active roots
            int progressTotal = 2;
            foreach (DocModelView docView in this.m_filterviews)
            {
                // reset state
                foreach(DocConceptRoot docRoot in docView.ConceptRoots)
                {
                    foreach(DocTemplateUsage docUsage in docRoot.Concepts)
                    {
                        docUsage.ResetValidation();
                    }
                }

                progressTotal += docView.ConceptRoots.Count;
            }
            this.m_formProgress.SetProgressTotal(progressTotal);
            int progress = 0;

            // build schema dynamically
            this.backgroundWorkerValidate.ReportProgress(++progress, "Compiling schema...");
            Dictionary<string, Type> typemap = new Dictionary<string, Type>();

            Compiler compiler = new Compiler(this.m_project, this.m_filterviews, this.m_filterexchange);
            System.Reflection.Emit.AssemblyBuilder assembly = compiler.Assembly;

            this.m_assembly = assembly;

            Type[] types = assembly.GetTypes();
            foreach (Type t in types)
            {
                typemap.Add(t.Name.ToUpper(), t);
            }

            int grandtotallist = 0;
            int grandtotalskip = 0;
            int grandtotalpass = 0;
            StringBuilder sb = new StringBuilder();

            // Example:
            // | IfcWall   | #2, #3 | PASS (30/30) |
            // | +Identity | #2     | FAIL (23/30) |

            try
            {
                m_loading = true;
                Dictionary<long, SEntity> instances = new Dictionary<long, SEntity>();
                this.backgroundWorkerValidate.ReportProgress(++progress, "Loading file...");
                using (FormatSPF format = new FormatSPF(Properties.Settings.Default.ValidateFile, typemap, instances))
                {
                    format.Load();

                    string[] errors = format.Errors;
                    if (errors != null && errors.Length > 0)
                    {
                        sb.AppendLine("This file contains format errors which may impact data validation results:");
                        sb.AppendLine("<ul>");
                        for (int i = 0; i < errors.Length; i++)
                        {
                            sb.AppendLine("<li>" + errors[i] + "</li>");
                        }
                        sb.AppendLine("</ul>");
                    }

                    // now iterate through each concept root
                    foreach (DocModelView docView in this.m_filterviews)
                    {
                        foreach (DocConceptRoot docRoot in docView.ConceptRoots)
                        {
                            if (this.backgroundWorkerValidate.CancellationPending)
                                return;

                            this.backgroundWorkerValidate.ReportProgress(++progress, docRoot);

                            Type typeEntity = null;
                            if (typemap.TryGetValue(docRoot.ApplicableEntity.Name.ToUpper(), out typeEntity))
                            {
                                // build list of instances
                                List<SEntity> list = new List<SEntity>();
                                foreach (SEntity instance in format.Instances.Values)
                                {
                                    if (typeEntity.IsInstanceOfType(instance))
                                    {
                                        list.Add(instance);
                                    }
                                }

                                //if (list.Count > 0)
                                {
                                    sb.AppendLine("<h3>" + docRoot.ApplicableEntity.Name + " (" + list.Count + ")</h3>");

                                    foreach (DocTemplateUsage docUsage in docRoot.Concepts)
                                    {
                                        ValidateConcept(docUsage, docView, DocExchangeRequirementEnum.NotRelevant, typeEntity, list, sb, typemap, ref grandtotalpass, ref grandtotalskip, ref grandtotallist);
                                    }
                                }
                            }
                        }
                    }
                    this.m_formatTest = format;
                }
            }
            catch (Exception x)
            {
                this.m_exception = x;
            }
            finally
            {
                m_loading = false;
            }

            sb.AppendLine("</table>");

            // create html doc
            int grandtotalpercent = 0;
            if (grandtotallist > 0)
            {
                grandtotalpercent = 100 * (grandtotalpass + grandtotalskip) / grandtotallist;
            }

            if (Properties.Settings.Default.ValidateReport)
            {
                string path = Properties.Settings.Default.ValidateFile + ".htm";
                using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create))
                {
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fs))
                    {
                        writer.WriteLine("<html>");
                        writer.WriteLine("<body>");

                        string exchange = null;
                        if (this.m_filterexchange != null)
                        {
                            exchange = this.m_filterexchange.Name;
                        }

                        writer.WriteLine("<h1>Validation Results</h1>");
                        writer.WriteLine("<table border='1'>");
                        writer.WriteLine("<tr><td>Instance File</td><td>" + Properties.Settings.Default.ValidateFile + "</td></tr>");
                        writer.WriteLine("<tr><td>Project File</td><td>" + this.m_file + "</td></tr>");
                        writer.WriteLine("<tr><td>Model View</td><td>" + this.m_filterviews[0].Name + "</td></tr>");
                        writer.WriteLine("<tr><td>Exchange</td><td>" + exchange + "</td></tr>");
                        writer.WriteLine("<tr><td>Tests Executed</td><td>" + grandtotallist + "</td></tr>");
                        writer.WriteLine("<tr><td>Tests Passed</td><td>" + grandtotalpass + "</td></tr>");
                        writer.WriteLine("<tr><td>Tests Ignored</td><td>" + grandtotalskip + "</td></tr>");
                        writer.WriteLine("<tr><td>Tests Percentage</td><td>" + grandtotalpercent + "%</td></tr>");
                        writer.WriteLine("</table>");

                        writer.WriteLine(sb.ToString());

                        writer.WriteLine("</body>");
                        writer.WriteLine("</html>");
                    }
                }

                // launch
                System.Diagnostics.Process.Start(path);
            }
        }
FormEdit