File size: 14,031 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 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2017 sliptonic <shopinthewoods@gmail.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
import Path.Base.SetupSheet as PathSetupSheet
import json
import sys
# Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
from CAMTests.PathTestUtils import PathTestBase
def refstring(string):
return string.replace(" u'", " '")
class SomeOp(object):
def __init__(self, obj):
Path.Log.track(obj, type(obj))
obj.addProperty("App::PropertyPercent", "StepOver", "Base", "Some help you are")
@classmethod
def SetupProperties(cls):
return ["StepOver"]
@classmethod
def Create(cls, name, obj=None, parentJob=None):
Path.Log.track(name, obj)
if obj is None:
obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", name)
obj.Proxy = SomeOp(obj)
return obj
class TestPathSetupSheet(PathTestBase):
def setUp(self):
self.doc = FreeCAD.newDocument("TestPathSetupSheet")
def tearDown(self):
FreeCAD.closeDocument(self.doc.Name)
def test00(self):
"""Verify SetupSheet templateAttributes"""
ss = PathSetupSheet.Create().Proxy
self.doc.recompute()
attrs = ss.templateAttributes(True, True)
self.assertEqualLocale(attrs[PathSetupSheet.Template.HorizRapid], "0.00 mm/s")
self.assertEqualLocale(attrs[PathSetupSheet.Template.VertRapid], "0.00 mm/s")
self.assertEqualLocale(attrs[PathSetupSheet.Template.SafeHeightOffset], "3.00 mm")
self.assertEqual(
attrs[PathSetupSheet.Template.SafeHeightExpression],
"OpStockZMax+SetupSheet.SafeHeightOffset",
)
self.assertEqualLocale(attrs[PathSetupSheet.Template.ClearanceHeightOffset], "5.00 mm")
self.assertEqual(
attrs[PathSetupSheet.Template.ClearanceHeightExpression],
"OpStockZMax+SetupSheet.ClearanceHeightOffset",
)
def test01(self):
"""Verify SetupSheet template attributes roundtrip."""
o1 = PathSetupSheet.Create()
self.doc.recompute()
o1.VertRapid = "10 mm/s"
o1.HorizRapid = "22 mm/s"
o1.SafeHeightOffset = "18 mm"
o1.SafeHeightExpression = "Hugo+Olga"
o1.ClearanceHeightOffset = "23 mm"
o1.ClearanceHeightExpression = "Peter+Paul"
o1.StartDepthExpression = "Alpha"
o1.FinalDepthExpression = "Omega"
o1.StepDownExpression = "1"
o2 = PathSetupSheet.Create()
self.doc.recompute()
o2.Proxy.setFromTemplate(o1.Proxy.templateAttributes())
self.doc.recompute()
# Need to compare the UserString's due to rounding errors depending on the
# user's unit settings - should have no impact on the validity
self.assertEqual(o1.VertRapid.UserString, o2.VertRapid.UserString)
self.assertEqual(o1.HorizRapid.UserString, o2.HorizRapid.UserString)
self.assertEqual(o1.SafeHeightOffset.UserString, o2.SafeHeightOffset.UserString)
self.assertEqual(o1.SafeHeightExpression, o2.SafeHeightExpression)
self.assertEqual(o1.ClearanceHeightOffset.UserString, o2.ClearanceHeightOffset.UserString)
self.assertEqual(o1.ClearanceHeightExpression, o2.ClearanceHeightExpression)
self.assertEqual(o1.StartDepthExpression, o2.StartDepthExpression)
self.assertEqual(o1.FinalDepthExpression, o2.FinalDepthExpression)
self.assertEqual(o1.StepDownExpression, o2.StepDownExpression)
def test02(self):
"""Verify default value detection logic."""
obj = PathSetupSheet.Create()
ss = obj.Proxy
self.assertTrue(ss.hasDefaultToolRapids())
self.assertTrue(ss.hasDefaultOperationHeights())
self.assertTrue(ss.hasDefaultOperationDepths())
obj.VertRapid = "1 mm/s"
self.assertFalse(ss.hasDefaultToolRapids())
obj.VertRapid = "0 mm/s"
self.assertTrue(ss.hasDefaultToolRapids())
obj.HorizRapid = "1 mm/s"
self.assertFalse(ss.hasDefaultToolRapids())
obj.HorizRapid = "0 mm/s"
self.assertTrue(ss.hasDefaultToolRapids())
obj.SafeHeightOffset = "0 mm"
self.assertFalse(ss.hasDefaultOperationHeights())
obj.SafeHeightOffset = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultSafeHeightOffset
)
self.assertTrue(ss.hasDefaultOperationHeights())
obj.ClearanceHeightOffset = "0 mm"
self.assertFalse(ss.hasDefaultOperationHeights())
obj.ClearanceHeightOffset = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultClearanceHeightOffset
)
self.assertTrue(ss.hasDefaultOperationHeights())
obj.SafeHeightExpression = "0 mm"
self.assertFalse(ss.hasDefaultOperationHeights())
obj.SafeHeightExpression = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultSafeHeightExpression
)
self.assertTrue(ss.hasDefaultOperationHeights())
obj.ClearanceHeightExpression = "0 mm"
self.assertFalse(ss.hasDefaultOperationHeights())
obj.ClearanceHeightExpression = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultClearanceHeightExpression
)
self.assertTrue(ss.hasDefaultOperationHeights())
obj.StartDepthExpression = ""
self.assertFalse(ss.hasDefaultOperationDepths())
obj.StartDepthExpression = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultStartDepthExpression
)
self.assertTrue(ss.hasDefaultOperationDepths())
obj.FinalDepthExpression = ""
self.assertFalse(ss.hasDefaultOperationDepths())
obj.FinalDepthExpression = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultFinalDepthExpression
)
self.assertTrue(ss.hasDefaultOperationDepths())
obj.StepDownExpression = ""
self.assertFalse(ss.hasDefaultOperationDepths())
obj.StepDownExpression = ss.decodeAttributeString(
PathSetupSheet.SetupSheet.DefaultStepDownExpression
)
self.assertTrue(ss.hasDefaultOperationDepths())
def test10(self):
"""Verify template attributes encoding/decoding of floats."""
ss = PathSetupSheet.Create().Proxy
self.assertEqual(ss.expressionReference(), "SetupSheet")
self.assertEqual(str(ss.encodeTemplateAttributes({"00": 13.00})), "{'00': 13.0}")
self.assertEqual(str(ss.decodeTemplateAttributes({"00": 13.00})), "{'00': 13.0}")
def test11(self):
"""Verify template attributes encoding/decoding of strings."""
ss = PathSetupSheet.Create().Proxy
self.assertEqual(
str(ss.encodeTemplateAttributes({"00": "hugo"})),
refstring("{'00': u'hugo'}"),
)
self.assertEqual(
str(ss.encodeTemplateAttributes({"00": "SetupSheet"})),
refstring("{'00': u'${SetupSheet}'}"),
)
self.assertEqual(
str(ss.encodeTemplateAttributes({"00": "SetupSheet.y"})),
refstring("{'00': u'${SetupSheet}.y'}"),
)
self.assertEqual(
str(ss.encodeTemplateAttributes({"00": "${hugo}"})),
refstring("{'00': u'${hugo}'}"),
)
self.assertEqual(
str(ss.decodeTemplateAttributes({"00": "hugo"})),
refstring("{'00': u'hugo'}"),
)
self.assertEqual(
str(ss.decodeTemplateAttributes({"00": "${SetupSheet}"})),
refstring("{'00': u'SetupSheet'}"),
)
self.assertEqual(
str(ss.decodeTemplateAttributes({"00": "${SetupSheet}.y"})),
refstring("{'00': u'SetupSheet.y'}"),
)
self.assertEqual(
str(ss.decodeTemplateAttributes({"00": "${SetupSheet}.y - ${SetupSheet}.z"})),
refstring("{'00': u'SetupSheet.y - SetupSheet.z'}"),
)
def test12(self):
"""Verify template attributes encoding/decoding of dictionaries."""
ss = PathSetupSheet.Create().Proxy
self.assertEqual(
str(ss.encodeTemplateAttributes({"00": {"01": "hugo"}})),
refstring("{'00': {'01': u'hugo'}}"),
)
self.assertEqual(
str(ss.encodeTemplateAttributes({"00": {"01": "SetupSheet.y - SetupSheet.z"}})),
refstring("{'00': {'01': u'${SetupSheet}.y - ${SetupSheet}.z'}}"),
)
self.assertEqual(
str(ss.decodeTemplateAttributes({"00": {"01": "hugo"}})),
refstring("{'00': {'01': u'hugo'}}"),
)
self.assertEqual(
str(ss.decodeTemplateAttributes({"00": {"01": "${SetupSheet}.y - ${SetupSheet}.z"}})),
refstring("{'00': {'01': u'SetupSheet.y - SetupSheet.z'}}"),
)
def test13(self):
"""Verify template attributes encoding/decoding of lists."""
ss = PathSetupSheet.Create().Proxy
attrs = {}
attrs["00"] = "x.SetupSheet"
attrs["01"] = [{"10": "SetupSheet", "11": "SetupSheet.y"}, {"20": "SetupSheet"}]
attrs["02"] = [
{
"a": [{"b": "SetupSheet"}, {"c": "SetupSheet"}],
"b": [{"b": "SetupSheet"}],
}
]
encoded = ss.encodeTemplateAttributes(attrs)
self.assertEqual(encoded["00"], "x.${SetupSheet}")
self.assertEqual(len(encoded["01"]), 2)
self.assertEqual(encoded["01"][0]["10"], "${SetupSheet}")
self.assertEqual(encoded["01"][0]["11"], "${SetupSheet}.y")
self.assertEqual(str(encoded["01"][1]), refstring("{'20': u'${SetupSheet}'}"))
self.assertEqual(len(encoded["02"]), 1)
self.assertEqual(len(encoded["02"][0]["a"]), 2)
self.assertEqual(str(encoded["02"][0]["a"][0]), refstring("{'b': u'${SetupSheet}'}"))
self.assertEqual(str(encoded["02"][0]["a"][1]), refstring("{'c': u'${SetupSheet}'}"))
self.assertEqual(len(encoded["02"][0]["b"]), 1)
self.assertEqual(str(encoded["02"][0]["b"][0]), refstring("{'b': u'${SetupSheet}'}"))
decoded = ss.decodeTemplateAttributes(encoded)
self.assertEqual(len(decoded), len(attrs))
self.assertEqual(decoded["00"], attrs["00"])
self.assertEqual(len(decoded["01"]), len(attrs["01"]))
self.assertEqual(decoded["01"][0]["10"], attrs["01"][0]["10"])
self.assertEqual(decoded["01"][0]["11"], attrs["01"][0]["11"])
self.assertEqual(decoded["01"][1]["20"], attrs["01"][1]["20"])
self.assertEqual(len(decoded["02"]), len(attrs["02"]))
self.assertEqual(len(decoded["02"][0]["a"]), len(attrs["02"][0]["a"]))
self.assertEqual(decoded["02"][0]["a"][0]["b"], attrs["02"][0]["a"][0]["b"])
self.assertEqual(decoded["02"][0]["a"][1]["c"], attrs["02"][0]["a"][1]["c"])
self.assertEqual(len(decoded["02"][0]["b"]), len(attrs["02"][0]["b"]))
self.assertEqual(decoded["02"][0]["b"][0]["b"], attrs["02"][0]["b"][0]["b"])
# just to be safe ...
s2 = PathSetupSheet.Create().Proxy
self.doc.recompute()
s2.setFromTemplate(ss.templateAttributes())
self.assertEqual(s2.expressionReference(), "SetupSheet001")
dec = s2.decodeTemplateAttributes(encoded)
# pick one
self.assertEqual(dec["01"][0]["11"], "SetupSheet001.y")
def test20(self):
"""Verify SetupSheet template op attributes roundtrip."""
opname = "whoop"
o1 = PathSetupSheet.Create()
PathSetupSheet.RegisterOperation(opname, SomeOp.Create, SomeOp.SetupProperties)
ptt = PathSetupSheet._RegisteredOps[opname].prototype("whoopsy")
pptt = ptt.getProperty("StepOver")
pptt.setupProperty(
o1,
PathSetupSheet.OpPropertyName(opname, pptt.name),
PathSetupSheet.OpPropertyGroup(opname),
75,
)
# save setup sheet in json "file"
attrs = o1.Proxy.templateAttributes(False, False, False, False, [opname])
encdd = o1.Proxy.encodeTemplateAttributes(attrs)
j1 = json.dumps({"SetupSheet": encdd}, sort_keys=True, indent=2)
# restore setup sheet from json "file"
j2 = json.loads(j1)
o2 = PathSetupSheet.Create()
o2.Proxy.setFromTemplate(j2["SetupSheet"])
op = SomeOp.Create(opname)
self.assertEqual(op.StepOver, 0)
o2.Proxy.setOperationProperties(op, opname)
self.assertEqual(op.StepOver, 75)
|