public static EPObj.Surface EPlusSurfacetoObject(List<string> detailedSurfaceString) { //create your log file writer, that will be used in stream writer at the bottom of this page StringBuilder output = new StringBuilder(); //need to add a try/catch clause and start to work on try/catches when I get the chance //initialize the surface to be returned EPObj.Surface currentSurface = new EPObj.Surface(); currentSurface.SurfaceCoords = new List<EPObj.CartCoord>(); currentSurface.surfaceType = EPObj.SurfaceTypes.Blank; currentSurface.outsideBoundary = EPObj.OutsideBoundary.Blank; StringBuilder logline = new StringBuilder(); //Constructions - Opaque Detailed... #region //start with the Regexes needed to parse out the opaque constructions //Regex for beginning of a detailed surface element, string startSurface = "BuildingSurface:Detailed,"; Regex surfaceYes = new Regex(startSurface); //Regex for surfaceName //string surfaceName = @"(?'ws1'\s*)(?'name'.*)(?'ws2'\s*)(?'surfaceName'!- Name)"; string surfaceName = @"(?'1'.*)(?'surfaceName'!- Name)"; //string surfaceName = @"(?'surfaceName'!- Name)"; Regex surfaceNameRegex = new Regex(surfaceName); //Regex for surface Type string surfaceType = @"(?'1'.*)(?'surfaceType'!- Surface Type)"; Regex surfaceTypeRegex = new Regex(surfaceType); //Regext for surfaceConstructionName string surfaceConstructionName = @"(?'1'.*)(?'construction'!- Construction Name)"; Regex surfaceConstructionNameRegex = new Regex(surfaceConstructionName); //Regext for surfaceZoneName string surfaceZoneName = @"(?'1'.*)(?'zoneName'!- Zone Name)"; Regex surfaceZoneNameRegex = new Regex(surfaceZoneName); //Regex for outsideBoundary string outsideBoundaryName = @"(?'1'.*)(?'outsideBoundaryName'!- Outside Boundary Condition)"; Regex outsideBoundaryRegex = new Regex(outsideBoundaryName); //Regex for outsideBoundary Condition string outsideBoundaryCondition = @"(?'1'.*)(?'outsideBoundaryCondition'!- Outside Boundary Condition Object)"; Regex outsideBoundaryConditionRegex = new Regex(outsideBoundaryCondition); //Regex for sunExposure string sunExposure = @"(?'1'.*)(?'sunExposure'!- Sun Exposure)"; Regex sunExposureRegex = new Regex(sunExposure); //Regex for windExposure string windExposure = @"(?'1'.*)(?'windExposure'!- Wind Exposure)"; Regex windExposureRegex = new Regex(windExposure); //Regex for ViewFactor string viewFactor = @"(?'1'.*)(?'viewFactor'!- View Factor to Ground)"; Regex viewFactorRegex = new Regex(viewFactor); //Regex for numberofVertices string numberofVertices = @"(?'1'.*)(?'vertices'!- Number of Vertices)"; Regex numberofVerticesRegex = new Regex(numberofVertices); //something else for the vertex //Regex for generic Vertex string typicalVertex = @"(?'1'.*)(?'Xvertex'!- X[0-9]*),\s*(?'Yvertex'Y[0-9]*),\s*(?'ZVertex'Z[0-9]*)\s*"; Regex typicalVertexRegex = new Regex(typicalVertex); //a regex for finding a semicolon string semicolon = @"(\S*)(;)(.*)"; Regex smicln = new Regex(semicolon); #endregion //make a list of spaces //special needed to allow the routine to run successfully //needed because the regex may return true in the wrong instance bool outsideBoundaryMatched = false; bool semicolonfound = false; bool detailedsurface = false; bool vertexMatching = false; foreach (string line in detailedSurfaceString) { #region MatchCollection surfaceStart = surfaceYes.Matches(line); if (surfaceStart.Count > 0) { detailedsurface = true; continue; } //now that a surface element is established in the IDF, we can work through it to create surface objects if (detailedsurface == true) { //Surface Name //get the name in the file if (!vertexMatching) { Match surfaceNameMatch = surfaceNameRegex.Match(line); int matchstart, matchlength = -1; if (surfaceNameMatch.Success) { matchstart = surfaceNameMatch.Index; matchlength = surfaceNameMatch.Length; //strip off the whitespace and comma string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(surfaceNameMatch.Groups["1"].Value); if (pure.Success) { currentSurface.name = pure.Groups["goods"].Value; continue; } } //Get Surface Type Match surfaceTypeMatch = surfaceTypeRegex.Match(line); if (surfaceTypeMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(surfaceTypeMatch.Groups["1"].Value); if (pure.Success) { string type = pure.Groups["goods"].Value; type = type.ToLower(); if (type == EPObj.EPSurface.SurfaceTypes.Ceiling.ToString().ToLower()) { currentSurface.surfaceType = EPObj.SurfaceTypes.Ceiling; continue; } else if (type == EPObj.EPSurface.SurfaceTypes.Floor.ToString().ToLower()) { currentSurface.surfaceType = EPObj.SurfaceTypes.Floor; continue; } else if (type == EPObj.EPSurface.SurfaceTypes.Roof.ToString().ToLower()) { currentSurface.surfaceType = EPObj.SurfaceTypes.Roof; continue; } else if (type == EPObj.EPSurface.SurfaceTypes.Wall.ToString().ToLower()) { currentSurface.surfaceType = EPObj.SurfaceTypes.Wall; continue; } } } //Get Construction Type Match constructionTypeMatch = surfaceConstructionNameRegex.Match(line); if (constructionTypeMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(constructionTypeMatch.Groups["1"].Value); if (pure.Success) { currentSurface.constructionName = pure.Groups["goods"].Value; continue; } } //GetZone Name Match zoneNameMatch = surfaceZoneNameRegex.Match(line); if (zoneNameMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(zoneNameMatch.Groups["1"].Value); if (pure.Success) { currentSurface.zoneName = pure.Groups["goods"].Value; continue; } } //GetOutside Boundary Name if (!outsideBoundaryMatched) { Match outsideBoundaryMatch = outsideBoundaryRegex.Match(line); if (outsideBoundaryMatch.Success) #region { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(outsideBoundaryMatch.Groups["1"].Value); if (pure.Success) { string type = pure.Groups["goods"].Value; type = type.ToLower(); if (type == EPObj.EPSurface.OutsideBoundary.Ground.ToString().ToLower()) { currentSurface.outsideBoundary = EPObj.OutsideBoundary.Ground; outsideBoundaryMatched = true; } else if (type == EPObj.EPSurface.OutsideBoundary.OtherSideCoefficients.ToString().ToLower()) { currentSurface.outsideBoundary = EPObj.OutsideBoundary.OtherSideCoefficients; outsideBoundaryMatched = true; } else if (type == EPObj.EPSurface.OutsideBoundary.OtherSideConditionsModel.ToString().ToLower()) { currentSurface.outsideBoundary = EPObj.OutsideBoundary.OtherSideConditionsModel; outsideBoundaryMatched = true; } else if (type == EPObj.EPSurface.OutsideBoundary.Outdoors.ToString().ToLower()) { currentSurface.outsideBoundary = EPObj.OutsideBoundary.Outdoors; outsideBoundaryMatched = true; } else if (type == EPObj.EPSurface.OutsideBoundary.Surface.ToString().ToLower()) { currentSurface.outsideBoundary = EPObj.OutsideBoundary.Surface; outsideBoundaryMatched = true; } else { currentSurface.outsideBoundary = EPObj.OutsideBoundary.Zone; outsideBoundaryMatched = true; } continue; } } } #endregion //Get Outside Boundary Condition Object Match outsideBoundaryConditionMatch = outsideBoundaryConditionRegex.Match(line); if (outsideBoundaryConditionMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(outsideBoundaryConditionMatch.Groups["1"].Value); if (pure.Success) { currentSurface.outsideBoundaryCondition = pure.Groups["goods"].Value; continue; } } //Get Sun Exposure Match sunExposureMatch = sunExposureRegex.Match(line); if (sunExposureMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(sunExposureMatch.Groups["1"].Value); if (pure.Success) { currentSurface.sunExposureVar = pure.Groups["goods"].Value; continue; } } //Get Wind Exposure Match windExposureMatch = windExposureRegex.Match(line); if (windExposureMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(windExposureMatch.Groups["1"].Value); if (pure.Success) { currentSurface.windExposureVar = pure.Groups["goods"].Value; continue; } } //View Factor Match viewFactorMatch = viewFactorRegex.Match(line); if (viewFactorMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(viewFactorMatch.Groups["1"].Value); if (pure.Success) { if (pure.Groups["goods"].Value == "AutoCalculate") { currentSurface.viewFactor = -999; } else { currentSurface.viewFactor = Convert.ToDouble(pure.Groups["goods"].Value); } continue; } } //Number of Vertices Match numVerticesMatch = numberofVerticesRegex.Match(line); if (numVerticesMatch.Success) { string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma',)"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(numVerticesMatch.Groups["1"].Value); if (pure.Success) { currentSurface.numVertices = Convert.ToInt32(pure.Groups["goods"].Value); continue; } } } //Get Vertices //loop through them until the end Match vertexMatch = typicalVertexRegex.Match(line); if (vertexMatch.Success) { vertexMatching = true; string purify = @"(?'ws'\s*)(?'goods'.*)(?'comma')"; Regex purifyRegex = new Regex(purify); Match pure = purifyRegex.Match(vertexMatch.Groups["1"].Value); if (pure.Success) { //extract the X,Y, and Z coordinate from the purified string //string coordinateString = @"(?'X'[-]\d+[\.]\d+|\d+),(?'Y'[-]\d+[\.]\d+|\d+),(?'Z'[-]\d+[\.]\d+|\d+)"; string coordinateString = @"(?'X'[-+]?([0-9]*\.[0-9]+|[0-9]+)),\s*(?'Y'[-+]?([0-9]*\.[0-9]+|[0-9]+)),\s*(?'Z'[-+]?([0-9]*\.[0-9]+|[0-9]+)\s*)"; Regex coordRegex = new Regex(coordinateString); Match XYZMatch = coordRegex.Match(pure.Groups["goods"].Value); if (XYZMatch.Success) { EPObj.CartCoord surfaceCoord = new EPObj.CartCoord(); surfaceCoord.X = Convert.ToDouble(XYZMatch.Groups["X"].Value); surfaceCoord.Y = Convert.ToDouble(XYZMatch.Groups["Y"].Value); surfaceCoord.Z = Convert.ToDouble(XYZMatch.Groups["Z"].Value); currentSurface.SurfaceCoords.Add(surfaceCoord); } } //see if there is a semi-colon Match smicolonMatch = smicln.Match(line); if (smicolonMatch.Success) { semicolonfound = true; vertexMatching = false; } } } } #endregion //close the reader //get the RHR Normal Vector EPObj.CartVect RHRNormalVector = EPObj.GetRHR(currentSurface.SurfaceCoords); logline.AppendLine(currentSurface.name + ", " + currentSurface.surfaceType + ", " + currentSurface.outsideBoundary.ToString()); Console.WriteLine(currentSurface.name + ", " + currentSurface.surfaceType + ", " + currentSurface.outsideBoundary.ToString()); //get azimuth currentSurface.azimuth = FindAzimuth(RHRNormalVector); //get tilt EPObj.MemorySafe_CartVect memRHR = EPObj.convertToMemorySafeVector(RHRNormalVector); currentSurface.tilt = FindTilt(memRHR); //in any case, return the current surface logline.AppendLine(RHRNormalVector.X.ToString() + ", " + RHRNormalVector.Y.ToString() + ", " + RHRNormalVector.Z.ToString() + ", " + currentSurface.azimuth.ToString() + ", " + currentSurface.tilt.ToString()); Console.WriteLine(RHRNormalVector.X.ToString() + ", " + RHRNormalVector.Y.ToString() + ", " + RHRNormalVector.Z.ToString() + ", " + currentSurface.azimuth.ToString() + ", " + currentSurface.tilt.ToString()); return currentSurface; }
public static List<EPObj.MemorySafe_Spaces> EPlusSpacestoObjectList(string idfname) { //find multipliers if they exist by looking for Zone Groups //I set this up to read the idf file twice, once in each try block List<EPObj.MemorySafe_Spaces> memSafeProjectSpaces = new List<EPObj.MemorySafe_Spaces>(); List<ZoneGroup> zoneGroups = new List<ZoneGroup>(); List<ZoneList> zoneLists = new List<ZoneList>(); try { //may also want to have //the zone list and affiliated spaces should come first Regex zoneListYes = new Regex(EPlusRegexString.startZoneList); Regex zoneGroupYes = new Regex(EPlusRegexString.startZoneGroup); Regex zoneGroupNameRegex = new Regex(EPlusRegexString.Name); Regex zoneListNameRegex = new Regex(EPlusRegexString.zoneListName); Regex zoneListNameMultiplier = new Regex(EPlusRegexString.zoneListMultiplier); Regex semicolon = new Regex(EPlusRegexString.semicolon); Encoding encoding; using (StreamReader reader = new StreamReader(idfname)) { string line; encoding = reader.CurrentEncoding; bool zoneListBool = false; bool zoneGroupBool = false; List<string> zoneListStrings = new List<string>(); List<string> zoneGroupStrings = new List<string>(); while ((line = reader.ReadLine()) != null) { Match zoneListFound = zoneListYes.Match(line); if (zoneListFound.Success) { zoneListBool = true; zoneListStrings.Add(line); continue; } if (zoneListBool) { //parse the line to get the name of the zone Match semiColonMatch = semicolon.Match(line); if (!semiColonMatch.Success) { zoneListStrings.Add(line); } else { zoneListStrings.Add(line); zoneListBool = false; ZoneList zoneList = MakeZoneList(zoneListStrings); zoneLists.Add(zoneList); zoneListStrings.Clear(); } } Match zoneGroupFound = zoneGroupYes.Match(line); if (zoneGroupFound.Success) { zoneGroupBool = true; zoneGroupStrings.Add(line); continue; } if (zoneGroupBool) { Match semiColonMatch = semicolon.Match(line); if (!semiColonMatch.Success) { zoneGroupStrings.Add(line); } else { zoneGroupStrings.Add(line); zoneGroupBool = false; ZoneGroup zoneGroup = MakeZoneGroup(zoneGroupStrings); zoneGroups.Add(zoneGroup); zoneGroupStrings.Clear(); } } //make a zone List object //find the zone group that is relate to this list } reader.Close(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } //set up the list of spaces that will be collected List<EPObj.Spaces> projectSpaces = new List<EPObj.Spaces>(); //set up the log string log = @"C:\Users\Chiensi\Documents\AAATerabuild\EnergyPlus\homework\log.txt"; StringBuilder logline = new StringBuilder(); try { Encoding encoding; StringBuilder output = new StringBuilder(); List<string> stuff = new List<string>(); //needed regular expressions to build the surface description prior to it being made an object string startSurface = "BuildingSurface:Detailed,"; Regex surfaceYes = new Regex(startSurface); string semicolon = @"(\S*)(;)(.*)"; Regex smicln = new Regex(semicolon); using (StreamReader reader = new StreamReader(idfname)) { string line; encoding = reader.CurrentEncoding; bool detailedsurface = false; //set up the surface EPObj.Surface currentSurface = new EPObj.Surface(); currentSurface.SurfaceCoords = new List<EPObj.CartCoord>(); currentSurface.surfaceType = EPObj.SurfaceTypes.Blank; currentSurface.outsideBoundary = EPObj.OutsideBoundary.Blank; int surfcount = 0; while ((line = reader.ReadLine()) != null) { #region MatchCollection surfaceStart = surfaceYes.Matches(line); if (surfaceStart.Count > 0) { detailedsurface = true; currentSurface.Clear(); stuff.Add(line); output.AppendLine(line); continue; } //now that a surface element is established in the IDF, we can work through it to create surface objects if (detailedsurface == true) { //GetAllDetailedSurfaces //use streamswriter to make a little text file that will then be turned into an object //write to the small output stream until you encounter a semi-colon stuff.Add(line); output.AppendLine(line); Match smicolMatch = smicln.Match(line); if (smicolMatch.Success) { detailedsurface = false; string pass = output.ToString(); //write the output file //send the output file to a function, returning a surface EPObj.Surface surfaceReturned = EPlusSurfacetoObject(stuff); //add a multiplier to the surface if needed foreach (ZoneGroup zoneGroup in zoneGroups) { string zoneListName = zoneGroup.zoneListName; foreach (ZoneList zoneList in zoneLists) { if (zoneList.name == zoneListName) { foreach (string zoneName in zoneList.zoneListNames) { if (surfaceReturned.zoneName == zoneName) { //add a multiplier to the surface surfaceReturned.multiplier = zoneGroup.zoneListMultiplier; } } } } } surfcount++; Console.WriteLine(surfcount.ToString()); //ModelingUtilities.BuildingObjects.Surface surfaceReturned = EPlusFunctions.EPlusSurfacetoObject("C:\\Temp\\detailedSurface.txt"); output.Clear(); stuff.Clear(); if (projectSpaces.Count == 0) { string tagline = "First project zone detected in Surfaces."; logline.AppendLine(line); string zoneName = surfaceReturned.zoneName; string surfaceName = surfaceReturned.name; logline.AppendLine(zoneName + ": " + surfaceName); Console.WriteLine(tagline); Console.WriteLine(zoneName + ": " + surfaceName); EPObj.Spaces spaceInstance = new EPObj.Spaces(); spaceInstance.spaceSurfaces = new List<EPObj.Surface>(); spaceInstance.name = surfaceReturned.zoneName; spaceInstance.spaceSurfaces.Add(surfaceReturned); projectSpaces.Add(spaceInstance); } else { //search for the space name in the existing List of Spaces bool spacefound = false; for (int i = 0; i < projectSpaces.Count; i++) { string projname = projectSpaces[i].name; if (projname == surfaceReturned.zoneName) { string tagline = "Existing project zone detected in Surfaces."; logline.AppendLine(tagline); Console.WriteLine(tagline); string zoneName = surfaceReturned.zoneName; string surfaceName = surfaceReturned.name; logline.AppendLine(zoneName + ": " + surfaceName); Console.WriteLine(zoneName + ": " + surfaceName); projectSpaces[i].spaceSurfaces.Add(surfaceReturned); spacefound = true; break; } } //if spacefound is never set to true if (!spacefound) { string tagline = "New project zone detected in Surfaces."; logline.AppendLine(tagline); Console.WriteLine(tagline); string zoneName = surfaceReturned.zoneName; string surfaceName = surfaceReturned.name; logline.AppendLine(zoneName + ": " + surfaceName); Console.WriteLine(zoneName + ": " + surfaceName); EPObj.Spaces spaceInstance = new EPObj.Spaces(); spaceInstance.spaceSurfaces = new List<EPObj.Surface>(); spaceInstance.name = surfaceReturned.zoneName; spaceInstance.spaceSurfaces.Add(surfaceReturned); projectSpaces.Add(spaceInstance); } } //semicolon match = true } //detailed surfaces = true } //while line reader is reading } //convert these spaces to memory safe spaces foreach (EPObj.Spaces space in projectSpaces) { string zoneName = space.name; int multiplier = space.multiplier; List<EPObj.MemorySafe_Surface> projectSpaceSurfaces = new List<EPObj.MemorySafe_Surface>(); foreach (EPObj.Surface surface in space.spaceSurfaces) { EPObj.MemorySafe_Surface memsurf = EPObj.convert2MemorySafeSurface(surface); projectSpaceSurfaces.Add(memsurf); } EPObj.MemorySafe_Spaces memSpace = new EPObj.MemorySafe_Spaces(zoneName, multiplier, projectSpaceSurfaces); memSafeProjectSpaces.Add(memSpace); } //streamreader reader.Close(); using (StreamWriter writer = new StreamWriter(log, false, encoding)) { writer.Write(logline.ToString()); } } #endregion } catch (Exception e) { Console.WriteLine(e.ToString()); } return memSafeProjectSpaces; }