mattercontrol/MatterControlApplication.cs

905 lines
28 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;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Gaming.Game;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
2015-04-08 15:20:10 -07:00
using MatterHackers.Agg.PlatformAbstract;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
2016-09-23 16:34:52 -07:00
using MatterHackers.GuiAutomation;
using MatterHackers.Localizations;
2014-01-29 19:09:30 -08:00
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PluginSystem;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.PrintQueue;
2014-05-06 18:25:16 -07:00
using MatterHackers.MatterControl.SettingsManagement;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.PolygonMesh.Processors;
using MatterHackers.RenderOpenGl.OpenGl;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath;
2015-05-20 13:37:08 -07:00
using Mindscape.Raygun4Net;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
2016-09-23 16:34:52 -07:00
public class MatterControlApplication : SystemWindow
2015-04-08 15:20:10 -07:00
{
#if DEBUG
//public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206";
public static string MCWSBaseUri { get; } = "https://mattercontrol-test.appspot.com";
#else
public static string MCWSBaseUri { get; } = "https://mattercontrol.appspot.com";
#endif
2016-11-11 17:06:12 -08:00
public static bool CameraInUseByExternalProcess { get; set; } = false;
public bool RestartOnClose = false;
2015-04-08 15:20:10 -07:00
private static readonly Vector2 minSize = new Vector2(600, 600);
private static MatterControlApplication instance;
private string[] commandLineArgs = null;
2015-04-08 15:20:10 -07:00
private string confirmExit = "Confirm Exit".Localize();
private bool DoCGCollectEveryDraw = false;
private int drawCount = 0;
private AverageMillisecondTimer millisecondTimer = new AverageMillisecondTimer();
private DataViewGraph msGraph;
2015-04-08 15:20:10 -07:00
private string savePartsSheetExitAnywayMessage = "You are currently saving a parts sheet, are you sure you want to exit?".Localize();
private bool ShowMemoryUsed = false;
public void ConfigureWifi()
{
}
2015-04-08 15:20:10 -07:00
private Stopwatch totalDrawTime = new Stopwatch();
2015-06-05 14:42:52 -07:00
#if true//!DEBUG
2015-06-05 18:31:48 -07:00
static RaygunClient _raygunClient = GetCorrectClient();
#endif
2015-05-20 13:37:08 -07:00
2015-06-05 14:42:52 -07:00
static RaygunClient GetCorrectClient()
{
if (OsInformation.OperatingSystem == OSType.Mac)
{
return new RaygunClient("qmMBpKy3OSTJj83+tkO7BQ=="); // this is the Mac key
}
else
{
return new RaygunClient("hQIlyUUZRGPyXVXbI6l1dA=="); // this is the PC key
}
}
public static bool IsLoading { get; private set; } = true;
2015-04-08 15:20:10 -07:00
static MatterControlApplication()
{
if (OsInformation.OperatingSystem == OSType.Mac && StaticData.Instance == 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 (StaticData.Instance == null) // it may already be initialized by tests
{
StaticData.Instance = new MatterHackers.Agg.FileSystemStaticData();
}
}
private MatterControlApplication(double width, double height)
: base(width, height)
{
ApplicationSettings.Instance.set("HardwareHasCamera", "false");
Name = "MatterControl";
// 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;
bool forceSofwareRendering = false;
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":
forceSofwareRendering = true;
GL.ForceSoftwareRendering();
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":
ShowMemoryUsed = true;
break;
case "DO_GC_COLLECT_EVERY_DRAW":
ShowMemoryUsed = true;
DoCGCollectEveryDraw = true;
break;
2016-04-18 11:31:31 -07:00
//case "CREATE_AND_SELECT_PRINTER":
// if (currentCommandIndex + 1 <= commandLineArgs.Length)
// {
// currentCommandIndex++;
// string argument = commandLineArgs[currentCommandIndex];
// string[] printerData = argument.Split(',');
// if (printerData.Length >= 2)
// {
// Printer ActivePrinter = new Printer();
// ActivePrinter.Name = "Auto: {0} {1}".FormatWith(printerData[0], printerData[1]);
// ActivePrinter.Make = printerData[0];
// ActivePrinter.Model = printerData[1];
// if (printerData.Length == 3)
// {
// ActivePrinter.ComPort = printerData[2];
// }
// PrinterSetupStatus test = new PrinterSetupStatus(ActivePrinter);
// test.LoadSettingsFromConfigFile(ActivePrinter.Make, ActivePrinter.Model);
// ActiveSliceSettings.Instance = ActivePrinter;
// }
// }
// break;
2015-04-08 15:20:10 -07:00
case "CONNECT_TO_PRINTER":
if (currentCommandIndex + 1 <= commandLineArgs.Length)
{
PrinterConnectionAndCommunication.Instance.ConnectToActivePrinter();
}
break;
case "START_PRINT":
if (currentCommandIndex + 1 <= commandLineArgs.Length)
{
bool hasBeenRun = false;
currentCommandIndex++;
string fullPath = commandLineArgs[currentCommandIndex];
QueueData.Instance.RemoveAll();
if (!string.IsNullOrEmpty(fullPath))
{
string fileName = Path.GetFileNameWithoutExtension(fullPath);
QueueData.Instance.AddItem(new PrintItemWrapper(new PrintItem(fileName, fullPath)));
PrinterConnectionAndCommunication.Instance.CommunicationStateChanged.RegisterEvent((sender, e) =>
{
if (!hasBeenRun && PrinterConnectionAndCommunication.Instance.CommunicationState == PrinterConnectionAndCommunication.CommunicationStates.Connected)
{
hasBeenRun = true;
PrinterConnectionAndCommunication.Instance.PrintActivePartIfPossible();
}
}, ref unregisterEvent);
}
}
break;
case "SLICE_AND_EXPORT_GCODE":
if (currentCommandIndex + 1 <= commandLineArgs.Length)
{
currentCommandIndex++;
string fullPath = commandLineArgs[currentCommandIndex];
QueueData.Instance.RemoveAll();
if (!string.IsNullOrEmpty(fullPath))
{
string fileName = Path.GetFileNameWithoutExtension(fullPath);
PrintItemWrapper printItemWrapper = new PrintItemWrapper(new PrintItem(fileName, fullPath));
QueueData.Instance.AddItem(printItemWrapper);
SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper);
ExportPrintItemWindow exportForTest = new ExportPrintItemWindow(printItemWrapper);
exportForTest.ExportGcodeCommandLineUtility(fileName);
}
}
break;
2015-04-08 15:20:10 -07:00
}
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.
}
}
//WriteTestGCodeFile();
#if !DEBUG
2015-04-08 15:20:10 -07:00
if (File.Exists("RunUnitTests.txt"))
#endif
2015-04-08 15:20:10 -07:00
{
#if IS_WINDOWS_FORMS
2015-04-08 15:20:10 -07:00
if (!Clipboard.IsInitialized)
{
Clipboard.SetSystemClipboard(new WindowsFormsClipboard());
}
#endif
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
// you can turn this on to debug some bounds issues
//GuiWidget.DebugBoundsUnderMouse = true;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
GuiWidget.DefaultEnforceIntegerBounds = true;
2014-01-29 19:09:30 -08:00
2016-04-29 07:45:15 -07:00
if (UserSettings.Instance.DisplayMode == ApplicationDisplayType.Touchscreen)
2015-04-08 15:20:10 -07:00
{
GuiWidget.DeviceScale = 1.3;
2015-04-08 15:20:10 -07:00
}
//GuiWidget.DeviceScale = 2;
using (new PerformanceTimer("Startup", "MainView"))
{
this.AddChild(ApplicationController.Instance.MainView);
}
2015-04-08 15:20:10 -07:00
this.MinimumSize = minSize;
this.Padding = new BorderDouble(0); //To be re-enabled once native borders are turned off
2014-01-29 19:09:30 -08:00
#if false // this is to test freeing gcodefile memory
2015-04-08 15:20:10 -07:00
Button test = new Button("test");
test.Click += (sender, e) =>
{
//MatterHackers.GCodeVisualizer.GCodeFile gcode = new GCodeVisualizer.GCodeFile();
//gcode.Load(@"C:\Users\lbrubaker\Downloads\drive assy.gcode");
SystemWindow window = new SystemWindow(100, 100);
window.ShowAsSystemWindow();
};
allControls.AddChild(test);
2014-01-29 19:09:30 -08:00
#endif
2015-04-08 15:20:10 -07:00
this.AnchorAll();
2014-01-29 19:09:30 -08:00
if (!forceSofwareRendering)
{
UseOpenGL = true;
}
string version = "1.6";
2014-05-22 16:55:28 -07:00
Title = "MatterHackers: MatterControl {0}".FormatWith(version);
2015-04-08 15:20:10 -07:00
if (OemSettings.Instance.WindowTitleExtra != null && OemSettings.Instance.WindowTitleExtra.Trim().Length > 0)
{
Title = Title + " - {1}".FormatWith(version, OemSettings.Instance.WindowTitleExtra);
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
UiThread.RunOnIdle(CheckOnPrinter);
2014-01-29 19:09:30 -08:00
string desktopPosition = ApplicationSettings.Instance.get(ApplicationSettingsKey.DesktopPosition);
2015-04-08 15:20:10 -07:00
if (desktopPosition != null && desktopPosition != "")
{
string[] sizes = desktopPosition.Split(',');
2015-04-08 15:20:10 -07:00
//If the desktop position is less than -10,-10, override
int xpos = Math.Max(int.Parse(sizes[0]), -10);
int ypos = Math.Max(int.Parse(sizes[1]), -10);
DesktopPosition = new Point2D(xpos, ypos);
}
}
2014-01-29 19:09:30 -08:00
public void TakePhoto(string imageFileName)
{
ImageBuffer noCameraImage = new ImageBuffer(640, 480);
Graphics2D graphics = noCameraImage.NewGraphics2D();
graphics.Clear(RGBA_Bytes.White);
graphics.DrawString("No Camera Detected", 320, 240, pointSize: 24, justification: Agg.Font.Justification.Center);
graphics.DrawString(DateTime.Now.ToString(), 320, 200, pointSize: 12, justification: Agg.Font.Justification.Center);
ImageIO.SaveImageData(imageFileName, noCameraImage);
PictureTaken?.Invoke(null, null);
}
bool dropWasOnChild = true;
public override void OnDragEnter(FileDropEventArgs fileDropEventArgs)
{
base.OnDragEnter(fileDropEventArgs);
if (!fileDropEventArgs.AcceptDrop)
{
// no child has accepted the drop
foreach (string file in fileDropEventArgs.DroppedFiles)
{
string extension = Path.GetExtension(file).ToUpper();
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension))
|| extension == ".GCODE"
|| extension == ".ZIP")
{
fileDropEventArgs.AcceptDrop = true;
}
}
dropWasOnChild = false;
}
else
{
dropWasOnChild = true;
}
}
public override void OnDragOver(FileDropEventArgs fileDropEventArgs)
{
base.OnDragOver(fileDropEventArgs);
if (!fileDropEventArgs.AcceptDrop)
{
// no child has accepted the drop
foreach (string file in fileDropEventArgs.DroppedFiles)
{
string extension = Path.GetExtension(file).ToUpper();
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension))
|| extension == ".GCODE"
|| extension == ".ZIP")
{
fileDropEventArgs.AcceptDrop = true;
}
}
dropWasOnChild = false;
}
else
{
dropWasOnChild = true;
}
}
public override void OnDragDrop(FileDropEventArgs fileDropEventArgs)
{
base.OnDragDrop(fileDropEventArgs);
if (!dropWasOnChild)
{
QueueDataWidget.DoAddFiles(fileDropEventArgs.DroppedFiles);
}
}
public enum ReportSeverity2 { Warning, Error }
public void ReportException(Exception e, string key = "", string value = "", ReportSeverity2 warningLevel = ReportSeverity2.Warning)
{
// Conditionally spin up error reporting if not on the Stable channel
string channel = UserSettings.Instance.get(UserSettingsKey.UpdateFeedType);
if (string.IsNullOrEmpty(channel) || channel != "release" || OemSettings.Instance.WindowTitleExtra == "Experimental")
{
#if !DEBUG
_raygunClient.Send(e);
#endif
}
}
2015-04-08 15:20:10 -07:00
private event EventHandler unregisterEvent;
public static MatterControlApplication Instance
{
get
{
if (instance == null)
{
instance = CreateInstance();
instance.ShowAsSystemWindow();
2015-04-08 15:20:10 -07:00
}
return instance;
}
}
public event EventHandler PictureTaken;
public static MatterControlApplication CreateInstance(int overrideWidth = -1, int overrideHeight = -1)
{
int width = 0;
int height = 0;
// check if the app has a size alread set
string windowSize = ApplicationSettings.Instance.get(ApplicationSettingsKey.WindowSize);
if (windowSize != null && windowSize != "")
{
// try and open our window matching the last size that we had for it.
string[] sizes = windowSize.Split(',');
width = Math.Max(int.Parse(sizes[0]), (int)minSize.x + 1);
height = Math.Max(int.Parse(sizes[1]), (int)minSize.y + 1);
}
else // try to set it to a big size or the min size
{
Point2D desktopSize = OsInformation.DesktopSize;
if (overrideWidth != -1)
{
width = overrideWidth;
}
else // try to set it to a good size
{
if (width < desktopSize.x)
{
width = 1280;
}
}
if (overrideHeight != -1)
{
height = overrideHeight;
}
else
{
if (height < desktopSize.y)
{
height = 720;
}
}
}
using (new PerformanceTimer("Startup", "Total"))
{
instance = new MatterControlApplication(width, height);
if (instance.DesktopPosition == new Point2D())
{
Point2D desktopSize = OsInformation.DesktopSize;
// Now try and center the window. If this is saved it will got overridden
instance.DesktopPosition = new Point2D((desktopSize.x - instance.Width) / 2, (desktopSize.y - instance.Height) / 2);
}
}
return instance;
}
2015-04-08 15:20:10 -07:00
[STAThread]
public static void Main()
{
PerformanceTimer.GetParentWindowFunction = () => { return MatterControlApplication.instance; };
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
2015-05-20 12:24:26 -07:00
// Make sure we have the right working directory as we assume everything relative to the executable.
2015-04-08 15:20:10 -07:00
Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
Datastore.Instance.Initialize();
2015-05-20 12:24:26 -07:00
#if !DEBUG
2015-05-22 13:00:57 -07:00
// Conditionally spin up error reporting if not on the Stable channel
string channel = UserSettings.Instance.get(UserSettingsKey.UpdateFeedType);
2015-05-20 13:37:08 -07:00
if (string.IsNullOrEmpty(channel) || channel != "release" || OemSettings.Instance.WindowTitleExtra == "Experimental")
2015-05-20 12:24:26 -07:00
#endif
{
2015-05-20 13:37:08 -07:00
System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
2015-05-20 12:24:26 -07:00
}
2015-04-08 15:20:10 -07:00
MatterControlApplication app = MatterControlApplication.Instance;
}
2015-05-20 13:37:08 -07:00
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
#if !DEBUG
_raygunClient.Send(e.Exception);
#endif
2015-05-20 13:37:08 -07:00
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
#if !DEBUG
_raygunClient.Send(e.ExceptionObject as Exception);
#endif
2015-05-20 13:37:08 -07:00
}
2015-04-08 15:20:10 -07:00
public static void WriteTestGCodeFile()
{
using (StreamWriter file = new StreamWriter("PerformanceTest.gcode"))
{
//int loops = 150000;
int loops = 150;
int steps = 200;
double radius = 50;
Vector2 center = new Vector2(150, 100);
file.WriteLine("G28 ; home all axes");
file.WriteLine("G90 ; use absolute coordinates");
file.WriteLine("G21 ; set units to millimeters");
file.WriteLine("G92 E0");
file.WriteLine("G1 F7800");
file.WriteLine("G1 Z" + (5).ToString());
WriteMove(file, center);
for (int loop = 0; loop < loops; loop++)
{
for (int step = 0; step < steps; step++)
{
Vector2 nextPosition = new Vector2(radius, 0);
nextPosition.Rotate(MathHelper.Tau / steps * step);
WriteMove(file, center + nextPosition);
}
}
file.WriteLine("M84 ; disable motors");
}
2015-04-08 15:20:10 -07:00
}
public void LaunchBrowser(string targetUri)
{
UiThread.RunOnIdle(() =>
2015-04-08 15:20:10 -07:00
{
System.Diagnostics.Process.Start(targetUri);
});
}
public override void OnClosed(EventArgs e)
{
UserSettings.Instance.Fields.StartCountDurringExit = UserSettings.Instance.Fields.StartCount;
TerminalWindow.CloseIfOpen();
if (PrinterConnectionAndCommunication.Instance.CommunicationState != PrinterConnectionAndCommunication.CommunicationStates.PrintingFromSd)
{
PrinterConnectionAndCommunication.Instance.Disable();
}
2015-04-08 15:20:10 -07:00
//Close connection to the local datastore
PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
SlicingQueue.Instance.ShutDownSlicingThread();
ApplicationController.Instance.OnApplicationClosed();
Datastore.Instance.Exit();
2015-04-08 15:20:10 -07:00
if (RestartOnClose)
{
string appPathAndFile = System.Reflection.Assembly.GetExecutingAssembly().Location;
string pathToAppFolder = Path.GetDirectoryName(appPathAndFile);
ProcessStartInfo runAppLauncherStartInfo = new ProcessStartInfo();
runAppLauncherStartInfo.Arguments = "\"{0}\" \"{1}\"".FormatWith(appPathAndFile, 1000);
runAppLauncherStartInfo.FileName = Path.Combine(pathToAppFolder, "Launcher.exe");
runAppLauncherStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
runAppLauncherStartInfo.CreateNoWindow = true;
Process.Start(runAppLauncherStartInfo);
}
base.OnClosed(e);
}
public override void OnClosing(out bool CancelClose)
{
// save the last size of the window so we can restore it next time.
ApplicationSettings.Instance.set(ApplicationSettingsKey.WindowSize, string.Format("{0},{1}", Width, Height));
ApplicationSettings.Instance.set(ApplicationSettingsKey.DesktopPosition, string.Format("{0},{1}", DesktopPosition.x, DesktopPosition.y));
2015-04-08 15:20:10 -07:00
//Save a snapshot of the prints in queue
QueueData.Instance.SaveDefaultQueue();
if (PrinterConnectionAndCommunication.Instance.PrinterIsPrinting)
{
2016-10-20 09:55:12 -07:00
if (PrinterConnectionAndCommunication.Instance.CommunicationState != PrinterConnectionAndCommunication.CommunicationStates.PrintingFromSd)
{
// Needed as we can't assign to CancelClose inside of the lambda below
bool continueWithShutdown = false;
2016-10-20 09:55:12 -07:00
StyledMessageBox.ShowMessageBox(
(shutdownConfirmed) => continueWithShutdown = shutdownConfirmed,
"Are you sure you want to abort the current print and close MatterControl?".Localize(),
"Abort Print".Localize(),
StyledMessageBox.MessageType.YES_NO);
2016-10-20 09:55:12 -07:00
if (continueWithShutdown)
{
PrinterConnectionAndCommunication.Instance.Disable();
2016-10-20 09:55:12 -07:00
this.Close();
CancelClose = false;
}
else
{
// It's safe to cancel an active print because PrinterConnectionAndCommunication.Disable will be called
// when MatterControlApplication.OnClosed is invoked
CancelClose = true;
}
}
2016-10-20 09:55:12 -07:00
else
{
bool continueWithShutdown = false;
2016-10-20 09:55:12 -07:00
StyledMessageBox.ShowMessageBox(
(shutdownConfirmed) => continueWithShutdown = shutdownConfirmed,
"Are you sure you want exit while a print is running from SD Card?\n\nNote: If you exit, it is recommended you wait until the print is completed before running MatterControl again.".Localize(),
"Exit while printing".Localize(),
StyledMessageBox.MessageType.YES_NO);
if (continueWithShutdown)
{
PrinterConnectionAndCommunication.Instance.Disable();
this.Close();
CancelClose = false;
}
else
{
// It's safe to cancel an active print because PrinterConnectionAndCommunication.Disable will be called
// when MatterControlApplication.OnClosed is invoked
CancelClose = true;
}
}
2015-04-08 15:20:10 -07:00
}
else if (PartsSheet.IsSaving())
{
StyledMessageBox.ShowMessageBox(onConfirmExit, savePartsSheetExitAnywayMessage, confirmExit, StyledMessageBox.MessageType.YES_NO);
CancelClose = true;
}
else
{
base.OnClosing(out CancelClose);
}
}
public override void OnDraw(Graphics2D graphics2D)
2015-04-08 15:20:10 -07:00
{
totalDrawTime.Restart();
GuiWidget.DrawCount = 0;
using (new PerformanceTimer("Draw Timer", "MC Draw"))
{
base.OnDraw(graphics2D);
}
2015-04-08 15:20:10 -07:00
totalDrawTime.Stop();
millisecondTimer.Update((int)totalDrawTime.ElapsedMilliseconds);
if (ShowMemoryUsed)
{
long memory = GC.GetTotalMemory(false);
2015-09-14 09:57:48 -07:00
this.Title = "Allocated = {0:n0} : {1:000}ms, d{2} Size = {3}x{4}, onIdle = {5:00}:{6:00}, widgetsDrawn = {7}".FormatWith(memory, millisecondTimer.GetAverage(), drawCount++, this.Width, this.Height, UiThread.CountExpired, UiThread.Count, GuiWidget.DrawCount);
2015-04-08 15:20:10 -07:00
if (DoCGCollectEveryDraw)
{
GC.Collect();
}
}
//msGraph.AddData("ms", totalDrawTime.ElapsedMilliseconds);
//msGraph.Draw(MatterHackers.Agg.Transform.Affine.NewIdentity(), graphics2D);
}
public override void OnLoad(EventArgs args)
{
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
TerminalWindow.ShowIfLeftOpen();
ApplicationController.Instance.OnLoadActions();
2016-09-12 21:55:40 -07:00
//HtmlWindowTest();
IsLoading = false;
}
private static void HtmlWindowTest()
{
try
2016-02-02 17:49:19 -08:00
{
2016-09-12 21:55:40 -07:00
SystemWindow htmlTestWindow = new SystemWindow(640, 480);
string htmlContent = "";
if (true)
{
string releaseNotesFile = Path.Combine("C:\\Users\\lbrubaker\\Downloads", "test1.html");
htmlContent = File.ReadAllText(releaseNotesFile);
}
else
{
WebClient webClient = new WebClient();
htmlContent = webClient.DownloadString("http://www.matterhackers.com/s/store?q=pla");
}
HtmlWidget content = new HtmlWidget(htmlContent, RGBA_Bytes.Black);
2016-02-02 17:49:19 -08:00
content.AddChild(new GuiWidget(HAnchor.AbsolutePosition, VAnchor.ParentBottomTop));
content.VAnchor |= VAnchor.ParentTop;
content.BackgroundColor = RGBA_Bytes.White;
2016-09-12 21:55:40 -07:00
htmlTestWindow.AddChild(content);
htmlTestWindow.BackgroundColor = RGBA_Bytes.Cyan;
2016-02-02 17:49:19 -08:00
UiThread.RunOnIdle((state) =>
{
2016-09-12 21:55:40 -07:00
htmlTestWindow.ShowAsSystemWindow();
2016-02-02 17:49:19 -08:00
}, 1);
}
2016-09-12 21:55:40 -07:00
catch
{
int stop = 1;
}
2015-04-08 15:20:10 -07:00
}
public override void OnMouseMove(MouseEventArgs mouseEvent)
2015-04-08 15:20:10 -07:00
{
if (GuiWidget.DebugBoundsUnderMouse)
{
Invalidate();
}
base.OnMouseMove(mouseEvent);
}
public override void OnParentChanged(EventArgs e)
{
if (File.Exists("RunUnitTests.txt"))
{
//DiagnosticWidget diagnosticView = new DiagnosticWidget(this);
}
base.OnParentChanged(e);
// 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);
}
2015-04-08 15:20:10 -07:00
}
public void OpenCameraPreview()
{
//Camera launcher placeholder (KP)
if (ApplicationSettings.Instance.get(ApplicationSettingsKey.HardwareHasCamera) == "true")
2015-04-08 15:20:10 -07:00
{
//Do something
}
else
{
//Do something else (like show warning message)
}
}
public void PlaySound(string fileName)
{
if (OsInformation.OperatingSystem == OSType.Windows)
{
using (var mediaStream = StaticData.Instance.OpenSteam(Path.Combine("Sounds", fileName)))
{
(new System.Media.SoundPlayer(mediaStream)).Play();
}
}
}
private static void WriteMove(StreamWriter file, Vector2 center)
{
file.WriteLine("G1 X" + center.x.ToString() + " Y" + center.y.ToString());
}
private void CheckOnPrinter()
2015-04-08 15:20:10 -07:00
{
try
{
PrinterConnectionAndCommunication.Instance.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
#else
PluginFinder<MatterControlPlugin> pluginFinder = new PluginFinder<MatterControlPlugin>();
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.Plugins)
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-04-08 15:20:10 -07:00
private void onConfirmExit(bool messageBoxResponse)
{
bool CancelClose;
if (messageBoxResponse)
{
base.OnClosing(out CancelClose);
}
}
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
}
2016-05-13 11:37:22 -07:00
bool showNamesUnderMouse = false;
public override void OnKeyDown(KeyEventArgs keyEvent)
{
if (keyEvent.KeyCode == Keys.F1)
2016-05-13 11:37:22 -07:00
{
showNamesUnderMouse = !showNamesUnderMouse;
}
base.OnKeyDown(keyEvent);
}
2015-05-05 07:25:47 -07:00
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();
}
public bool IsNetworkConnected()
{
return true;
}
}
2015-04-08 15:20:10 -07:00
}