mattercontrol/MatterControlApplication.cs

261 lines
8.9 KiB
C#
Raw Normal View History

2014-01-29 19:09:30 -08:00
/*
Copyright (c) 2016, Lars Brubaker, Kevin Pope
2014-01-29 19:09:30 -08:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
2015-04-08 15:20:10 -07:00
modification, are permitted provided that the following conditions are met:
2014-01-29 19:09:30 -08:00
1. Redistributions of source code must retain the above copyright notice, this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer.
2014-01-29 19:09:30 -08:00
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and/or other materials provided with the distribution.
2014-01-29 19:09:30 -08:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies,
2014-01-29 19:09:30 -08:00
either expressed or implied, of the FreeBSD Project.
*/
2016-09-23 16:34:52 -07:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
2017-03-15 16:17:06 -07:00
using System.Linq;
2016-09-23 16:34:52 -07:00
using System.Net;
using System.Threading;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
2017-03-15 16:17:06 -07:00
using MatterHackers.DataConverters3D;
using MatterHackers.ImageProcessing;
2014-01-29 19:09:30 -08:00
using MatterHackers.MatterControl.DataStorage;
2017-03-15 16:17:06 -07:00
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.MatterControl.PluginSystem;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.PolygonMesh.Processors;
using MatterHackers.RenderOpenGl.OpenGl;
2015-05-20 13:37:08 -07:00
using Mindscape.Raygun4Net;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
public class MatterControlApplication : GuiWidget
2015-04-08 15:20:10 -07:00
{
#if DEBUG
2017-01-23 16:56:41 -08:00
//public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206";
public static string MCWSBaseUri { get; } = "https://mattercontrol-test.appspot.com";
2017-01-23 16:56:41 -08:00
#else
public static string MCWSBaseUri { get; } = "https://mattercontrol.appspot.com";
#endif
public static bool CameraInUseByExternalProcess { get; set; } = false;
public bool RestartOnClose = false;
private string[] commandLineArgs = null;
2015-04-08 15:20:10 -07:00
public static bool IsLoading { get; private set; } = true;
public static void RequestPowerShutDown()
{
// does nothing on windows
}
2015-04-08 15:20:10 -07:00
static MatterControlApplication()
{
if (AggContext.OperatingSystem == OSType.Mac && AggContext.StaticData == null)
{
// Set working directory - this duplicates functionality in Main but is necessary on OSX as Main fires much later (after the constructor in this case)
2015-12-23 16:23:19 -08:00
// resulting in invalid paths due to path tests running before the working directory has been overridden. Setting the value before initializing StaticData
// works around this architectural difference.
Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
}
2015-04-08 15:20:10 -07:00
// Because fields on this class call localization methods and because those methods depend on the StaticData provider and because the field
// initializers run before the class constructor, we need to init the platform specific provider in the static constructor (or write a custom initializer method)
//
// Initialize a standard file system backed StaticData provider
if (AggContext.StaticData == null) // it may already be initialized by tests
{
AggContext.StaticData = new MatterHackers.Agg.FileSystemStaticData();
}
}
public MatterControlApplication(double width, double height)
: base(width, height)
{
this.Name = "MatterControlApplication Widget";
ApplicationSettings.Instance.set("HardwareHasCamera", "false");
// set this at startup so that we can tell next time if it got set to true in close
UserSettings.Instance.Fields.StartCount = UserSettings.Instance.Fields.StartCount + 1;
this.commandLineArgs = Environment.GetCommandLineArgs();
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
2015-04-08 15:20:10 -07:00
for (int currentCommandIndex = 0; currentCommandIndex < commandLineArgs.Length; currentCommandIndex++)
{
string command = commandLineArgs[currentCommandIndex];
string commandUpper = command.ToUpper();
2015-04-08 15:20:10 -07:00
switch (commandUpper)
{
case "FORCE_SOFTWARE_RENDERING":
GL.HardwareAvailable = false;
break;
2015-04-08 15:20:10 -07:00
case "CLEAR_CACHE":
AboutWidget.DeleteCacheData(0);
2015-04-08 15:20:10 -07:00
break;
case "SHOW_MEMORY":
DesktopRootSystemWindow.ShowMemoryUsed = true;
2015-04-08 15:20:10 -07:00
break;
}
if (MeshFileIo.ValidFileExtensions().Contains(Path.GetExtension(command).ToUpper()))
{
// If we are the only instance running then do nothing.
// Else send these to the running instance so it can load them.
}
}
using (new PerformanceTimer("Startup", "MainView"))
{
this.AddChild(ApplicationController.Instance.MainView);
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AnchorAll();
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
UiThread.RunOnIdle(CheckOnPrinter);
}
public override void OnLoad(EventArgs args)
{
// Moved from OnParentChanged
if (File.Exists("RunUnitTests.txt"))
{
//DiagnosticWidget diagnosticView = new DiagnosticWidget(this);
}
// now that we are all set up lets load our plugins and allow them their chance to set things up
FindAndInstantiatePlugins();
if (ApplicationController.Instance.PluginsLoaded != null)
{
ApplicationController.Instance.PluginsLoaded.CallEvents(null, null);
}
foreach (string arg in commandLineArgs)
2015-04-08 15:20:10 -07:00
{
string argExtension = Path.GetExtension(arg).ToUpper();
if (argExtension.Length > 1
&& MeshFileIo.ValidFileExtensions().Contains(argExtension))
2015-04-08 15:20:10 -07:00
{
QueueData.Instance.AddItem(new PrintItemWrapper(new PrintItem(Path.GetFileName(arg), Path.GetFullPath(arg))));
2015-04-08 15:20:10 -07:00
}
}
2015-04-08 15:20:10 -07:00
ApplicationController.Instance.OnLoadActions();
2016-09-12 21:55:40 -07:00
//HtmlWindowTest();
IsLoading = false;
2017-03-15 16:17:06 -07:00
base.OnLoad(args);
2016-09-12 21:55:40 -07:00
}
private void CheckOnPrinter()
2015-04-08 15:20:10 -07:00
{
try
{
2017-09-17 21:08:16 -07:00
ApplicationController.Instance.ActivePrinter.Connection.OnIdle();
}
2015-04-08 15:20:10 -07:00
catch (Exception e)
{
Debug.Print(e.Message);
GuiWidget.BreakInDebugger();
#if DEBUG
throw e;
#endif
}
2015-04-08 15:20:10 -07:00
UiThread.RunOnIdle(CheckOnPrinter);
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private void FindAndInstantiatePlugins()
{
2014-01-29 19:09:30 -08:00
#if false
2015-04-08 15:20:10 -07:00
string pluginDirectory = Path.Combine("..", "..", "..", "MatterControlPlugins", "bin");
2014-01-29 19:09:30 -08:00
#if DEBUG
2015-04-08 15:20:10 -07:00
pluginDirectory = Path.Combine(pluginDirectory, "Debug");
2014-01-29 19:09:30 -08:00
#else
2015-04-08 15:20:10 -07:00
pluginDirectory = Path.Combine(pluginDirectory, "Release");
2014-01-29 19:09:30 -08:00
#endif
2015-04-08 15:20:10 -07:00
if (!Directory.Exists(pluginDirectory))
{
string dataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
2015-04-08 15:20:10 -07:00
pluginDirectory = Path.Combine(dataPath, "Plugins");
}
// TODO: this should look in a plugin folder rather than just the application directory (we probably want it in the user folder).
PluginFinder<MatterControlPlugin> pluginFinder = new PluginFinder<MatterControlPlugin>(pluginDirectory);
2014-01-29 19:09:30 -08:00
#endif
2015-04-08 15:20:10 -07:00
string oemName = ApplicationSettings.Instance.GetOEMName();
foreach (MatterControlPlugin plugin in PluginFinder.CreateInstancesOf<MatterControlPlugin>())
2015-04-08 15:20:10 -07:00
{
string pluginInfo = plugin.GetPluginInfoJSon();
Dictionary<string, string> nameValuePairs = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(pluginInfo);
2015-04-08 15:20:10 -07:00
if (nameValuePairs != null && nameValuePairs.ContainsKey("OEM"))
{
if (nameValuePairs["OEM"] == oemName)
{
plugin.Initialize(this);
}
}
else
{
plugin.Initialize(this);
}
}
}
2014-01-29 19:09:30 -08:00
2015-05-05 07:25:47 -07:00
private static void AssertDebugNotDefined()
{
#if DEBUG
throw new Exception("DEBUG is defined and should not be!");
#endif
}
public static void CheckKnownAssemblyConditionalCompSymbols()
{
MatterControlApplication.AssertDebugNotDefined();
MatterHackers.GCodeVisualizer.GCodeFile.AssertDebugNotDefined();
MatterHackers.Agg.Graphics2D.AssertDebugNotDefined();
MatterHackers.Agg.UI.SystemWindow.AssertDebugNotDefined();
MatterHackers.Agg.ImageProcessing.InvertLightness.AssertDebugNotDefined();
MatterHackers.Localizations.TranslationMap.AssertDebugNotDefined();
MatterHackers.MarchingSquares.MarchingSquaresByte.AssertDebugNotDefined();
MatterHackers.MatterControl.PluginSystem.MatterControlPlugin.AssertDebugNotDefined();
MatterHackers.MatterSlice.MatterSlice.AssertDebugNotDefined();
MatterHackers.MeshVisualizer.MeshViewerWidget.AssertDebugNotDefined();
MatterHackers.RenderOpenGl.GLMeshTrianglePlugin.AssertDebugNotDefined();
}
}
}