Ejemplo n.º 1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            mainWindow = new MainWindow();
            mainWindow.Show();

            System.Console.WriteLine($"Starting AllChemist {Version}");

            //Sets up buttons that generate a new model and/or load a new rulesets
            mainWindow.NewGridButton.Click     += (s, e) => { CleanUpModel(); InitializeModel(); };
            mainWindow.RulesetLoadButton.Click += (s, e) =>
            {
                rulesetInterpreter = new FromFileRulesetCreator();
                rulesetInterpreter.LoadRuleset();
                CleanUpModel();
                InitializeModel();
            };
            mainWindow.ConwayRulesetLoadButton.Click += (s, e) =>
            {
                rulesetInterpreter = new ConwayRulesetCreator("23/3");
                rulesetInterpreter.LoadRuleset();
                CleanUpModel();
                InitializeModel();
            };
            mainWindow.ExportButton.Click += (s, e) =>
            {
                BitmapExporter.Export(viewContainer.GetView <BitmapWorldView>().Bitmap, new PngBitmapEncoder());
            };

            controllerContainer = new DefaultControllerContainer(mainWindow);
            viewContainer       = new DefaultViewContainer(mainWindow);

            rulesetInterpreter = new ConwayRulesetCreator("23/3");

            InitializeModel();
        }
