private FileOpResult ResultReportGenerator(string path, ResultReportType type, ResultReportFileMode mode) { StringBuilder HTMLString = new StringBuilder(); if (path == null) return FileOpResult.Fail; switch (type) { case ResultReportType.HTML: switch (mode) { case ResultReportFileMode.Create: HTMLString = ParseImpellerToHTMLString("Impeller 0", mode); try { File.WriteAllText(path, HTMLString.ToString()); } catch { return FileOpResult.Fail; } break; case ResultReportFileMode.Append: try { HTMLString = new StringBuilder(File.ReadAllText(path)); } catch { return FileOpResult.Fail; } if (Regex.IsMatch(HTMLString.ToString(), @"reporttype=""CalculationReport""")) { int impellerCount = Regex.Matches(HTMLString.ToString(), @"<div class=""Impeller""").Count; HTMLString.Replace( "</body>", ParseImpellerToHTMLString("Impeller " + impellerCount, mode) .Append("\r\n</body>") .ToString() ); try { File.WriteAllText(path, HTMLString.ToString()); } catch { return FileOpResult.Fail; } } else { return FileOpResult.UnknownFormat; } break; default: return FileOpResult.Fail; } break; default: return FileOpResult.Fail; } return FileOpResult.Succeed; }
private StringBuilder ParseImpellerToHTMLString(string ImpellerName, ResultReportFileMode mode) { //懒得用DOM方式所以写得太丑啦不要看呀>~< StringBuilder HTMLString = new StringBuilder(); try { if (mode == ResultReportFileMode.Create) { HTMLString.Append(@"<!DOCTYPE HTML> <html lang=""zh-CN"" reporttype=""CalculationReport"" reportver=""") .Append(App.ResourceAssembly.GetName().Version.ToString()) .Append(@"""> <head> <meta charset=""UTF-8""> <title>Impeller Vibration Checker Report</title> <style type=""text/css""> body{ background:#111111; } .Impeller{ height:20em; width:90%; left:3%; margin:2em 0 2em 0; background:#4496E7; border: solid 0.7em #4496E7; border-radius: 0.7em; box-shadow:0 0 1em 0.3em #fff; position:relative; } .ContentBlock{ top:0%; width:49.5%; background:#ccd; box-shadow:0 0 1em 0.05em #fff inset; position:absolute; overflow:auto; } .ResultBlock{ top:20%; height:80%; left:0; } .ImpellerArgsBlock{ top:10%; height:90%; right:0; } .ResultHeader{ top:10%; height:7%; position:absolute; } .ImpellerArgsHeader{ top:0%; height:7%; left:50.5%; position:absolute; } .ImpellerNameContainer{ top:0%; left:0%; height:1.4em; width:49.5%; overflow:hidden; position:absolute; } .ImpellerName{ height:100%; width:100%; min-width:1em; display:inline; margin-left:1em; position:absolute; background:#E8EEFF; cursor:default; } .Content{ margin:1em; } h{ text-shadow: 0 0 6px #FFFFFF, 0 0 6px #FFFFFF; cursor:default; } .CloseButton{ right:0.5em; height:1.5em; width:1.5em; border-radius:0.75em; background:#E8EEFF; position:absolute; } .MinButton{ right:3em; top:0.45em; height:0.6em; width:1.5em; border-radius:0.1em; background:#E8EEFF; position:absolute; } .btn:hover{ background:#D3D9EA; } .CloseButton:hover{ background:#FF7C7C; } .btn:active{ background:#D3D9EA; box-shadow:0 1px 2px rgba(0, 0, 0, 0.3) inset; } .CloseButton:active{ background:#DD5D5D; } #Header{ font-family:georgia; font-weight:bold; font-size: 2.5em; text-shadow: 0px 0px 10px #E8EEFF, 1px 1px 5px #00F; color: #EEE ; text-align: center; cursor:default; } .toolbtn{ height:2em; width:12%; top:1em; font-family:georgia; font-weight:bold; font-size: 1em; color: #EEE ; background:#4D90FE; border: 1px solid #3079ED; position:absolute; } #ShrikAllBtn{ left:23%; } #ExpandAllBtn{ left:43%; } #ToggleAllBtn{ left:63%; } .toolbtn:hover{ background:#357AE8; border:1px solid #2F5BB7; } .toolbtn:focus{ background:#4D90FE; border:1px solid #4D90FE; box-shadow:0 0 0 1px rgba(255, 255, 255, 0.5) inset; } .toolbtn:active{ background:#357AE8; border:1px solid #2F5BB7; box-shadow:0 1px 2px rgba(0, 0, 0, 0.3) inset; } </style> <script type=""text/javascript""> function toggleSize(impeller){ var size = impeller.getAttribute(""size""); if(size === ""max""){ impeller.getElementsByClassName(""ResultHeader"")[0].style.display = ""none""; impeller.getElementsByClassName(""ImpellerArgsHeader"")[0].style.display = ""none""; impeller.getElementsByClassName(""ContentBlock"")[0].style.display = ""none""; impeller.getElementsByClassName(""ContentBlock"")[1].style.display = ""none""; impeller.getElementsByClassName(""ImpellerName"")[0].title = ""Click to Expand""; impeller.getElementsByClassName(""ImpellerNameContainer"")[0].style.width = ""80%""; impeller.getElementsByClassName(""MinButton"")[0].title = ""Expand""; impeller.getElementsByClassName(""MinButton"")[0].style.height = ""1.2em""; impeller.getElementsByClassName(""MinButton"")[0].style.top = ""0.15em""; impeller.style.height = ""1.5em""; impeller.setAttribute(""size"", ""min""); } else if(size === ""min""){ impeller.getElementsByClassName(""ResultHeader"")[0].style.display = ""block""; impeller.getElementsByClassName(""ImpellerArgsHeader"")[0].style.display = ""block""; impeller.getElementsByClassName(""ContentBlock"")[0].style.display = ""block""; impeller.getElementsByClassName(""ContentBlock"")[1].style.display = ""block""; impeller.getElementsByClassName(""ImpellerName"")[0].title = ""Click to Shrink""; impeller.getElementsByClassName(""ImpellerNameContainer"")[0].style.width = ""49.5%""; impeller.getElementsByClassName(""MinButton"")[0].title = ""Shrink""; impeller.getElementsByClassName(""MinButton"")[0].style.height = ""0.6em""; impeller.getElementsByClassName(""MinButton"")[0].style.top = ""0.45em""; impeller.style.height = ""20em""; impeller.setAttribute(""size"", ""max""); }else{ return; } } function batchToggleSize(mode){ var impellers = document.getElementsByClassName(""Impeller""); switch(mode) { case ""shrink"": for(var index = 0; index < impellers.length; index++){ impellers[index].setAttribute(""size"", ""max""); toggleSize(impellers[index]); } break; case ""expand"": for(var index = 0; index < impellers.length; index++){ impellers[index].setAttribute(""size"", ""min""); toggleSize(impellers[index]); } break; case ""toggle"": for(var index = 0; index < impellers.length; index++){ toggleSize(impellers[index]); } break; default: break; } } </script> </head> <body> <div id=""Header""> Impeller Vibration Checker Report </div> <div id=""Toolbar"" style=""position:relative;height:3em;""> <button id=""ShrikAllBtn"" class=""toolbtn"" onclick=""batchToggleSize('shrink')"">Shrink All</button> <button id=""ExpandAllBtn"" class=""toolbtn"" onclick=""batchToggleSize('expand')"">Expand All</button> <button id=""ToggleAllBtn"" class=""toolbtn"" onclick=""batchToggleSize('toggle')"">Toggle All</button> </div>" ); } //叶片部分 HTMLString.Append(@" <div class=""Impeller"" size=""max""> <div class=""ImpellerNameContainer""> <h>Impeller Name :</h> <div class=""ImpellerName btn"" title=""Click to shrink"" onclick=""toggleSize(this.parentElement.parentElement)""> "); //加入叶片名 HTMLString.Append(ImpellerName + "\r\n"); HTMLString.Append(@" </div> </div> <div class=""ResultHeader""> <h>Calculation Result :</h> </div> <div class=""ResultBlock ContentBlock""> <div class=""Result Content""> <img src=""data:image/png;base64,"); //加入base64结果图 HTMLString.Append( ExportCanvasToImageOrBase64String( resultCanvas, canvasSize: new Size(resultCanvas.ActualWidth * 1.1, resultCanvas.ActualHeight * 1.2) ) ); HTMLString.Append(@"""></img><br/> "); //加入结果内容 HTMLString.Append( EncodeHTMLComponent(ParseResultToString()).Replace(@"<br/>", "\r\n\t\t\t\t<br/>") ); HTMLString.Append(@" </div> </div> <div class=""ImpellerArgsHeader""> <h>Impeller Arguments :</h> </div> <div class=""ImpellerArgsBlock ContentBlock""> <div class=""ImpellerArgs Content""> "); //加入叶片参数 HTMLString.Append( EncodeHTMLComponent(ParseImpellerArgsToString()).Replace(@"<br/>", "\r\n\t\t\t\t<br/>") ); HTMLString.Append("\r\n\t\t\t\t<br/>\r\n\t\t\t\t<br/>"); HTMLString.Append( EncodeHTMLComponent(ParseSectionsToString()).Replace(@"<br/>", "\r\n\t\t\t\t<br/>") ); HTMLString.Append(@" </div> </div> <div class=""CloseButton btn"" title=""Hide this impeller temporarily"" onclick=""this.parentElement.style.display = 'none'""></div> <div class=""MinButton btn"" title=""Shrink"" onclick=""toggleSize(this.parentElement)""></div> </div > " ); if(mode == ResultReportFileMode.Create){ HTMLString.Append( @"</body> </html>" ); } } catch { return null; } return HTMLString; }