/// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string iPath   = "";
            bool   iButton = false;
            GH_Structure <GH_Brep> iGeometry;        //I don't need to write "= new GH_Structure<GH_Brep>();" since it is associated with an out keyword in DA.GetDataTree(...)
            var iConstantFolder = new List <TextFile>();
            var iSystemFolder   = new List <TextFile>();
            var iZeroFolder     = new List <TextFile>();

            DA.GetData(0, ref iPath);
            DA.GetData(1, ref iButton);
            DA.GetDataTree(2, out iGeometry);
            DA.GetDataList(3, iConstantFolder);
            DA.GetDataList(4, iSystemFolder);
            DA.GetDataList(5, iZeroFolder);


            DataTree <Brep> convertedGeomTree = new DataTree <Brep>();

            int i = 0;

            foreach (GH_Path path in iGeometry.Paths)
            {
                foreach (var geom in iGeometry.get_Branch(path))
                {
                    Brep convertedBrep = null;
                    GH_Convert.ToBrep(geom, ref convertedBrep, 0);
                    convertedGeomTree.Add(convertedBrep, new GH_Path(i));
                }
                i += 1;
            }

            string fileLocation   = "";
            string folderLocation = "";

            if (iPath != "")
            {
                folderLocation = iPath;
            }
            else
            {
                fileLocation = this.OnPingDocument().FilePath;
                if (fileLocation == null)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Either save the GH definition or manually assign a path to write to.");
                }
                else
                {
                    folderLocation = new DirectoryInfo(fileLocation).Parent.FullName + @"\";
                }
            }

            if (iButton)
            {
                // Specify names for folders. add some more stuff

                string openFoamFolder = System.IO.Path.Combine(folderLocation, convertedGeomTree.Branch(0)[0].GetUserString("RotAngle") + "deg");

                string constantPath = System.IO.Path.Combine(openFoamFolder, "constant");
                string systemPath   = System.IO.Path.Combine(openFoamFolder, "system");
                string zeroPath     = System.IO.Path.Combine(openFoamFolder, "0");

                string polyMeshPath   = System.IO.Path.Combine(constantPath, "polyMesh\\");
                string triSurfacePath = System.IO.Path.Combine(constantPath, "triSurface\\");

                // Generate directories
                Directory.CreateDirectory(zeroPath);
                Directory.CreateDirectory(polyMeshPath);
                Directory.CreateDirectory(triSurfacePath);
                Directory.CreateDirectory(systemPath);

                // Write text files
                foreach (var constantFile in iConstantFolder)
                {
                    File.WriteAllText(System.IO.Path.Combine(polyMeshPath, constantFile.GetName()), constantFile.GetFileText());
                }

                foreach (var systemFile in iSystemFolder)
                {
                    File.WriteAllText(System.IO.Path.Combine(systemPath, systemFile.GetName()), systemFile.GetFileText());
                }

                foreach (var bcFile in iZeroFolder)
                {
                    File.WriteAllText(System.IO.Path.Combine(zeroPath, bcFile.GetName()), bcFile.GetFileText());
                }

                // Export .stl's
                foreach (GH_Path path in convertedGeomTree.Paths)
                {
                    ExportStl(convertedGeomTree.Branch(path), triSurfacePath + convertedGeomTree.Branch(path)[0].GetUserString("Name"));
                }



                // Write static text files.
                List <Brep> tempGeomList = new List <Brep>();
                foreach (GH_Path path in convertedGeomTree.Paths)
                {
                    tempGeomList.Add(convertedGeomTree.Branch(path)[0]);
                }

                File.WriteAllText(System.IO.Path.Combine(openFoamFolder, "foam.job"), StaticTextFiles.GetFoam());
                File.WriteAllText(System.IO.Path.Combine(openFoamFolder, "mesh.job"), StaticTextFiles.GetMesh());

                //File.WriteAllText(System.IO.Path.Combine(constantPath, "LESProperties"), StaticTextFiles.GetLESProperties());
                File.WriteAllText(System.IO.Path.Combine(constantPath, "RASProperties"), StaticTextFiles.GetRASProperties());
                File.WriteAllText(System.IO.Path.Combine(constantPath, "transportProperties"), StaticTextFiles.GetTransportProperties());
                File.WriteAllText(System.IO.Path.Combine(constantPath, "turbulenceProperties"), StaticTextFiles.GetTurbulenceProperties());

                File.WriteAllText(System.IO.Path.Combine(systemPath, "decomposeParDict"), StaticTextFiles.GetDecomposeParDict());
                File.WriteAllText(System.IO.Path.Combine(systemPath, "fvSolution"), StaticTextFiles.GetFVSolution());
                File.WriteAllText(System.IO.Path.Combine(systemPath, "fvSchemes"), StaticTextFiles.GetFVSchemes());
                File.WriteAllText(System.IO.Path.Combine(systemPath, "surfaceFeatureExtractDict"), StaticTextFiles.GetSurfaceFeatureExtractDict(tempGeomList));
                File.WriteAllText(System.IO.Path.Combine(systemPath, "forces"), StaticTextFiles.GetForcesFunction(tempGeomList));

                File.WriteAllText(System.IO.Path.Combine(zeroPath, "p"), StaticTextFiles.GetP(tempGeomList));
                File.WriteAllText(System.IO.Path.Combine(zeroPath, "nut"), StaticTextFiles.GetNut(tempGeomList));
                File.WriteAllText(System.IO.Path.Combine(zeroPath, "nuTilda"), StaticTextFiles.GetNuTilda(tempGeomList));
            }
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string iPath   = "";
            bool   iButton = false;
            GH_Structure <GH_Brep> iGeometry;
            //GH_Structure<IGH_GeometricGoo> iGeometry = new GH_Structure<IGH_GeometricGoo>();
            var iConstantFolder = new List <TextFile>();
            var iSystemFolder   = new List <TextFile>();
            var iZeroFolder     = new List <TextFile>();
            var iWIND           = new TextFile();

            DA.GetData(0, ref iPath);
            DA.GetData(1, ref iButton);
            DA.GetDataTree(2, out iGeometry);
            DA.GetDataList(3, iConstantFolder);
            DA.GetDataList(4, iSystemFolder);
            DA.GetDataList(5, iZeroFolder);
            DA.GetData(6, ref iWIND);

            /*DataTree<Brep> convertedGeomTree = new DataTree<Brep>();
             * List<GH_Path> pathList = new List<GH_Path>();
             *
             * foreach (var path in iGeometry.Paths)
             *  for (int i = 0; i < iGeometry.get_Branch(path).Count; i++)
             *      pathList.Add(path);
             *
             * var flattenedGeom = iGeometry.FlattenData();
             * for (int i = 0; i < flattenedGeom.Count; i++)
             * {
             *  flattenedGeom[i].CastTo(out Brep tempBrep);
             *  convertedGeomTree.Add(tempBrep, pathList[i]);
             * }*/

            DataTree <Brep> convertedGeomTree = new DataTree <Brep>();

            int  x             = 0;
            Brep convertedBrep = null;

            foreach (GH_Path path in iGeometry.Paths)
            {
                foreach (var geom in iGeometry.get_Branch(path))
                {
                    GH_Convert.ToBrep(geom, ref convertedBrep, 0);
                    convertedGeomTree.Add(convertedBrep, new GH_Path(x));
                    convertedBrep = null;
                }
                x += 1;
            }


            string fileLocation   = "";
            string folderLocation = "";

            if (iPath != "")
            {
                folderLocation = iPath;
            }
            else
            {
                fileLocation = this.OnPingDocument().FilePath;
                if (fileLocation == null)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Either save the GH definition or manually assign a path to write to.");
                }
                else
                {
                    folderLocation = new DirectoryInfo(fileLocation).Parent.FullName + @"\";
                }
            }

            if (iButton)
            {
                // Specify names for folders. add some more stuff

                string openFoamFolder = System.IO.Path.Combine(folderLocation, convertedGeomTree.Branch(0)[0].GetUserString("RotAngle") + "deg");
                string windFolder     = System.IO.Path.Combine(folderLocation, "WIND");

                string constantPath = System.IO.Path.Combine(openFoamFolder, "constant");
                string systemPath   = System.IO.Path.Combine(openFoamFolder, "system");
                string zeroPath     = System.IO.Path.Combine(openFoamFolder, "0");

                string polyMeshPath     = System.IO.Path.Combine(constantPath, "polyMesh\\");
                string triSurfacePath   = System.IO.Path.Combine(constantPath, "triSurface\\");
                string boundaryDataPath = System.IO.Path.Combine(constantPath, "boundaryData\\");

                // Generate directories
                Directory.CreateDirectory(zeroPath);
                Directory.CreateDirectory(polyMeshPath);
                Directory.CreateDirectory(triSurfacePath);
                Directory.CreateDirectory(boundaryDataPath);
                Directory.CreateDirectory(systemPath);
                Directory.CreateDirectory(windFolder);


                // Write text files
                foreach (var constantFile in iConstantFolder)
                {
                    File.WriteAllText(System.IO.Path.Combine(polyMeshPath, constantFile.GetName()), constantFile.GetFileText());
                }

                foreach (var systemFile in iSystemFolder)
                {
                    File.WriteAllText(System.IO.Path.Combine(systemPath, systemFile.GetName()), systemFile.GetFileText());
                }

                foreach (var bcFile in iZeroFolder)
                {
                    File.WriteAllText(System.IO.Path.Combine(zeroPath, bcFile.GetName()), bcFile.GetFileText());
                }

                File.WriteAllText(System.IO.Path.Combine(windFolder, iWIND.GetName()), iWIND.GetFileText());


                // Export .stl's
                foreach (GH_Path path in convertedGeomTree.Paths)
                {
                    ExportStl(convertedGeomTree.Branch(path), triSurfacePath + convertedGeomTree.Branch(path)[0].GetUserString("Name"));
                }



                // Write static text files.
                List <Brep> tempGeomList = new List <Brep>();
                foreach (GH_Path path in convertedGeomTree.Paths)
                {
                    tempGeomList.Add(convertedGeomTree.Branch(path)[0]);
                }

                File.WriteAllText(System.IO.Path.Combine(openFoamFolder, "foam.job"), StaticTextFiles.GetFoam());
                File.WriteAllText(System.IO.Path.Combine(openFoamFolder, "mesh.job"), StaticTextFiles.GetMesh());

                //File.WriteAllText(System.IO.Path.Combine(constantPath, "LESProperties"), StaticTextFiles.GetLESProperties());
                File.WriteAllText(System.IO.Path.Combine(constantPath, "RASProperties"), StaticTextFiles.GetRASProperties());
                File.WriteAllText(System.IO.Path.Combine(constantPath, "transportProperties"), StaticTextFiles.GetTransportProperties());
                File.WriteAllText(System.IO.Path.Combine(constantPath, "turbulenceProperties"), StaticTextFiles.GetTurbulenceProperties());

                File.WriteAllText(System.IO.Path.Combine(systemPath, "decomposeParDict"), StaticTextFiles.GetDecomposeParDict());
                File.WriteAllText(System.IO.Path.Combine(systemPath, "fvSolution"), StaticTextFiles.GetFVSolution());
                File.WriteAllText(System.IO.Path.Combine(systemPath, "fvSchemes"), StaticTextFiles.GetFVSchemes());
                File.WriteAllText(System.IO.Path.Combine(systemPath, "surfaceFeatureExtractDict"), StaticTextFiles.GetSurfaceFeatureExtractDict(tempGeomList));
                File.WriteAllText(System.IO.Path.Combine(systemPath, "forces"), StaticTextFiles.GetForcesFunction(convertedGeomTree));

                File.WriteAllText(System.IO.Path.Combine(zeroPath, "p"), StaticTextFiles.GetP(tempGeomList));
                File.WriteAllText(System.IO.Path.Combine(zeroPath, "nut"), StaticTextFiles.GetNut(tempGeomList));
                File.WriteAllText(System.IO.Path.Combine(zeroPath, "nuTilda"), StaticTextFiles.GetNuTilda(tempGeomList));
            }
        }