Ejemplo n.º 2
0
        protected override Bitmap GetThumbnailImage(uint width) // Implemented abstract function in the base class
        {
            MemoryStream memStream = new MemoryStream();



            Thread thread = new Thread(() =>
            {
                try
                {
                    string color              = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Marlin3DprinterTool", "Color", "Blue");
                    var stlReader             = new StLReader();
                    stlReader.DefaultMaterial = new DiffuseMaterial(
                        new SolidColorBrush((Color)ColorConverter.ConvertFromString(color)));



                    //stlReader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));
                    var model = stlReader.Read(SelectedItemStream);



                    //...create UI controls...

                    Viewport3D viewport = new Viewport3D();


                    // viewport.Measure(new System.Windows.Size(320, 240));
                    viewport.Measure(new Size(width, width));
                    // viewport.Arrange(new Rect(0, 0, 320, 240));
                    viewport.Arrange(new Rect(0, 0, width, width));

                    ModelVisual3D root = new ModelVisual3D();

                    viewport.Children.Add(root);


                    var camera               = new PerspectiveCamera();
                    camera.Position          = new Point3D(2, 16, 20);
                    camera.LookDirection     = new Vector3D(-2, -16, -20);
                    camera.UpDirection       = new Vector3D(0, 0, 1);
                    camera.FieldOfView       = 45;
                    camera.NearPlaneDistance = 0.1;
                    camera.FarPlaneDistance  = double.PositiveInfinity;
                    viewport.Camera          = camera;


                    root.Children.Add(new DefaultLights());

                    root.Content = model;



                    camera.ZoomExtents(viewport);

                    Brush background        = new SolidColorBrush(Colors.Transparent);
                    BitmapExporter exporter = new BitmapExporter
                    {
                        OversamplingMultiplier = 2,
                        Background             = background
                    };


                    exporter.Export(viewport, memStream);
                }
                catch (Exception treadException)
                {
                }
            });


            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();


            Bitmap thumbnailBitmap = (Bitmap)Image.FromStream(memStream);

            return(thumbnailBitmap);
        }
        protected override Bitmap GetThumbnailImage(uint width) // Implemented abstract function in the base class
        {
            string debug = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Marlin3DprinterTool", "Debug", "false");


            Logger(debug, "Creating Memorystream", EventLogEntryType.Information, 100);
            MemoryStream memStream = new MemoryStream();


            Logger(debug, "Creating Thread", EventLogEntryType.Information, 200);
            Thread thread = new Thread(() =>
            {
                Logger(debug, "Try Thread", EventLogEntryType.Information, 210);
                try
                {
                    Logger(debug, "Get Color", EventLogEntryType.Information, 220);
                    string color = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Marlin3DprinterTool", "Color", "Blue");

                    Logger(debug, $"Color: {color}", EventLogEntryType.Information, 222);

                    Logger(debug, "Start reader", EventLogEntryType.Information, 230);
                    var stlReader             = new StLReader();
                    stlReader.DefaultMaterial = new DiffuseMaterial(
                        new SolidColorBrush((Color)ColorConverter.ConvertFromString(color)));

                    Logger(debug, "Reader configured", EventLogEntryType.Information, 240);

                    //stlReader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));

                    Logger(debug, "Start reader the model", EventLogEntryType.Information, 250);
                    var model = stlReader.Read(SelectedItemStream);
                    Logger(debug, "Model ok", EventLogEntryType.Information, 260);


                    Logger(debug, "Create UI control", EventLogEntryType.Information, 300);
                    //...create UI controls...

                    Logger(debug, "Create Viewport", EventLogEntryType.Information, 310);
                    Viewport3D viewport = new Viewport3D();
                    Logger(debug, "Viewport done", EventLogEntryType.Information, 320);


                    Logger(debug, $"viewport.Measure: {width}", EventLogEntryType.Information, 330);
                    // viewport.Measure(new System.Windows.Size(320, 240));
                    viewport.Measure(new Size(width, width));
                    // viewport.Arrange(new Rect(0, 0, 320, 240));
                    Logger(debug, $"viewport.Arrange: {width}", EventLogEntryType.Information, 340);
                    viewport.Arrange(new Rect(0, 0, width, width));


                    Logger(debug, "ModelVisual3D", EventLogEntryType.Information, 350);
                    ModelVisual3D root = new ModelVisual3D();

                    Logger(debug, "viewport.Children.Add", EventLogEntryType.Information, 360);
                    viewport.Children.Add(root);


                    Logger(debug, "Camera", EventLogEntryType.Information, 400);
                    var camera               = new PerspectiveCamera();
                    camera.Position          = new Point3D(2, 16, 20);
                    camera.LookDirection     = new Vector3D(-2, -16, -20);
                    camera.UpDirection       = new Vector3D(0, 0, 1);
                    camera.FieldOfView       = 45;
                    camera.NearPlaneDistance = 0.1;
                    camera.FarPlaneDistance  = double.PositiveInfinity;
                    viewport.Camera          = camera;
                    Logger(debug, "Camera Done", EventLogEntryType.Information, 410);



                    Logger(debug, "Add light", EventLogEntryType.Information, 420);
                    root.Children.Add(new DefaultLights());
                    Logger(debug, "Add model", EventLogEntryType.Information, 430);
                    root.Content = model;


                    Logger(debug, "Do camera.ZoomExtents", EventLogEntryType.Information, 440);
                    camera.ZoomExtents(viewport);


                    Logger(debug, "Do background", EventLogEntryType.Information, 450);
                    Brush background = new SolidColorBrush(Colors.Transparent);

                    Logger(debug, "Do a BitmapExporter", EventLogEntryType.Information, 460);
                    BitmapExporter exporter = new BitmapExporter
                    {
                        OversamplingMultiplier = 2,
                        Background             = background
                    };

                    Logger(debug, "Fill the exporter", EventLogEntryType.Information, 470);
                    exporter.Export(viewport, memStream);
                }
                catch (Exception errorException)
                {
                    Logger(debug, errorException.Message, EventLogEntryType.Error, errorException.HResult);
                    //TODO: Empty Catch
                }
            });


            Logger(debug, "Thread STA", EventLogEntryType.Information, 500);
            thread.SetApartmentState(ApartmentState.STA);
            Logger(debug, "Thread Sstart", EventLogEntryType.Information, 510);
            thread.Start();
            Logger(debug, "Thread Join", EventLogEntryType.Information, 520);
            thread.Join();

            Logger(debug, "Create bitmap from memstream", EventLogEntryType.Information, 530);
            Bitmap thumbnailBitmap = (Bitmap)Image.FromStream(memStream);

            Logger(debug, "Return Bitmap!", EventLogEntryType.Information, 500);
            return(thumbnailBitmap);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Loads the Stl model into a Viewport3D and renders a bitmap of it.
        /// </summary>
        /// <param name="stream">The Stream instance for the Stl content.</param>
        /// <param name="cx">The maximum thumbnail size, in pixels.</param>
        /// <returns>A thumbnail rendered from the Stl model.</returns>
        public static Bitmap GetThumbnail(Stream stream, uint cx)
        {
            if (cx > MaxThumbnailSize || stream == null || stream.Length == 0)
            {
                return(null);
            }

            Bitmap thumbnail = null;

            var stlReader = new StLReader
            {
                DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 201, 36))),
            };

            var model = stlReader.Read(stream);

            if (model.Bounds == Rect3D.Empty)
            {
                return(null);
            }

            var viewport = new System.Windows.Controls.Viewport3D();

            viewport.Measure(new Size(cx, cx));
            viewport.Arrange(new Rect(0, 0, cx, cx));

            var modelVisual = new ModelVisual3D()
            {
                Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180)),
            };

            viewport.Children.Add(modelVisual);
            viewport.Children.Add(new DefaultLights());

            var perspectiveCamera = new PerspectiveCamera
            {
                Position          = new Point3D(1, 2, 1),
                LookDirection     = new Vector3D(-1, -2, -1),
                UpDirection       = new Vector3D(0, 0, 1),
                FieldOfView       = 20,
                NearPlaneDistance = 0.1,
                FarPlaneDistance  = double.PositiveInfinity,
            };

            viewport.Camera = perspectiveCamera;

            modelVisual.Content = model;

            perspectiveCamera.ZoomExtents(viewport);

            var bitmapExporter = new BitmapExporter
            {
                Background             = new SolidColorBrush(Colors.Transparent),
                OversamplingMultiplier = 1,
            };

            var bitmapStream = new MemoryStream();

            bitmapExporter.Export(viewport, bitmapStream);

            bitmapStream.Position = 0;

            thumbnail = new Bitmap(bitmapStream);

            return(thumbnail);
        }
