Ejemplo n.º 1
0
        private static void OutputSummary(ScanFile.ScanFile scanFile, string summaryFileName)
        {
            float minX = float.MaxValue;
            float minY = float.MaxValue;
            float maxX = float.MinValue;
            float maxY = float.MinValue;

            StreamWriter writer = new StreamWriter(File.Open(summaryFileName, FileMode.Create));

            writer.WriteLine(scanFile.Version);
            writer.WriteLine($"Layer: {scanFile.Layer}");
            writer.WriteLine();

            writer.WriteLine("Parameter Sets");
            writer.WriteLine("ID\tType\tLaser Power\tLaser Speed");

            foreach (ParameterSet parameterSet in scanFile.ParameterSets)
            {
                writer.WriteLine($"{parameterSet.Id}\t{parameterSet.Type}\t{parameterSet.LaserPower}\t{parameterSet.LaserSpeed}");
            }

            writer.WriteLine();

            writer.WriteLine("Contours");
            writer.WriteLine("Count\tType\tx1, y1, x2, y2 ...");

            foreach (Contour contour in scanFile.Contours)
            {
                string line = $"{contour.Points.Count}\t{contour.Type}\t";
                foreach (Point point in contour.Points)
                {
                    line += $"{point.X}\t{point.Y}\t";
                }
                writer.WriteLine(line);
            }

            writer.WriteLine();

            foreach (ScanLineBlock block in scanFile.ScanLineBlocks)
            {
                writer.WriteLine(string.Empty);
                writer.WriteLine($"Scan Area Index: {block.ScanAreaId}");
                writer.WriteLine($"Rotation Angle: {block.RotationAngle}");
                writer.WriteLine($"Parameter Set: {block.ParameterSetId}");
                writer.WriteLine("Scan Lines");
                writer.WriteLine("x1\ty1\tx2\ty2");

                foreach (ScanLine scanLine in block.ScanLines)
                {
                    UpdateMinMax(scanLine, ref minX, ref minY, ref maxX, ref maxY);
                    writer.WriteLine($"{scanLine.X1}\t{scanLine.Y1}\t{scanLine.X2}\t{scanLine.Y2}");
                }

                writer.WriteLine();
            }

            writer.WriteLine($"Min x,y\tMax x,y\n{minX},{minY}\t{maxX},{maxY}\n");

            writer.Dispose();
        }
        private static void ReadContourBlock(ScanFile.ScanFile scanFile, byte[] data)
        {
            int offset       = 0;
            int contourCount = BitConverter.ToInt32(data, offset);

            offset += sizeof(int);

            for (int i = 0; i < contourCount; i++)
            {
                var contour = new Contour();

                contour.Type = BitConverter.ToInt32(data, offset);
                offset      += sizeof(int);

                int pointCount = BitConverter.ToInt32(data, offset);
                offset += sizeof(int);

                for (int j = 0; j < pointCount; j++)
                {
                    float x = BitConverter.ToSingle(data, offset);
                    offset += sizeof(float);
                    float y = BitConverter.ToSingle(data, offset);
                    offset += sizeof(float);

                    contour.Points.Add(new Point(x, y));
                }

                scanFile.Contours.Add(contour);
            }
        }
        public static ScanFile.ScanFile Read(string fileName)
        {
            var scanFile = new ScanFile.ScanFile();

            BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open));

            while (reader.BaseStream.Position != reader.BaseStream.Length)
            {
                int    blockType = reader.ReadInt32();
                int    blockSize = reader.ReadInt32();
                byte[] data      = reader.ReadBytes(blockSize);

                if (blockType == DATA_BLOCK_VERSION)
                {
                    ReadVersionBlock(scanFile, data);
                }
                else if (blockType == DATA_BLOCK_LAYER)
                {
                    ReadLayerBlock(scanFile, data);
                }
                else if (blockType == DATA_BLOCK_CONTOURS)
                {
                    ReadContourBlock(scanFile, data);
                }
                else if (blockType == DATA_BLOCK_PARAMETER_SETS)
                {
                    ReadParameterSetBlock(scanFile, data);
                }
                else if (blockType == DATA_BLOCK_SCAN_LINES)
                {
                    ReadScanLineBlock(scanFile, data);
                }
                else
                {
                    throw new System.Exception($"Unknown block type: {blockType}");
                }
            }

            reader.Dispose();
            return(scanFile);
        }
        private static void ReadParameterSetBlock(ScanFile.ScanFile scanFile, byte[] data)
        {
            int offset            = 0;
            int parameterSetCount = BitConverter.ToInt32(data, offset);

            offset += sizeof(int);

            for (int i = 0; i < parameterSetCount; i++)
            {
                // get fields
                int id = BitConverter.ToInt32(data, offset);
                offset += sizeof(int);
                int type = BitConverter.ToInt32(data, offset);
                offset += sizeof(int);
                int laserPower = BitConverter.ToInt32(data, offset);
                offset += sizeof(int);
                float laserSpeed = BitConverter.ToSingle(data, offset);
                offset += sizeof(float);

                scanFile.ParameterSets.Add(new ParameterSet(id, type, laserPower, laserSpeed));
            }
        }
        private static void ReadScanLineBlock(ScanFile.ScanFile scanFile, byte[] data)
        {
            int           offset = 0;
            ScanLineBlock block  = new ScanLineBlock();

            // count
            int scanLineCount = BitConverter.ToInt32(data, offset);

            offset += sizeof(int);

            // parameter set id
            block.ParameterSetId = BitConverter.ToInt32(data, offset);
            offset += sizeof(int);

            // rotation angle
            block.RotationAngle = BitConverter.ToSingle(data, offset);
            offset += sizeof(float);

            // Scan Area Id
            block.ScanAreaId = BitConverter.ToInt32(data, offset);
            offset          += sizeof(int);

            for (int i = 0; i < scanLineCount; i++)
            {
                float x1 = BitConverter.ToSingle(data, offset);
                offset += sizeof(float);
                float y1 = BitConverter.ToSingle(data, offset);
                offset += sizeof(float);
                float x2 = BitConverter.ToSingle(data, offset);
                offset += sizeof(float);
                float y2 = BitConverter.ToSingle(data, offset);
                offset += sizeof(float);

                block.ScanLines.Add(new ScanLine(x1, y1, x2, y2));
            }

            scanFile.ScanLineBlocks.Add(block);
        }
 private static void ReadLayerBlock(ScanFile.ScanFile scanFile, byte[] data)
 {
     scanFile.Layer = BitConverter.ToInt32(data, 0);
 }
 private static void ReadVersionBlock(ScanFile.ScanFile scanFile, byte[] data)
 {
     scanFile.Version = Encoding.ASCII.GetString(data, 0, data.Length);
 }