More generic dependencies to break Part view coupling to Printer

This commit is contained in:
John Lewin 2017-09-15 16:49:21 -07:00
parent fee3871cfb
commit 8f9953ef12
9 changed files with 164 additions and 166 deletions

View file

@ -34,9 +34,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.OpenGlGui;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.MatterControl;
@ -45,7 +43,6 @@ using MatterHackers.PolygonMesh;
using MatterHackers.RenderOpenGl;
using MatterHackers.RenderOpenGl.OpenGl;
using MatterHackers.VectorMath;
using Newtonsoft.Json;
namespace MatterHackers.MeshVisualizer
{
@ -79,13 +76,13 @@ namespace MatterHackers.MeshVisualizer
private InteractionLayer interactionLayer;
private PrinterConfig printerConfig;
private BedConfig sceneContext;
public MeshViewerWidget(PrinterConfig printerConfig, InteractionLayer interactionLayer, string startingTextMessage = "", EditorType editorType = EditorType.Part)
public MeshViewerWidget(BedConfig sceneContext, InteractionLayer interactionLayer, string startingTextMessage = "", EditorType editorType = EditorType.Part)
{
this.EditorMode = editorType;
this.scene = printerConfig.Bed.Scene;
this.printerConfig = printerConfig;
this.scene = sceneContext.Scene;
this.sceneContext = sceneContext;
var activePrintItem = ApplicationController.Instance.ActivePrintItem;
@ -163,56 +160,6 @@ namespace MatterHackers.MeshVisualizer
};
}
UiThread.RunOnIdle(() =>
{
Task.Run(() =>
{
try
{
string url = printerConfig.Settings.GetValue("PrinterShapeUrl");
string extension = printerConfig.Settings.GetValue("PrinterShapeExtension");
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(extension))
{
return;
}
using (var stream = ApplicationController.Instance.LoadHttpAsset(url))
{
var mesh = MeshFileIo.Load(stream, extension, CancellationToken.None).Mesh;
UiThread.RunOnIdle(() =>
{
printerShape = mesh;
this.Invalidate();
Task.Run(() =>
{
BspNode bspTree = null;
// if there is a chached bsp tree load it
var meshHashCode = mesh.GetLongHashCode();
string cachePath = ApplicationController.CacheablePath("MeshBspData", $"{meshHashCode}.bsp");
if (File.Exists(cachePath))
{
JsonConvert.DeserializeObject<BspNode>(File.ReadAllText(cachePath));
}
else
{
// else calculate it
bspTree = FaceBspTree.Create(mesh, 20, true);
// and save it
File.WriteAllText(cachePath, JsonConvert.SerializeObject(bspTree));
}
// set the mesh to use the new tree
UiThread.RunOnIdle(() => mesh.FaceBspTree = bspTree);
});
});
}
}
catch { }
});
});
base.OnLoad(args);
}
@ -509,8 +456,6 @@ namespace MatterHackers.MeshVisualizer
}
}
private Mesh printerShape;
public enum EditorType { Printer, Part }
public EditorType EditorMode { get; set; } = EditorType.Part;
@ -597,16 +542,16 @@ namespace MatterHackers.MeshVisualizer
{
bedColor = new RGBA_Bytes(this.BedColor, this.BedColor.alpha / 4);
}
GLHelper.Render(printerConfig.Bed.Mesh, bedColor, RenderTypes.Shaded, World.ModelviewMatrix);
if (printerShape != null)
GLHelper.Render(sceneContext.Mesh, bedColor, RenderTypes.Shaded, World.ModelviewMatrix);
if (sceneContext.PrinterShape != null)
{
GLHelper.Render(printerShape, bedColor, RenderTypes.Shaded, World.ModelviewMatrix);
GLHelper.Render(sceneContext.PrinterShape, bedColor, RenderTypes.Shaded, World.ModelviewMatrix);
}
}
if (printerConfig.Bed.BuildVolumeMesh != null && RenderBuildVolume)
if (sceneContext.BuildVolumeMesh != null && RenderBuildVolume)
{
GLHelper.Render(printerConfig.Bed.BuildVolumeMesh, this.BuildVolumeColor, RenderTypes.Shaded, World.ModelviewMatrix);
GLHelper.Render(sceneContext.BuildVolumeMesh, this.BuildVolumeColor, RenderTypes.Shaded, World.ModelviewMatrix);
}
}
else
@ -626,7 +571,6 @@ namespace MatterHackers.MeshVisualizer
GL.Vertex3(width, i, 0);
GL.Vertex3(-width, i, 0);
}
GL.Color4(255, 0, 0, 255);

View file

