2014-04-15 18:13:27 -07:00
/ *
2017-06-05 21:57:45 -07:00
Copyright ( c ) 2017 , Kevin Pope , John Lewin
2014-04-15 18:13:27 -07: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-04-15 18:13:27 -07: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-04-15 18:13:27 -07: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-04-15 18:13:27 -07: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-04-15 18:13:27 -07:00
either expressed or implied , of the FreeBSD Project .
* /
2017-06-05 21:57:45 -07:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
2014-04-15 18:13:27 -07:00
using MatterHackers.Agg ;
2017-08-20 02:34:39 -07:00
using MatterHackers.Agg.Platform ;
2014-04-15 18:13:27 -07:00
using MatterHackers.Agg.UI ;
2017-03-15 16:17:06 -07:00
using MatterHackers.DataConverters3D ;
2015-04-08 15:20:10 -07:00
using MatterHackers.Localizations ;
2014-04-15 18:13:27 -07:00
using MatterHackers.MatterControl.DataStorage ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2018-07-31 15:56:46 -07:00
using MatterHackers.PolygonMesh.Processors ;
2014-04-15 18:13:27 -07:00
namespace MatterHackers.MatterControl.PrintQueue
{
2017-03-15 16:17:06 -07:00
public class ItemChangedArgs : EventArgs
2015-04-08 15:20:10 -07:00
{
2017-03-15 16:17:06 -07:00
public int Index { get ; private set ; }
2015-04-08 15:20:10 -07:00
2017-03-15 16:17:06 -07:00
internal ItemChangedArgs ( int index )
2015-04-08 15:20:10 -07:00
{
2017-03-15 16:17:06 -07:00
this . Index = index ;
2015-04-08 15:20:10 -07:00
}
}
public class QueueData
{
public static readonly string SdCardFileName = "SD_CARD" ;
private List < PrintItemWrapper > printItems = new List < PrintItemWrapper > ( ) ;
public List < PrintItemWrapper > PrintItems
{
get { return printItems ; }
}
public RootedObjectEventHandler ItemAdded = new RootedObjectEventHandler ( ) ;
public RootedObjectEventHandler ItemRemoved = new RootedObjectEventHandler ( ) ;
private static QueueData instance ;
public static QueueData Instance
{
get
{
if ( instance = = null )
{
instance = new QueueData ( ) ;
instance . LoadDefaultQueue ( ) ;
}
return instance ;
}
}
public void RemoveAt ( int index )
{
2016-12-03 19:44:39 -08:00
if ( index > = 0 & & index < ItemCount )
2015-04-08 15:20:10 -07:00
{
2017-09-15 18:45:21 -07:00
PrintItems . RemoveAt ( index ) ;
2015-04-08 15:20:10 -07:00
2017-09-15 18:45:21 -07:00
OnItemRemoved ( new ItemChangedArgs ( index ) ) ;
SaveDefaultQueue ( ) ;
2015-04-08 15:20:10 -07:00
}
}
public void OnItemRemoved ( EventArgs e )
{
ItemRemoved . CallEvents ( this , e ) ;
}
public PrintItemWrapper GetPrintItemWrapper ( int index )
{
if ( index > = 0 & & index < PrintItems . Count )
{
return PrintItems [ index ] ;
}
return null ;
}
public int GetIndex ( PrintItemWrapper printItem )
{
return PrintItems . IndexOf ( printItem ) ;
}
public string [ ] GetItemNames ( )
{
2016-12-01 15:14:53 -08:00
List < string > itemNames = new List < string > ( ) ;
2015-04-08 15:20:10 -07:00
for ( int i = 0 ; i < PrintItems . Count ; i + + )
{
itemNames . Add ( PrintItems [ i ] . Name ) ;
}
return itemNames . ToArray ( ) ;
}
2015-07-27 19:03:54 -07:00
public string GetItemName ( int itemIndex )
{
return PrintItems [ itemIndex ] . Name ;
}
2016-10-06 14:31:52 -07:00
public List < PrintItem > CreateReadOnlyPartList ( bool includeProtectedItems )
2015-04-08 15:20:10 -07:00
{
List < PrintItem > listToReturn = new List < PrintItem > ( ) ;
2016-12-03 19:44:39 -08:00
for ( int i = 0 ; i < ItemCount ; i + + )
2015-04-08 15:20:10 -07:00
{
2016-10-06 14:31:52 -07:00
var printItem = GetPrintItemWrapper ( i ) . PrintItem ;
2018-07-31 15:56:46 -07:00
if ( includeProtectedItems
2016-10-06 14:31:52 -07:00
| | ! printItem . Protected )
{
listToReturn . Add ( printItem ) ;
}
2015-04-08 15:20:10 -07:00
}
return listToReturn ;
}
2017-06-20 07:24:17 -07:00
private static readonly bool Is32Bit = IntPtr . Size = = 4 ;
2015-04-08 15:20:10 -07:00
private PrintItemWrapper partUnderConsideration = null ;
2014-04-15 18:13:27 -07:00
2015-01-10 16:18:08 -08:00
public enum ValidateSizeOn32BitSystems { Required , Skip }
2015-04-08 15:20:10 -07:00
2015-07-20 16:09:52 -07:00
public void AddItem ( PrintItemWrapper item , int indexToInsert = - 1 , ValidateSizeOn32BitSystems checkSize = ValidateSizeOn32BitSystems . Required )
2015-04-08 15:20:10 -07:00
{
2017-06-20 07:24:17 -07:00
if ( Is32Bit )
2015-01-10 16:18:08 -08:00
{
2015-04-08 15:20:10 -07:00
// Check if the part we are adding is BIG. If it is warn the user and
2015-01-10 16:18:08 -08:00
// possibly don't add it
2015-01-14 11:51:53 -08:00
bool warnAboutFileSize = false ;
2015-02-18 08:10:33 -08:00
long estimatedMemoryUse = 0 ;
2015-02-18 12:20:58 -08:00
if ( File . Exists ( item . FileLocation )
& & checkSize = = ValidateSizeOn32BitSystems . Required )
2015-01-10 16:18:08 -08:00
{
2018-07-31 15:56:46 -07:00
estimatedMemoryUse = GetEstimatedMemoryUse ( item . FileLocation ) ;
2015-02-18 08:10:33 -08:00
2018-05-04 09:45:05 -07:00
// If we have less than 2 gigs memory, warn on smaller file size
if ( AggContext . PhysicalMemory < 2000000000 )
2015-02-18 08:10:33 -08:00
{
if ( estimatedMemoryUse > 100000000 )
{
warnAboutFileSize = true ;
}
}
else
{
if ( estimatedMemoryUse > 500000000 )
{
warnAboutFileSize = true ;
}
}
2015-01-14 11:51:53 -08:00
}
2015-01-10 16:18:08 -08:00
2015-01-14 11:51:53 -08:00
if ( warnAboutFileSize )
{
partUnderConsideration = item ;
// Show a dialog and only load the part to the queue if the user clicks yes.
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-01-10 16:18:08 -08:00
{
2015-02-18 12:20:58 -08:00
string memoryWarningMessage = "Are you sure you want to add this part ({0}) to the Queue?\nThe 3D part you are trying to load may be too complicated and cause performance or stability problems.\n\nConsider reducing the geometry before proceeding." . Localize ( ) . FormatWith ( item . Name ) ;
2018-07-31 15:56:46 -07:00
StyledMessageBox . ShowMessageBox (
UserSaidToAllowAddToQueue , memoryWarningMessage , "File May Cause Problems" . Localize ( ) , StyledMessageBox . MessageType . YES_NO , "Add To Queue" , "Do Not Add" ) ;
2015-01-14 11:51:53 -08:00
// show a dialog to tell the user there is an update
} ) ;
return ;
}
else
{
DoAddItem ( item , indexToInsert ) ;
2015-01-10 16:18:08 -08:00
}
}
else
{
DoAddItem ( item , indexToInsert ) ;
}
2015-04-08 15:20:10 -07:00
}
2015-01-10 16:18:08 -08:00
2018-07-31 15:56:46 -07:00
public static long GetEstimatedMemoryUse ( string fileLocation )
{
switch ( Path . GetExtension ( fileLocation ) . ToUpper ( ) )
{
case ".STL" :
return StlProcessing . GetEstimatedMemoryUse ( fileLocation ) ;
case ".AMF" :
return AmfDocument . GetEstimatedMemoryUse ( fileLocation ) ;
case ".OBJ" :
throw new NotImplementedException ( ) ;
}
return 0 ;
}
2015-04-08 15:20:10 -07:00
private void UserSaidToAllowAddToQueue ( bool messageBoxResponse )
2015-01-10 16:18:08 -08:00
{
if ( messageBoxResponse )
{
DoAddItem ( partUnderConsideration , - 1 ) ;
}
}
2017-03-15 16:17:06 -07:00
private void DoAddItem ( PrintItemWrapper item , int insertAt )
2015-01-10 16:18:08 -08:00
{
2017-03-15 16:17:06 -07:00
if ( insertAt = = - 1 )
2015-01-10 16:18:08 -08:00
{
2018-07-31 15:56:46 -07:00
insertAt = PrintItems . Count ;
2015-01-10 16:18:08 -08:00
}
2017-03-15 16:17:06 -07:00
PrintItems . Insert ( insertAt , item ) ;
OnItemAdded ( new ItemChangedArgs ( insertAt ) ) ;
2015-01-10 16:18:08 -08:00
SaveDefaultQueue ( ) ;
}
2015-04-08 15:20:10 -07:00
public void LoadDefaultQueue ( )
{
RemoveAll ( ) ;
ManifestFileHandler manifest = new ManifestFileHandler ( null ) ;
List < PrintItem > partFiles = manifest . ImportFromJson ( ) ;
if ( partFiles ! = null )
{
foreach ( PrintItem item in partFiles )
{
2015-07-20 16:09:52 -07:00
AddItem ( new PrintItemWrapper ( item ) , - 1 , QueueData . ValidateSizeOn32BitSystems . Skip ) ;
2015-04-08 15:20:10 -07:00
}
}
RemoveAllSdCardFiles ( ) ;
}
public void RemoveAllSdCardFiles ( )
{
2016-12-03 19:44:39 -08:00
for ( int i = ItemCount - 1 ; i > = 0 ; i - - )
2015-04-08 15:20:10 -07:00
{
PrintItem printItem = PrintItems [ i ] . PrintItem ;
if ( printItem . FileLocation = = QueueData . SdCardFileName )
{
RemoveAt ( i ) ;
}
}
}
public void OnItemAdded ( EventArgs e )
{
ItemAdded . CallEvents ( this , e ) ;
}
public void SaveDefaultQueue ( )
{
2016-10-06 14:31:52 -07:00
List < PrintItem > partList = CreateReadOnlyPartList ( true ) ;
2015-04-08 15:20:10 -07:00
ManifestFileHandler manifest = new ManifestFileHandler ( partList ) ;
manifest . ExportToJson ( ) ;
}
2016-12-03 19:44:39 -08:00
public int ItemCount
2015-04-08 15:20:10 -07:00
{
get
{
return PrintItems . Count ;
}
}
public void RemoveAll ( )
{
for ( int i = PrintItems . Count - 1 ; i > = 0 ; i - - )
{
RemoveAt ( i ) ;
}
}
}
2014-04-15 18:13:27 -07:00
}