///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void TreeBuilder()
        {
            /////////////Catalog Guids and other constants//////////////////
            const string spoolGuid     = "21670682-ab38-484c-bed8-a212650ee25a";
            const string dummyLoadGuid = "85a21fd2-86ad-4744-b1eb-fc6fb232596b";
            const double pi            = Math.PI;

            /////////////End Catalog Guids and other constants//////////////

            PPLPole         pole         = cPPLMain.GetPole();
            List <TreeNode> treeNodeList = cPPLMain.cCatalogManager.GetNodes(PPLCatalogForm.CATALOG_TYPE.MASTER);
            //dummy load case needs to be added to the pole in order to populate the Aux Data fields
            PPLEnvironment dummyLoadCase = FindElement(dummyLoadGuid, treeNodeList) as PPLEnvironment;

            pole.AddChild(dummyLoadCase);
            string sapPM   = pole.GetValueString("Aux Data 1");
            string poleLoc = pole.GetValueString("Aux Data 3");

            //Get pole info from planning spreadsheet
            string pdFilePath      = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + sapPM + " Planning Data.xlsx";
            string treeAttachSheet = "Ocalc Tree Attachments";

            SLDocument            planningData = new SLDocument(pdFilePath, treeAttachSheet);
            SLWorksheetStatistics wsStats      = planningData.GetWorksheetStatistics();
            int    numRows        = wsStats.NumberOfRows;
            int    numCols        = wsStats.NumberOfColumns;
            string instructionStr = string.Empty;

            for (int i = 2; i <= numRows; i++)
            {
                if (planningData.GetCellValueAsString(i, 1).Contains(poleLoc))
                //if (planningData.GetCellValueAsString(i, 1).Equals(poleLoc))
                {
                    instructionStr = planningData.GetCellValueAsString(i, 2);
                }
            }
            planningData.CloseWithoutSaving();

            //assign variables from instruction array
            string[] instructionArr = instructionStr.Split(',');
            string   hftdGuid       = instructionArr[0];

            string go95Guid = instructionArr[1];
            string wireGuid = instructionArr[2];

            double[] distanceArr = instructionArr[3].Split(':').Select(Double.Parse).ToArray();

            //check for spans over 125' and exit program if found
            for (int i = 0; i <= distanceArr.Length - 1; i++)
            {
                if (distanceArr[i] > 125)
                {
                    PPLMessageBox.Show("One or more spans to this pole exceeds 125'. Interset is required", "Cannot Build Pole!");
                    goto end;
                }
            }


            double[] headingArr = instructionArr[4].Split(':').Select(Double.Parse).ToArray();


            ////if length of distanceArr != 2 ... need to figure out logic for this though the case would be very rare
            //if (distanceArr.Length == 2)
            //{
            //    double buryDepth = 7 * 12;
            //    pole.SetValue("BuryDepthInInches", buryDepth);
            //    PPLEnvironment hftdLoadCase = FindElement(hftdGuid, treeNodeList) as PPLEnvironment;
            //    PPLEnvironment go95LoadCase = FindElement(go95Guid, treeNodeList) as PPLEnvironment;

            //    //in the PPL_Lib library the Substitute method takes two arguments, PPLElement and PPLMain
            //    //but this gives an error in the script.  When using this script for testing you have to comment out
            //    //the pPPLMain.  But when using it on the PGE computer you must uncomment it or it will throw an error
            //    dummyLoadCase.Substitute(hftdLoadCase, pPPLMain);
            //    //pole.AddChild(hftdLoadCase);
            //    pole.AddChild(go95LoadCase);

            //    PPLInsulator spool = FindElement(spoolGuid, treeNodeList) as PPLInsulator;
            //    double deltaAngle = Math.Abs(headingArr[1] - headingArr[0]);
            //    if (deltaAngle > 180)
            //    {
            //        spool.CoordinateA = ((Math.Max(headingArr[0], headingArr[1]) - Math.Min(headingArr[0], headingArr[1])) / 2 + Math.Min(headingArr[0], headingArr[1]) + 180) * pi / 180;
            //    }

            //    else
            //    {
            //        spool.CoordinateA = ((Math.Max(headingArr[0], headingArr[1]) - Math.Min(headingArr[0], headingArr[1])) / 2 + Math.Min(headingArr[0], headingArr[1])) * (pi / 180);
            //    }
            //    spool.CoordinateZ = pole.LengthInInches - 9;
            //    spool.SetValue("Side", "Inline");
            //    pole.AddChild(spool);
            //    spool.SnapToParent();

            //    PPLSpan[] spans = new PPLSpan[distanceArr.Length];
            //    for (int i = 0; i <= distanceArr.Length - 1; i++)
            //    {
            //        spans[i] = FindElement(wireGuid, treeNodeList) as PPLSpan;
            //        spans[i].CoordinateA = (headingArr[i] * pi / 180) - spool.CoordinateA;
            //        spans[i].SetValue("SpanDistanceInInches", distanceArr[i] * 12);
            //        spool.AddChild(spans[i]);
            //        spans[i].SnapToInsulator();
            //    }
            //}
            //else
            //{
            //    PPLMessageBox.Show("Cannot build pole!");
            //}

            double buryDepth = 6.5 * 12;

            pole.SetValue("BuryDepthInInches", buryDepth);
            PPLEnvironment hftdLoadCase = FindElement(hftdGuid, treeNodeList) as PPLEnvironment;
            PPLEnvironment go95LoadCase = FindElement(go95Guid, treeNodeList) as PPLEnvironment;

            //in the PPL_Lib library the Substitute method takes two arguments, PPLElement and PPLMain
            //but this gives an error in the script.  When using this script for testing you have to comment out
            //the pPPLMain.  But when using it on the PGE computer you must uncomment it or it will throw an error
            dummyLoadCase.Substitute(hftdLoadCase /*, cPPLMain*/);
            //pole.AddChild(hftdLoadCase);
            pole.AddChild(go95LoadCase);


            double minHeading;
            double maxHeading;

            if (distanceArr.Length < 4)
            {
                PPLInsulator spool = FindElement(spoolGuid, treeNodeList) as PPLInsulator;

                if (distanceArr.Length == 2)
                {
                    minHeading = Math.Min(headingArr[0], headingArr[1]);
                    maxHeading = Math.Max(headingArr[0], headingArr[1]);
                }

                else if (distanceArr.Length == 3)
                {
                    minHeading = headingArr[0];
                    maxHeading = headingArr[0];
                    for (int i = 0; i < headingArr.Length - 1; i++)
                    {
                        if (minHeading > headingArr[i])
                        {
                            minHeading = headingArr[i];
                        }
                        else if (maxHeading < headingArr[i])
                        {
                            maxHeading = headingArr[i];
                        }
                    }
                }
                else
                {
                    PPLMessageBox.Show("Cannot build pole!");
                    goto end;
                }

                double deltaAngle = maxHeading - minHeading;
                if (deltaAngle > 180)
                {
                    spool.CoordinateA = ((deltaAngle / 2) + minHeading + 180) * pi / 180;
                }
                else
                {
                    spool.CoordinateA = ((deltaAngle / 2) + minHeading) * (pi / 180);
                }

                spool.CoordinateZ = pole.LengthInInches - 9;
                spool.SetValue("Side", "Inline");
                pole.AddChild(spool);
                spool.SnapToParent();

                PPLSpan[] spans = new PPLSpan[distanceArr.Length];
                for (int i = 0; i <= distanceArr.Length - 1; i++)
                {
                    spans[i]             = FindElement(wireGuid, treeNodeList) as PPLSpan;
                    spans[i].CoordinateA = (headingArr[i] * pi / 180) - spool.CoordinateA;
                    spans[i].SetValue("SpanDistanceInInches", distanceArr[i] * 12);
                    spool.AddChild(spans[i]);
                    spans[i].SnapToInsulator();
                }
            }

            else if (distanceArr.Length == 4)
            {
                PPLInsulator[] spools     = new PPLInsulator[2];
                PPLSpan[]      multiSpans = new PPLSpan[headingArr.Length];

                double[] minHead = new double[2];
                minHead[0] = Math.Min(headingArr[0], headingArr[2]);
                minHead[1] = Math.Min(headingArr[1], headingArr[3]);

                double[] maxHead = new double[2];
                maxHead[0] = Math.Max(headingArr[0], headingArr[2]);
                maxHead[1] = Math.Max(headingArr[1], headingArr[3]);

                double[] deltaAng = new double[2];
                //deltaAng[0] = Math.Abs(headingArr[0] - headingArr[2]);
                //deltaAng[1] = Math.Abs(headingArr[1] - headingArr[3]);

                int spansIndex = 0;
                int lessThan   = 2;

                for (int i = 0; i <= spools.Length - 1; i++)
                {
                    deltaAng[i] = maxHead[i] - minHead[i];
                    spools[i]   = FindElement(spoolGuid, treeNodeList) as PPLInsulator;
                    if (deltaAng[i] > 180)
                    {
                        spools[i].CoordinateA = ((deltaAng[i] / 2) + minHead[i] + 180) * pi / 180;
                    }
                    else
                    {
                        spools[i].CoordinateA = ((deltaAng[i] / 2) + minHead[i]) * (pi / 180);
                    }


                    spools[i].CoordinateZ = pole.LengthInInches - 9;
                    spools[i].SetValue("Side", "Inline");
                    pole.AddChild(spools[i]);
                    spools[i].SnapToParent();
                    for (int j = spansIndex; j <= lessThan; j += 2)
                    {
                        multiSpans[j]             = FindElement(wireGuid, treeNodeList) as PPLSpan;
                        multiSpans[j].CoordinateA = (headingArr[j] * pi / 180) - spools[i].CoordinateA;
                        multiSpans[j].SetValue("SpanDistanceInInches", distanceArr[j] * 12);
                        spools[i].AddChild(multiSpans[j]);
                        multiSpans[j].SnapToInsulator();
                    }
                    spansIndex++;
                    lessThan++;
                }
            }
            OcalcReload(cPPLMain);
end:
            { }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void SaveFiles()
        {
            Form currForm = Form.ActiveForm;

            Form1 frm = new Form1();

            frm.Text = "Saving Files";
            frm.Show();
            foreach (Control ct in frm.Controls)
            {
                if (ct is ProgressBar)
                {
                    ProgressBar bar = ct as ProgressBar;
                    bar.Increment(50);
                }
            }
            //pbar.Width = 700;
            //int width = pbar.Width;
            //frm.Controls.Add(pbar);
            //frm.Show();


            PPLPole               pole         = cPPLMain.GetPole();
            List <TreeNode>       treeNodeList = cPPLMain.cCatalogManager.GetNodes(PPLCatalogForm.CATALOG_TYPE.MASTER);
            List <PPLEnvironment> envList      = pole.GetEnvironments();
            string sapPM        = pole.GetValueString("Aux Data 1");
            string ocalcPoleLoc = pole.GetValueString("Aux Data 3");


            //get existing HFTD load case and replace it from the treelist to fix NESC issue
            foreach (PPLEnvironment env in envList)
            {
                string loadCaseName = env.GetValueAndConvertToString("Name");
                string loadCaseCode = env.GetValueAndConvertToString("Method");
                if (loadCaseName.Contains("HFTD") && loadCaseCode == "NESC")
                {
                    PPLEnvironment replaceLoadCase = FindCase(loadCaseName, treeNodeList) as PPLEnvironment;
                    //in the PPL_Lib library the Substitute method takes two arguments, PPLElement and PPLMain
                    //but this gives an error in the script.  When using this script for testing you have to comment out
                    //the pPPLMain.  But when using it on the PGE computer you must uncomment it or it will throw an error
                    env.Substitute(replaceLoadCase /*, cPPLMain*/);
                    //pPPLMain.SelectLoadCase(env);
                    OcalcReload(cPPLMain);
                }
            }
            //define folder names
            string desktopPath                = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string desktopPMFolder            = desktopPath + @"\" + sapPM + " Ocalc Files";
            string pplxFolder                 = desktopPMFolder + "\\PPLX";
            string pplxFileName               = pplxFolder + "\\" + ocalcPoleLoc + ".pplx";
            string customReportFolder         = desktopPMFolder + "\\Custom Reports";
            string customReportFileName       = customReportFolder + "\\" + ocalcPoleLoc + ".xlsx";
            string go95Folder                 = desktopPMFolder + "\\GO95 Analysis";
            string go95FileName               = go95Folder + "\\" + ocalcPoleLoc + ".pdf";
            string osmosePath                 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //location of custom report excel file
            string osmoseCustomReportFileName = osmosePath + "\\OsmosePPL\\Reports\\BCF Report v1.4.xlsx";

            //create folders from folder names
            System.IO.Directory.CreateDirectory(desktopPMFolder);
            System.IO.Directory.CreateDirectory(pplxFolder);
            System.IO.Directory.CreateDirectory(customReportFolder);
            System.IO.Directory.CreateDirectory(go95Folder);

            SavePPLX(cPPLMain, pplxFileName);

            SaveGO95(cPPLMain, pole, go95FileName);

            SaveCustomReport(cPPLMain, osmoseCustomReportFileName, customReportFileName);



            PPLMessageBox.Show("Files Saved!");
        }