@ -120,13 +120,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
private ThemeConfig theme;
private PrinterConfig printer;
public Vector3 BedCenter
{
get
{
return new Vector3(printer.Bed.BedCenter);
return new Vector3(sceneContext.BedCenter);
}
}
@ -138,15 +136,15 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
internal ViewGcodeBasic gcodeViewer;
public InteractionLayer InteractionLayer { get; }
PrinterConnection printerConnection;
public View3DWidget(PrinterConnection printerConnection, PrintItemWrapper printItemWrapper, PrinterConfig printer, AutoRotate autoRotate, ViewControls3D viewControls3D, ThemeConfig theme, OpenMode openMode = OpenMode.Viewing, MeshViewerWidget.EditorType editorType = MeshViewerWidget.EditorType.Part)
private BedConfig sceneContext;
public View3DWidget(PrintItemWrapper printItemWrapper, BedConfig sceneContext, AutoRotate autoRotate, ViewControls3D viewControls3D, ThemeConfig theme, OpenMode openMode = OpenMode.Viewing, MeshViewerWidget.EditorType editorType = MeshViewerWidget.EditorType.Part)
{
this.printerConnection = printerConnection;
var smallMarginButtonFactory = theme.SmallMarginButtonFactory;
this.printer = printer;
this.Scene = this.printer.Bed.Scene;
this.sceneContext = sceneContext;
this.Scene = sceneContext.Scene;
this.TrackballTumbleWidget = new TrackballTumbleWidget(ApplicationController.Instance.Printer.Bed.World)
{
@ -179,12 +177,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
};
// MeshViewer
meshViewerWidget = new MeshViewerWidget(printer, this.InteractionLayer, editorType: editorType);
meshViewerWidget = new MeshViewerWidget(sceneContext, this.InteractionLayer, editorType: editorType);
meshViewerWidget.AnchorAll();
this.InteractionLayer.AddChild(meshViewerWidget);
// The slice layers view
gcodeViewer = new ViewGcodeBasic(printer, viewControls3D);
gcodeViewer = new ViewGcodeBasic(sceneContext, viewControls3D);
gcodeViewer.AnchorAll();
gcodeViewer.Visible = false;
this.InteractionLayer.AddChild(gcodeViewer);
@ -486,17 +484,39 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
UiThread.RunOnIdle(AutoSpin);
if (printer.Bed.RendererOptions.SyncToPrint)
if (sceneContext.RendererOptions.SyncToPrint)
{
printerConnection.CommunicationStateChanged.RegisterEvent(SetEditControlsBasedOnPrinterState, ref unregisterEvents);
// make sure we lock the controls if we are printing or paused
switch (printerConnection.CommunicationState)
if (sceneContext.Printer != null)
{
case CommunicationStates.Printing:
case CommunicationStates.Paused:
LockEditControls();
break;
sceneContext.Printer.Connection.CommunicationStateChanged.RegisterEvent(
(s, e) =>
{
if (sceneContext.RendererOptions.SyncToPrint
&& sceneContext.Printer != null)
{
switch (sceneContext.Printer.Connection.CommunicationState)
{
case CommunicationStates.Printing:
case CommunicationStates.Paused:
LockEditControls();
break;
default:
UnlockEditControls();
break;
}
}
},
ref unregisterEvents);
// make sure we lock the controls if we are printing or paused
switch (sceneContext.Printer.Connection.CommunicationState)
{
case CommunicationStates.Printing:
case CommunicationStates.Paused:
LockEditControls();
break;
}
}
}
@ -624,12 +644,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
// This shows the BVH as rects around the scene items
//Scene?.TraceData().RenderBvhRecursive(0, 3);
if (gcodeViewer?.loadedGCode == null || printer.Bed.GCodeRenderer == null || !gcodeViewer.Visible)
if (sceneContext.LoadedGCode == null || sceneContext.GCodeRenderer == null || !gcodeViewer.Visible)
{
return;
}
printer.Bed.Render3DLayerFeatures(e);
sceneContext.Render3DLayerFeatures(e);
}
public override void OnKeyDown(KeyEventArgs keyEvent)
@ -1515,7 +1535,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
world.Reset();
world.Scale = .03;
world.Translate(-new Vector3(printer.Bed.BedCenter));
world.Translate(-new Vector3(sceneContext.BedCenter));
world.Rotate(Quaternion.FromEulerAngles(new Vector3(0, 0, MathHelper.Tau / 16)));
world.Rotate(Quaternion.FromEulerAngles(new Vector3(-MathHelper.Tau * .19, 0, 0)));
}
@ -2243,9 +2263,9 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
private void meshViewerWidget_LoadDone(object sender, EventArgs e)
{
if (printer.Bed.RendererOptions.SyncToPrint)
if (sceneContext.RendererOptions.SyncToPrint)
{
switch (printerConnection.CommunicationState)
switch (sceneContext.Printer?.Connection.CommunicationState)
{
case CommunicationStates.Printing:
case CommunicationStates.Paused:
@ -2350,24 +2370,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
this.saveAsWindow = null;
}
private void SetEditControlsBasedOnPrinterState(object sender, EventArgs e)
{
if (printer.Bed.RendererOptions.SyncToPrint)
{
switch (printerConnection.CommunicationState)
{
case CommunicationStates.Printing:
case CommunicationStates.Paused:
LockEditControls();
break;
default:
UnlockEditControls();
break;
}
}
}
public Vector2 DragSelectionStartPosition { get; private set; }
public bool DragSelectionInProgress { get; private set; }
public Vector2 DragSelectionEndPosition { get; private set; }