public void Main(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, ComponentStartMode startMode)
        {
            // Check if OpenModelica is installed properly
            if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OPENMODELICAHOME")))
            {
                this.Logger.WriteFailed(
                    "OpenModelica is probably not installed. OPENMODELICAHOME environment variable needs to be set. <a href=\"https://openmodelica.org\">https://openmodelica.org</a>");

                return;
            }

            // nothing is open, import multiple components to folder 'ModelicaImports'
            if (currentobj == null)
            {
                MessageBox.Show(
                    "New Components/TestComponents will be created in the folder 'ModelicaImports'.",
                    "For Your Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                this.Logger.WriteInfo("New Components/TestComponents will be created in the folder 'ModelicaImports'.");

                // call ModelicaModelPicker with selectMultiple = true
                using (ModelicaModelPicker modelpicker = new ModelicaModelPicker(true, project, this.Logger))
                {
                    var dialogResult = modelpicker.ShowDialog();
                    if (dialogResult != System.Windows.Forms.DialogResult.OK)
                    {
                        this.Logger.WriteInfo("Modelica import was cancelled by the user.");
                        return;
                    }
                }
            }
            else
            {
                if ((currentobj.Meta.Name != typeof(CyPhy.Component).Name &&
                    currentobj.Meta.Name != typeof(CyPhy.TestComponent).Name))
                {
                    this.Logger.WriteError("Please open a Component or TestComponent and try again.");
                    return;
                }

                if (currentobj.Meta.Name == typeof(CyPhy.TestComponent).Name)
                {
                    var childObjects = currentobj.ChildObjects;

                    foreach (MgaObject child in childObjects)
                    {
                        if (child.MetaBase.Name == "ModelicaModel")
                        {
                            this.Logger.WriteError("TestComponent {0} already has a ModelicaModel. Only one is allowed.", currentobj.Name);
                            return;
                        }
                    }
                }

                CyPhy.ComponentType cyphyComponent = null;
                if (currentobj.Meta.Name == typeof(CyPhy.Component).Name)
                {
                    cyphyComponent = CyPhyClasses.Component.Cast(currentobj);
                }
                else if (currentobj.Meta.Name == typeof(CyPhy.TestComponent).Name)
                {
                    cyphyComponent = CyPhyClasses.TestComponent.Cast(currentobj);
                }

                // per META-2674
                if (cyphyComponent.IsLib)
                {
                    this.Logger.WriteError("Cannot modify a model in an attached Library; please select a valid Component or TestComponent.");
                    return;
                }

                // per META-2673
                if (cyphyComponent.IsInstance)
                {
                    this.Logger.WriteError("Cannot modify an Instance; please select a non-Instance Component or TestComponent.");
                    return;
                }

                string message = string.Format("All selected Modelica models will be added to {0}.", currentobj.Name);

                MessageBox.Show(
                    message,
                    "For Your Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                this.Logger.WriteInfo(message);

                // call ModelicaModelPicker with selectMultiple = false
                using (ModelicaModelPicker modelpicker = new ModelicaModelPicker(false, project, this.Logger, cyphyComponent))
                {
                    var dialogResult = modelpicker.ShowDialog();
                    if (dialogResult != System.Windows.Forms.DialogResult.OK)
                    {
                        this.Logger.WriteInfo("Modelica import was cancelled by the user.");
                        return;
                    }
                }
            }
        }
Example #2
0
        public void Main(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, ComponentStartMode startMode)
        {
            // Check if OpenModelica is installed properly
            if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OPENMODELICAHOME")))
            {
                this.Logger.WriteFailed(
                    "OpenModelica is probably not installed. OPENMODELICAHOME environment variable needs to be set. <a href=\"https://openmodelica.org\">https://openmodelica.org</a>");

                return;
            }

            // nothing is open, import multiple components to folder 'ModelicaImports'
            if (currentobj == null)
            {
                MessageBox.Show(
                    "New Components/TestComponents will be created in the folder 'ModelicaImports'.",
                    "For Your Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                this.Logger.WriteInfo("New Components/TestComponents will be created in the folder 'ModelicaImports'.");

                // call ModelicaModelPicker with selectMultiple = true
                using (ModelicaModelPicker modelpicker = new ModelicaModelPicker(true, project, this.Logger))
                {
                    var dialogResult = modelpicker.ShowDialog();
                    if (dialogResult != System.Windows.Forms.DialogResult.OK)
                    {
                        this.Logger.WriteInfo("Modelica import was cancelled by the user.");
                        return;
                    }
                }
            }
            else
            {
                if ((currentobj.Meta.Name != typeof(CyPhy.Component).Name &&
                     currentobj.Meta.Name != typeof(CyPhy.TestComponent).Name))
                {
                    this.Logger.WriteError("Please open a Component or TestComponent and try again.");
                    return;
                }

                if (currentobj.Meta.Name == typeof(CyPhy.TestComponent).Name)
                {
                    var childObjects = currentobj.ChildObjects;

                    foreach (MgaObject child in childObjects)
                    {
                        if (child.MetaBase.Name == "ModelicaModel")
                        {
                            this.Logger.WriteError("TestComponent {0} already has a ModelicaModel. Only one is allowed.", currentobj.Name);
                            return;
                        }
                    }
                }

                CyPhy.ComponentType cyphyComponent = null;
                if (currentobj.Meta.Name == typeof(CyPhy.Component).Name)
                {
                    cyphyComponent = CyPhyClasses.Component.Cast(currentobj);
                }
                else if (currentobj.Meta.Name == typeof(CyPhy.TestComponent).Name)
                {
                    cyphyComponent = CyPhyClasses.TestComponent.Cast(currentobj);
                }

                // per META-2674
                if (cyphyComponent.IsLib)
                {
                    this.Logger.WriteError("Cannot modify a model in an attached Library; please select a valid Component or TestComponent.");
                    return;
                }

                // per META-2673
                if (cyphyComponent.IsInstance)
                {
                    this.Logger.WriteError("Cannot modify an Instance; please select a non-Instance Component or TestComponent.");
                    return;
                }

                string message = string.Format("All selected Modelica models will be added to {0}.", currentobj.Name);

                MessageBox.Show(
                    message,
                    "For Your Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                this.Logger.WriteInfo(message);

                // call ModelicaModelPicker with selectMultiple = false
                using (ModelicaModelPicker modelpicker = new ModelicaModelPicker(false, project, this.Logger, cyphyComponent))
                {
                    var dialogResult = modelpicker.ShowDialog();
                    if (dialogResult != System.Windows.Forms.DialogResult.OK)
                    {
                        this.Logger.WriteInfo("Modelica import was cancelled by the user.");
                        return;
                    }
                }
            }
        }