12 Dateien schreiben


01 Beschriftung
using System.IO;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _12_Dateien_schreiben_01_Beschriftung
{
  [Start]
  public void Function()
  {
    string projectPath = PathMap.SubstitutePath("$(PROJECTPATH)");

    Progress progress = new Progress("SimpleProgress");
    progress.SetAllowCancel(true);
    progress.SetAskOnCancel(false);
    progress.SetNeededSteps(3);
    progress.SetTitle("Beschriftung");
    progress.ShowImmediately();

    if (!progress.Canceled())
    {
      progress.BeginPart(50,
        "Artikelsummenstückliste wird erstellt...");

      string fileName = Path.Combine(projectPath,
        "Artikelsummenstückliste.xlsx");                 

      ActionCallingContext context1 = new ActionCallingContext();
      context1.AddParameter("configscheme", "Summarized parts list");
      context1.AddParameter("filterscheme", "");
      context1.AddParameter("sortscheme", "");
      context1.AddParameter("language", "de_DE");
      context1.AddParameter("destinationfile", fileName);
      context1.AddParameter("recrepeat", "1");
      context1.AddParameter("taskrepeat", "1");
      context1.AddParameter("showoutput", "1");
      context1.AddParameter("type", "PROJECT");
      new CommandLineInterpreter().Execute("label", context1);

      progress.EndPart();
    }

    if (!progress.Canceled())
    {
      progress.BeginPart(50, 
        "Betriebsmittelbeschriftung wird erstellt...");

      string fileName = Path.Combine(projectPath,
        "Betriebsmittelliste.xlsx");

      ActionCallingContext context2 = new ActionCallingContext();
      context2.AddParameter("configscheme", "Device tag list");
      context2.AddParameter("filterscheme", "");
      context2.AddParameter("sortscheme", "");
      context2.AddParameter("language", "de_DE");
      context2.AddParameter("destinationfile", fileName);
      context2.AddParameter("recrepeat", "1");
      context2.AddParameter("taskrepeat", "1");
      context2.AddParameter("showoutput", "1");
      context2.AddParameter("type", "PROJECT");
      new CommandLineInterpreter().Execute("label", context2);

      progress.EndPart();
    }

    progress.EndPart(true);
  }
}


02 Beschriftung mit Überprüfung
using System;
using System.IO;
using System.Windows.Forms;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _12_Dateien_schreiben_02_Beschriftung_mit_Ueberpruefung
{
  [Start]
  public void Function()
  {
    string fileName;

    fileName = GetFileName("Artikelsummenstückliste.xlsx");
    if (fileName != null)
    {
      ActionCallingContext context1 = new ActionCallingContext();
      context1.AddParameter("configscheme", "Summarized parts list");
      context1.AddParameter("filterscheme", "");
      context1.AddParameter("sortscheme", "");
      context1.AddParameter("language", "de_DE");
      context1.AddParameter("destinationfile", fileName);
      context1.AddParameter("recrepeat", "1");
      context1.AddParameter("taskrepeat", "1");
      context1.AddParameter("showoutput", "1");
      context1.AddParameter("type", "PROJECT");
      new CommandLineInterpreter().Execute("label", context1);
    }

    fileName = GetFileName("Betriebsmittelliste.xlsx");
    if (fileName != null)
    {
      ActionCallingContext context2 = new ActionCallingContext();
      context2.AddParameter("configscheme", "Device tag list");
      context2.AddParameter("filterscheme", "");
      context2.AddParameter("sortscheme", "");
      context2.AddParameter("language", "de_DE");
      context2.AddParameter("destinationfile", fileName);
      context2.AddParameter("recrepeat", "1");
      context2.AddParameter("taskrepeat", "1");
      context2.AddParameter("showoutput", "1");
      context2.AddParameter("type", "PROJECT");
      new CommandLineInterpreter().Execute("label", context2);
    }
  }

  private static string GetFileName(string fileName)
  {
    string projectPath = PathMap.SubstitutePath("$(PROJECTPATH)");
    string fullFileName = Path.Combine(projectPath, fileName);

    if (File.Exists(fullFileName))
    {
      DialogResult dialogResult = MessageBox.Show(
        "Die Datei '" +
        fullFileName +
        "' existiert bereits." +
        Environment.NewLine +
        Environment.NewLine +
        "Wollen Sie die Datei überschreiben?",
        "Beschriftung",
        MessageBoxButtons.YesNo,
        MessageBoxIcon.Question
      );

      if (dialogResult == DialogResult.No)
      {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.DefaultExt = "xls";
        sfd.FileName = fileName;
        sfd.Filter = "Excel-Datei (*.xlsx)|*.xlsx";
        sfd.InitialDirectory = projectPath;
        sfd.Title = "Speicherort für " + fileName + " wählen:";
        sfd.ValidateNames = true;

        DialogResult sfdDialogResult = sfd.ShowDialog();
        if (sfdDialogResult == DialogResult.OK)
        {
          fullFileName = sfd.FileName;
        }
        else if (sfdDialogResult == DialogResult.Cancel)
        {
          fullFileName = null;
        }
      }
    }
    return fullFileName;
  }
}


