Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Suplanus committed Mar 12, 2019
1 parent 53584d0 commit cebb17c
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 257 deletions.
362 changes: 181 additions & 181 deletions Suplanus.Sepla/Application/EplanOffline.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -9,208 +8,209 @@

namespace Suplanus.Sepla.Application
{
/// <summary>
/// EplanOffline class to use EPLAN Offline Application
/// </summary>
public class EplanOffline
{
/// <summary>
/// EplanOffline class to use EPLAN Offline Application
/// Returns if the EPLAN-Application is running
/// </summary>
public class EplanOffline
public bool IsRunning
{
/// <summary>
/// EPLAN Application
/// </summary>
public EplApplication Application;

/// <summary>
/// EPLAN Bin path e.g.: C:\Program Files\EPLAN\Platform\2.6.3\Bin
/// </summary>
public string BinPath;

/// <summary>
/// LicenseFile to start with given package
/// </summary>
public string LicenseFile;

/// <summary>
/// SystemConfiguration with environment paths
/// </summary>
public string SystemConfiguration;

/// <summary>
/// Init EPLAN with given bin path and license file (optional)
/// </summary>
/// <param name="binPath"></param>
/// <param name="systemConfiguration"></param>
/// <param name="licenseFile"></param>
public EplanOffline(string binPath,string systemConfiguration, string licenseFile = null)
{
BinPath = binPath;
SystemConfiguration = systemConfiguration;
LicenseFile = licenseFile;
}
get
{
var running = Application != null;
return running;
}
}

/// <summary>
/// Starts EPLAN in ConsoleApplication
/// You have to set Attribut <c>[STAThread]</c> for Main void
/// </summary>
public void StartWithoutGui()
{
Start();
}
/// <summary>
/// EPLAN Application
/// </summary>
public EplApplication Application;

/// <summary>
/// EPLAN Bin path e.g.: C:\Program Files\EPLAN\Platform\2.6.3\Bin
/// </summary>
public string BinPath;

/// <summary>
/// LicenseFile to start with given package
/// </summary>
public string LicenseFile;

/// <summary>
/// SystemConfiguration with environment paths
/// </summary>
public string SystemConfiguration;

/// <summary>
/// Init EPLAN with given bin path and license file (optional)
/// </summary>
/// <param name="binPath"></param>
/// <param name="systemConfiguration"></param>
/// <param name="licenseFile"></param>
public EplanOffline(string binPath, string systemConfiguration, string licenseFile = null)
{
BinPath = binPath;
SystemConfiguration = systemConfiguration;
LicenseFile = licenseFile;
}

/// <summary>
/// Starts EPLAN in ConsoleApplication
/// You have to set Attribut <c>[STAThread]</c> for Main void
/// </summary>
public void StartWithoutGui()
{
Start();
}

/// <summary>
/// Starts EPLAN with the last version of Electric P8 and attach to (WPF) window
/// </summary>
/// <param name="window"></param>
public void StartWpf(Window window)
{
IntPtr handle = new WindowInteropHelper(window).Handle;
Start(handle);
}

/// <summary>
/// Starts EPLAN with the last version of Electric P8 and attach to (WPF) window
/// </summary>
/// <param name="window"></param>
public Task<bool> StartWpfAsync(Window window)
{
IntPtr handle = new WindowInteropHelper(window).Handle;

var tcs = new TaskCompletionSource<bool>();
Thread thread = new Thread(() =>
{
Start(handle);
tcs.SetResult(true);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

tcs.Task.Wait();
return tcs.Task;
}

/// <summary>
/// Starts EPLAN with the last version of Electric P8 and attach to (WPF) window
/// </summary>
/// <param name="window"></param>
public void StartWpf(Window window)
public static Task<T> StartSTATask<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
Thread thread = new Thread(() =>
{
try
{
IntPtr handle = new WindowInteropHelper(window).Handle;
Start(handle);
tcs.SetResult(func());
}

/// <summary>
/// Starts EPLAN with the last version of Electric P8 and attach to (WPF) window
/// </summary>
/// <param name="window"></param>
public Task<bool> StartWpfAsync(Window window)
catch (Exception e)
{
IntPtr handle = new WindowInteropHelper(window).Handle;

var tcs = new TaskCompletionSource<bool>();
Thread thread = new Thread(() =>
{
Start(handle);
tcs.SetResult(true);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

tcs.Task.Wait();
return tcs.Task;
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}

/// <summary>
/// Starts EPLAN with the last version of Electric P8 and attach to (WF) form
/// </summary>
/// <param name="form"></param>
public void StartWindowsForms(Form form)
{
IntPtr handle = form.Handle;
Start(handle);
}

public static Task<T> StartSTATask<T>(Func<T> func)
/// <summary>
/// Starts the application
/// </summary>
/// <param name="handle"></param>
private void Start(IntPtr handle)
{
if (!IsRunning)
{
try
{
var tcs = new TaskCompletionSource<T>();
Thread thread = new Thread(() =>
{
try
{
tcs.SetResult(func());
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
EplApplication eplApplication = new EplApplication();
eplApplication.EplanBinFolder = BinPath;
if (!string.IsNullOrEmpty(SystemConfiguration))
{
eplApplication.SystemConfiguration = SystemConfiguration;
}
eplApplication.QuietMode = EplApplication.QuietModes.ShowAllDialogs;
eplApplication.SetMainFrame(handle);
if (!string.IsNullOrEmpty(LicenseFile))
{
eplApplication.LicenseFile = LicenseFile; // Set specific license
}
eplApplication.Init("", true, true);
Application = eplApplication;
}

/// <summary>
/// Starts EPLAN with the last version of Electric P8 and attach to (WF) form
/// </summary>
/// <param name="form"></param>
public void StartWindowsForms(Form form)
catch
{
IntPtr handle = form.Handle;
Start(handle);
Application = null;
}
}
}

/// <summary>
/// Returns if the EPLAN-Application is running
/// </summary>
public bool IsRunning
/// <summary>
/// Starts the application without Gui
/// </summary>
private void Start()
{
if (!IsRunning)
{
try
{
get
{
var running = Application != null;
return running;
}
EplApplication eplApplication = new EplApplication();
eplApplication.EplanBinFolder = BinPath;
if (!string.IsNullOrEmpty(SystemConfiguration))
{
eplApplication.SystemConfiguration = SystemConfiguration;
}
if (!string.IsNullOrEmpty(LicenseFile))
{
eplApplication.LicenseFile = LicenseFile; // Set specific licence
}
eplApplication.QuietMode = EplApplication.QuietModes.ShowAllDialogs;
eplApplication.Init("", true, true);
Application = eplApplication;
}

/// <summary>
/// Starts the application
/// </summary>
/// <param name="handle"></param>
private void Start(IntPtr handle)
catch
{
if (!IsRunning)
{
try
{
EplApplication eplApplication = new EplApplication();
eplApplication.EplanBinFolder = BinPath;
if (!string.IsNullOrEmpty(SystemConfiguration))
{
eplApplication.SystemConfiguration = SystemConfiguration;
}
eplApplication.QuietMode = EplApplication.QuietModes.ShowAllDialogs;
eplApplication.SetMainFrame(handle);
if (!string.IsNullOrEmpty(LicenseFile))
{
eplApplication.LicenseFile = LicenseFile; // Set specific licence
}
eplApplication.Init("", true, true);
Application = eplApplication;
}
catch
{
Application = null;
}
}
Application = null;
}
}
}

/// <summary>
/// Starts the application without Gui
/// </summary>
private void Start()
/// <summary>
/// Release all objects
/// <note type="caution">
/// Needed for eplan runtime exceptions, there is a known issue (T1094381), EPLAN says Microsoft should fix this
/// problem
/// Workaround: Enable native code debugging in visual studio
/// </note>
/// </summary>
public void Close()
{
// T1094381: There is a known problem with console applications, that visual studio not quit the debugging session, workaround: enable native code debugging in project
if (Application != null)
{
try
{
if (!IsRunning)
{
try
{
EplApplication eplApplication = new EplApplication();
eplApplication.EplanBinFolder = BinPath;
if (!string.IsNullOrEmpty(SystemConfiguration))
{
eplApplication.SystemConfiguration = SystemConfiguration;
}
if (!string.IsNullOrEmpty(LicenseFile))
{
eplApplication.LicenseFile = LicenseFile; // Set specific licence
}
eplApplication.QuietMode = EplApplication.QuietModes.ShowAllDialogs;
eplApplication.Init("", true, true);
Application = eplApplication;
}
catch
{
Application = null;
}
}
Application.Exit();
}

/// <summary>
/// Release all objects
/// <note type="caution">
/// Needed for eplan runtime exceptions, there is a known issue (T1094381), EPLAN says Microsoft should fix this problem
/// Workaround: Enable native code debugging in visual studio
/// </note>
/// </summary>
public void Close()
finally
{
// T1094381: There is a known problem with console applications, that visual studio not quit the debugging session, workaround: enable native code debugging in project
if (Application != null)
{
try
{
Application.Exit();
}
finally
{
Application = null;
}
}
Application = null;
}
}
}
}
}
}
Loading

0 comments on commit cebb17c

Please sign in to comment.