File size: 13,973 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2024 Ondsel <development@ondsel.com> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD
import Path
from Path.Main.Sanity import ReportGenerator, Sanity
from Path.Main.Sanity.ImageBuilder import (
DummyImageBuilder,
ImageBuilderFactory,
ImageBuilder,
)
import os
import Path.Post.Command as PathPost
from Path.Post.Processor import PostProcessor
import unittest
from unittest.mock import patch, MagicMock
import urllib
import tempfile
from CAMTests.PathTestUtils import PathTestBase
class TestCAMSanity(PathTestBase):
@classmethod
def setUpClass(cls):
FreeCAD.ConfigSet("SuppressRecomputeRequiredDialog", "True")
cls.doc = FreeCAD.open(FreeCAD.getHomePath() + "/Mod/CAM/CAMTests/boxtest.fcstd")
FreeCAD.ConfigSet("SuppressRecomputeRequiredDialog", "")
cls.job = cls.doc.getObject("Job")
@classmethod
def tearDownClass(cls):
FreeCAD.closeDocument("boxtest")
def setUp(self):
self.temp_file = tempfile.NamedTemporaryFile()
def tearDown(self):
pass
def test00(self):
"""Test no output location"""
with self.assertRaises(TypeError):
S = Sanity.CAMSanity(self.job)
def test010(self):
# """Test CAMSanity using a DummyImageBuilder"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
self.assertTrue(os.path.isdir(S.filelocation))
self.assertEqual(self.temp_file.name, S.output_file)
def test020(self):
"""Test erroneous output location"""
filename = "/tmpXXXX/THIS_DOESN'T_EXIST"
with self.assertRaises(ValueError):
S = Sanity.CAMSanity(self.job, output_file=filename)
# This test fails A headless image generation routine is needed.
# def test40(self):
# """Test image generation"""
# import imghdr # fixme: not available in python3.13
# path = FreeCAD.getUserMacroDir()
# image_builder = ImageBuilder.ImageBuilderFactory.get_image_builder(path)
# file_name = image_builder.build_image(self.doc.getObject("Box"), "theBox")
# print(f"filename:{file_name}")
# # This test shouldn't be enabled in production as is.
# # it writes to the user macro directory as a persistent location.
# # writing to tempdir fails
# # the tempfile get cleaned up before we can do any asserts.
# # Need to find a way to write semi-persistently for the tests to pass
# with open(file_name, 'rb') as f:
# header = f.read(32) # Read the first 32 bytes
# self.assertTrue(imghdr.what(None, header) is not None)
def test050(self):
"""Test base data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._baseObjectData()
self.assertIsInstance(data, dict)
self.assertIn("baseimage", data)
self.assertIn("bases", data)
def test060(self):
"""Test design data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._designData()
self.assertIsInstance(data, dict)
self.assertIn("FileName", data)
self.assertIn("LastModifiedDate", data)
self.assertIn("Customer", data)
self.assertIn("Designer", data)
self.assertIn("JobDescription", data)
self.assertIn("JobLabel", data)
self.assertIn("Sequence", data)
self.assertIn("JobType", data)
def test070(self):
"""Test tool data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._toolData()
self.assertIn("squawkData", data)
for key in data.keys():
if isinstance(key, int):
val = data["key"]
self.assertIsInstance(val, dict)
self.assertIn("bitShape", val)
self.assertIn("description", val)
self.assertIn("manufacturer", val)
self.assertIn("url", val)
self.assertIn("inspectionNotes", val)
self.assertIn("diameter", val)
self.assertIn("shape", val)
self.assertIn("partNumber", val)
def test080(self):
"""Test run data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._runData()
self.assertIsInstance(data, dict)
self.assertIn("cycletotal", data)
self.assertIn("jobMinZ", data)
self.assertIn("jobMaxZ", data)
self.assertIn("jobDescription", data)
self.assertIn("squawkData", data)
self.assertIn("operations", data)
def test090(self):
"""Test output data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._outputData()
self.assertIsInstance(data, dict)
self.assertIn("lastpostprocess", data)
self.assertIn("lastgcodefile", data)
self.assertIn("optionalstops", data)
self.assertIn("programmer", data)
self.assertIn("machine", data)
self.assertIn("postprocessor", data)
self.assertIn("postprocessorFlags", data)
self.assertIn("filesize", data)
self.assertIn("linecount", data)
self.assertIn("outputfilename", data)
self.assertIn("squawkData", data)
def test100(self):
"""Test fixture data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._fixtureData()
self.assertIsInstance(data, dict)
self.assertIn("fixtures", data)
self.assertIn("orderBy", data)
self.assertIn("datumImage", data)
self.assertIn("squawkData", data)
def test110(self):
"""Test stock data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._stockData()
self.assertIsInstance(data, dict)
self.assertIn("xLen", data)
self.assertIn("yLen", data)
self.assertIn("zLen", data)
self.assertIn("material", data)
self.assertIn("stockImage", data)
self.assertIn("squawkData", data)
def test120(self):
"""Test squawk data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S.squawk(
"CAMSanity",
"Test Message",
squawkType="TIP",
)
self.assertIsInstance(data, dict)
self.assertIn("Date", data)
self.assertIn("Operator", data)
self.assertIn("Note", data)
self.assertIn("squawkType", data)
self.assertIn("squawkIcon", data)
def test130(self):
"""Test tool data"""
with patch(
"Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder"
) as mock_factory:
dummy_builder = DummyImageBuilder(self.temp_file.name)
mock_factory.return_value = dummy_builder
S = Sanity.CAMSanity(self.job, output_file=self.temp_file.name)
data = S._toolData()
self.assertIsInstance(data, dict)
self.assertIn("squawkData", data)
for key, val in data.items():
if key == "squawkData":
continue
else:
self.assertIsInstance(val, dict)
self.assertIn("bitShape", val)
self.assertIn("description", val)
self.assertIn("manufacturer", val)
self.assertIn("url", val)
self.assertIn("inspectionNotes", val)
self.assertIn("diameter", val)
self.assertIn("shape", val)
self.assertIn("partNumber", val)
# def test140(self):
# """Test Generate Report"""
# with patch('Path.Main.Sanity.ImageBuilder.ImageBuilderFactory.get_image_builder') as mock_factory:
# dummy_builder = DummyImageBuilder(self.temp_file.name)
# mock_factory.return_value = dummy_builder
# S = Sanity.CAMSanity(self.job, output_file= self.temp_file.name)
# html_content = S.get_output_report()
# self.assertIsInstance(html_content, str)
# def test150(self):
# """Test Post Processing a File"""
# def exportObjectsWith(objs, partname, job, sequence, postname):
# Path.Log.track(partname, sequence)
# Path.Log.track(objs)
# Path.Log.track(objs, partname)
# postArgs = Path.Preferences.defaultPostProcessorArgs()
# if hasattr(job, "PostProcessorArgs") and job.PostProcessorArgs:
# postArgs = job.PostProcessorArgs
# elif hasattr(job, "PostProcessor") and job.PostProcessor:
# postArgs = ""
# Path.Log.track(postArgs)
# filename = f"output-{partname}-{sequence}.ngc"
# processor = PostProcessor.load(postname)
# gcode = processor.export(objs, filename, postArgs)
# return (gcode, filename)
# doc = FreeCAD.open(FreeCAD.getHomePath() + "/Mod/CAM/CAMTests/boxtest.fcstd")
# job = self.doc.getObject("Job")
# postlist = PathPost.buildPostList(job)
# filenames = []
# for idx, section in enumerate(postlist):
# partname = section[0]
# sublist = section[1]
# gcode, name = exportObjectsWith(sublist, partname, job, idx, "linuxcnc")
# filenames.append(name)
# with tempfile.TemporaryDirectory() as temp_dir:
# output_file_path = temp_dir
# output_file_name = os.path.join(output_file_path, "test.ngc")
# def test200(self):
# """Generate Report This test is disabled but useful during development to see the report"""
# with tempfile.NamedTemporaryFile() as temp_file:
# S= Sanity.CAMSanity(self.job, output_file=temp_file.name)
# shape = self.doc.getObject("Box")
# html_content = S.get_output_report()
# encoded_html = urllib.parse.quote(html_content)
# data_uri = f"data:text/html;charset=utf-8,{encoded_html}"
# import webbrowser
# webbrowser.open(data_uri)
|