|
| 1 | +/* |
| 2 | +---- Copyright Start ---- |
| 3 | +
|
| 4 | +This file is part of the OpenVectorFormatTools collection. This collection provides tools to facilitate the usage of the OpenVectorFormat. |
| 5 | +
|
| 6 | +Copyright (C) 2024 Digital-Production-Aachen |
| 7 | +
|
| 8 | +This library is free software; you can redistribute it and/or |
| 9 | +modify it under the terms of the GNU Lesser General Public |
| 10 | +License as published by the Free Software Foundation; either |
| 11 | +version 2.1 of the License, or (at your option) any later version. |
| 12 | +
|
| 13 | +This library is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | +Lesser General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU Lesser General Public |
| 19 | +License along with this library; if not, write to the Free Software |
| 20 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | +
|
| 22 | +---- Copyright End ---- |
| 23 | +*/ |
| 24 | + |
| 25 | +using ILTFileReader.OVFToCLIAdapter; |
| 26 | +using OpenVectorFormat; |
| 27 | +using OpenVectorFormat.AbstractReaderWriter; |
| 28 | +using OpenVectorFormat.ILTFileReader; |
| 29 | +using OpenVectorFormat.ILTFileReader.Controller; |
| 30 | +using System; |
| 31 | +using System.Collections.Generic; |
| 32 | +using System.IO; |
| 33 | +using System.Runtime.CompilerServices; |
| 34 | +using System.Text; |
| 35 | +using System.Threading.Tasks; |
| 36 | +using static OpenVectorFormat.ILTFileReader.Controller.CliFileAccess; |
| 37 | + |
| 38 | +namespace ILTFileReaderAdapter.OVFToCLIAdapter |
| 39 | +{ |
| 40 | + public class CLIWriterAdapter : FileWriter |
| 41 | + { |
| 42 | + public override FileWriteOperation FileOperationInProgress => _fileOperationInProgress; |
| 43 | + |
| 44 | + private FileWriteOperation _fileOperationInProgress = FileWriteOperation.None; |
| 45 | + |
| 46 | + private Job jobShell; |
| 47 | + private IFileReaderWriterProgress progress; |
| 48 | + |
| 49 | + private static List<string> fileFormats = new List<string>() { ".cli" }; |
| 50 | + private BinaryWriter binaryWriter; |
| 51 | + private StreamWriter steamWriter; |
| 52 | + private CliFileAccess cliAdapter = new CliFileAccess(); |
| 53 | + |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// List of file format extensions supported by this file reader. |
| 57 | + /// </summary> |
| 58 | + public static new List<string> SupportedFileFormats => fileFormats; |
| 59 | + |
| 60 | + public override Job JobShell => jobShell; |
| 61 | + |
| 62 | + public override void Dispose() |
| 63 | + { |
| 64 | + binaryWriter?.Dispose(); |
| 65 | + steamWriter?.Dispose(); |
| 66 | + } |
| 67 | + |
| 68 | + public override Task SimpleJobWriteAsync(Job job, string filename, IFileReaderWriterProgress progress) |
| 69 | + { |
| 70 | + |
| 71 | + var map = new Dictionary<int, Tuple<float, float>>(); |
| 72 | + |
| 73 | + if (CliPlus) |
| 74 | + { |
| 75 | + foreach (var part in job.MarkingParamsMap) |
| 76 | + { |
| 77 | + map.Add(part.Key, Tuple.Create(part.Value.LaserPowerInW, part.Value.LaserSpeedInMmPerS)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + var adapter = new CliFileAccess(map); |
| 82 | + adapter.WriteFile(filename, new OVFCliJob(job) { Units = CliFormatSettings.Instance.Units }); |
| 83 | + |
| 84 | + return Task.CompletedTask; |
| 85 | + } |
| 86 | + |
| 87 | + public override void StartWritePartial(Job jobShell, string filename, IFileReaderWriterProgress progress) |
| 88 | + { |
| 89 | + this.jobShell = jobShell; |
| 90 | + this.progress = progress; |
| 91 | + |
| 92 | + var map = new Dictionary<int, Tuple<float, float>>(); |
| 93 | + |
| 94 | + if (CliPlus) |
| 95 | + { |
| 96 | + foreach (var part in jobShell.MarkingParamsMap) |
| 97 | + { |
| 98 | + map.Add(part.Key, Tuple.Create(part.Value.LaserPowerInW, part.Value.LaserSpeedInMmPerS)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + using (var sW = new StreamWriter(filename, false)) |
| 103 | + { |
| 104 | + WriteHeader(sW, new OVFCliJob(jobShell) { Units = CliFormatSettings.Instance.Units }); |
| 105 | + } |
| 106 | + |
| 107 | + cliAdapter = new CliFileAccess(map) ; |
| 108 | + |
| 109 | + if (CliFormatSettings.Instance.dataFormatType == DataFormatType.binary) |
| 110 | + binaryWriter = new BinaryWriter(new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None)); |
| 111 | + else |
| 112 | + steamWriter = new StreamWriter(new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None), Encoding.ASCII); |
| 113 | + } |
| 114 | + |
| 115 | + public override void SimpleJobWrite(Job job, string filename, IFileReaderWriterProgress progress = null) |
| 116 | + { |
| 117 | + var map = new Dictionary<int, Tuple<float, float>>(); |
| 118 | + |
| 119 | + if (CliPlus) |
| 120 | + { |
| 121 | + foreach (var part in job.MarkingParamsMap) |
| 122 | + { |
| 123 | + map.Add(part.Key, Tuple.Create(part.Value.LaserPowerInW, part.Value.LaserSpeedInMmPerS)); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + var adapter = new CliFileAccess(map); |
| 128 | + adapter.WriteFile(filename, new OVFCliJob(job) { Units = CliFormatSettings.Instance.Units }); |
| 129 | + } |
| 130 | + |
| 131 | + public override void AppendWorkPlane(WorkPlane workPlane) |
| 132 | + { |
| 133 | + if(CliFormatSettings.Instance.dataFormatType == DataFormatType.binary) |
| 134 | + cliAdapter.AppendLayer(binaryWriter, new OVFCliLayer(workPlane)); |
| 135 | + else |
| 136 | + cliAdapter.AppendLayer(steamWriter, new OVFCliLayer(workPlane)); |
| 137 | + } |
| 138 | + |
| 139 | + public override void AppendVectorBlock(VectorBlock block) |
| 140 | + { |
| 141 | + throw new NotImplementedException(); |
| 142 | + } |
| 143 | + |
| 144 | + public void WriteStartGeometry() |
| 145 | + { |
| 146 | + if(CliFormatSettings.Instance.dataFormatType == DataFormatType.ASCII) |
| 147 | + { |
| 148 | + steamWriter.WriteLine(""); |
| 149 | + steamWriter.WriteLine("$$GEOMETRYSTART"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public void WriteEndGeometry() |
| 154 | + { |
| 155 | + if (CliFormatSettings.Instance.dataFormatType == DataFormatType.ASCII) |
| 156 | + { |
| 157 | + steamWriter.WriteLine(""); |
| 158 | + steamWriter.WriteLine("$$GEOMETRYEND"); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments