using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Base;
using Eplan.EplApi.DataModel;
using Eplan.EplApi.DataModel.Graphics;
using Eplan.EplApi.DataModel.MasterData;
using Eplan.EplApi.HEServices;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Label = System.Windows.Forms.Label;
using Project = Eplan.EplApi.DataModel.Project;
namespace Test.EplAddin.InsertPageMacros
{
///
/// This class implements a EPLAN action. The Action will register the Addins in that Registerst.
///
///
public class ActionInsertPageMacros : IEplAction
{
//Name und Schaltflächentext der Aktion
internal const string ACTION = "InsertPageMacros";
internal const string BUTTON = "Seitenmakros einfuegen";
///
/// Execution of the Action.
///
/// True: Execution of the Action was successful
public bool Execute(ActionCallingContext ctx)
{
SelectionSet selectionSet = new SelectionSet();
Project project = selectionSet.GetCurrentProject(false);
if (project == null)
{
MessageBox.Show("Es muss erst ein Projekt ausgewählt werden.", "Vorgang abgebrochen!");
return false;
}
try
{
string path = "Eingabefeld";
//Makro ermitteln
if (ShowDialog(BUTTON, "Makroname (inkl. Pfad):", ref path) == DialogResult.OK)
{
//Wenn das Makro nicht existiert (falscher Pfad oder Dateityp), Aktion beenden
if ((!File.Exists(path)) || (!(Path.GetExtension(path) == ".emp")))
{
MessageBox.Show($"Das angegebene Makro \"{path}\" existiert nicht", "Vorgang abgebrochen!");
return true;
}
try
{
//Seitenmakro einfügen
InsertMacro(project, path);
}
catch (Exception ex)
{
new BaseException("Fehler: " + ex.Message, MessageLevel.Error).FixMessage();
}
}
}
catch { }
return true;
}
///
/// Function is called through the ApplicationFramework
///
/// Under this name, this Action in the system is registered
/// With this overload priority, this Action is registered
/// true: the return parameters are valid
public bool OnRegister(ref string Name, ref int Ordinal)
{
Name = ACTION;
Ordinal = 20;
return true;
}
///
/// Documentation function for the Action; is called of the system as required
/// Bescheibungstext delivers for the Action itself and if the Action String-parameters ("Kommandozeile")
/// also name and description of the single parameters evaluates
///
/// This object must be filled with the information of the Action.
public void GetActionProperties(ref ActionProperties actionProperties)
{
// Description 1st parameter
// ActionParameterProperties firstParam= new ActionParameterProperties();
// firstParam.set("Param1", "1. Parameter for ActionInsertPageMacros");
// actionProperties.addParameter(firstParam);
// Description 2nd parameter
// ActionParameterProperties firstParam= new ActionParameterProperties();
// firstParam.set("Param2", "2. Parameter for ActionInsertPageMacros");
// actionProperties.addParameter(firstParam);
}
private DialogResult ShowDialog(string title, string promptText, ref string value)
{
//Dialogfenster mit Eingabefeld
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Abbrechen";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 18, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
private void InsertMacro(Project project, string path)
{
//Seitenmakro platzieren
List pages = PlacePageMacro(project, path);
//Wenn keine Seiten platziert wurden, Aktion beeenden
if (pages.Count == 0)
return;
//Benutzereinstellungen prüfen (Auswahldialog für Wertesätze von Platzhalterobjekten öffnen: Beim Einfügen von Seitenmakros)
Settings settings = new Settings();
if (settings.GetBoolSetting("USER.MacrosGui.PagePlaceholderAutoOpen", 0))
AssignPlaceHolderRecords(pages);
}
private List PlacePageMacro(Project project, string path)
{
List pages = new List();
try
{
//Makro öffnen
PageMacro pageMacro = new PageMacro();
pageMacro.Open(path, project);
//Keine bestehenden Seiten im Projekt überschreiben
bool[] overwrite = new bool[pageMacro.Pages.Count()];
//Seiten einfügen
Insert insert = new Insert();
StorableObject[] storableObjects = insert.PageMacro(
pageMacro,
project,
overwrite,
PageMacro.Enums.NumerationMode.NumberPreferPrefix
);
foreach (Page page in storableObjects)
pages.Add(page);
}
catch (Exception ex)
{
new BaseException("Fehler: " + ex.Message, MessageLevel.Error).FixMessage();
MessageBox.Show("Das Seitenmakro konnte nicht platziert werden.", "Vorgang abgebrochen!");
}
return pages;
}
private void AssignPlaceHolderRecords(List pages)
{
foreach (Page page in pages)
{
foreach (Placement placement in page.AllPlacements)
{
if (!(placement is PlaceHolder))
continue;
PlaceHolder placeHolder = (PlaceHolder)placement;
//Wertesätze ermitteln
StringCollection records = placeHolder.GetRecordNames();
//Wenn keine Wertesätze vorhanden, Platzhalterobjekt überspringen
if (records.Count == 0)
continue;
//Wertesatz auswählen
ListSelectDecisionContext listSelectDecisionContext = new ListSelectDecisionContext(records, records[0], placeHolder.Name);
listSelectDecisionContext.AllowMultiSelect = false;
var result = new Decider().Decide(listSelectDecisionContext);
//Wertesatz setzen
if (result == EnumDecisionReturn.eOK)
placeHolder.ApplyRecord(listSelectDecisionContext.SelectedEntry);
}
}
}
}
}