Ejemplo n.º 5
0
        protected Bitmap GetThumbnailImage(int width) // Implemented abstract function in the base class
        {
            bool everythingisOk = true;



            MemoryStream memStream = new MemoryStream();



            Thread thread = new Thread(() =>
            {
                //string tracefile = @"C:\temp\stlviewer.stl_log";
                string tracefile = TargetFile.Replace(".stl", ".stl_log");


                Logg("_______________________________________________________________________________");
                FileInfo fileInfo = new FileInfo(TargetFile);
                Logg($"Name: {fileInfo.Name}");
                Logg($"Dir.: {fileInfo.Directory}");

                Logg($"Size: {fileInfo.Length}");
                Logg("1. Tread started");


                object convertFromString = null;
                Color color              = new Color();
                ModelVisual3D root       = new ModelVisual3D();
                Model3DGroup model       = new Model3DGroup();
                PerspectiveCamera camera = new PerspectiveCamera();
                BitmapExporter exporter  = new BitmapExporter();


                if (everythingisOk)
                {
                    try
                    {
                        string colorString = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Marlin3DprinterTool", "Color", "Blue");

                        convertFromString = ColorConverter.ConvertFromString(colorString);
                        Logg($"2. Color: {convertFromString}");
                        if (convertFromString != null)
                        {
                            color = (Color)convertFromString;
                        }
                    }
                    catch (Exception e)
                    {
                        Logg($"Something goes wrong in Color {e.Message}");
                        everythingisOk = false;
                    }
                }

                if (everythingisOk)
                {
                    if (convertFromString != null)
                    {
                        Viewport3D viewport = new Viewport3D();

                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"3. STLreader");
                                StLReader stlReader       = new StLReader();
                                stlReader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(color));

                                Logg($"4. Read the Targetfile {TargetFile}");
                                model = stlReader.Read(TargetFile);
                                Logg($"5. Model read. New Viewport");
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in STLreader {e.Message}");
                                everythingisOk = false;
                            }
                        }



                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"6. Arrange with / height {width}/{width}");
                                viewport.Measure(new Size(width, width));
                                viewport.Arrange(new Rect(0, 0, width, width));

                                Logg($"7. Create root");

                                viewport.Children.Add(root);


                                Logg($"8. Create camera");

                                camera.Position          = new Point3D(2, 16, 20);
                                camera.LookDirection     = new Vector3D(-2, -16, -20);
                                camera.UpDirection       = new Vector3D(0, 0, 1);
                                camera.FieldOfView       = 45;
                                camera.NearPlaneDistance = 0.1;
                                camera.FarPlaneDistance  = double.PositiveInfinity;
                                viewport.Camera          = camera;
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in STLarrange {e.Message}");
                                everythingisOk = false;
                            }
                        }

                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"8.1. Create light");
                                root.Children.Add(new DefaultLights());
                                Logg($"8.2 Load model");
                                root.Content = model;


                                camera.ZoomExtents(viewport);
                                Logg($"9. Create Background");
                                Brush background = new SolidColorBrush(Colors.Transparent);

                                Logg($"10. Create Exporter");

                                exporter.Background             = background;
                                exporter.OversamplingMultiplier = 1;
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in Background/Light {e.Message}");
                                everythingisOk = false;
                            }
                        }

                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"11. Export to memorystream");
                                exporter.Export(viewport, memStream);
                                //Logg($"11. Export to {tempBitmap}");
                                //FileStream fs = new FileStream(tempBitmap, FileMode.Create, FileAccess.ReadWrite);
                                //exporter.Export(viewport, fs);
                                //fs.Flush();
                                //fs.Close();
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in Export to memory Stream {e.Message}");
                                everythingisOk = false;
                            }
                        }
                    }
                }
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();



            if (everythingisOk)
            {
                try
                {
                    Logg($"12. Create Bitmap from memorystream");
                    Bitmap thumbnailBitmap = (Bitmap)Image.FromStream(memStream);

                    return(thumbnailBitmap);
                }
                catch (Exception e)
                {
                    Logg($"Something goes wrong in Bitmap from messageStream {e.Message}");
                    Logg("_______________________________________________________________________________");

                    return(null);
                }
            }
            Logg("_______________________________________________________________________________");

            return(null);
        }