public AasxPluginResultBase ActivateAction(string action, params object[] args)
        {
            if (action == "set-json-options" && args != null && args.Length >= 1 && args[0] is string)
            {
                var newOpt = Newtonsoft.Json.JsonConvert.DeserializeObject <AasxPluginExportTable.ExportTableOptions>(
                    (args[0] as string));
                if (newOpt != null)
                {
                    this.options = newOpt;
                }
            }

            if (action == "get-json-options")
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(
                    this.options, Newtonsoft.Json.Formatting.Indented);
                return(new AasxPluginResultBaseObject("OK", json));
            }

            if (action == "get-licenses")
            {
                var lic = new AasxPluginResultLicense();
                lic.shortLicense = "The OpenXML SDK is under MIT license." + Environment.NewLine +
                                   "The ClosedXML library is under MIT license." + Environment.NewLine +
                                   "The ExcelNumberFormat number parser is licensed under the MIT license." + Environment.NewLine +
                                   "The FastMember reflection access is licensed under Apache License 2.0 (Apache - 2.0).";

                lic.isStandardLicense = true;
                lic.longLicense       = AasxPluginHelper.LoadLicenseTxtFromAssemblyDir(
                    "LICENSE.txt", Assembly.GetExecutingAssembly());

                return(lic);
            }

            if (action == "get-events" && this.eventStack != null)
            {
                // try access
                return(this.eventStack.PopEvent());
            }

            if (action == "export-submodel" && args != null && args.Length >= 3 &&
                args[0] is IFlyoutProvider && args[1] is AdminShell.AdministrationShellEnv &&
                args[2] is AdminShell.Submodel)
            {
                // flyout provider
                var fop = args[0] as IFlyoutProvider;

                // which Submodel
                var env = args[1] as AdminShell.AdministrationShellEnv;
                var sm  = args[2] as AdminShell.Submodel;
                if (env == null || sm == null)
                {
                    return(null);
                }

                // the Submodel elements need to have parents
                sm.SetAllParents();

                // prepare list of items to be exported
                var list = new ExportTableAasEntitiesList();
                ExportTable_EnumerateSubmodel(list, env, broadSearch: false, depth: 1, sm: sm, sme: null);

                // handle the export dialogue
                var uc = new ExportTableFlyout();
                uc.Presets = this.options.Presets;
                fop?.StartFlyoverModal(uc);
                if (uc.Result == null)
                {
                    return(null);
                }
                var job = uc.Result;

                // get the output file
                var dlg = new Microsoft.Win32.SaveFileDialog();
                // ReSharper disable EmptyGeneralCatchClause
                try
                {
                    dlg.InitialDirectory = System.IO.Path.GetDirectoryName(
                        System.AppDomain.CurrentDomain.BaseDirectory);
                }
                catch { }
                // ReSharper enable EmptyGeneralCatchClause
                dlg.Title = "Select text file to be exported";

                if (job.Format == (int)ExportTableRecord.FormatEnum.TSF)
                {
                    dlg.FileName   = "new.txt";
                    dlg.DefaultExt = "*.txt";
                    dlg.Filter     =
                        "Tab separated file (*.txt)|*.txt|Tab separated file (*.tsf)|*.tsf|All files (*.*)|*.*";
                }
                if (job.Format == (int)ExportTableRecord.FormatEnum.LaTex)
                {
                    dlg.FileName   = "new.tex";
                    dlg.DefaultExt = "*.tex";
                    dlg.Filter     = "LaTex file (*.tex)|*.tex|All files (*.*)|*.*";
                }
                if (job.Format == (int)ExportTableRecord.FormatEnum.Excel)
                {
                    dlg.FileName   = "new.xlsx";
                    dlg.DefaultExt = "*.xlsx";
                    dlg.Filter     = "Microsoft Excel (*.xlsx)|*.xlsx|All files (*.*)|*.*";
                }
                if (job.Format == (int)ExportTableRecord.FormatEnum.Word)
                {
                    dlg.FileName   = "new.docx";
                    dlg.DefaultExt = "*.docx";
                    dlg.Filter     = "Microsoft Word (*.docx)|*.docx|All files (*.*)|*.*";
                }

                fop?.StartFlyover(new EmptyFlyout());
                var res = dlg.ShowDialog(fop?.GetWin32Window());

                try
                {
                    if (res == true)
                    {
                        Log.Info("Exporting table: {0}", dlg.FileName);
                        var success = false;
                        try
                        {
                            if (job.Format == (int)ExportTableRecord.FormatEnum.TSF)
                            {
                                success = job.ExportTabSeparated(dlg.FileName, list);
                            }
                            if (job.Format == (int)ExportTableRecord.FormatEnum.LaTex)
                            {
                                success = job.ExportLaTex(dlg.FileName, list);
                            }
                            if (job.Format == (int)ExportTableRecord.FormatEnum.Excel)
                            {
                                success = job.ExportExcel(dlg.FileName, list);
                            }
                            if (job.Format == (int)ExportTableRecord.FormatEnum.Word)
                            {
                                success = job.ExportWord(dlg.FileName, list);
                            }
                        }
                        catch
                        {
                            success = false;
                        }

                        if (!success)
                        {
                            fop?.MessageBoxFlyoutShow(
                                "Some error occured while exporting the table. Please refer to the log messages.",
                                "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "When exporting table, an error occurred");
                }

                fop?.CloseFlyover();
            }

            // default
            return(null);
        }
Esempio n. 2
0
        public AasxPluginResultBase ActivateAction(string action, params object[] args)
        {
            if (action == "set-json-options" && args != null && args.Length >= 1 && args[0] is string)
            {
                var newOpt = Newtonsoft.Json.JsonConvert.DeserializeObject <AasxPluginExportTable.ExportTableOptions>(
                    (args[0] as string));
                if (newOpt != null)
                {
                    this.options = newOpt;
                }
            }

            if (action == "get-json-options")
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(
                    this.options, Newtonsoft.Json.Formatting.Indented);
                return(new AasxPluginResultBaseObject("OK", json));
            }

            if (action == "get-licenses")
            {
                var lic = new AasxPluginResultLicense();
                lic.shortLicense = "The OpenXML SDK is under MIT license." + Environment.NewLine +
                                   "The ClosedXML library is under MIT license." + Environment.NewLine +
                                   "The ExcelNumberFormat number parser is licensed under the MIT license." + Environment.NewLine +
                                   "The FastMember reflection access is licensed under Apache License 2.0 (Apache - 2.0).";

                lic.isStandardLicense = true;
                lic.longLicense       = AasxPluginHelper.LoadLicenseTxtFromAssemblyDir(
                    "LICENSE.txt", Assembly.GetExecutingAssembly());

                return(lic);
            }

            if (action == "get-events" && _eventStack != null)
            {
                // try access
                return(_eventStack.PopEvent());
            }

            if ((action == "export-submodel" || action == "import-submodel") &&
                args != null && args.Length >= 3 &&
                args[0] is IFlyoutProvider && args[1] is AdminShell.AdministrationShellEnv &&
                args[2] is AdminShell.Submodel)
            {
                // flyout provider
                var fop = args[0] as IFlyoutProvider;

                // which Submodel
                var env = args[1] as AdminShell.AdministrationShellEnv;
                var sm  = args[2] as AdminShell.Submodel;
                if (env == null || sm == null)
                {
                    return(null);
                }

                // the Submodel elements need to have parents
                sm.SetAllParents();

                // handle the export dialogue
                var uc = new ExportTableFlyout((action == "export-submodel")
                    ? "Export SubmodelElements as Table"
                    : "Import SubmodelElements from Table");
                uc.Presets = this.options.Presets;
                fop?.StartFlyoverModal(uc);
                fop?.CloseFlyover();
                if (uc.Result == null)
                {
                    if (uc.CloseForHelp)
                    {
                        // give over to event stack
                        var evt = new AasxPluginResultEventDisplayContentFile();
                        evt.fn = "https://github.com/admin-shell-io/aasx-package-explorer/tree/" +
                                 "MIHO/AddPluginForKnownSubmodels/src/AasxPluginExportTable/help";
                        evt.mimeType = System.Net.Mime.MediaTypeNames.Text.Html;
                        _eventStack?.PushEvent(evt);
                    }

                    return(null);
                }

                if (action == "export-submodel")
                {
                    Export(uc.Result, fop, sm, env);
                }

                if (action == "import-submodel")
                {
                    Import(uc.Result, fop, sm, env);
                }
            }

            if (action == "export-uml" &&
                args != null && args.Length >= 3 &&
                args[0] is IFlyoutProvider && args[1] is AdminShell.AdministrationShellEnv &&
                args[2] is AdminShell.Submodel)
            {
                var fn = (args.Length >= 4) ? args[3] as string : null;

                // flyout provider (will be required in the future)
                var fop = args[0] as IFlyoutProvider;

                // which Submodel
                var env = args[1] as AdminShell.AdministrationShellEnv;
                var sm  = args[2] as AdminShell.Submodel;
                if (env == null || sm == null)
                {
                    return(null);
                }

                // the Submodel elements need to have parents
                sm.SetAllParents();

                // prep options
                var exop = options.UmlExport;
                if (exop == null)
                {
                    exop = new ExportUmlOptions();
                }

                // dialogue for user options
                var uc = new ExportUmlFlyout();
                uc.Result = exop;
                fop?.StartFlyoverModal(uc);
                fop?.CloseFlyover();
                if (uc.Result == null)
                {
                    return(null);
                }

                // ask for filename
                var dlg = new Microsoft.Win32.SaveFileDialog();
                try
                {
                    dlg.InitialDirectory = System.IO.Path.GetDirectoryName(
                        System.AppDomain.CurrentDomain.BaseDirectory);
                }
                catch (Exception ex)
                {
                    AdminShellNS.LogInternally.That.SilentlyIgnoredError(ex);
                }
                dlg.Title = "Select file for UML export ..";

                if (uc.Result.Format == ExportUmlOptions.ExportFormat.PlantUml)
                {
                    dlg.FileName   = "new.uml";
                    dlg.DefaultExt = "*.uml";
                    dlg.Filter     =
                        "PlantUML text file (*.uml)|*.uml|All files (*.*)|*.*";
                }
                else
                {
                    dlg.FileName   = "new.xml";
                    dlg.DefaultExt = "*.xml";
                    dlg.Filter     =
                        "XMI file (*.xml)|*.xml|All files (*.*)|*.*";
                }

                fop?.StartFlyover(new EmptyFlyout());
                var fnres = dlg.ShowDialog(fop?.GetWin32Window());
                fop?.CloseFlyover();
                if (fnres != true)
                {
                    return(null);
                }
                fn = dlg.FileName;

                // use functionality
                ExportUml.ExportUmlToFile(env, sm, uc.Result, fn);
                Log.Info($"Export UML data to file: {fn}");

                // copy?
                if (uc.Result.CopyToPasteBuffer)
                {
                    try
                    {
                        var lines = File.ReadAllText(fn);
                        Clipboard.SetData(DataFormats.Text, lines);
                        Log.Info("Export UML data copied to paste buffer.");
                    }
                    catch (Exception ex)
                    {
                        AdminShellNS.LogInternally.That.SilentlyIgnoredError(ex);
                    }
                }
            }

            // default
            return(null);
        }