03 PDF beim Schließen erzeugen
using System;
using System.IO;
using System.Windows.Forms;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _12_Dateien_schreiben_03_PDF_beim_Schliessen_erzeugen
{
  [DeclareEventHandler("Eplan.EplApi.OnUserPreCloseProject")]
  public void Function()
  {
    string projectPath = PathMap.SubstitutePath("$(PROJECTPATH)");
    string projectName = PathMap.SubstitutePath("$(PROJECTNAME)");

    DialogResult dialogResult = MessageBox.Show(
        "Soll ein PDF für das Projekt'" +
        Environment.NewLine +
        projectName +
        "'" +
        Environment.NewLine +
        "erzeugt werden?",
        "PDF-Export",
        MessageBoxButtons.YesNo,
        MessageBoxIcon.Question
        );

    if (dialogResult == DialogResult.Yes)
    {
      Progress progress = new Progress("SimpleProgress");
      progress.SetAllowCancel(true);
      progress.SetAskOnCancel(true);
      progress.BeginPart(100, "");
      progress.ShowImmediately();

      CommandLineInterpreter cli = new CommandLineInterpreter();
      ActionCallingContext acc = new ActionCallingContext();

      string fullFileName = Path.Combine(projectPath, projectName);
      acc.AddParameter("TYPE", "PDFPROJECTSCHEME");
      acc.AddParameter("EXPORTFILE", fullFileName);
      acc.AddParameter("EXPORTSCHEME", "EPLAN_default_value");

      cli.Execute("export", acc);

      progress.EndPart(true);
    }
  }
}


04 Textdatei schreiben
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;

public class _12_Dateien_schreiben_04_Textdatei_schreiben
{
  [Start]
  public void Function()
  {
    string fileName = 
      PathMap.SubstitutePath(@"$(PROJECTPATH)\Testdatei.txt");

    StringBuilder sb = new StringBuilder();
    sb.Append("Beispiel");
    sb.Append("text");
    sb.AppendLine();
    sb.Append("mit mehreren Zeilen");

    File.WriteAllText(fileName, sb.ToString(), Encoding.Unicode);

    MessageBox.Show(
        "Textdatei erfolgreich exportiert.",
        "Information",
        MessageBoxButtons.OK,
        MessageBoxIcon.Information
        );

    Process.Start(fileName);
  }
}


05 XML Datei schreiben
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;

public class _12_Dateien_schreiben_05_XML_Datei_schreiben
{
  [Start]
  public void Function()
  {
    // Init
    List<Person> persons = new List<Person>();

    Person max = new Person("Max", "Mustermann",
      "München", "Musterstraße 1");
    persons.Add(max);

    Person maria = new Person("Maria", "Musterfrau",
      "Nürnberg", "Musterstraße 2");
    persons.Add(maria);

    string xmlPath = PathMap.SubstitutePath("$(MD_XML)");
    string fileName = Path.Combine(xmlPath, "Persons.xml");

    // Write
    WriteXml(persons, fileName);
    MessageBox.Show("XML-Datei erzeugt");
  }

  public static void WriteXml(List<Person> persons, string fileName)
  {
    XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.Encoding = Encoding.UTF8;
    XmlWriter writer = XmlWriter.Create(fileName, settings);

    serializer.Serialize(writer, persons, namespaces);
    writer.Close();
    writer.Dispose();
  }

  public class Person
  {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public AddressClass Address { get; set; }

    public string FullName
    {
      get { return FirstName + " " + LastName; }
    }

    public Person()
    {
    }

    public Person(string firstName, string lastName,
      string city, string street)
    {
      FirstName = firstName;
      LastName = lastName;

      Address = new AddressClass();
      Address.City = city;
      Address.Street = street;
    }

    public class AddressClass
    {
      [XmlAttribute]
      public string City { get; set; }

      [XmlAttribute]
      public string Street { get; set; }
    }
  }
}