2016-06-21 11:23:26 -07:00
/ *
Copyright ( c ) 2016 , Lars Brubaker
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
1. Redistributions of source code must retain the above copyright notice , this
list of conditions and the following disclaimer .
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
and / or other materials provided with the distribution .
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
of the authors and should not be interpreted as representing official policies ,
either expressed or implied , of the FreeBSD Project .
* /
using MatterHackers.Agg ;
2015-04-08 15:20:10 -07:00
using System ;
2014-02-15 18:06:03 -08:00
using System.Collections.Generic ;
using System.Text ;
namespace MatterHackers.MatterControl.SlicerConfiguration
{
2015-04-08 15:20:10 -07:00
public static class GCodeProcessing
{
2016-06-21 11:23:26 -07:00
private static MappedSetting [ ] replaceWithSettingsStrings = new MappedSetting [ ]
2014-12-10 12:07:06 -08:00
{
2015-04-14 15:27:11 -07:00
// Have a mapping so that MatterSlice while always use a setting that can be set. (the user cannot set first_layer_bedTemperature in MatterSlice)
2016-06-21 11:23:26 -07:00
new AsPercentOfReferenceOrDirect ( "first_layer_speed" , "first_layer_speed" , "infill_speed" , 60 ) ,
new AsPercentOfReferenceOrDirect ( "external_perimeter_speed" , "external_perimeter_speed" , "perimeter_speed" , 60 ) ,
new AsPercentOfReferenceOrDirect ( "raft_print_speed" , "raft_print_speed" , "infill_speed" , 60 ) ,
2016-08-11 16:09:45 -07:00
new MappedSetting ( SettingsKey . bed_remove_part_temperature , SettingsKey . bed_remove_part_temperature ) ,
2016-06-21 11:23:26 -07:00
new MappedSetting ( "bridge_fan_speed" , "bridge_fan_speed" ) ,
new MappedSetting ( "bridge_speed" , "bridge_speed" ) ,
new MappedSetting ( "extruder_wipe_temperature" , "extruder_wipe_temperature" ) ,
2016-07-12 17:46:48 -07:00
new MappedSetting ( SettingsKey . filament_diameter , SettingsKey . filament_diameter ) ,
2016-06-21 11:23:26 -07:00
new MappedSetting ( "first_layer_bed_temperature" , SettingsKey . bed_temperature ) ,
new MappedSetting ( "first_layer_temperature" , "temperature" ) ,
new MappedSetting ( "max_fan_speed" , "max_fan_speed" ) ,
new MappedSetting ( "min_fan_speed" , "min_fan_speed" ) ,
new MappedSetting ( "retract_length" , "retract_length" ) ,
new MappedSetting ( "temperature" , "temperature" ) ,
new MappedSetting ( "z_offset" , "z_offset" ) ,
new MappedSetting ( SettingsKey . bed_temperature , SettingsKey . bed_temperature ) ,
new ScaledSingleNumber ( "infill_speed" , "infill_speed" , 60 ) ,
new ScaledSingleNumber ( "min_print_speed" , "min_print_speed" , 60 ) ,
new ScaledSingleNumber ( "perimeter_speed" , "perimeter_speed" , 60 ) ,
new ScaledSingleNumber ( "retract_speed" , "retract_speed" , 60 ) ,
new ScaledSingleNumber ( "support_material_speed" , "support_material_speed" , 60 ) ,
new ScaledSingleNumber ( "travel_speed" , "travel_speed" , 60 ) ,
2015-04-14 15:27:11 -07:00
} ;
2014-12-10 12:07:06 -08:00
2015-04-08 15:20:10 -07:00
public static string ReplaceMacroValues ( string gcodeWithMacros )
{
2016-06-21 11:23:26 -07:00
foreach ( MappedSetting mappedSetting in replaceWithSettingsStrings )
2015-04-08 15:20:10 -07:00
{
// do the replacement with {} (curly brackets)
{
2016-06-21 11:23:26 -07:00
string thingToReplace = "{" + "{0}" . FormatWith ( mappedSetting . CanonicalSettingsName ) + "}" ;
gcodeWithMacros = gcodeWithMacros . Replace ( thingToReplace , mappedSetting . Value ) ;
2015-04-08 15:20:10 -07:00
}
// do the replacement with [] (square brackets) Slic3r uses only square brackets
{
2016-06-21 11:23:26 -07:00
string thingToReplace = "[" + "{0}" . FormatWith ( mappedSetting . CanonicalSettingsName ) + "]" ;
gcodeWithMacros = gcodeWithMacros . Replace ( thingToReplace , mappedSetting . Value ) ;
2015-04-08 15:20:10 -07:00
}
}
2014-02-15 18:06:03 -08:00
2015-04-08 15:20:10 -07:00
return gcodeWithMacros ;
}
}
2016-03-07 14:34:56 -08:00
public class MappedSetting
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
public MappedSetting ( string canonicalSettingsName , string exportedName )
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
this . CanonicalSettingsName = canonicalSettingsName ;
this . ExportedName = exportedName ;
2015-04-08 15:20:10 -07:00
}
2016-03-07 14:34:56 -08:00
public double ParseDouble ( string textValue , double valueOnError = 0 )
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
double value ;
if ( ! double . TryParse ( textValue , out value ) )
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
MatterControlApplication . BreakInDebugger ( "Slicing value is not a double." ) ;
return valueOnError ;
2015-04-08 15:20:10 -07:00
}
2014-05-09 11:28:55 -07:00
2015-04-08 15:20:10 -07:00
return value ;
}
2014-05-09 11:28:55 -07:00
2016-03-07 14:34:56 -08:00
public double ParseDoubleFromRawValue ( string canonicalSettingsName , double valueOnError = 0 )
2015-04-08 15:20:10 -07:00
{
2016-06-15 17:18:39 -07:00
return ParseDouble ( ActiveSliceSettings . Instance . GetValue ( canonicalSettingsName ) , valueOnError ) ;
2015-04-08 15:20:10 -07:00
}
2014-05-09 11:28:55 -07:00
2016-03-07 14:34:56 -08:00
public string ExportedName { get ; }
2014-02-15 18:06:03 -08:00
2016-03-07 14:34:56 -08:00
public string CanonicalSettingsName { get ; }
2014-02-15 18:06:03 -08:00
2016-06-15 17:18:39 -07:00
public virtual string Value = > ActiveSliceSettings . Instance . GetValue ( CanonicalSettingsName ) ;
2015-04-08 15:20:10 -07:00
}
2014-03-20 12:16:04 -07:00
2016-03-07 14:34:56 -08:00
public class MapFirstValue : MappedSetting
2015-06-11 09:23:29 -07:00
{
2016-03-07 14:34:56 -08:00
public MapFirstValue ( string canonicalSettingsName , string exportedName )
: base ( canonicalSettingsName , exportedName )
2015-06-11 09:23:29 -07:00
{
}
2016-03-07 14:34:56 -08:00
public override string Value = > base . Value . Contains ( "," ) ? base . Value . Split ( ',' ) [ 0 ] : base . Value ;
2015-06-11 09:23:29 -07:00
}
2016-03-12 11:58:47 -08:00
2016-03-13 16:04:24 -07:00
// Replaces escaped newline characters with unescaped newline characters
public class UnescapeNewlineCharacters : MappedSetting
2016-03-12 11:58:47 -08:00
{
2016-03-13 16:04:24 -07:00
public UnescapeNewlineCharacters ( string canonicalSettingsName , string exportedName )
2016-03-12 11:58:47 -08:00
: base ( canonicalSettingsName , exportedName )
{
}
2016-03-13 16:04:24 -07:00
public override string Value = > base . Value . Replace ( "\\n" , "\n" ) ;
2016-03-12 11:58:47 -08:00
}
2016-03-07 14:34:56 -08:00
/// <summary>
/// Setting will appear in the editor, but it will not be passed to the slicing engine.
/// These values are used in other parts of MatterControl, not slicing, but are held in the slicing data.
/// </summary>
/// <seealso cref="MatterHackers.MatterControl.SlicerConfiguration.MappedSetting" />
public class VisibleButNotMappedToEngine : MappedSetting
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
public VisibleButNotMappedToEngine ( string canonicalSettingsName )
: base ( canonicalSettingsName , "" )
2015-04-08 15:20:10 -07:00
{
}
2014-05-11 06:28:39 -07:00
2016-03-07 14:34:56 -08:00
public override string Value = > null ;
2015-04-08 15:20:10 -07:00
}
2014-05-07 15:03:43 -07:00
2015-04-08 15:20:10 -07:00
public class MapStartGCode : InjectGCodeCommands
{
2016-03-13 16:04:24 -07:00
private bool escapeNewlineCharacters ;
2016-03-07 14:34:56 -08:00
2016-03-13 16:04:24 -07:00
public MapStartGCode ( string canonicalSettingsName , string exportedName , bool escapeNewlineCharacters )
2016-03-07 14:34:56 -08:00
: base ( canonicalSettingsName , exportedName )
{
2016-03-13 16:04:24 -07:00
this . escapeNewlineCharacters = escapeNewlineCharacters ;
2016-03-07 14:34:56 -08:00
}
2015-04-08 15:20:10 -07:00
2016-03-07 14:34:56 -08:00
public override string Value
2015-04-08 15:20:10 -07:00
{
get
{
StringBuilder newStartGCode = new StringBuilder ( ) ;
foreach ( string line in PreStartGCode ( SlicingQueue . extrudersUsed ) )
{
newStartGCode . Append ( line + "\n" ) ;
}
2016-03-07 14:34:56 -08:00
newStartGCode . Append ( GCodeProcessing . ReplaceMacroValues ( base . Value ) ) ;
2015-04-08 15:20:10 -07:00
foreach ( string line in PostStartGCode ( SlicingQueue . extrudersUsed ) )
{
newStartGCode . Append ( "\n" ) ;
newStartGCode . Append ( line ) ;
}
2016-03-13 16:04:24 -07:00
if ( escapeNewlineCharacters )
2015-04-08 15:20:10 -07:00
{
return newStartGCode . ToString ( ) . Replace ( "\n" , "\\n" ) ;
}
return newStartGCode . ToString ( ) ;
}
}
public List < string > PreStartGCode ( List < bool > extrudersUsed )
{
2016-06-15 17:18:39 -07:00
string startGCode = ActiveSliceSettings . Instance . GetValue ( "start_gcode" ) ;
2015-04-08 15:20:10 -07:00
string [ ] preStartGCodeLines = startGCode . Split ( new string [ ] { "\\n" } , StringSplitOptions . RemoveEmptyEntries ) ;
List < string > preStartGCode = new List < string > ( ) ;
preStartGCode . Add ( "; automatic settings before start_gcode" ) ;
AddDefaultIfNotPresent ( preStartGCode , "G21" , preStartGCodeLines , "set units to millimeters" ) ;
AddDefaultIfNotPresent ( preStartGCode , "M107" , preStartGCodeLines , "fan off" ) ;
2016-06-16 10:31:18 -07:00
double bed_temperature = ActiveSliceSettings . Instance . GetValue < double > ( SettingsKey . bed_temperature ) ;
2015-04-08 15:20:10 -07:00
if ( bed_temperature > 0 )
{
string setBedTempString = string . Format ( "M190 S{0}" , bed_temperature ) ;
AddDefaultIfNotPresent ( preStartGCode , setBedTempString , preStartGCodeLines , "wait for bed temperature to be reached" ) ;
}
2016-06-16 10:31:18 -07:00
int numberOfHeatedExtruders = ActiveSliceSettings . Instance . GetValue < int > ( SettingsKey . extruder_count ) ;
2014-10-13 16:32:54 -07:00
2015-02-19 10:56:54 -08:00
// Start heating all the extruder that we are going to use.
2015-06-17 15:43:37 -07:00
for ( int extruderIndex0Based = 0 ; extruderIndex0Based < numberOfHeatedExtruders ; extruderIndex0Based + + )
2015-04-08 15:20:10 -07:00
{
2015-06-17 15:43:37 -07:00
if ( extrudersUsed . Count > extruderIndex0Based
& & extrudersUsed [ extruderIndex0Based ] )
2015-04-08 15:20:10 -07:00
{
2016-07-18 14:15:37 -07:00
string materialTemperature = ActiveSliceSettings . Instance . Helpers . ExtruderTemperature ( extruderIndex0Based ) ;
2016-04-18 11:31:31 -07:00
if ( ! string . IsNullOrEmpty ( materialTemperature ) & & materialTemperature ! = "0" )
2015-04-08 15:20:10 -07:00
{
2015-06-17 15:43:37 -07:00
string setTempString = "M104 T{0} S{1}" . FormatWith ( extruderIndex0Based , materialTemperature ) ;
AddDefaultIfNotPresent ( preStartGCode , setTempString , preStartGCodeLines , string . Format ( "start heating extruder {0}" , extruderIndex0Based + 1 ) ) ;
2015-04-08 15:20:10 -07:00
}
}
}
2014-10-13 17:00:25 -07:00
2015-02-19 10:56:54 -08:00
// If we need to wait for the heaters to heat up before homing then set them to M109 (heat and wait).
2016-06-15 17:18:39 -07:00
if ( ActiveSliceSettings . Instance . GetValue ( "heat_extruder_before_homing" ) = = "1" )
2015-02-19 10:56:54 -08:00
{
2015-06-17 15:43:37 -07:00
for ( int extruderIndex0Based = 0 ; extruderIndex0Based < numberOfHeatedExtruders ; extruderIndex0Based + + )
2015-02-19 10:56:54 -08:00
{
2015-06-17 15:43:37 -07:00
if ( extrudersUsed . Count > extruderIndex0Based
& & extrudersUsed [ extruderIndex0Based ] )
2015-02-19 10:56:54 -08:00
{
2016-07-18 14:15:37 -07:00
string materialTemperature = ActiveSliceSettings . Instance . Helpers . ExtruderTemperature ( extruderIndex0Based ) ;
2016-04-18 11:31:31 -07:00
if ( ! string . IsNullOrEmpty ( materialTemperature ) & & materialTemperature ! = "0" )
2015-02-19 10:56:54 -08:00
{
2015-06-17 15:43:37 -07:00
string setTempString = "M109 T{0} S{1}" . FormatWith ( extruderIndex0Based , materialTemperature ) ;
2015-09-21 15:27:32 -07:00
AddDefaultIfNotPresent ( preStartGCode , setTempString , preStartGCodeLines , string . Format ( "wait for extruder {0}" , extruderIndex0Based + 1 ) ) ;
2015-02-19 10:56:54 -08:00
}
}
}
}
2015-04-08 15:20:10 -07:00
SwitchToFirstActiveExtruder ( extrudersUsed , preStartGCodeLines , preStartGCode ) ;
preStartGCode . Add ( "; settings from start_gcode" ) ;
2014-05-07 15:03:43 -07:00
2015-04-08 15:20:10 -07:00
return preStartGCode ;
}
2014-05-07 15:03:43 -07:00
2015-04-08 15:20:10 -07:00
private void SwitchToFirstActiveExtruder ( List < bool > extrudersUsed , string [ ] preStartGCodeLines , List < string > preStartGCode )
{
// make sure we are on the first active extruder
for ( int extruderIndex = 0 ; extruderIndex < extrudersUsed . Count ; extruderIndex + + )
{
if ( extrudersUsed [ extruderIndex ] )
{
// set the active extruder to the first one that will be printing
AddDefaultIfNotPresent ( preStartGCode , "T{0}" . FormatWith ( extruderIndex ) , preStartGCodeLines , "set the active extruder to {0}" . FormatWith ( extruderIndex ) ) ;
break ; // then break so we don't set it to a different ones
}
}
}
public List < string > PostStartGCode ( List < bool > extrudersUsed )
{
2016-06-15 17:18:39 -07:00
string startGCode = ActiveSliceSettings . Instance . GetValue ( "start_gcode" ) ;
2015-04-08 15:20:10 -07:00
string [ ] postStartGCodeLines = startGCode . Split ( new string [ ] { "\\n" } , StringSplitOptions . RemoveEmptyEntries ) ;
2014-05-07 15:03:43 -07:00
2015-04-08 15:20:10 -07:00
List < string > postStartGCode = new List < string > ( ) ;
postStartGCode . Add ( "; automatic settings after start_gcode" ) ;
2014-10-14 16:23:41 -07:00
2016-06-16 10:31:18 -07:00
int numberOfHeatedExtruders = ActiveSliceSettings . Instance . GetValue < int > ( SettingsKey . extruder_count ) ;
2014-10-13 17:00:25 -07:00
2016-03-07 14:34:56 -08:00
// don't set the extruders to heating if we already waited for them to reach temp
2016-06-15 17:18:39 -07:00
if ( ActiveSliceSettings . Instance . GetValue ( "heat_extruder_before_homing" ) ! = "1" )
2015-02-19 10:56:54 -08:00
{
2015-06-17 15:43:37 -07:00
for ( int extruderIndex0Based = 0 ; extruderIndex0Based < numberOfHeatedExtruders ; extruderIndex0Based + + )
2015-02-19 10:56:54 -08:00
{
2015-06-17 15:43:37 -07:00
if ( extrudersUsed . Count > extruderIndex0Based
& & extrudersUsed [ extruderIndex0Based ] )
2015-02-19 10:56:54 -08:00
{
2016-07-18 14:15:37 -07:00
string materialTemperature = ActiveSliceSettings . Instance . Helpers . ExtruderTemperature ( extruderIndex0Based ) ;
2016-04-18 11:31:31 -07:00
if ( ! string . IsNullOrEmpty ( materialTemperature ) & & materialTemperature ! = "0" )
2015-02-19 10:56:54 -08:00
{
2015-06-17 15:43:37 -07:00
string setTempString = "M109 T{0} S{1}" . FormatWith ( extruderIndex0Based , materialTemperature ) ;
2015-09-21 15:27:32 -07:00
AddDefaultIfNotPresent ( postStartGCode , setTempString , postStartGCodeLines , string . Format ( "wait for extruder {0} to reach temperature" , extruderIndex0Based + 1 ) ) ;
2015-02-19 10:56:54 -08:00
}
}
}
}
2014-10-13 16:32:54 -07:00
2015-04-08 15:20:10 -07:00
SwitchToFirstActiveExtruder ( extrudersUsed , postStartGCodeLines , postStartGCode ) ;
AddDefaultIfNotPresent ( postStartGCode , "G90" , postStartGCodeLines , "use absolute coordinates" ) ;
postStartGCode . Add ( string . Format ( "{0} ; {1}" , "G92 E0" , "reset the expected extruder position" ) ) ;
AddDefaultIfNotPresent ( postStartGCode , "M82" , postStartGCodeLines , "use absolute distance for extrusion" ) ;
2014-05-07 15:03:43 -07:00
2015-04-08 15:20:10 -07:00
return postStartGCode ;
}
}
2014-05-07 15:03:43 -07:00
2016-03-07 14:34:56 -08:00
public class MappedToBoolString : MappedSetting
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
public MappedToBoolString ( string canonicalSettingsName , string exportedName ) : base ( canonicalSettingsName , exportedName )
2015-04-08 15:20:10 -07:00
{
}
2014-03-24 12:21:21 -07:00
2016-03-07 14:34:56 -08:00
public override string Value = > ( base . Value = = "1" ) ? "True" : "False" ;
2015-04-08 15:20:10 -07:00
}
2014-03-24 12:21:21 -07:00
2015-06-11 09:23:29 -07:00
public class ScaledSingleNumber : MapFirstValue
2015-04-08 15:20:10 -07:00
{
internal double scale ;
2016-03-07 14:34:56 -08:00
internal ScaledSingleNumber ( string matterControlName , string exportedName , double scale = 1 ) : base ( matterControlName , exportedName )
{
this . scale = scale ;
}
public override string Value
2015-04-08 15:20:10 -07:00
{
get
{
double ratio = 0 ;
2016-03-07 14:34:56 -08:00
if ( base . Value . Contains ( "%" ) )
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
string withoutPercent = base . Value . Replace ( "%" , "" ) ;
ratio = ParseDouble ( withoutPercent ) / 100.0 ;
2015-04-08 15:20:10 -07:00
}
else
{
2016-03-07 14:34:56 -08:00
ratio = ParseDouble ( base . Value ) ;
2015-04-08 15:20:10 -07:00
}
return ( ratio * scale ) . ToString ( ) ;
}
}
}
2016-03-13 16:04:24 -07:00
public class InjectGCodeCommands : UnescapeNewlineCharacters
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
public InjectGCodeCommands ( string canonicalSettingsName , string exportedName )
: base ( canonicalSettingsName , exportedName )
2015-04-08 15:20:10 -07:00
{
}
protected void AddDefaultIfNotPresent ( List < string > linesAdded , string commandToAdd , string [ ] linesToCheckIfAlreadyPresent , string comment )
{
string command = commandToAdd . Split ( ' ' ) [ 0 ] . Trim ( ) ;
bool foundCommand = false ;
foreach ( string line in linesToCheckIfAlreadyPresent )
{
if ( line . StartsWith ( command ) )
{
foundCommand = true ;
break ;
}
}
if ( ! foundCommand )
{
linesAdded . Add ( string . Format ( "{0} ; {1}" , commandToAdd , comment ) ) ;
}
}
}
2016-03-07 14:34:56 -08:00
public class AsCountOrDistance : MappedSetting
2015-04-24 13:09:25 -07:00
{
2015-05-06 15:01:53 -07:00
private string keyToUseAsDenominatorForCount ;
2016-03-07 14:34:56 -08:00
public AsCountOrDistance ( string canonicalSettingsName , string exportedName , string keyToUseAsDenominatorForCount )
: base ( canonicalSettingsName , exportedName )
2015-04-24 13:09:25 -07:00
{
2015-05-01 18:44:43 -07:00
this . keyToUseAsDenominatorForCount = keyToUseAsDenominatorForCount ;
2015-04-24 13:09:25 -07:00
}
2016-03-07 14:34:56 -08:00
public override string Value
2015-04-24 13:09:25 -07:00
{
get
{
2016-03-07 14:34:56 -08:00
if ( base . Value . Contains ( "mm" ) )
2015-04-24 13:09:25 -07:00
{
2016-03-07 14:34:56 -08:00
string withoutMm = base . Value . Replace ( "mm" , "" ) ;
2016-06-15 17:18:39 -07:00
string distanceString = ActiveSliceSettings . Instance . GetValue ( keyToUseAsDenominatorForCount ) ;
2016-03-07 14:34:56 -08:00
double denominator = ParseDouble ( distanceString , 1 ) ;
int layers = ( int ) ( ParseDouble ( withoutMm ) / denominator + . 5 ) ;
2015-04-24 13:09:25 -07:00
return layers . ToString ( ) ;
}
2016-03-07 14:34:56 -08:00
return base . Value ;
2015-04-24 13:09:25 -07:00
}
}
}
2016-03-07 14:34:56 -08:00
public class AsPercentOfReferenceOrDirect : MappedSetting
2015-04-08 15:20:10 -07:00
{
2015-06-09 11:30:14 -07:00
string originalReference ;
double scale ;
2015-04-08 15:20:10 -07:00
2016-03-07 14:34:56 -08:00
public AsPercentOfReferenceOrDirect ( string canonicalSettingsName , string exportedName , string originalReference , double scale = 1 )
: base ( canonicalSettingsName , exportedName )
{
this . scale = scale ;
this . originalReference = originalReference ;
}
public override string Value
2015-04-08 15:20:10 -07:00
{
get
{
2015-06-09 11:30:14 -07:00
double finalValue = 0 ;
2016-03-07 14:34:56 -08:00
if ( base . Value . Contains ( "%" ) )
2015-04-08 15:20:10 -07:00
{
2016-03-07 14:34:56 -08:00
string withoutPercent = base . Value . Replace ( "%" , "" ) ;
double ratio = ParseDouble ( withoutPercent ) / 100.0 ;
2016-06-15 17:18:39 -07:00
string originalReferenceString = ActiveSliceSettings . Instance . GetValue ( originalReference ) ;
2016-03-07 14:34:56 -08:00
double valueToModify = ParseDouble ( originalReferenceString ) ;
2015-06-09 11:30:14 -07:00
finalValue = valueToModify * ratio ;
}
else
{
2016-03-07 14:34:56 -08:00
finalValue = ParseDouble ( base . Value ) ;
2015-04-08 15:20:10 -07:00
}
2015-06-09 11:30:14 -07:00
if ( finalValue = = 0 )
{
2016-06-15 17:18:39 -07:00
finalValue = ParseDouble ( ActiveSliceSettings . Instance . GetValue ( originalReference ) ) ;
2015-06-09 11:30:14 -07:00
}
finalValue * = scale ;
return finalValue . ToString ( ) ;
2015-04-08 15:20:10 -07:00
}
}
}
}