2014-02-15 18:06:03 -08:00
/ *
2016-04-18 11:31:31 -07:00
Copyright ( c ) 2016 , Lars Brubaker , John Lewin
2014-02-15 18:06:03 -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-02-15 18:06:03 -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-02-15 18:06:03 -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-02-15 18:06:03 -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-02-15 18:06:03 -08:00
either expressed or implied , of the FreeBSD Project .
* /
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg ;
using MatterHackers.Agg.UI ;
2014-02-04 13:30:42 -08:00
using MatterHackers.Localizations ;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.ContactForm ;
2016-02-04 09:50:00 -08:00
using MatterHackers.MatterControl.PrinterCommunication ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
2016-04-18 11:31:31 -07:00
using Newtonsoft.Json ;
2015-04-08 15:20:10 -07:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO ;
2015-11-30 16:19:32 -08:00
using System.Linq ;
2015-04-08 15:20:10 -07:00
using System.Text ;
2014-01-29 19:09:30 -08:00
2014-02-15 18:06:03 -08:00
namespace MatterHackers.MatterControl.SlicerConfiguration
2014-01-29 19:09:30 -08:00
{
2016-04-18 11:31:31 -07:00
using System.Net ;
using ConfigurationPage.PrintLeveling ;
using SettingsDictionary = Dictionary < string , string > ;
using DataStorage ;
using Agg.PlatformAbstract ;
2016-04-30 18:18:15 -07:00
using Newtonsoft.Json.Linq ;
2016-05-17 18:27:08 -07:00
using System.Collections.ObjectModel ;
2016-04-18 11:31:31 -07:00
public class SettingsProfile
2015-04-08 15:20:10 -07:00
{
private static string configFileExtension = "slice" ;
2016-04-18 11:31:31 -07:00
public RootedObjectEventHandler DoPrintLevelingChanged = new RootedObjectEventHandler ( ) ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
private LayeredProfile layeredProfile ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
public bool PrinterSelected = > layeredProfile . OemProfile . OemLayer . Keys . Count > 0 ;
internal SettingsProfile ( LayeredProfile profile )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
layeredProfile = profile ;
2015-04-08 15:20:10 -07:00
}
2016-05-16 16:21:42 -07:00
public string ID = > layeredProfile . ID ;
2016-04-18 11:31:31 -07:00
public SettingsLayer BaseLayer = > layeredProfile . BaseLayer ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
public SettingsLayer OemLayer = > layeredProfile . OemProfile . OemLayer ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
public SettingsLayer UserLayer = > layeredProfile . UserLayer ;
2015-04-08 15:20:10 -07:00
2016-05-16 16:21:42 -07:00
public string ActiveMaterialKey
{
get
{
return layeredProfile . ActiveMaterialKey ;
}
set
{
layeredProfile . ActiveMaterialKey = value ;
}
}
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
public string ActiveQualityKey
{
get
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
return layeredProfile . ActiveQualityKey ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
set
{
layeredProfile . ActiveQualityKey = value ;
}
}
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
internal void RunInTransaction ( Action < SettingsProfile > action )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
// TODO: Implement RunInTransaction
// Suspend writes
action ( this ) ;
// Commit
2015-04-08 15:20:10 -07:00
}
2016-06-08 19:02:34 -07:00
public List < SettingsLayer > MaterialLayers = > layeredProfile . MaterialLayers ;
2016-04-19 11:24:09 -07:00
2016-06-08 19:02:34 -07:00
public List < SettingsLayer > QualityLayers = > layeredProfile . QualityLayers ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
public class SettingsConverter
{
public static void LoadConfigurationSettingsFromFileAsUnsaved ( string pathAndFileName )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
try
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
if ( File . Exists ( pathAndFileName ) )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
string [ ] lines = System . IO . File . ReadAllLines ( pathAndFileName ) ;
foreach ( string line in lines )
{
//Ignore commented lines
if ( line . Trim ( ) ! = "" & & ! line . StartsWith ( "#" ) )
{
string [ ] settingLine = line . Split ( '=' ) ;
if ( settingLine . Length > 1 )
{
string keyName = settingLine [ 0 ] . Trim ( ) ;
string settingDefaultValue = settingLine [ 1 ] . Trim ( ) ;
//Add the setting to the active layer
//SaveValue(keyName, settingDefaultValue);
throw new NotImplementedException ( "load to dictionary" ) ;
}
}
}
2015-04-08 15:20:10 -07:00
}
}
2016-04-18 11:31:31 -07:00
catch ( Exception e )
{
Debug . Print ( e . Message ) ;
GuiWidget . BreakInDebugger ( ) ;
Debug . WriteLine ( string . Format ( "Error loading configuration: {0}" , e ) ) ;
}
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
}
2015-04-08 15:20:10 -07:00
2016-05-08 14:13:16 -07:00
internal void SaveChanges ( )
{
layeredProfile . Save ( ) ;
}
2016-04-27 18:57:51 -07:00
public string ExtruderTemperature ( int extruderIndex )
2016-04-18 11:31:31 -07:00
{
if ( extruderIndex > = layeredProfile . MaterialSettingsKeys . Count )
2015-04-08 15:20:10 -07:00
{
2016-05-03 10:59:37 -07:00
// MaterialSettingsKeys is empty or lacks a value for the given extruder index
//
// If extruder index zero was requested, return the layer cascade temperature value, otherwise null
return ( extruderIndex = = 0 ) ? layeredProfile . GetValue ( "temperature" ) : null ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
string materialKey = layeredProfile . MaterialSettingsKeys [ extruderIndex ] ;
2016-05-03 10:59:37 -07:00
if ( extruderIndex = = 0 & & ( string . IsNullOrEmpty ( materialKey ) | | layeredProfile . UserLayer . ContainsKey ( "temperature" ) ) )
{
// In the case where a user override exists or MaterialSettingsKeys is populated with multiple extruder
// positions but position 0 is empty and thus unassigned, use layer cascade to resolve temp
return layeredProfile . GetValue ( "temperature" ) ;
}
// Otherwise, use the SettingsLayers that is bound to this extruder
2016-04-18 11:31:31 -07:00
SettingsLayer layer = layeredProfile . GetMaterialLayer ( materialKey ) ;
2016-04-28 11:53:01 -07:00
string result = "0" ;
layer ? . TryGetValue ( "temperature" , out result ) ;
2016-04-18 11:31:31 -07:00
return result ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
internal SettingsLayer MaterialLayer ( string key )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
return layeredProfile . GetMaterialLayer ( key ) ;
2016-04-22 13:19:26 -07:00
}
2016-04-27 18:57:51 -07:00
internal SettingsLayer QualityLayer ( string key )
2016-04-22 13:19:26 -07:00
{
2016-04-18 11:31:31 -07:00
return layeredProfile . GetQualityLayer ( key ) ;
2015-04-08 15:20:10 -07:00
}
2016-05-03 18:22:59 -07:00
internal bool PublishBedImage ( )
{
return ActiveValue ( "MatterControl.PublishBedImage" ) = = "1" ;
}
2016-04-18 11:31:31 -07:00
internal void SetMaterialPreset ( int extruderIndex , string text )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
layeredProfile . SetMaterialPreset ( extruderIndex , text ) ;
2015-04-08 15:20:10 -07:00
}
2014-04-08 19:33:09 -07:00
2015-01-23 08:03:59 -08:00
public bool HasFan ( )
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "has_fan" ) = = "1" ;
2015-01-23 08:03:59 -08:00
}
2015-05-22 11:11:32 -07:00
public bool CenterOnBed ( )
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "center_part_on_bed" ) = = "1" ;
2015-05-22 11:11:32 -07:00
}
2015-04-08 15:20:10 -07:00
public bool ShowResetConnection ( )
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "show_reset_connection" ) = = "1" ;
2015-04-08 15:20:10 -07:00
}
2015-02-25 16:14:32 -08:00
2015-01-23 08:03:59 -08:00
public bool HasHardwareLeveling ( )
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "has_hardware_leveling" ) = = "1" ;
2015-01-23 08:03:59 -08:00
}
2014-06-19 11:25:28 -07:00
2015-01-23 08:03:59 -08:00
public bool HasSdCardReader ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "has_sd_card_reader" ) = = "1" ;
2015-04-08 15:20:10 -07:00
}
2014-06-19 11:25:28 -07:00
2016-04-27 18:57:51 -07:00
public double BedTemperature ( )
2015-02-16 16:16:06 -08:00
{
2016-04-27 18:57:51 -07:00
double targetTemp = 0 ;
if ( HasHeatedBed ( ) )
2015-02-16 16:16:06 -08:00
{
2016-04-27 18:57:51 -07:00
double . TryParse ( ActiveValue ( "bed_temperature" ) , out targetTemp ) ;
2015-02-16 16:16:06 -08:00
}
2016-04-27 18:57:51 -07:00
return targetTemp ;
2015-02-16 16:16:06 -08:00
}
2015-04-23 20:19:00 -07:00
/// <summary>
/// Control the PS_ON pin via M80/81 if enabled in firmware and printer settings, allowing the printer board to toggle off the ATX power supply
/// </summary>
2016-04-27 18:57:51 -07:00
public bool HasPowerControl ( )
2015-04-23 20:19:00 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "has_power_control" ) = = "1" ;
2015-04-23 20:19:00 -07:00
}
2015-04-08 15:20:10 -07:00
public bool HasHeatedBed ( )
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "has_heated_bed" ) = = "1" ;
2015-04-08 15:20:10 -07:00
}
2014-02-05 12:04:05 -08:00
2016-04-27 18:57:51 -07:00
public bool SupportEnabled ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "support_material" ) = = "1" ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public bool ShowFirmwareUpdater ( )
2016-03-07 14:11:36 -08:00
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "include_firmware_updater" ) = = "Simple Arduino" ;
2016-03-07 14:11:36 -08:00
}
2016-04-27 18:57:51 -07:00
public int SupportExtruder ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return int . Parse ( ActiveValue ( "support_material_extruder" ) ) ;
2015-04-08 15:20:10 -07:00
}
2014-02-05 12:04:05 -08:00
2016-04-27 18:57:51 -07:00
public int [ ] LayerToPauseOn ( )
2015-11-30 16:19:32 -08:00
{
2016-04-27 18:57:51 -07:00
string [ ] userValues = ActiveValue ( "layer_to_pause" ) . Split ( ';' ) ;
2015-11-30 16:19:32 -08:00
2016-04-27 18:57:51 -07:00
int temp ;
return userValues . Where ( v = > int . TryParse ( v , out temp ) ) . Select ( v = >
{
2015-12-29 12:57:24 -08:00
//Convert from 0 based index to 1 based index
int val = int . Parse ( v ) ;
// Special case for user entered zero that pushes 0 to 1, otherwise val = val - 1 for 1 based index
return val = = 0 ? 1 : val - 1 ;
2016-04-27 18:57:51 -07:00
} ) . ToArray ( ) ;
2015-11-30 16:19:32 -08:00
}
2016-04-27 18:57:51 -07:00
public double ProbePaperWidth ( )
2015-08-03 15:48:36 -07:00
{
2016-04-27 18:57:51 -07:00
return double . Parse ( ActiveValue ( "manual_probe_paper_width" ) ) ;
2015-08-03 15:48:36 -07:00
}
2016-04-27 18:57:51 -07:00
public bool RaftEnabled ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "create_raft" ) = = "1" ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public int RaftExtruder ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return int . Parse ( ActiveValue ( "raft_extruder" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public double MaxFanSpeed ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ParseDouble ( ActiveValue ( "max_fan_speed" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public double FillDensity ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
string fillDensityValueString = ActiveValue ( "fill_density" ) ;
if ( fillDensityValueString . Contains ( "%" ) )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
string onlyNumber = fillDensityValueString . Replace ( "%" , "" ) ;
double ratio = ParseDouble ( onlyNumber ) / 100 ;
return ratio ;
}
else
{
return ParseDouble ( ActiveValue ( "fill_density" ) ) ;
2015-04-08 15:20:10 -07:00
}
}
2016-04-27 18:57:51 -07:00
public double MinFanSpeed ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ParseDouble ( ActiveValue ( "min_fan_speed" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public double FirstLayerHeight ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
string firstLayerValueString = ActiveValue ( "first_layer_height" ) ;
if ( firstLayerValueString . Contains ( "%" ) )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
string onlyNumber = firstLayerValueString . Replace ( "%" , "" ) ;
double ratio = ParseDouble ( onlyNumber ) / 100 ;
return LayerHeight ( ) * ratio ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
double firstLayerValue ;
firstLayerValue = ParseDouble ( firstLayerValueString ) ;
return firstLayerValue ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
internal string MaterialPresetKey ( int extruderIndex )
2016-04-18 11:31:31 -07:00
{
return layeredProfile . GetMaterialPresetKey ( extruderIndex ) ;
}
2016-04-27 18:57:51 -07:00
public double FirstLayerExtrusionWidth ( )
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
AsPercentOfReferenceOrDirect mapper = new AsPercentOfReferenceOrDirect ( "first_layer_extrusion_width" , "notused" , "nozzle_diameter" ) ;
2015-04-08 15:20:10 -07:00
2016-03-07 14:34:56 -08:00
double firstLayerValue = ParseDouble ( mapper . Value ) ;
2015-04-08 15:20:10 -07:00
return firstLayerValue ;
}
private static double ParseDouble ( string firstLayerValueString )
{
double firstLayerValue ;
if ( ! double . TryParse ( firstLayerValueString , out firstLayerValue ) )
{
throw new Exception ( string . Format ( "Format cannot be parsed. FirstLayerHeight '{0}'" , firstLayerValueString ) ) ;
}
return firstLayerValue ;
}
2016-04-27 18:57:51 -07:00
public double LayerHeight ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ParseDouble ( ActiveValue ( "layer_height" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public Vector2 BedSize ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveVector2 ( "bed_size" ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public MeshVisualizer . MeshViewerWidget . BedShape BedShape ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
switch ( ActiveValue ( "bed_shape" ) )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
case "rectangular" :
return MeshVisualizer . MeshViewerWidget . BedShape . Rectangular ;
2015-04-08 15:20:10 -07:00
2016-04-27 18:57:51 -07:00
case "circular" :
return MeshVisualizer . MeshViewerWidget . BedShape . Circular ;
2015-04-08 15:20:10 -07:00
2016-04-27 18:57:51 -07:00
default :
2014-02-24 22:18:46 -08:00
#if DEBUG
2016-04-27 18:57:51 -07:00
throw new NotImplementedException ( string . Format ( "'{0}' is not a known bed_shape." , ActiveValue ( "bed_shape" ) ) ) ;
2014-02-24 22:18:46 -08:00
#else
return MeshVisualizer . MeshViewerWidget . BedShape . Rectangular ;
#endif
2015-04-08 15:20:10 -07:00
}
}
2016-04-27 18:57:51 -07:00
public Vector2 BedCenter ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveVector2 ( "print_center" ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public double BuildHeight ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ParseDouble ( ActiveValue ( "build_height" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public Vector2 PrintCenter ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ActiveVector2 ( "print_center" ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public int ExtruderCount ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
if ( ExtrudersShareTemperature ( ) )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return 1 ;
}
2015-04-08 15:20:10 -07:00
2016-04-27 18:57:51 -07:00
int extruderCount ;
string extruderCountString = ActiveValue ( "extruder_count" ) ;
if ( ! int . TryParse ( extruderCountString , out extruderCount ) )
{
return 1 ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
return extruderCount ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public bool ExtrudersShareTemperature ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ( int . Parse ( ActiveValue ( "extruders_share_temperature" ) ) = = 1 ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public Vector2 ExtruderOffset ( int extruderIndex )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
string currentOffsets = ActiveValue ( "extruder_offset" ) ;
2015-04-08 15:20:10 -07:00
string [ ] offsets = currentOffsets . Split ( ',' ) ;
int count = 0 ;
foreach ( string offset in offsets )
{
if ( count = = extruderIndex )
{
string [ ] xy = offset . Split ( 'x' ) ;
return new Vector2 ( double . Parse ( xy [ 0 ] ) , double . Parse ( xy [ 1 ] ) ) ;
}
count + + ;
}
return Vector2 . Zero ;
}
2016-04-27 18:57:51 -07:00
public double NozzleDiameter ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ParseDouble ( ActiveValue ( "nozzle_diameter" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public double FilamentDiameter ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
return ParseDouble ( ActiveValue ( "filament_diameter" ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public bool LevelingRequiredToPrint ( )
2016-01-16 21:33:11 -08:00
{
2016-04-27 18:57:51 -07:00
return ActiveValue ( "print_leveling_required_to_print" ) = = "1" ;
2016-01-16 21:33:11 -08:00
}
2016-04-18 11:31:31 -07:00
private PrintLevelingData printLevelingData = null ;
2016-04-28 09:41:27 -07:00
public PrintLevelingData GetPrintLevelingData ( )
2015-04-08 15:20:10 -07:00
{
2016-04-28 09:41:27 -07:00
if ( printLevelingData = = null )
2015-04-08 15:20:10 -07:00
{
2016-04-28 09:41:27 -07:00
printLevelingData = PrintLevelingData . Create (
ActiveSliceSettings . Instance ,
layeredProfile . GetValue ( "MatterControl.PrintLevelingData" ) ,
layeredProfile . GetValue ( "MatterControl.PrintLevelingProbePositions" ) ) ;
2015-04-08 15:20:10 -07:00
2016-04-28 09:41:27 -07:00
PrintLevelingPlane . Instance . SetPrintLevelingEquation (
printLevelingData . SampledPosition0 ,
printLevelingData . SampledPosition1 ,
printLevelingData . SampledPosition2 ,
ActiveSliceSettings . Instance . PrintCenter ( ) ) ;
2016-04-22 13:19:26 -07:00
}
2016-04-28 09:41:27 -07:00
return printLevelingData ;
2016-04-22 13:19:26 -07:00
}
2016-04-28 09:41:27 -07:00
public void SetPrintLevelingData ( PrintLevelingData data )
2015-04-08 15:20:10 -07:00
{
2016-04-28 09:41:27 -07:00
printLevelingData = data ;
layeredProfile . SetActiveValue ( "MatterControl.PrintLevelingData" , JsonConvert . SerializeObject ( data ) ) ;
2015-04-08 15:20:10 -07:00
2016-04-28 09:41:27 -07:00
}
public bool DoPrintLeveling ( )
{
2016-05-02 14:59:26 -07:00
return layeredProfile . GetValue ( "MatterControl.PrintLevelingEnabled" ) = = "1" ;
2016-04-28 09:41:27 -07:00
}
2016-05-02 14:59:26 -07:00
public void DoPrintLeveling ( bool doLeveling )
2016-04-28 09:41:27 -07:00
{
// Early exit if already set
2016-05-02 14:59:26 -07:00
if ( doLeveling = = this . DoPrintLeveling ( ) )
2015-04-08 15:20:10 -07:00
{
2016-04-28 09:41:27 -07:00
return ;
}
2016-04-18 11:31:31 -07:00
2016-05-02 14:59:26 -07:00
layeredProfile . SetActiveValue ( "MatterControl.PrintLevelingEnabled" , doLeveling ? "1" : "0" ) ;
2016-04-18 11:31:31 -07:00
2016-04-28 09:41:27 -07:00
DoPrintLevelingChanged . CallEvents ( this , null ) ;
2016-04-18 11:31:31 -07:00
2016-05-02 14:59:26 -07:00
if ( doLeveling )
2016-04-28 09:41:27 -07:00
{
PrintLevelingData levelingData = ActiveSliceSettings . Instance . GetPrintLevelingData ( ) ;
PrintLevelingPlane . Instance . SetPrintLevelingEquation (
levelingData . SampledPosition0 ,
levelingData . SampledPosition1 ,
levelingData . SampledPosition2 ,
ActiveSliceSettings . Instance . PrintCenter ( ) ) ;
2015-04-08 15:20:10 -07:00
}
}
2016-04-18 11:31:31 -07:00
private static readonly SlicingEngineTypes defaultEngineType = SlicingEngineTypes . MatterSlice ;
2016-04-28 09:41:27 -07:00
public SlicingEngineTypes ActiveSliceEngineType ( )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
string engineType = layeredProfile . GetValue ( "MatterControl.SlicingEngine" ) ;
if ( string . IsNullOrEmpty ( engineType ) )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
return defaultEngineType ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
var engine = ( SlicingEngineTypes ) Enum . Parse ( typeof ( SlicingEngineTypes ) , engineType ) ;
return engine ;
2016-04-28 09:41:27 -07:00
}
public void ActiveSliceEngineType ( SlicingEngineTypes type )
{
SetActiveValue ( "MatterControl.SlicingEngine" , type . ToString ( ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-27 18:57:51 -07:00
public SliceEngineMapping ActiveSliceEngine ( )
2015-04-08 15:20:10 -07:00
{
2016-04-28 09:41:27 -07:00
switch ( ActiveSliceEngineType ( ) )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
case SlicingEngineTypes . CuraEngine :
return EngineMappingCura . Instance ;
2016-04-18 11:31:31 -07:00
2016-04-27 18:57:51 -07:00
case SlicingEngineTypes . MatterSlice :
return EngineMappingsMatterSlice . Instance ;
2016-04-18 11:31:31 -07:00
2016-04-27 18:57:51 -07:00
case SlicingEngineTypes . Slic3r :
return Slic3rEngineMappings . Instance ;
2015-04-08 15:20:10 -07:00
2016-04-27 18:57:51 -07:00
default :
return null ;
2016-04-18 11:31:31 -07:00
}
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
///<summary>
///Returns the first matching value discovered while enumerating the settings layers
///</summary>
2016-04-27 18:57:51 -07:00
public string ActiveValue ( string sliceSetting )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
return layeredProfile . GetValue ( sliceSetting ) ;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2016-05-16 16:21:42 -07:00
public string GetActiveValue ( string sliceSetting , IEnumerable < SettingsLayer > layerCascade )
2015-04-08 15:20:10 -07:00
{
2016-05-16 16:21:42 -07:00
return layeredProfile . GetValue ( sliceSetting , layerCascade ) ;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
public void SetActiveValue ( string sliceSetting , string sliceValue )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
layeredProfile . SetActiveValue ( sliceSetting , sliceValue ) ;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
public void SetActiveValue ( string sliceSetting , string sliceValue , SettingsLayer persistenceLayer )
2014-10-09 12:23:31 -07:00
{
2016-04-18 11:31:31 -07:00
layeredProfile . SetActiveValue ( sliceSetting , sliceValue , persistenceLayer ) ;
2014-10-09 12:23:31 -07:00
}
2016-04-18 11:31:31 -07:00
public void ClearValue ( string sliceSetting )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
layeredProfile . ClearValue ( sliceSetting ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
public void ClearValue ( string sliceSetting , SettingsLayer persistenceLayer )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
layeredProfile . ClearValue ( sliceSetting , persistenceLayer ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
/// <summary>
/// Returns whether or not the setting is overridden by the active layer
/// </summary>
public bool SettingExistsInLayer ( string sliceSetting , NamedSettingsLayers layer )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
if ( layeredProfile = = null )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
return false ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
switch ( layer )
{
case NamedSettingsLayers . Quality :
return layeredProfile ? . QualityLayer ? . ContainsKey ( sliceSetting ) = = true ;
case NamedSettingsLayers . Material :
return layeredProfile ? . MaterialLayer ? . ContainsKey ( sliceSetting ) = = true ;
case NamedSettingsLayers . User :
return layeredProfile ? . UserLayer ? . ContainsKey ( sliceSetting ) = = true ;
default :
return false ;
2015-04-08 15:20:10 -07:00
}
}
2016-04-27 18:57:51 -07:00
public Vector2 ActiveVector2 ( string sliceSetting )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
string [ ] twoValues = ActiveValue ( sliceSetting ) . Split ( ',' ) ;
2016-04-18 11:31:31 -07:00
if ( twoValues . Length ! = 2 )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
throw new Exception ( string . Format ( "Not parsing {0} as a Vector2" , sliceSetting ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
Vector2 valueAsVector2 = new Vector2 ( ) ;
valueAsVector2 . x = ParseDouble ( twoValues [ 0 ] ) ;
valueAsVector2 . y = ParseDouble ( twoValues [ 1 ] ) ;
return valueAsVector2 ;
2015-04-08 15:20:10 -07:00
}
2016-06-09 14:58:39 -07:00
public void ExportAsMatterControlConfig ( )
2015-04-08 15:20:10 -07:00
{
2016-06-09 14:58:39 -07:00
FileDialog . SaveFileDialog (
new SaveFileDialogParams ( "MatterControl Printer Export|*.printer" , title : "Export Printer Settings" ) ,
( saveParams ) = >
{
File . WriteAllText ( saveParams . FileName , JsonConvert . SerializeObject ( layeredProfile , Formatting . Indented ) ) ;
} ) ;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2016-06-09 14:58:39 -07:00
public void ExportAsSlic3rConfig ( )
2014-10-09 12:23:31 -07:00
{
2016-06-09 14:58:39 -07:00
FileDialog . SaveFileDialog (
new SaveFileDialogParams ( "Save Slice Configuration" . Localize ( ) + "|*." + configFileExtension )
{
FileName = "default_settings.ini"
} ,
( saveParams ) = >
{
if ( ! string . IsNullOrEmpty ( saveParams . FileName ) )
{
GenerateConfigFile ( saveParams . FileName , false ) ;
}
} ) ;
}
public void ExportAsCuraConfig ( )
{
throw new NotImplementedException ( ) ;
2015-04-08 15:20:10 -07:00
}
2014-10-09 12:23:31 -07:00
2016-05-03 11:26:41 -07:00
public long GetLongHashCode ( )
2015-04-08 15:20:10 -07:00
{
2016-05-02 16:34:13 -07:00
var bigStringForHashCode = new StringBuilder ( ) ;
foreach ( var keyValue in this . BaseLayer )
2015-04-08 15:20:10 -07:00
{
2016-05-02 16:34:13 -07:00
string activeValue = ActiveValue ( keyValue . Key ) ;
bigStringForHashCode . Append ( keyValue . Key ) ;
bigStringForHashCode . Append ( activeValue ) ;
}
2016-04-18 11:31:31 -07:00
2016-05-03 11:10:35 -07:00
string value = bigStringForHashCode . ToString ( ) ;
2016-04-18 11:31:31 -07:00
2016-05-03 11:26:41 -07:00
return agg_basics . ComputeHash ( bigStringForHashCode . ToString ( ) ) ;
2014-10-09 12:23:31 -07:00
}
2016-04-25 16:49:55 -07:00
public void GenerateConfigFile ( string fileName , bool replaceMacroValues )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
using ( var outstream = new StreamWriter ( fileName ) )
2015-04-08 15:20:10 -07:00
{
2016-05-31 17:58:47 -07:00
foreach ( var key in this . KnownSettings . Where ( k = > ! k . StartsWith ( "MatterControl." ) ) )
2016-04-25 16:49:55 -07:00
{
2016-05-31 17:58:47 -07:00
string activeValue = ActiveValue ( key ) ;
2016-04-18 11:31:31 -07:00
if ( replaceMacroValues )
{
activeValue = GCodeProcessing . ReplaceMacroValues ( activeValue ) ;
}
2016-05-31 17:58:47 -07:00
outstream . Write ( string . Format ( "{0} = {1}\n" , key , activeValue ) ) ;
2016-04-25 16:49:55 -07:00
activeValue = GCodeProcessing . ReplaceMacroValues ( activeValue ) ;
}
2015-04-08 15:20:10 -07:00
}
}
public bool IsValid ( )
{
try
{
2016-04-27 18:57:51 -07:00
if ( LayerHeight ( ) > NozzleDiameter ( ) )
2015-04-08 15:20:10 -07:00
{
string error = "'Layer Height' must be less than or equal to the 'Nozzle Diameter'." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "Layer Height = {0}\nNozzle Diameter = {1}" . Localize ( ) , LayerHeight ( ) , NozzleDiameter ( ) ) ;
2015-06-09 11:30:14 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'General' -> 'Layers/Surface'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
else if ( FirstLayerHeight ( ) > NozzleDiameter ( ) )
2015-04-08 15:20:10 -07:00
{
2016-02-09 15:05:03 -08:00
string error = "'First Layer Height' must be less than or equal to the 'Nozzle Diameter'." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "First Layer Height = {0}\nNozzle Diameter = {1}" . Localize ( ) , FirstLayerHeight ( ) , NozzleDiameter ( ) ) ;
2015-06-09 11:30:14 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'General' -> 'Layers/Surface'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-02-04 09:50:00 -08:00
// If we have print leveling turned on then make sure we don't have any leveling commands in the start gcode.
2016-04-28 09:41:27 -07:00
if ( PrinterConnectionAndCommunication . Instance . ActivePrinter . DoPrintLeveling ( ) )
2016-02-04 09:50:00 -08:00
{
2016-04-27 18:57:51 -07:00
string [ ] startGCode = ActiveValue ( "start_gcode" ) . Replace ( "\\n" , "\n" ) . Split ( '\n' ) ;
2016-02-04 09:50:00 -08:00
foreach ( string startGCodeLine in startGCode )
{
if ( startGCodeLine . StartsWith ( "G29" ) )
{
string error = "Start G-Code cannot contain G29 if Print Leveling is enabled." . Localize ( ) ;
string details = "Your Start G-Code should not contain a G29 if you are planning on using print leveling. Change your start G-Code or turn off print leveling" . Localize ( ) ;
2016-03-21 14:41:32 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Printer' -> 'Custom G-Code' -> 'Start G-Code'" . Localize ( ) ;
2016-02-04 09:50:00 -08:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
if ( startGCodeLine . StartsWith ( "G30" ) )
{
string error = "Start G-Code cannot contain G30 if Print Leveling is enabled." . Localize ( ) ;
string details = "Your Start G-Code should not contain a G30 if you are planning on using print leveling. Change your start G-Code or turn off print leveling" . Localize ( ) ;
2016-03-21 14:41:32 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Printer' -> 'Custom G-Code' -> 'Start G-Code'" . Localize ( ) ;
2016-02-04 09:50:00 -08:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
}
}
2016-04-27 18:57:51 -07:00
if ( FirstLayerExtrusionWidth ( ) > NozzleDiameter ( ) * 4 )
2015-04-08 15:20:10 -07:00
{
2016-02-09 15:05:03 -08:00
string error = "'First Layer Extrusion Width' must be less than or equal to the 'Nozzle Diameter' * 4." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "First Layer Extrusion Width = {0}\nNozzle Diameter = {1}" . Localize ( ) , ActiveValue ( "first_layer_extrusion_width" ) , NozzleDiameter ( ) ) ;
2015-07-31 12:22:32 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Filament' -> 'Extrusion' -> 'First Layer'" . Localize ( ) ;
2015-06-09 11:30:14 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
if ( FirstLayerExtrusionWidth ( ) < = 0 )
2015-06-09 11:30:14 -07:00
{
2016-02-09 15:05:03 -08:00
string error = "'First Layer Extrusion Width' must be greater than 0." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "First Layer Extrusion Width = {0}" . Localize ( ) , ActiveValue ( "first_layer_extrusion_width" ) ) ;
2015-07-31 12:22:32 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Filament' -> 'Extrusion' -> 'First Layer'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
if ( MinFanSpeed ( ) > 100 )
2015-04-08 15:20:10 -07:00
{
2016-02-09 15:05:03 -08:00
string error = "The Minimum Fan Speed can only go as high as 100%." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "It is currently set to {0}." . Localize ( ) , MinFanSpeed ( ) ) ;
2016-02-09 15:05:03 -08:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Filament' -> 'Cooling'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
if ( MaxFanSpeed ( ) > 100 )
2015-04-08 15:20:10 -07:00
{
2016-02-09 15:05:03 -08:00
string error = "The Maximum Fan Speed can only go as high as 100%." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "It is currently set to {0}." . Localize ( ) , MaxFanSpeed ( ) ) ;
2016-02-09 15:05:03 -08:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Filament' -> 'Cooling'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
if ( ExtruderCount ( ) < 1 )
2015-04-08 15:20:10 -07:00
{
string error = "The Extruder Count must be at least 1." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "It is currently set to {0}." . Localize ( ) , ExtruderCount ( ) ) ;
2016-02-09 15:05:03 -08:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'Printer' -> 'Features'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
if ( FillDensity ( ) < 0 | | FillDensity ( ) > 1 )
2015-04-08 15:20:10 -07:00
{
2016-02-09 15:05:03 -08:00
string error = "The Fill Density must be between 0 and 1." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "It is currently set to {0}." . Localize ( ) , FillDensity ( ) ) ;
2015-06-09 11:30:14 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'General' -> 'Infill'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return false ;
}
2016-04-27 18:57:51 -07:00
if ( FillDensity ( ) = = 1
& & ActiveValue ( "infill_type" ) ! = "LINES" )
2015-10-20 15:39:59 -07:00
{
string error = "Solid Infill works best when set to LINES." . Localize ( ) ;
2016-04-27 18:57:51 -07:00
string details = string . Format ( "It is currently set to {0}." . Localize ( ) , ActiveValue ( "infill_type" ) ) ;
2015-10-20 15:39:59 -07:00
string location = "Location: 'Settings & Controls' -> 'Settings' -> 'General' -> 'Infill Type'" . Localize ( ) ;
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2}" , error , details , location ) , "Slice Error" . Localize ( ) ) ;
return true ;
}
2016-03-04 11:11:20 -08:00
2015-06-09 11:30:14 -07:00
string normalSpeedLocation = "Location: 'Settings & Controls' -> 'Settings' -> 'General' -> 'Speed'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
// If the given speed is part of the current slice engine then check that it is greater than 0.
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "bridge_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "external_perimeter_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "first_layer_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "gap_fill_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "infill_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "perimeter_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "small_perimeter_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "solid_infill_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "support_material_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "top_solid_infill_speed" , normalSpeedLocation ) ) return false ;
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "travel_speed" , normalSpeedLocation ) ) return false ;
2015-06-09 11:30:14 -07:00
string retractSpeedLocation = "Location: 'Settings & Controls' -> 'Settings' -> 'Filament' -> 'Filament' -> 'Retraction'" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
if ( ! ValidateGoodSpeedSettingGreaterThan0 ( "retract_speed" , retractSpeedLocation ) ) return false ;
}
catch ( Exception e )
{
2015-09-11 10:48:24 -07:00
Debug . Print ( e . Message ) ;
2015-09-17 13:45:26 -07:00
GuiWidget . BreakInDebugger ( ) ;
2015-04-08 15:20:10 -07:00
string stackTraceNoBackslashRs = e . StackTrace . Replace ( "\r" , "" ) ;
ContactFormWindow . Open ( "Parse Error while slicing" . Localize ( ) , e . Message + stackTraceNoBackslashRs ) ;
return false ;
}
return true ;
}
private bool ValidateGoodSpeedSettingGreaterThan0 ( string speedSetting , string speedLocation )
{
2016-04-27 18:57:51 -07:00
string actualSpeedValueString = ActiveValue ( speedSetting ) ;
2015-04-08 15:20:10 -07:00
string speedValueString = actualSpeedValueString ;
if ( speedValueString . EndsWith ( "%" ) )
{
speedValueString = speedValueString . Substring ( 0 , speedValueString . Length - 1 ) ;
}
bool valueWasNumber = true ;
double speedToCheck ;
if ( ! double . TryParse ( speedValueString , out speedToCheck ) )
{
valueWasNumber = false ;
}
if ( ! valueWasNumber
2016-04-27 18:57:51 -07:00
| | ( ActiveSliceSettings . Instance . ActiveSliceEngine ( ) . MapContains ( speedSetting )
2015-04-08 15:20:10 -07:00
& & speedToCheck < = 0 ) )
{
OrganizerSettingsData data = SliceSettingsOrganizer . Instance . GetSettingsData ( speedSetting ) ;
if ( data ! = null )
{
string error = string . Format ( "The '{0}' must be greater than 0." . Localize ( ) , data . PresentationName ) ;
string details = string . Format ( "It is currently set to {0}." . Localize ( ) , actualSpeedValueString ) ;
StyledMessageBox . ShowMessageBox ( null , string . Format ( "{0}\n\n{1}\n\n{2} -> '{3}'" , error , details , speedLocation , data . PresentationName ) , "Slice Error" . Localize ( ) ) ;
}
return false ;
}
return true ;
}
2016-04-18 11:31:31 -07:00
2016-04-28 09:41:27 -07:00
public bool DoAutoConnect ( )
2016-04-18 11:31:31 -07:00
{
2016-05-02 14:59:26 -07:00
return layeredProfile . GetValue ( "MatterControl.AutoConnect" ) = = "1" ;
2016-04-18 11:31:31 -07:00
}
2016-05-02 14:59:26 -07:00
public void SetAutoConnect ( bool autoConnectPrinter )
2016-04-18 11:31:31 -07:00
{
2016-05-02 14:59:26 -07:00
layeredProfile . SetActiveValue ( "MatterControl.AutoConnect" , autoConnectPrinter ? "1" : "0" ) ;
2016-04-18 11:31:31 -07:00
}
2016-04-28 09:41:27 -07:00
public string BaudRate ( )
2016-04-18 11:31:31 -07:00
{
2016-04-28 09:41:27 -07:00
return layeredProfile . GetValue ( "MatterControl.BaudRate" ) ;
2016-04-18 11:31:31 -07:00
}
2016-05-02 14:59:26 -07:00
public void SetBaudRate ( string baudRate )
2016-04-18 11:31:31 -07:00
{
2016-05-02 14:59:26 -07:00
layeredProfile . SetActiveValue ( "MatterControl.BaudRate" , baudRate ) ;
2016-04-18 11:31:31 -07:00
}
2016-04-28 09:41:27 -07:00
public string ComPort ( )
2016-04-18 11:31:31 -07:00
{
2016-05-06 18:50:38 -07:00
return layeredProfile . GetValue ( string . Format ( "MatterControl.{0}.ComPort" , Environment . MachineName ) ) ;
2016-04-18 11:31:31 -07:00
}
2016-04-28 09:41:27 -07:00
public void SetComPort ( string port )
2016-04-18 11:31:31 -07:00
{
2016-05-06 18:50:38 -07:00
layeredProfile . SetActiveValue ( string . Format ( "MatterControl.{0}.ComPort" , Environment . MachineName ) , port ) ;
2016-04-28 09:41:27 -07:00
}
2016-05-06 18:50:38 -07:00
public void SetComPort ( string port , SettingsLayer layer )
{
layeredProfile . SetActiveValue ( string . Format ( "MatterControl.{0}.ComPort" , Environment . MachineName ) , port , layer ) ;
}
2016-04-28 09:41:27 -07:00
public string SlicingEngine ( )
{
return layeredProfile . GetValue ( "MatterControl.SlicingEngine" ) ;
}
public void SetSlicingEngine ( string engine )
{
layeredProfile . SetActiveValue ( "MatterControl.SlicingEngine" , engine ) ;
}
public string DriverType ( )
{
return layeredProfile . GetValue ( "MatterControl.DriverType" ) ;
}
public void SetDriverType ( string driver )
{
layeredProfile . SetActiveValue ( "MatterControl.DriverType" , driver ) ;
}
public string DeviceToken ( )
{
return layeredProfile . GetValue ( "MatterControl.DeviceToken" ) ;
}
public void SetDeviceToken ( string token )
{
layeredProfile . SetActiveValue ( "MatterControl.DeviceToken" , token ) ;
2016-04-18 11:31:31 -07:00
}
public string DeviceType = > layeredProfile . GetValue ( "MatterControl.DeviceType" ) ;
public string Make = > layeredProfile . GetValue ( "MatterControl.Make" ) ;
// Rename to PrinterName
2016-04-28 09:41:27 -07:00
public string Name ( )
2016-04-18 11:31:31 -07:00
{
2016-04-28 09:41:27 -07:00
return layeredProfile . GetValue ( "MatterControl.PrinterName" ) ;
2016-04-18 11:31:31 -07:00
}
2016-04-28 09:41:27 -07:00
public void SetName ( string name )
2016-04-18 11:31:31 -07:00
{
2016-04-28 09:41:27 -07:00
layeredProfile . SetActiveValue ( "MatterControl.PrinterName" , name ) ;
}
2016-04-18 11:31:31 -07:00
public string Model = > layeredProfile . GetValue ( "MatterControl.Model" ) ;
2016-04-30 18:18:15 -07:00
HashSet < string > knownSettings = null ;
public HashSet < string > KnownSettings
{
get
{
if ( knownSettings = = null )
{
string propertiesJson = StaticData . Instance . ReadAllText ( Path . Combine ( "SliceSettings" , "Properties.json" ) ) ;
var settingsData = JArray . Parse ( propertiesJson ) ;
knownSettings = new HashSet < string > ( settingsData . Select ( s = > s [ "SlicerConfigName" ] . Value < string > ( ) ) ) ;
}
return knownSettings ;
}
}
2016-04-28 09:41:27 -07:00
public string ManualMovementSpeeds ( )
2016-04-18 11:31:31 -07:00
{
2016-04-28 09:41:27 -07:00
return layeredProfile . GetValue ( "MatterControl.ManualMovementSpeeds" ) ;
2016-04-18 11:31:31 -07:00
}
2016-04-28 09:41:27 -07:00
public void SetManualMovementSpeeds ( string speed )
{
layeredProfile . SetActiveValue ( "MatterControl.ManualMovementSpeeds" , speed ) ;
}
2016-04-18 11:31:31 -07:00
private List < string > printerDrivers = null ;
public List < string > PrinterDrivers ( )
{
if ( printerDrivers = = null )
{
printerDrivers = GetPrintDrivers ( ) ;
}
return printerDrivers ;
}
private List < string > GetPrintDrivers ( )
{
var drivers = new List < string > ( ) ;
//Determine what if any drivers are needed
2016-04-27 18:57:51 -07:00
string infFileNames = ActiveValue ( "windows_driver" ) ;
2016-04-18 11:31:31 -07:00
if ( ! string . IsNullOrEmpty ( infFileNames ) )
{
string [ ] fileNames = infFileNames . Split ( ',' ) ;
foreach ( string fileName in fileNames )
{
switch ( OsInformation . OperatingSystem )
{
case OSType . Windows :
string pathForInf = Path . GetFileNameWithoutExtension ( fileName ) ;
// TODO: It's really unexpected that the driver gets copied to the temp folder every time a printer is setup. I'd think this only needs
// to happen when the infinstaller is run (More specifically - move this to *after* the user clicks Install Driver)
string infPath = Path . Combine ( "Drivers" , pathForInf ) ;
string infPathAndFileToInstall = Path . Combine ( infPath , fileName ) ;
if ( StaticData . Instance . FileExists ( infPathAndFileToInstall ) )
{
// Ensure the output directory exists
string destTempPath = Path . GetFullPath ( Path . Combine ( ApplicationDataStorage . ApplicationUserDataPath , "data" , "temp" , "inf" , pathForInf ) ) ;
if ( ! Directory . Exists ( destTempPath ) )
{
Directory . CreateDirectory ( destTempPath ) ;
}
string destTempInf = Path . GetFullPath ( Path . Combine ( destTempPath , fileName ) ) ;
// Sync each file from StaticData to the location on disk for serial drivers
foreach ( string file in StaticData . Instance . GetFiles ( infPath ) )
{
using ( Stream outstream = File . OpenWrite ( Path . Combine ( destTempPath , Path . GetFileName ( file ) ) ) )
using ( Stream instream = StaticData . Instance . OpenSteam ( file ) )
{
instream . CopyTo ( outstream ) ;
}
}
drivers . Add ( destTempInf ) ;
}
break ;
default :
break ;
}
}
}
return drivers ;
}
2016-05-08 14:13:16 -07:00
public List < GCodeMacro > Macros = > layeredProfile . Macros ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
public class SettingsLayer : SettingsDictionary
{
public SettingsLayer ( ) { }
public SettingsLayer ( Dictionary < string , string > settingsDictionary )
{
2016-04-29 15:00:10 -07:00
foreach ( var keyValue in settingsDictionary )
2016-04-18 11:31:31 -07:00
{
2016-04-29 15:00:10 -07:00
this [ keyValue . Key ] = keyValue . Value ;
2016-04-18 11:31:31 -07:00
}
}
2016-05-16 16:21:42 -07:00
public string ID
{
get
{
// TODO: Short term hack to silently upgrade existing profiles with missing ID
string layerKey = ValueOrDefault ( "MatterControl.LayerID" ) ;
if ( string . IsNullOrEmpty ( layerKey ) )
{
layerKey = Guid . NewGuid ( ) . ToString ( ) ;
ID = layerKey ;
}
return layerKey ;
}
set
{
this [ "MatterControl.LayerID" ] = value ;
}
}
public string Name
{
get
{
return ValueOrDefault ( "MatterControl.LayerName" ) ;
}
set
{
this [ "MatterControl.LayerName" ] = value ;
}
}
public string Source
{
get
{
return ValueOrDefault ( "MatterControl.LayerSource" ) ;
}
set
{
this [ "MatterControl.LayerSource" ] = value ;
}
}
public string ETag
{
get
{
return ValueOrDefault ( "MatterControl.LayerETag" ) ;
}
set
{
this [ "MatterControl.LayerETag" ] = value ;
}
}
2016-04-18 11:31:31 -07:00
public string ValueOrDefault ( string key , string defaultValue = "" )
{
string foundValue ;
this . TryGetValue ( key , out foundValue ) ;
return foundValue ? ? defaultValue ;
}
public string ValueOrNull ( string key )
{
string foundValue ;
this . TryGetValue ( key , out foundValue ) ;
return foundValue ;
}
2016-04-27 17:34:33 -07:00
public static SettingsLayer LoadFromIni ( TextReader reader )
{
var layer = new SettingsLayer ( ) ;
string line ;
while ( ( line = reader . ReadLine ( ) ) ! = null )
{
var segments = line . Split ( '=' ) ;
if ( ! line . StartsWith ( "#" ) & & ! string . IsNullOrEmpty ( line ) )
{
string key = segments [ 0 ] . Trim ( ) ;
layer [ key ] = segments [ 1 ] . Trim ( ) ;
}
}
return layer ;
}
public static SettingsLayer LoadFromIni ( string filePath )
{
var settings = from line in File . ReadAllLines ( filePath )
let segments = line . Split ( '=' )
where ! line . StartsWith ( "#" ) & & ! string . IsNullOrEmpty ( line )
select new
{
Key = segments [ 0 ] . Trim ( ) ,
Value = segments [ 1 ] . Trim ( )
} ;
var layer = new SettingsLayer ( ) ;
foreach ( var setting in settings )
{
layer [ setting . Key ] = setting . Value ;
}
return layer ;
}
2016-05-16 16:21:42 -07:00
public SettingsLayer Clone ( )
{
string id = Guid . NewGuid ( ) . ToString ( ) ;
return new SettingsLayer ( this as Dictionary < string , string > )
{
ID = id ,
Name = this . Name ,
ETag = this . ETag ,
Source = this . Source
} ;
}
2016-04-18 11:31:31 -07:00
}
public class ProfileData
{
2016-05-17 18:27:08 -07:00
public ObservableCollection < PrinterInfo > Profiles { get ; set ; } = new ObservableCollection < PrinterInfo > ( ) ;
2016-04-18 11:31:31 -07:00
}
public class PrinterInfo
{
public string Make { get ; set ; } = "Unknown" ;
public string Model { get ; set ; } = "Unknown" ;
public string Name { get ; set ; }
public string ComPort { get ; set ; }
2016-05-02 14:59:26 -07:00
public bool AutoConnect { get ; set ; }
2016-04-18 11:31:31 -07:00
public string Id { get ; set ; }
public string BaudRate { get ; set ; }
public string DriverType { get ; internal set ; }
internal void Delete ( )
{
throw new NotImplementedException ( ) ;
}
}
2015-04-08 15:20:10 -07:00
}