Exemple #1
0
        public static Slices SliceImage(int SliceWidth, int SliceHeight)
        {
            Slices _slices = new Slices();
            int    p1      = GetBitmap.Width / SliceWidth;
            int    p2      = GetBitmap.Height / SliceHeight;

            for (int y = 0; y < p2; y++)
            {
                for (int x = 0; x < p1; x++)
                {
                    int valX = x * SliceWidth;
                    int valY = y * SliceHeight;

                    var rect = new Rectangle
                    {
                        Width    = SliceWidth,
                        Height   = SliceHeight,
                        Size     = new Size(SliceWidth, SliceHeight),
                        Location = new Point(valX, valY),
                        X        = valX,
                        Y        = valY,
                    };

                    var          bmp = GetBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    Slices.Slice slc = new Slices.Slice
                    {
                        getBitmap = bmp,
                        part      = x,
                    };
                    _slices.slices.Add(slc);
                }
            }
            return(_slices);
        }
Exemple #2
0
        private static long CalculateWhitePixel(Slices.Slice slice)
        {
            var amountOfWhitePixels = RenderEngineHelper.ConvertPolyTreesToPixels(pixelValues, slice.ModelPolyTrees, PrintJob.SelectedPrinter.ProjectorResolutionX, PrintJob.SelectedPrinter.ProjectorResolutionY, true);

            amountOfWhitePixels     += RenderEngineHelper.ConvertPolyTreesToPixels(pixelValues, slice.SupportPolyTrees, PrintJob.SelectedPrinter.ProjectorResolutionX, PrintJob.SelectedPrinter.ProjectorResolutionY, false);
            TotatAmountofActivePixel = TotatAmountofActivePixel + amountOfWhitePixels;
            return(TotatAmountofActivePixel);
        }
 public void PostSlice(Slices.Slice renderSlice, AtumPrinter selectedPrinter, PluginTypes.PostSliceActionType postSliceActions)
 {
     ////apply warp correction to polynode
     //if (renderSlice.ModelPolyTrees.Length > 0 && renderSlice.ModelPolyTrees[0].Count > 0)
     //{
     //    foreach (var modelPolynode in renderSlice.ModelPolyTrees[0][0]._allPolys)
     //    {
     //        LensWarpCorrection.CalculateByContour(modelPolynode, selectedPrinter);
     //    }
     //}
 }
        public static void RenderSliceToPolygons(Slices.Slice slice, Slices.RenderSliceInfo renderSlice)
        {
            var pixelValues = new byte[renderSlice.PrinterResolutionX * renderSlice.PrinterResolutionY];
            var pixelLength = pixelValues.Length;
            //        Array.Clear(pixelValues, 0, pixelLength);

            int          beginPixel      = 0;
            int          beginPixelOfRow = 0;
            int          endPixel        = 0;
            int          endPixelOfRow   = 0;
            SlicePoint2D p1;
            SlicePoint2D p2;

            //model points
            if (slice.ModelPoints.Count % 2 == 0)
            {
                for (int cnt = 0; cnt < slice.ModelPoints.Count; cnt += 2)      // increment by 2
                {
                    p1 = slice.ModelPoints[cnt];
                    p2 = slice.ModelPoints[cnt + 1];
                    if (p1.X > p2.X)     //flip over
                    {
                        var pTemp = p1;
                        p1 = p2;
                        p2 = pTemp;
                    }
                    p1.X++;

                    if (p1.X <= p2.X)
                    {
                        if (p2.Y >= 0 && p1.Y >= 0)
                        {
                            beginPixel      = (p1.Y * renderSlice.PrinterResolutionX + p1.X);
                            beginPixelOfRow = (((p1.Y) * renderSlice.PrinterResolutionX));
                            if (p1.X < 0)
                            {
                                beginPixel = (p1.Y * renderSlice.PrinterResolutionX);
                            }

                            //get end pixel of row
                            endPixel      = ((p2.Y * renderSlice.PrinterResolutionX + p2.X));
                            endPixelOfRow = (((p1.Y + 1) * renderSlice.PrinterResolutionX)) - 1;

                            if (endPixel > endPixelOfRow)
                            {
                                endPixel = endPixelOfRow;
                            }

                            if (pixelValues.Length - 1 >= beginPixel && beginPixel >= 0)
                            {
                                pixelValues[beginPixel] = 255;
                            }

                            if (p2.X < 0)
                            {
                                endPixel = ((p2.Y * renderSlice.PrinterResolutionX));
                            }

                            //fill color between begin and endpoints
                            if (pixelValues.Length - 1 >= endPixel && endPixel >= 0)
                            {
                                pixelValues[endPixel] = 255;

                                if (beginPixel < endPixel)
                                {
                                    for (var betweenPixel = beginPixel + 1; betweenPixel < endPixel; betweenPixel++)
                                    {
                                        if (betweenPixel >= 0)
                                        {
                                            pixelValues[betweenPixel] = 255;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (slice.SupportPoints.Count % 2 == 0)                          // is even
            {
                for (int cnt = 0; cnt < slice.SupportPoints.Count; cnt += 2) // increment by 2
                {
                    p1 = slice.SupportPoints[cnt];
                    p2 = slice.SupportPoints[cnt + 1];
                    if (p1.X > p2.X)     //flip over
                    {
                        var pTemp = p1;
                        p1 = p2;
                        p2 = pTemp;
                    }
                    p1.X++;

                    if (p1.X <= p2.X)
                    {
                        if (p2.Y >= 0 && p1.Y >= 0)
                        {
                            beginPixel      = (p1.Y * renderSlice.PrinterResolutionX + p1.X);
                            beginPixelOfRow = (((p1.Y) * renderSlice.PrinterResolutionX));
                            if (p1.X < 0)
                            {
                                beginPixel = (p1.Y * renderSlice.PrinterResolutionX);
                            }

                            //get end pixel of row
                            endPixel      = ((p2.Y * renderSlice.PrinterResolutionX + p2.X));
                            endPixelOfRow = (((p1.Y + 1) * renderSlice.PrinterResolutionX));

                            if (endPixel > endPixelOfRow)
                            {
                                endPixel = endPixelOfRow;
                            }

                            if (pixelValues.Length - 1 >= beginPixel && beginPixel >= 0)
                            {
                                pixelValues[beginPixel] = 255;
                            }

                            if (p2.X < 0)
                            {
                                endPixel = ((p2.Y * renderSlice.PrinterResolutionX));
                            }

                            if (pixelValues.Length - 1 >= endPixel && endPixel >= 0)
                            {
                                pixelValues[endPixel] = 255;

                                if (beginPixel < endPixel)
                                {
                                    for (var betweenPixel = beginPixel + 1; betweenPixel < endPixel; betweenPixel++)
                                    {
                                        if (betweenPixel >= 0)
                                        {
                                            pixelValues[betweenPixel] = 255;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            else      // flag error
            {
                Console.WriteLine("Row y=" + " odd # of points = " + slice.SupportPoints.Count + " - Model may have holes");
            }

            //convert pixel values to polylines
            for (var pixelRowIndex = 0; pixelRowIndex < renderSlice.PrinterResolutionY; pixelRowIndex++)
            {
                var beginRowIndex       = renderSlice.PrinterResolutionX * pixelRowIndex;
                var startOfPolygon      = false;
                var startOfPolygonIndex = 0;
                var endOfPolygonIndex   = 0;

                for (var pixelColumnIndex = 0; pixelColumnIndex < renderSlice.PrinterResolutionX; pixelColumnIndex++)
                {
                    if (pixelValues[beginRowIndex + pixelColumnIndex] == 255)
                    {
                        if (!startOfPolygon)
                        {
                            startOfPolygon      = true;
                            startOfPolygonIndex = pixelColumnIndex;
                        }
                    }
                    else if (pixelValues[beginRowIndex + pixelColumnIndex] == 0)
                    {
                        if (startOfPolygon)
                        {
                            endOfPolygonIndex = pixelColumnIndex;
                            startOfPolygon    = false;

                            //add polygon to return list
                            var slicePolygon = new SlicePolyLine3D();
                            slicePolygon.Points.Add(new Vector3(startOfPolygonIndex, pixelRowIndex, renderSlice.SliceHeight));
                            slicePolygon.Points.Add(new Vector3(endOfPolygonIndex, pixelRowIndex, renderSlice.SliceHeight));
                            SimulationPolyLines.Add(slicePolygon);
                        }
                    }
                }
            }



            lock (_totalProcessedSlicesLock)
            {
                TotalProcessedSlices++;
            }

            if (TotalProcessedSlices == TotalAmountSlices)
            {
                if (SimulationPrintJobCompleted != null)
                {
                    //convert polygons to vectors
                    STLModel           = new STLModel3D(STLModel3D.TypeObject.Model, true);
                    STLModel.Triangles = new TriangleInfoList();

                    var triangleIndex = 0;
                    var arrayIndex    = 0;
                    foreach (var polyLine in SimulationPolyLines)
                    {
                        if (polyLine != null)
                        {
                            var polygonCube = new Shapes.Cube(polyLine.Points[1].X - polyLine.Points[0].X + 1, polyLine.Points[1].Y - polyLine.Points[0].Y + 1, 1);
                            //Console.WriteLine(polygonCube.Center);
                            for (var cubeVertexIndex = 0; cubeVertexIndex < polygonCube.VertexArray.Length; cubeVertexIndex += 3)
                            {
                                var triangle = new Triangle();
                                triangle.Normal = polygonCube.VertexArray[cubeVertexIndex].Normal;

                                triangle.Vectors[0].Position = new Vector3(-48, -20, 0);
                                triangle.Vectors[0].Normal   = triangle.Vectors[1].Normal = triangle.Vectors[2].Normal = triangle.Normal;
                                triangle.Vectors[0].Color    = triangle.Vectors[1].Color = triangle.Vectors[2].Color = new Byte4()
                                {
                                    A = 128, B = 255
                                };
                                triangle.Vectors[1].Position = new Vector3(-48, -20, 0);
                                triangle.Vectors[2].Position = new Vector3(-48, -20, 0);


                                triangle.Vectors[0].Position += polygonCube.VertexArray[cubeVertexIndex].Position / 20;
                                triangle.Vectors[1].Position += polygonCube.VertexArray[cubeVertexIndex + 1].Position / 20;
                                triangle.Vectors[2].Position += polygonCube.VertexArray[cubeVertexIndex + 2].Position / 20;

                                //add 0 point
                                triangle.Vectors[0].Position += new Vector3(polyLine.Points[0].X / 20, polyLine.Points[0].Y / 20, polyLine.Points[0].Z);
                                triangle.Vectors[1].Position += new Vector3(polyLine.Points[0].X / 20, polyLine.Points[0].Y / 20, polyLine.Points[0].Z);
                                triangle.Vectors[2].Position += new Vector3(polyLine.Points[0].X / 20, polyLine.Points[0].Y / 20, polyLine.Points[0].Z);

                                if (triangleIndex > 33333)
                                {
                                    IsReadyForRendering = true;
                                    break;
                                    triangleIndex = 0;
                                    arrayIndex++;
                                    STLModel.Triangles.Add(new List <Triangle>());
                                }

                                triangleIndex++;
                                STLModel.Triangles[arrayIndex].Add(triangle);
                            }
                        }
                    }

                    IsReadyForRendering = true;
                    SimulationPrintJobCompleted(null, null);
                }
            }
        }
        internal static int RenderSliceThread(object renderSliceInfo)
        {
            if (Managers.PerformanceSettingsManager.Settings.PrintJobGenerationMaxMemoryLimitEnabled)
            {
                while ((Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024) > Managers.PerformanceSettingsManager.Settings.PrintJobGenerationMaxMemory)
                {
                    Thread.Sleep(1000);
                    GC.Collect();
                    GC.Collect();

                    Atum.DAL.Managers.LoggingManager.WriteToLog("Render Engine", "Memory management", "Waiting for free memory");
                }
            }
            var renderSlice = (Core.Slices.RenderSliceInfo)renderSliceInfo;

            Slices.Slice slice = new Slices.Slice(new List <SlicePolyLine3D>(), new List <List <SlicePolyLine3D> >(), new List <SlicePolyLine3D>(), renderSlice.PrinterResolutionX, renderSlice.PrinterResolutionY, renderSlice.SliceHeight);


            foreach (var object3d in ObjectView.Objects3D)
            {
                var supportZPolys = new List <List <SlicePolyLine3D> >();

                if (object3d is STLModel3D && (!(object3d is GroundPane)))
                {
                    var stlModel = (STLModel3D)object3d;

                    foreach (var supportCone in stlModel.SupportStructure)
                    {
                        if (!supportCone.Hidden)
                        {
                            var supportConeZPolys = GetZPolys(supportCone, renderSlice.SliceHeight);
                            supportZPolys.Add(GetZIntersections(supportConeZPolys, renderSlice.SliceHeight));
                        }
                    }

                    foreach (var supportCone in stlModel.Triangles.HorizontalSurfaces.SupportStructure)
                    {
                        if (!supportCone.Hidden)
                        {
                            var supportConeZPolys = GetZPolys(supportCone, renderSlice.SliceHeight);
                            supportZPolys.Add(GetZIntersections(supportConeZPolys, renderSlice.SliceHeight));
                        }
                    }

                    foreach (var supportCone in stlModel.Triangles.FlatSurfaces.SupportStructure)
                    {
                        if (!supportCone.Hidden)
                        {
                            var supportConeZPolys = GetZPolys(supportCone, renderSlice.SliceHeight);
                            supportZPolys.Add(GetZIntersections(supportConeZPolys, renderSlice.SliceHeight));
                        }
                    }

                    if (renderSlice.SliceHeight <= (0.2f * renderSlice.Material.ShrinkFactor) && stlModel.SupportBasement && stlModel.SupportBasementStructure != null)
                    {
                        var supportBase = (STLModel3D)stlModel.SupportBasementStructure.Clone();
                        supportBase.MoveTranslation = stlModel.MoveTranslation;

                        foreach (var triangle in supportBase.Triangles[0])
                        {
                            triangle.CalcMinMaxZ();
                        }
                        var supportConeZPolys = GetZPolys(supportBase, renderSlice.SliceHeight);
                        supportZPolys.Add(GetZIntersections(supportConeZPolys, renderSlice.SliceHeight));
                    }
                    var modelZPolys = GetZIntersections(GetZPolys(stlModel, renderSlice.SliceHeight), renderSlice.SliceHeight);

                    using (var modelSlice = new Core.Slices.Slice(modelZPolys, supportZPolys, null, renderSlice.PrinterResolutionX, renderSlice.PrinterResolutionY, renderSlice.SliceHeight))
                    {
                        modelSlice.RenderSliceAsByteArray();
                        slice.ModelPoints.AddRange(modelSlice.ModelPoints);
                        slice.SupportPoints.AddRange(modelSlice.SupportPoints);
                    }
                }
            }

            RenderSliceToPolygons(slice, renderSlice);

            return(renderSlice.SliceIndex);
        }
Exemple #6
0
        private static bool RenderSliceToPNG(Slices.Slice slice)
        {
            var result = false;

            //create pic object
            Bitmap t = null;

            lock (_renderBitmap)
            {
                t = (Bitmap)_renderBitmap.Clone();
            }

            lock (pixelValues)
            {
                var pixelLength = pixelValues.Length;
                Array.Clear(pixelValues, 0, pixelLength);
                Array.Clear(pixelBleedingValues, 0, pixelLength);

                //render model points
                var amountOfWhitePixels = CalculateWhitePixel(slice);

                //render bleeding points
                if (PrintJob.Material.BleedingOffset > 0 && slice.SliceIndex > 5)
                {
                    if (slice.ModelBleedingPolyTrees != null && slice.ModelBleedingPolyTrees.Length > 0)
                    {
                        RenderEngineHelper.ConvertPolyTreesToPixels(pixelBleedingValues, slice.ModelBleedingPolyTrees, PrintJob.SelectedPrinter.ProjectorResolutionX, PrintJob.SelectedPrinter.ProjectorResolutionY, true);

                        var emptyBleedingModelPolyTrees = new List <PolyTree> [slice.ModelBleedingPolyTrees.Length];
                        for (var modelBleedingPolyTreeIndex = 0; modelBleedingPolyTreeIndex < slice.ModelBleedingPolyTrees.Length; modelBleedingPolyTreeIndex++)
                        {
                            if (slice.ModelBleedingPolyTrees[modelBleedingPolyTreeIndex] == null)
                            {
                                //process no bleeding information available
                                emptyBleedingModelPolyTrees[modelBleedingPolyTreeIndex] = slice.ModelPolyTrees[modelBleedingPolyTreeIndex];
                            }
                        }

                        //convert empty polygons to bleeding pixels
                        RenderEngineHelper.ConvertPolyTreesToPixels(pixelBleedingValues, emptyBleedingModelPolyTrees, PrintJob.SelectedPrinter.ProjectorResolutionX, PrintJob.SelectedPrinter.ProjectorResolutionY, true, 0);

                        //do bleeding merge model pixel active and bleeding not active then not active
                        for (var modelPixelIndex = 0; modelPixelIndex < pixelLength; modelPixelIndex += 3)
                        {
                            if (pixelValues[modelPixelIndex] == 255 && pixelBleedingValues[modelPixelIndex] == 0)
                            {
                                pixelValues[modelPixelIndex]     = 0;
                                pixelValues[modelPixelIndex + 1] = 0;
                                pixelValues[modelPixelIndex + 2] = 0;

                                amountOfWhitePixels -= 1;
                            }
                        }
                    }
                    else
                    {
                        amountOfWhitePixels = 0;
                    }
                }

                //render support points

                if (amountOfWhitePixels > UserProfileManager.UserProfile.Settings_PrintJob_FirstSlice_MinAmountOfPixels)
                {
                    result = true;
                }
                else if (amountOfWhitePixels == 0 && slice.SliceIndex > 5)
                {
                    lock (_emptyPNGs)
                    {
                        _emptyPNGs.Add(slice.SliceIndex, 0);
                    }
                }

                //Get a reference to the images pixel data
                BitmapData picData           = t.LockBits(new Rectangle(0, 0, PrintJob.SelectedPrinter.ProjectorResolutionX, PrintJob.SelectedPrinter.ProjectorResolutionY), ImageLockMode.WriteOnly, t.PixelFormat);
                IntPtr     pixelStartAddress = picData.Scan0;

                //Copy the pixel data into the bitmap structure
                System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, pixelStartAddress, pixelValues.Length);
                t.UnlockBits(picData);
            }

            try
            {
                if (!_emptyPNGs.ContainsKey(slice.SliceIndex))
                {
                    using (var memStream = new MemoryStream())
                    {
                        //           t.Save("Slices\\" + slice.SliceIndex + ".png");
                        t.Save(memStream, ImageFormat.Png);
                        t.Dispose();
                        memStream.Position = 0;

                        lock (_zipStream)
                        {
                            var entry = new ZipEntry(slice.SliceIndex + ".png");
                            _zipStream.PutNextEntry(entry);

                            try
                            {
                                byte[] transferBuffer = new byte[1024];
                                int    bytesRead;
                                do
                                {
                                    bytesRead = memStream.Read(transferBuffer, 0, transferBuffer.Length);
                                    _zipStream.Write(transferBuffer, 0, bytesRead);
                                }while (bytesRead > 0);
                            }
                            finally
                            {
                                memStream.Close();
                            }

                            _zipStream.CloseEntry();

                            //LoggingManager.WriteToLog("RenderSliceThread", "Zip location", _);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                LoggingManager.WriteToLog("RenderSliceThread", "Save image (exception)", exc.Message);
            }

            return(result);
        }
Exemple #7
0
        private static bool RenderSliceToXYSmootingPNG(Slices.Slice slice)
        {
            var result = false;

            //create pic object
            Bitmap t = null;

            lock (_renderBitmapXYSmoothing)
            {
                t = (Bitmap)_renderBitmapXYSmoothing.Clone();
            }

            var g = Graphics.FromImage(t);

            foreach (var modelPolyTree in slice.ModelPolyTrees)
            {
                if (modelPolyTree.Count > 0)
                {
                    var clipperOffset = new ClipperOffset();
                    foreach (var polyNode in modelPolyTree[0]._allPolys)
                    {
                        clipperOffset.AddPath(polyNode.Contour, JoinType.jtMiter, EndType.etClosedPolygon);
                    }

                    var offsetResult = new PolyTree();
                    clipperOffset.Execute(ref offsetResult, -(CONTOURPOINT_TO_VECTORPOINT_FACTOR / 2));

                    var a = 0;
                    foreach (var polyNode in offsetResult._allPolys)
                    {
                        if (a == 0)
                        {
                            a++;
                            continue;
                        }
                        var pathPoints = new List <PointF>();
                        foreach (var polyNodePoint in polyNode.Contour)
                        {
                            pathPoints.Add(polyNodePoint.AsPointF());
                        }

                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        g.DrawPolygon(new Pen(Color.White, 1), pathPoints.ToArray());
                    }
                }
            }

            using (var memStream = new MemoryStream())
            {
                t.Save(memStream, ImageFormat.Png);
                t.Dispose();
                memStream.Position = 0;

                lock (_zipStream)
                {
                    var entry = new ZipEntry(slice.SliceIndex + "-xys.png");
                    _zipStream.PutNextEntry(entry);

                    try
                    {
                        byte[] transferBuffer = new byte[1024];
                        int    bytesRead;
                        do
                        {
                            bytesRead = memStream.Read(transferBuffer, 0, transferBuffer.Length);
                            _zipStream.Write(transferBuffer, 0, bytesRead);
                        }while (bytesRead > 0);
                    }
                    finally
                    {
                        memStream.Close();
                    }

                    _zipStream.CloseEntry();

                    //LoggingManager.WriteToLog("RenderSliceThread", "Zip location", _);
                }
            }

            return(result);
        }
Exemple #8
0
        private static int RenderSliceThread(object sliceInfoObject)
        {
            var sliceInfo = (Slices.Slice)sliceInfoObject;

            try
            {
                if (PerformanceSettingsManager.Settings.PrintJobGenerationMaxMemoryLimitEnabled)
                {
                    while ((Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024) > Managers.PerformanceSettingsManager.Settings.PrintJobGenerationMaxMemory)
                    {
                        Thread.Sleep(1000);
                        GC.Collect();
                        GC.Collect();

                        LoggingManager.WriteToLog("Render Engine", "Memory management", "Waiting for free memory");
                    }
                }

                var modelsToSliceCount = 0;

                foreach (var object3d in ObjectView.Objects3D)
                {
                    var supportZPolys = new List <List <SlicePolyLine3D> >();

                    if (object3d is STLModel3D && (!(object3d is GroundPane)))
                    {
                        var stlModel = (STLModel3D)object3d;
                        modelsToSliceCount++;
                    }
                }

                Slices.Slice slice = new Slices.Slice(sliceInfo.SliceIndex, new List <SlicePolyLine3D>(), new List <List <SlicePolyLine3D> >(), new List <SlicePolyLine3D>(), sliceInfo.SliceHeight);
                slice.ModelPolyTrees         = new List <PolyTree> [modelsToSliceCount];
                slice.ModelBleedingPolyTrees = new List <PolyTree> [modelsToSliceCount];
                slice.SupportPolyTrees       = new List <PolyTree> [modelsToSliceCount];

                var currentModelIndex = 0;
                foreach (var object3d in ObjectView.Objects3D)
                {
                    var supportZPolys = new List <List <SlicePolyLine3D> >();

                    if (object3d is STLModel3D && (!(object3d is GroundPane)))
                    {
                        var stlModel = (STLModel3D)object3d;
                        if (!_cancelRendering)
                        {
                            foreach (var supportCone in stlModel.TotalObjectSupportCones)
                            {
                                if (supportCone != null)
                                {
                                    if (supportCone is SupportConeV2)
                                    {
                                    }
                                    else
                                    {
                                        if (!supportCone.Hidden)
                                        {
                                            supportZPolys.Add(GetZIntersectionsSupport(supportCone, sliceInfo.SliceHeight));
                                        }
                                    }
                                }
                            }

                            if (sliceInfo.SliceHeight <= (UserProfileManager.UserProfile.SupportEngine_Basement_Thickness * PrintJob.Material.ShrinkFactor) && stlModel.SupportBasement && stlModel.SupportBasementStructure != null)
                            {
                                foreach (var triangle in stlModel.SupportBasementStructure.Triangles[0])
                                {
                                    triangle.CalcMinMaxZ();
                                }

                                var supportBasementZPolys = GetZPolys(stlModel.SupportBasementStructure, stlModel.SupportBasementStructure.SliceIndexes, sliceInfo.SliceHeight);
                                supportZPolys.Add(GetZIntersections(supportBasementZPolys, sliceInfo.SliceHeight));
                            }
                        }

                        List <SlicePolyLine3D> modelZPolys         = null;
                        List <SlicePolyLine3D> modelBleedingZPolys = null;
                        if (!_cancelRendering)
                        {
                            modelZPolys = GetZIntersections(GetZPolys(stlModel, stlModel.SliceIndexes, sliceInfo.SliceHeight), sliceInfo.SliceHeight);

                            //post processing plugins
                            foreach (var plugin in PluginManager.LoadedPlugins)
                            {
                                if (plugin.HasPostSliceMethod &&
                                    (plugin.PostSliceActionType & Plugins.PluginTypes.PostSliceActionType.Bleeding) == Plugins.PluginTypes.PostSliceActionType.Bleeding &&
                                    sliceInfo.SliceHeight > PrintJob.Material.BleedingOffset && PrintJob.Material.BleedingOffset > 0f
                                    )
                                {
                                    modelBleedingZPolys = plugin.PostSlice(stlModel, sliceInfo.SliceHeight + (float)PrintJob.Material.BleedingOffset, Plugins.PluginTypes.PostSliceActionType.Bleeding);
                                }
                            }
                        }

                        if (!_cancelRendering)
                        {
                            using (var modelWithSupportSlice = new Slices.Slice(sliceInfo.SliceIndex, modelZPolys, supportZPolys, modelBleedingZPolys, sliceInfo.SliceHeight))
                            {
                                modelWithSupportSlice.ConvertModelIntersectionsToPolyTrees(sliceInfo.SliceHeight, RenderEngine.PrintJob.SelectedPrinter,
                                                                                           PrintJobManager.CurrentPrintJobSettings.Material,
                                                                                           PrintJobManager.CurrentPrintJobSettings.Material.SupportProfiles.First(),
                                                                                           null, null);
                                modelWithSupportSlice.ConvertSupportIntersectionsToPolyTrees(sliceInfo.SliceHeight, RenderEngine.PrintJob.SelectedPrinter);

                                slice.ModelPolyTrees[currentModelIndex] = new List <PolyTree>();
                                slice.ModelPolyTrees[currentModelIndex].AddRange(modelWithSupportSlice.ModelPolyTrees[0]);

                                slice.SupportPolyTrees[currentModelIndex] = new List <PolyTree>();
                                slice.SupportPolyTrees[currentModelIndex].AddRange(modelWithSupportSlice.SupportPolyTrees[0]);

                                foreach (var supportCone in stlModel.TotalObjectSupportCones)
                                {
                                    if (supportCone is SupportConeV2)
                                    {
                                        var supportConeV2 = (SupportConeV2)supportCone;
                                        if (supportConeV2.SliceContours.ContainsKey(sliceInfo.SliceHeight))
                                        {
                                            slice.SupportPolyTrees[currentModelIndex].Add(supportConeV2.SliceContours[sliceInfo.SliceHeight]);

                                            foreach (var interlinkConnectionIndex in supportConeV2.InterlinkConnections)
                                            {
                                                if (interlinkConnectionIndex.Value != null)
                                                {
                                                    foreach (var interlinkConnection in interlinkConnectionIndex.Value)
                                                    {
                                                        if (interlinkConnection != null && interlinkConnection.SliceContours != null && interlinkConnection.SliceContours.ContainsKey(sliceInfo.SliceHeight))
                                                        {
                                                            slice.SupportPolyTrees[currentModelIndex].Add(interlinkConnection.SliceContours[sliceInfo.SliceHeight]);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                if (modelBleedingZPolys != null && modelBleedingZPolys.Count > 0)
                                {
                                    if (sliceInfo.SliceHeight < 1f)
                                    {
                                    }

                                    modelWithSupportSlice.ConvertModelBleedingIntersectionsToPolyTrees(sliceInfo.SliceHeight, RenderEngine.PrintJob.SelectedPrinter);
                                    slice.ModelBleedingPolyTrees[currentModelIndex] = new List <PolyTree>();
                                    slice.ModelBleedingPolyTrees[currentModelIndex].AddRange(modelWithSupportSlice.ModelBleedingPolyTrees[0]);
                                }
                            }
                        }

                        currentModelIndex++;
                    }
                }

                //output to printjob
                if (!_cancelRendering)
                {
                    var validSlice = RenderSliceToPNG(slice);

                    if (PrintJob.Material.XYSmoothingEnabled)
                    {
                        var xySmoothingSlice = RenderSliceToXYSmootingPNG(slice);
                    }


                    if (sliceInfo.SliceIndex % 50 == 0)
                    {
                        MemoryHelpers.ForceGCCleanup();
                    }

                    lock (_totalProcessedSlicesLock)
                    {
                        TotalProcessedSlices++;
                    }

                    if (TotalProcessedSlices == TotalAmountSlices)
                    {
                        #region Calculate Volume

                        var layerThickness     = RenderEngine.PrintJob.Material.LT2;
                        var currentPixelVolume = Math.Pow(RenderEngine.PrintJob.SelectedPrinter.PrinterXYResolutionAsInt / 1000f, 2);
                        RenderEngine.PrintJob.TotalPrintVolume = (float)Math.Ceiling((decimal)(currentPixelVolume * (layerThickness / 1000f) * RenderEngine.TotatAmountofActivePixel));

                        #endregion

                        Debug.WriteLine("Total slices with white pixels: " + (TotalAmountSlices - _emptyPNGs.Count).ToString());
                        PostRender();

                        RenderToPrintjobCompleted?.Invoke(PrintJob, null);
                    }
                }
            }
            catch (Exception exc)
            {
            }

            return(sliceInfo.SliceIndex);
        }
Exemple #9
0
        public static object Render()
        {
            _asyncTasks.Clear();

            TotalProcessedSlices     = 0;
            TotatAmountofActivePixel = 0;
            //define zip and bitmap stream
            _emptyPNGs               = new SortedList <int, int>();
            pixelValues              = new byte[RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionX * RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionY * 3];
            pixelBleedingValues      = new byte[RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionX * RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionY * 3];
            _renderBitmap            = new Bitmap(RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionX, RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionY, PixelFormat.Format24bppRgb);
            _renderBitmapXYSmoothing = (Bitmap)_renderBitmap.Clone();

            if (_zipStream == null || _zipStream.IsFinished)
            {
                //remove diacritics from filepath
                PrintJob.SlicesPath = Path.Combine(Settings.RenderEnginePrintjobsPath, Helpers.StringHelper.RemoveDiacritics(PrintJob.Name));
                var hasLocalWriteAccess = FileSystemManager.HasWriteAccess(Settings.BasePath);
                if (!hasLocalWriteAccess)
                {
                    PrintJob.SlicesPath = Path.Combine(Settings.RenderEngineRoamingPrintjobsPath, Helpers.StringHelper.RemoveDiacritics(PrintJob.Name));
                }

                if (!Directory.Exists(PrintJob.SlicesPath))
                {
                    Directory.CreateDirectory(PrintJob.SlicesPath);
                }
                _tempFileStream = new FileStream(Path.Combine(PrintJob.SlicesPath, "slices.zip"), FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                _zipStream      = new ZipOutputStream(_tempFileStream);

                //checksum file
                var checksumFilePath = Path.Combine(PrintJob.SlicesPath, "checksum.crc");
                if (File.Exists(checksumFilePath))
                {
                    File.Delete(checksumFilePath);
                }
                using (FileStream checksumFile = new FileStream(checksumFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                }
            }

            try
            {
                _stopWatch = new Stopwatch();
                _stopWatch.Start();


                //var sliceIndex = 0;
                var sliceCount = 0;

                //update triangles with min/max z
                var topPoint = 0f;
                foreach (var object3d in ObjectView.Objects3D)
                {
                    if (object3d is STLModel3D && !(object3d is Core.Models.Defaults.BasicCorrectionModel))
                    {
                        var stlModel = (STLModel3D)object3d;
                        // if (UserProfileManager.UserProfiles[0].Settings_PrintJob_MirrorObjects) stlModel.Mirror. (false, false);
                        stlModel.UpdateTrianglesMinMaxZ();
                        if (stlModel.TopPoint > topPoint)
                        {
                            topPoint = stlModel.TopPoint;
                        }
                    }
                }

                //
                //determine slicecount
                var initialLayersHeight = 0f;
                for (var initialLayerIndex = 1; initialLayerIndex < PrintJob.Material.InitialLayers + 1; initialLayerIndex++)
                {
                    initialLayersHeight = (float)PrintJob.Material.LT1 * initialLayerIndex;

                    sliceCount++;

                    if (initialLayersHeight > topPoint)
                    {
                        break;
                    }
                }

                if (initialLayersHeight < topPoint)
                {
                    var remainingLayersHeight = topPoint - initialLayersHeight;
                    var remainingLayersCount  = (remainingLayersHeight / PrintJob.Material.LT2) + 1;

                    for (var remainingLayerIndex = 0; remainingLayerIndex < remainingLayersCount; remainingLayerIndex++)
                    {
                        sliceCount++;
                    }
                }


                var highestModelIndex = 0;
                TotalAmountSlices = 0;
                foreach (var object3d in ObjectView.Objects3D)
                {
                    if (object3d is STLModel3D && (!(object3d is GroundPane)))
                    {
                        var stlModel = object3d as STLModel3D;
                        //  stlModel.Triangles.UpdateConnections();
                        stlModel.CalcSliceIndexes(PrintJob.Material, true);
                        if (stlModel.SliceIndexes.Count >= TotalAmountSlices)
                        {
                            TotalAmountSlices = stlModel.SliceIndexes.Count;
                            highestModelIndex = stlModel.Index;
                        }
                    }
                }

                var currentSliceIndex = 0;
                if (highestModelIndex > 0 && TotalAmountSlices > 0)
                {
                    SortedDictionary <float, List <TriangleConnectionInfo> > stlModelIndexes = null;
                    foreach (var object3d in ObjectView.Objects3D)
                    {
                        if (object3d is STLModel3D && (!(object3d is GroundPane)))
                        {
                            var stlModel = object3d as STLModel3D;
                            if (stlModel.Index == highestModelIndex)
                            {
                                stlModelIndexes = stlModel.SliceIndexes;
                                break;
                            }
                        }
                    }

                    foreach (var sliceHeight in stlModelIndexes.Keys)
                    {
                        if (!_cancelRendering)
                        {
                            var sliceIndexAsync = currentSliceIndex;
                            var sliceInfo       = new Slices.Slice(sliceIndexAsync, new List <SlicePolyLine3D>(), new List <List <SlicePolyLine3D> >(), new List <SlicePolyLine3D>(), sliceHeight);

                            if (PerformanceSettingsManager.Settings.PrintJobGenerationMultiThreading && sliceInfo.SliceIndex > 0)
                            {
                                _asyncTasks.Add(Task.Run(() => RenderSliceThread(sliceInfo)));
                            }
                            else
                            {
                                RenderSliceThread(sliceInfo);
                            }
                        }

                        currentSliceIndex++;
                    }
                }

                //cleanjob image
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
            }

            return(null);
        }
 public void PostSlice(Slices.Slice renderSlice, DAL.Hardware.AtumPrinter selectedPrinter, PluginTypes.PostSliceActionType postSliceActions)
 {
 }