using System; using System.IO; using System.Linq; // R: T391849, 2026-05-21 using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Threading; using Skm.Ptx.Base; using Skm.Ptx.Data; using Skm.Ptx.Data.Emf; using Skm.Ptx.Obj; using Skm.Ptx.Resources.Ctrls; using Telerik.Reporting; using Telerik.ReportViewer.Wpf; using Telerik.Windows.Controls; using Microsoft.Win32;// R: T519095, 2026-07-04 namespace Skm.Ptx.CrystalReport { public partial class TelerikReportViewerDoc : PtxDoc { static readonly string[] dictionaries = new[] { "/Telerik.ReportViewer.Wpf.Themes;component/Themes/{0}/System.Windows.xaml", "/Telerik.ReportViewer.Wpf.Themes;component/Themes/{0}/Telerik.Windows.Controls.xaml", "/Telerik.ReportViewer.Wpf.Themes;component/Themes/{0}/Telerik.Windows.Controls.Input.xaml", "/Telerik.ReportViewer.Wpf.Themes;component/Themes/{0}/Telerik.Windows.Controls.Navigation.xaml", "/Telerik.ReportViewer.Wpf.Themes;component/Themes/{0}/Telerik.ReportViewer.Wpf.xaml" }; protected override Size? DocSize { get { return new Size(640, 480); } } private string m_header_name; protected override string DocHeader { get { return m_header_name; } } public override DataContextType DataContextType { get { return DataContextType.NONE; } } public override string ScenarioLayoutName { get { return GetType().Name + InitGuid; } } private string m_FilePath; private Data.Emf.Report m_Report; private ReportViewer m_WPFReportViewer; [LayoutProp] public Guid InitGuid { get; set; } public TelerikReportViewerDoc() { try { //2021-09-07 (JT): I think this is the WPF version. m_WPFReportViewer = new ReportViewer(); Content = m_WPFReportViewer; } catch (Exception ex) { MessageBox.Show(ex.Message); } } public TelerikReportViewerDoc(Data.Emf.Report report) : this() { m_Report = report; InitGuid = report.Res.ResKey != null ? (Guid)report.Res.ResKey : Guid.Empty; } // R: T519095, 2026-08-05 public static void SaveReport(Res res) { bool isRpt = res.Name.EndsWith(".rpt", StringComparison.OrdinalIgnoreCase);// R: T519095, 2026-08-05 Data.Emf.Report report = res.Report.FirstOrDefault(r => r.ResID == res.ResID); if (report != null) { byte[] reportBytes = report.Data; // R: T519095, 2026-08-05 SaveFileDialog dlg = new SaveFileDialog(); dlg.Title = isRpt ? "Save Crystal Report": "Save Telerik Report"; dlg.Filter = isRpt ? "Crystal Report(*.rpt) | *.rpt" : "Telerik Report (*.trdp)|*.trdp"; dlg.DefaultExt = isRpt ? ".rpt":".trdp"; dlg.AddExtension = true; dlg.OverwritePrompt = true; dlg.FileName = res.Name; bool? result = dlg.ShowDialog(); if (result == true) { try { File.WriteAllBytes(dlg.FileName, reportBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } protected override bool OnOpen() { if (m_Report == null && InitGuid != Guid.Empty) { Res res = DataHelper.GetRes(InitGuid); m_Report = res != null ? res.ResLink.Report : null; } if (m_Report != null) { m_header_name = m_Report.Res.Name; ReportType reportTye = m_Report.Res.Name.Contains("rpt") ? ReportType.CR : ReportType.TR; Data.Emf.Report.ReadData(m_Report, reportTye, out m_FilePath); } CreateReportViewer(); return true; } private void CreateReportViewer() { try { // R: T519095, 2026-08-05 // Create Main Grid Grid mainGrid = new Grid(); mainGrid.RowDefinitions.Add(new RowDefinition()); this.Content = mainGrid; // Create Report Viewer m_WPFReportViewer = new ReportViewer(); ApplyThemeToReportViewer(m_WPFReportViewer, "Windows11"); // R: T391849, 2026-08-05 // Enable search metadata on-demand for faster initial loading m_WPFReportViewer.SearchMetadataOnDemand = true; // Create the TRDP file string filePath = CreateReportFile(); // Create ReportSource ReportSource reportSource; if (m_Report.Res.Name == "IEC909_Study_Report.trdp") { reportSource = CreateIEC909ReportSource(filePath); } else { reportSource = CreateUriReportSource(filePath); } m_WPFReportViewer.ReportSource = reportSource; Grid.SetRow(m_WPFReportViewer, 0); mainGrid.Children.Add(m_WPFReportViewer); m_WPFReportViewer.HorizontalAlignment = HorizontalAlignment.Stretch; m_WPFReportViewer.VerticalAlignment = VerticalAlignment.Stretch; m_WPFReportViewer.RefreshReport(); } catch (Exception ex) { PTX.Current.Log(ex.ToString()); } } // R: T519095, 2026-08-05 private string CreateReportFile() { string sDir = Directory.GetCurrentDirectory(); string folderPath = Path.Combine(sDir, "TelerikReports"); Directory.CreateDirectory(folderPath); // R: T519095, 2026-08-05 PTX.Current.Log($"m_Report == null : {m_Report == null}"); if (m_Report != null) { PTX.Current.Log($"Report Name : {m_Report.Res?.Name}"); PTX.Current.Log($"Report Data Null : {m_Report.Data == null}"); } string filePath = Path.Combine(folderPath, m_Report.Res.Name); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { fs.Write(m_Report.Data, 0, m_Report.Data.Length); } return filePath; } // R: T519095, 2026-08-05 private UriReportSource CreateUriReportSource(string filePath) { UriReportSource reportSource = new UriReportSource(); reportSource.Uri = filePath; AddCommonParameters(reportSource); return reportSource; } // R: T519095, 2026-08-05 private InstanceReportSource CreateIEC909ReportSource(string filePath) { StudyCase sc = PTX.Current.PrjScenario.StudyCase.FirstOrDefault(x => x.StudyCaseType == (short)StudyCaseType.BS_SC_60909); if (sc != null) { ReportDataProvider.StudyCaseData = sc.Data; } ReportPackager packager = new ReportPackager(); Telerik.Reporting.Report report_; using (FileStream stream = File.OpenRead(filePath)) { report_ = (Telerik.Reporting.Report)packager.UnpackageDocument(stream); } // Attach runtime datasource report_ = IEC909RuntimeReportManager.PrepareReport(report_); // Show report InstanceReportSource instanceSource = new InstanceReportSource(); instanceSource.ReportDocument = report_; AddCommonParameters(instanceSource); return instanceSource; } // R: T519095, 2026-08-05 private void AddCommonParameters(ReportSource reportSource) { reportSource.Parameters.Add( new Parameter("Name", PTX.Current.ProjectName)); reportSource.Parameters.Add( new Parameter("Scenario_Name", PTX.Current.PrjScenario.Name)); string combineDbConnection = "Data Source=" + PTX.Current.ProjectOpenDbInfo.ServerName + ";Initial Catalog=" + PTX.Current.ProjectOpenDbInfo.DbName + "; Integrated Security = True;"; reportSource.Parameters.Add( new Parameter("SqlQuery", combineDbConnection)); if (m_Report.Res.Name == "ArcFlash_IEEE1584_Metric_Calcm2.trdp" || m_Report.Res.Name == "StudyOption.trdp") { Parameter parameter_AF = new Parameter("NFPA_BoundaryEquationOption", PTX.Current.PrjScenario.StudySetupArcFlash.NFPA_BoundaryEquationOption); reportSource.Parameters.Add(parameter_AF); Parameter parameter_AF_1 = new Parameter("Below_240", PTX.Current.PrjScenario.StudySetupArcFlash.Below240Class0Option.ToString()); reportSource.Parameters.Add(parameter_AF_1); Parameter parameter_AF_2 = new Parameter("Generator_Motor", PTX.Current.PrjScenario.StudySetupArcFlash.nGenDecayReCalc.ToString()); reportSource.Parameters.Add(parameter_AF_2); Parameter parameter_AF_3 = new Parameter("Induction_Motor", PTX.Current.PrjScenario.StudySetupArcFlash.IndMtrCycle.ToString()); reportSource.Parameters.Add(parameter_AF_3); Parameter parameter_AF_4 = new Parameter("Apply_Gen", PTX.Current.PrjScenario.StudySetupArcFlash.bApplyToGen.ToString()); reportSource.Parameters.Add(parameter_AF_4); Parameter parameter_AF_5 = new Parameter("Exclude_hp", PTX.Current.PrjScenario.StudySetupArcFlash.bExcludeLess75hp.ToString()); reportSource.Parameters.Add(parameter_AF_5); Parameter parameter_AF_6 = new Parameter("Fuse_current", PTX.Current.PrjScenario.StudySetupArcFlash.CurrentLimitingOption.ToString()); reportSource.Parameters.Add(parameter_AF_6); } } // R: T391849, 2026-05-21 private static void ApplyThemeToReportViewer(FrameworkElement element,string theme) { // R: T391849, 2026-08-05 ResourceDictionary targetResources = Application.Current?.Resources ?? element.Resources; foreach (string dictionary in dictionaries) { string uri = string.Format(dictionary, theme); bool alreadyExists = targetResources.MergedDictionaries.Any(d => d.Source != null && d.Source.OriginalString == uri); if (!alreadyExists) { targetResources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( uri, UriKind.RelativeOrAbsolute) }); } } Windows11ThemeSizeHelper.Helper.IsInCompactMode = false; Windows11Palette.LoadPreset( Windows11Palette.ColorVariation.System); } } }