test_threemf_tools.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. """Unit tests for 3MF parsing utilities (threemf_tools.py).
  2. Tests G-code parsing, filament length-to-weight conversion,
  3. and cumulative layer usage lookup.
  4. """
  5. import io
  6. import json
  7. import math
  8. import zipfile
  9. from backend.app.utils.threemf_tools import (
  10. extract_embedded_presets_from_3mf,
  11. extract_filament_usage_from_3mf,
  12. extract_plate_extruder_set_from_3mf,
  13. extract_project_filaments_from_3mf,
  14. get_cumulative_usage_at_layer,
  15. mm_to_grams,
  16. parse_gcode_layer_filament_usage,
  17. )
  18. def create_mock_3mf(slice_info_content: str) -> io.BytesIO:
  19. """Create a mock 3MF file (ZIP) with slice_info.config content."""
  20. buffer = io.BytesIO()
  21. with zipfile.ZipFile(buffer, "w") as zf:
  22. zf.writestr("Metadata/slice_info.config", slice_info_content)
  23. buffer.seek(0)
  24. return buffer
  25. class TestParseGcodeLayerFilamentUsage:
  26. """Tests for parse_gcode_layer_filament_usage()."""
  27. def test_single_filament_single_layer(self):
  28. """Single filament extruding on one layer."""
  29. gcode = """
  30. M620 S0
  31. G1 X10 Y10 E5.0
  32. G1 X20 Y20 E3.0
  33. """
  34. result = parse_gcode_layer_filament_usage(gcode)
  35. assert result == {0: {0: 8.0}}
  36. def test_multi_layer_single_filament(self):
  37. """Single filament across multiple layers."""
  38. gcode = """
  39. M620 S0
  40. G1 X10 Y10 E10.0
  41. M73 L1
  42. G1 X20 Y20 E5.0
  43. M73 L2
  44. G1 X30 Y30 E7.0
  45. """
  46. result = parse_gcode_layer_filament_usage(gcode)
  47. assert result[0] == {0: 10.0}
  48. assert result[1] == {0: 15.0}
  49. assert result[2] == {0: 22.0}
  50. def test_multi_material(self):
  51. """Multiple filaments switching via M620."""
  52. gcode = """
  53. M620 S0
  54. G1 E10.0
  55. M73 L1
  56. M620 S1
  57. G1 E5.0
  58. M620 S0
  59. G1 E3.0
  60. M73 L2
  61. G1 E2.0
  62. """
  63. result = parse_gcode_layer_filament_usage(gcode)
  64. # Layer 0: filament 0 = 10mm
  65. assert result[0] == {0: 10.0}
  66. # Layer 1: filament 0 = 13mm (10+3), filament 1 = 5mm
  67. assert result[1] == {0: 13.0, 1: 5.0}
  68. # Layer 2: filament 0 = 15mm (13+2)
  69. assert result[2] == {0: 15.0, 1: 5.0}
  70. def test_retractions_ignored(self):
  71. """Negative E values (retractions) should be ignored."""
  72. gcode = """
  73. M620 S0
  74. G1 E10.0
  75. G1 E-2.0
  76. G1 E5.0
  77. """
  78. result = parse_gcode_layer_filament_usage(gcode)
  79. assert result == {0: {0: 15.0}}
  80. def test_m620_s255_unloads(self):
  81. """M620 S255 means unload - extrusion after should be ignored."""
  82. gcode = """
  83. M620 S0
  84. G1 E10.0
  85. M620 S255
  86. G1 E5.0
  87. """
  88. result = parse_gcode_layer_filament_usage(gcode)
  89. assert result == {0: {0: 10.0}}
  90. def test_m620_with_suffix(self):
  91. """M620 S0A format (filament ID with suffix letter)."""
  92. gcode = """
  93. M620 S0A
  94. G1 E10.0
  95. M620 S1A
  96. G1 E5.0
  97. """
  98. result = parse_gcode_layer_filament_usage(gcode)
  99. assert result == {0: {0: 10.0, 1: 5.0}}
  100. def test_comments_ignored(self):
  101. """Comment lines and inline comments are ignored."""
  102. gcode = """
  103. ; This is a comment
  104. M620 S0
  105. G1 X10 E5.0 ; inline comment with E value
  106. G1 E3.0
  107. """
  108. result = parse_gcode_layer_filament_usage(gcode)
  109. assert result == {0: {0: 8.0}}
  110. def test_empty_gcode(self):
  111. """Empty G-code returns empty dict."""
  112. assert parse_gcode_layer_filament_usage("") == {}
  113. assert parse_gcode_layer_filament_usage("\n\n\n") == {}
  114. def test_no_extrusion(self):
  115. """G-code with moves but no extrusion."""
  116. gcode = """
  117. G1 X10 Y10
  118. G1 X20 Y20
  119. """
  120. assert parse_gcode_layer_filament_usage(gcode) == {}
  121. def test_no_active_filament_extrusion_ignored(self):
  122. """Extrusion before any M620 is ignored (no active filament)."""
  123. gcode = """
  124. G1 E10.0
  125. M620 S0
  126. G1 E5.0
  127. """
  128. result = parse_gcode_layer_filament_usage(gcode)
  129. assert result == {0: {0: 5.0}}
  130. def test_g0_g2_g3_extrusion(self):
  131. """G0, G2, G3 with E parameter are also tracked."""
  132. gcode = """
  133. M620 S0
  134. G0 E1.0
  135. G1 E2.0
  136. G2 E3.0
  137. G3 E4.0
  138. """
  139. result = parse_gcode_layer_filament_usage(gcode)
  140. assert result == {0: {0: 10.0}}
  141. def test_cumulative_across_layers(self):
  142. """Values are cumulative, not per-layer."""
  143. gcode = """
  144. M620 S0
  145. G1 E100.0
  146. M73 L1
  147. G1 E100.0
  148. M73 L2
  149. G1 E100.0
  150. """
  151. result = parse_gcode_layer_filament_usage(gcode)
  152. assert result[0] == {0: 100.0}
  153. assert result[1] == {0: 200.0}
  154. assert result[2] == {0: 300.0}
  155. class TestMmToGrams:
  156. """Tests for mm_to_grams()."""
  157. def test_default_pla_175(self):
  158. """Default PLA 1.75mm conversion."""
  159. # 1000mm of 1.75mm PLA at 1.24 g/cm³
  160. # Volume = π × (0.0875cm)² × 100cm = 2.405cm³
  161. # Weight = 2.405 × 1.24 = 2.982g
  162. result = mm_to_grams(1000.0)
  163. expected = math.pi * (0.0875**2) * 100 * 1.24
  164. assert abs(result - expected) < 0.001
  165. def test_zero_length(self):
  166. """Zero length returns zero weight."""
  167. assert mm_to_grams(0.0) == 0.0
  168. def test_custom_diameter(self):
  169. """Custom diameter (2.85mm) changes result."""
  170. result_175 = mm_to_grams(1000.0, diameter_mm=1.75)
  171. result_285 = mm_to_grams(1000.0, diameter_mm=2.85)
  172. # 2.85mm filament has more volume per mm
  173. assert result_285 > result_175
  174. ratio = (2.85 / 1.75) ** 2 # Volume scales with diameter²
  175. assert abs(result_285 / result_175 - ratio) < 0.001
  176. def test_custom_density(self):
  177. """Different density (ABS vs PLA)."""
  178. pla = mm_to_grams(1000.0, density_g_cm3=1.24)
  179. abs_ = mm_to_grams(1000.0, density_g_cm3=1.04)
  180. assert pla > abs_
  181. assert abs(pla / abs_ - 1.24 / 1.04) < 0.001
  182. def test_known_value(self):
  183. """Verify against a known calculation.
  184. 1m (1000mm) of 1.75mm PLA at 1.24 g/cm³:
  185. r = 0.0875 cm, L = 100 cm
  186. V = π × 0.0875² × 100 = 2.4053 cm³
  187. m = 2.4053 × 1.24 = 2.9826 g
  188. """
  189. result = mm_to_grams(1000.0, 1.75, 1.24)
  190. assert abs(result - 2.9826) < 0.01
  191. class TestGetCumulativeUsageAtLayer:
  192. """Tests for get_cumulative_usage_at_layer()."""
  193. def test_exact_layer_match(self):
  194. """Target layer exists exactly in the data."""
  195. data = {0: {0: 100.0}, 5: {0: 500.0}, 10: {0: 1000.0}}
  196. assert get_cumulative_usage_at_layer(data, 5) == {0: 500.0}
  197. def test_between_layers(self):
  198. """Target is between recorded layers - uses the closest lower one."""
  199. data = {0: {0: 100.0}, 5: {0: 500.0}, 10: {0: 1000.0}}
  200. # Layer 7 is between 5 and 10, should return layer 5's data
  201. assert get_cumulative_usage_at_layer(data, 7) == {0: 500.0}
  202. def test_beyond_last_layer(self):
  203. """Target is beyond the last recorded layer."""
  204. data = {0: {0: 100.0}, 5: {0: 500.0}}
  205. assert get_cumulative_usage_at_layer(data, 100) == {0: 500.0}
  206. def test_before_first_layer(self):
  207. """Target is before any recorded data."""
  208. data = {5: {0: 500.0}, 10: {0: 1000.0}}
  209. assert get_cumulative_usage_at_layer(data, 3) == {}
  210. def test_empty_data(self):
  211. """Empty layer_usage returns empty dict."""
  212. assert get_cumulative_usage_at_layer({}, 5) == {}
  213. def test_none_data(self):
  214. """None layer_usage returns empty dict."""
  215. assert get_cumulative_usage_at_layer(None, 5) == {}
  216. def test_multi_filament(self):
  217. """Multi-filament data at target layer."""
  218. data = {
  219. 0: {0: 50.0},
  220. 5: {0: 200.0, 1: 100.0},
  221. 10: {0: 400.0, 1: 250.0, 2: 50.0},
  222. }
  223. result = get_cumulative_usage_at_layer(data, 8)
  224. assert result == {0: 200.0, 1: 100.0}
  225. def test_layer_zero(self):
  226. """Target layer 0."""
  227. data = {0: {0: 10.0}, 1: {0: 20.0}}
  228. assert get_cumulative_usage_at_layer(data, 0) == {0: 10.0}
  229. class TestExtractFilamentUsageFrom3mf:
  230. """Tests for extract_filament_usage_from_3mf function."""
  231. def test_extract_single_filament(self, tmp_path):
  232. """Test extracting a single filament."""
  233. xml_content = """<?xml version="1.0" encoding="UTF-8"?>
  234. <config>
  235. <filament id="1" used_g="50.5" type="PLA" color="#FF0000"/>
  236. </config>
  237. """
  238. mock_3mf = create_mock_3mf(xml_content)
  239. file_path = tmp_path / "test.3mf"
  240. file_path.write_bytes(mock_3mf.read())
  241. result = extract_filament_usage_from_3mf(file_path)
  242. assert len(result) == 1
  243. assert result[0]["slot_id"] == 1
  244. assert result[0]["used_g"] == 50.5
  245. assert result[0]["type"] == "PLA"
  246. assert result[0]["color"] == "#FF0000"
  247. def test_extract_multiple_filaments(self, tmp_path):
  248. """Test extracting multiple filaments."""
  249. xml_content = """<?xml version="1.0" encoding="UTF-8"?>
  250. <config>
  251. <filament id="1" used_g="50.5" type="PLA" color="#FF0000"/>
  252. <filament id="2" used_g="30.2" type="PETG" color="#00FF00"/>
  253. <filament id="3" used_g="10.0" type="ABS" color="#0000FF"/>
  254. </config>
  255. """
  256. mock_3mf = create_mock_3mf(xml_content)
  257. file_path = tmp_path / "test.3mf"
  258. file_path.write_bytes(mock_3mf.read())
  259. result = extract_filament_usage_from_3mf(file_path)
  260. assert len(result) == 3
  261. assert result[0]["slot_id"] == 1
  262. assert result[1]["slot_id"] == 2
  263. assert result[2]["slot_id"] == 3
  264. def test_extract_filament_with_plate_id(self, tmp_path):
  265. """Test extracting filament for a specific plate."""
  266. xml_content = """<?xml version="1.0" encoding="UTF-8"?>
  267. <config>
  268. <plate>
  269. <metadata key="index" value="1"/>
  270. <filament id="1" used_g="25.0" type="PLA" color="#FF0000"/>
  271. </plate>
  272. <plate>
  273. <metadata key="index" value="2"/>
  274. <filament id="1" used_g="75.0" type="PETG" color="#00FF00"/>
  275. </plate>
  276. </config>
  277. """
  278. mock_3mf = create_mock_3mf(xml_content)
  279. file_path = tmp_path / "test.3mf"
  280. file_path.write_bytes(mock_3mf.read())
  281. result = extract_filament_usage_from_3mf(file_path, plate_id=2)
  282. assert len(result) == 1
  283. assert result[0]["used_g"] == 75.0
  284. assert result[0]["type"] == "PETG"
  285. def test_missing_slice_info_returns_empty(self, tmp_path):
  286. """Test that missing slice_info.config returns empty list."""
  287. buffer = io.BytesIO()
  288. with zipfile.ZipFile(buffer, "w") as zf:
  289. zf.writestr("other_file.txt", "content")
  290. buffer.seek(0)
  291. file_path = tmp_path / "test.3mf"
  292. file_path.write_bytes(buffer.read())
  293. result = extract_filament_usage_from_3mf(file_path)
  294. assert result == []
  295. def test_invalid_file_returns_empty(self, tmp_path):
  296. """Test that invalid file returns empty list."""
  297. file_path = tmp_path / "invalid.3mf"
  298. file_path.write_text("not a zip file")
  299. result = extract_filament_usage_from_3mf(file_path)
  300. assert result == []
  301. def test_nonexistent_file_returns_empty(self, tmp_path):
  302. """Test that nonexistent file returns empty list."""
  303. file_path = tmp_path / "nonexistent.3mf"
  304. result = extract_filament_usage_from_3mf(file_path)
  305. assert result == []
  306. def test_filament_without_id_is_skipped(self, tmp_path):
  307. """Test that filament without id is skipped."""
  308. xml_content = """<?xml version="1.0" encoding="UTF-8"?>
  309. <config>
  310. <filament used_g="50.5" type="PLA" color="#FF0000"/>
  311. <filament id="2" used_g="30.0" type="PETG" color="#00FF00"/>
  312. </config>
  313. """
  314. mock_3mf = create_mock_3mf(xml_content)
  315. file_path = tmp_path / "test.3mf"
  316. file_path.write_bytes(mock_3mf.read())
  317. result = extract_filament_usage_from_3mf(file_path)
  318. assert len(result) == 1
  319. assert result[0]["slot_id"] == 2
  320. def test_invalid_used_g_is_skipped(self, tmp_path):
  321. """Test that filament with invalid used_g is skipped."""
  322. xml_content = """<?xml version="1.0" encoding="UTF-8"?>
  323. <config>
  324. <filament id="1" used_g="invalid" type="PLA" color="#FF0000"/>
  325. <filament id="2" used_g="30.0" type="PETG" color="#00FF00"/>
  326. </config>
  327. """
  328. mock_3mf = create_mock_3mf(xml_content)
  329. file_path = tmp_path / "test.3mf"
  330. file_path.write_bytes(mock_3mf.read())
  331. result = extract_filament_usage_from_3mf(file_path)
  332. assert len(result) == 1
  333. assert result[0]["slot_id"] == 2
  334. def test_missing_optional_fields(self, tmp_path):
  335. """Test that missing type and color default to empty string."""
  336. xml_content = """<?xml version="1.0" encoding="UTF-8"?>
  337. <config>
  338. <filament id="1" used_g="50.5"/>
  339. </config>
  340. """
  341. mock_3mf = create_mock_3mf(xml_content)
  342. file_path = tmp_path / "test.3mf"
  343. file_path.write_bytes(mock_3mf.read())
  344. result = extract_filament_usage_from_3mf(file_path)
  345. assert len(result) == 1
  346. assert result[0]["type"] == ""
  347. assert result[0]["color"] == ""
  348. # ---------------------------------------------------------------------------
  349. # Tests for extract_project_filaments_from_3mf — used by the slice modal as
  350. # fallback when the sidecar can't run a preview slice.
  351. # ---------------------------------------------------------------------------
  352. def _make_3mf_with(files: dict[str, bytes | str]) -> zipfile.ZipFile:
  353. buf = io.BytesIO()
  354. with zipfile.ZipFile(buf, "w") as zf:
  355. for name, content in files.items():
  356. zf.writestr(name, content if isinstance(content, (bytes, str)) else str(content))
  357. buf.seek(0)
  358. return zipfile.ZipFile(buf, "r")
  359. class TestExtractProjectFilamentsFrom3mf:
  360. """The helper backfills the slice modal when slice_info.config is empty
  361. (raw project files) and the sidecar is unreachable."""
  362. def test_returns_empty_when_project_settings_missing(self):
  363. with _make_3mf_with({"placeholder.txt": "hi"}) as zf:
  364. assert extract_project_filaments_from_3mf(zf) == []
  365. def test_happy_path_returns_one_entry_per_slot(self):
  366. proj = {
  367. "filament_type": ["PLA", "PETG"],
  368. "filament_colour": ["#000000", "#FFFFFF"],
  369. }
  370. with _make_3mf_with({"Metadata/project_settings.config": json.dumps(proj)}) as zf:
  371. out = extract_project_filaments_from_3mf(zf)
  372. assert [(f["slot_id"], f["type"], f["color"]) for f in out] == [
  373. (1, "PLA", "#000000"),
  374. (2, "PETG", "#FFFFFF"),
  375. ]
  376. def test_mismatched_array_lengths_use_max_with_blanks(self):
  377. proj = {
  378. "filament_type": ["PLA", "PETG", "ABS"],
  379. "filament_colour": ["#000000"],
  380. }
  381. with _make_3mf_with({"Metadata/project_settings.config": json.dumps(proj)}) as zf:
  382. out = extract_project_filaments_from_3mf(zf)
  383. assert len(out) == 3
  384. assert out[0]["color"] == "#000000"
  385. assert out[1]["color"] == ""
  386. assert out[2]["color"] == ""
  387. def test_corrupt_json_returns_empty_no_exception(self):
  388. with _make_3mf_with({"Metadata/project_settings.config": b"{not json"}) as zf:
  389. assert extract_project_filaments_from_3mf(zf) == []
  390. def test_root_is_list_returns_empty(self):
  391. # Defensive: spec says it's a dict, but a file shipping a top-level
  392. # list (or anything non-dict) shouldn't crash the modal.
  393. with _make_3mf_with({"Metadata/project_settings.config": json.dumps([])}) as zf:
  394. assert extract_project_filaments_from_3mf(zf) == []
  395. def test_empty_arrays_returns_empty(self):
  396. proj = {"filament_type": [], "filament_colour": []}
  397. with _make_3mf_with({"Metadata/project_settings.config": json.dumps(proj)}) as zf:
  398. assert extract_project_filaments_from_3mf(zf) == []
  399. # ---------------------------------------------------------------------------
  400. # Tests for extract_plate_extruder_set_from_3mf — three sources unioned:
  401. # object top-level extruder, per-part extruder, painted-face quadtree leaves.
  402. # ---------------------------------------------------------------------------
  403. def _model_settings(plate_id: int, objects: list[dict]) -> str:
  404. """Build a minimal model_settings.config XML for tests. Each object dict
  405. can have: id, extruder (top-level), parts (list of {extruder}).
  406. The plate references all object ids."""
  407. parts_xml = []
  408. for obj in objects:
  409. oid = obj["id"]
  410. ext = obj.get("extruder")
  411. parts = obj.get("parts", [])
  412. ext_meta = f'<metadata key="extruder" value="{ext}"/>' if ext is not None else ""
  413. part_blocks = "".join(
  414. f'<part id="{i}" subtype="normal_part"><metadata key="extruder" value="{p["extruder"]}"/></part>'
  415. for i, p in enumerate(parts)
  416. if p.get("extruder") is not None
  417. )
  418. parts_xml.append(f'<object id="{oid}"><metadata key="name" value="o{oid}"/>{ext_meta}{part_blocks}</object>')
  419. instances = "".join(
  420. f'<model_instance><metadata key="object_id" value="{o["id"]}"/></model_instance>' for o in objects
  421. )
  422. plate = f'<plate><metadata key="plater_id" value="{plate_id}"/>{instances}</plate>'
  423. return f'<?xml version="1.0"?><config>{"".join(parts_xml)}{plate}</config>'
  424. class TestExtractPlateExtruderSetFrom3mf:
  425. def test_returns_empty_set_when_model_settings_missing(self):
  426. with _make_3mf_with({"placeholder.txt": "hi"}) as zf:
  427. assert extract_plate_extruder_set_from_3mf(zf, plate_id=1) == set()
  428. def test_object_top_level_extruder_only(self):
  429. xml = _model_settings(plate_id=1, objects=[{"id": "10", "extruder": 2}])
  430. with _make_3mf_with({"Metadata/model_settings.config": xml}) as zf:
  431. assert extract_plate_extruder_set_from_3mf(zf, plate_id=1) == {2}
  432. def test_per_part_extruder_unions_with_top_level(self):
  433. # Object's default is 1; one of its parts overrides to 3 (multi-color
  434. # via a sub-mesh). Union both — the slicer needs profiles for both.
  435. xml = _model_settings(
  436. plate_id=1,
  437. objects=[{"id": "10", "extruder": 1, "parts": [{"extruder": 3}]}],
  438. )
  439. with _make_3mf_with({"Metadata/model_settings.config": xml}) as zf:
  440. assert extract_plate_extruder_set_from_3mf(zf, plate_id=1) == {1, 3}
  441. def test_unknown_plate_id_returns_empty_set(self):
  442. xml = _model_settings(plate_id=1, objects=[{"id": "10", "extruder": 2}])
  443. with _make_3mf_with({"Metadata/model_settings.config": xml}) as zf:
  444. assert extract_plate_extruder_set_from_3mf(zf, plate_id=99) == set()
  445. def test_corrupt_xml_returns_empty_set_no_exception(self):
  446. with _make_3mf_with({"Metadata/model_settings.config": "<not valid xml"}) as zf:
  447. assert extract_plate_extruder_set_from_3mf(zf, plate_id=1) == set()
  448. def test_zero_extruder_value_ignored(self):
  449. # Bambu's 0 means "use object default" — not a real slot.
  450. xml = _model_settings(plate_id=1, objects=[{"id": "10", "extruder": 0}])
  451. with _make_3mf_with({"Metadata/model_settings.config": xml}) as zf:
  452. assert extract_plate_extruder_set_from_3mf(zf, plate_id=1) == set()
  453. def test_painted_face_above_threshold_kept(self):
  454. # 60/40 split: 60 triangles painted with extruder 1, 40 with ext 2.
  455. # Threshold is 5%; both above. The dominant ones are real colours.
  456. triangles = []
  457. for _ in range(60):
  458. triangles.append('<triangle v1="0" v2="1" v3="2" paint_color="1"/>')
  459. for _ in range(40):
  460. triangles.append('<triangle v1="0" v2="1" v3="2" paint_color="2"/>')
  461. per_obj = (
  462. '<?xml version="1.0"?>'
  463. '<model><resources><object id="100" type="model"><mesh>'
  464. "<triangles>" + "".join(triangles) + "</triangles>"
  465. "</mesh></object></resources><build/></model>"
  466. )
  467. ms = (
  468. '<?xml version="1.0"?><config>'
  469. '<object id="10"><metadata key="name" value="o"/></object>'
  470. '<plate><metadata key="plater_id" value="1"/>'
  471. '<model_instance><metadata key="object_id" value="10"/></model_instance>'
  472. "</plate></config>"
  473. )
  474. threed = (
  475. '<?xml version="1.0"?>'
  476. '<model xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"'
  477. ' xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06">'
  478. "<resources>"
  479. '<object id="10" type="model"><components>'
  480. '<component p:path="/3D/Objects/o100.model" objectid="100"/>'
  481. "</components></object>"
  482. "</resources><build/></model>"
  483. )
  484. with _make_3mf_with(
  485. {
  486. "Metadata/model_settings.config": ms,
  487. "3D/3dmodel.model": threed,
  488. "3D/Objects/o100.model": per_obj,
  489. }
  490. ) as zf:
  491. result = extract_plate_extruder_set_from_3mf(zf, plate_id=1)
  492. # Both real colours kept (60/40 well above 5% threshold); the dropped
  493. # threshold case is the regression that motivates this test.
  494. assert result == {1, 2}
  495. def test_painted_face_below_threshold_dropped_as_noise(self):
  496. # 99 triangles at ext 1, 1 triangle at ext 9 (1% — below 5%
  497. # threshold). The 1% leaf is a single-leaf accident.
  498. triangles = []
  499. for _ in range(99):
  500. triangles.append('<triangle v1="0" v2="1" v3="2" paint_color="1"/>')
  501. triangles.append('<triangle v1="0" v2="1" v3="2" paint_color="9"/>')
  502. per_obj = (
  503. '<?xml version="1.0"?>'
  504. '<model><resources><object id="100" type="model"><mesh>'
  505. "<triangles>" + "".join(triangles) + "</triangles>"
  506. "</mesh></object></resources><build/></model>"
  507. )
  508. ms = (
  509. '<?xml version="1.0"?><config>'
  510. '<object id="10"><metadata key="name" value="o"/></object>'
  511. '<plate><metadata key="plater_id" value="1"/>'
  512. '<model_instance><metadata key="object_id" value="10"/></model_instance>'
  513. "</plate></config>"
  514. )
  515. threed = (
  516. '<?xml version="1.0"?>'
  517. '<model xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"'
  518. ' xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06">'
  519. '<resources><object id="10" type="model"><components>'
  520. '<component p:path="/3D/Objects/o100.model" objectid="100"/>'
  521. "</components></object></resources><build/></model>"
  522. )
  523. with _make_3mf_with(
  524. {
  525. "Metadata/model_settings.config": ms,
  526. "3D/3dmodel.model": threed,
  527. "3D/Objects/o100.model": per_obj,
  528. }
  529. ) as zf:
  530. result = extract_plate_extruder_set_from_3mf(zf, plate_id=1)
  531. # Single-leaf accident at 1% filtered as noise; only the dominant
  532. # extruder survives.
  533. assert result == {1}
  534. def test_missing_per_object_model_file_silently_skipped(self):
  535. ms = (
  536. '<?xml version="1.0"?><config>'
  537. '<object id="10"><metadata key="extruder" value="2"/></object>'
  538. '<plate><metadata key="plater_id" value="1"/>'
  539. '<model_instance><metadata key="object_id" value="10"/></model_instance>'
  540. "</plate></config>"
  541. )
  542. threed = (
  543. '<?xml version="1.0"?>'
  544. '<model xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"'
  545. ' xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06">'
  546. '<resources><object id="10" type="model"><components>'
  547. '<component p:path="/3D/Objects/missing.model" objectid="999"/>'
  548. "</components></object></resources><build/></model>"
  549. )
  550. with _make_3mf_with(
  551. {"Metadata/model_settings.config": ms, "3D/3dmodel.model": threed},
  552. ) as zf:
  553. # Top-level metadata still works; missing component model file
  554. # is silently skipped without crashing.
  555. assert extract_plate_extruder_set_from_3mf(zf, plate_id=1) == {2}
  556. class TestExtractEmbeddedPresetsFrom3mf:
  557. """Printer / process preset names read from project_settings.config so the
  558. SliceModal can default its dropdowns to the file's own config (#1325)."""
  559. def test_extracts_printer_and_process(self):
  560. config = json.dumps(
  561. {
  562. "printer_settings_id": "Bambu Lab X1 Carbon 0.4 nozzle",
  563. "print_settings_id": "0.20mm Standard @BBL X1C",
  564. "filament_settings_id": ["Bambu PLA Basic @BBL X1C"],
  565. }
  566. )
  567. with _make_3mf_with({"Metadata/project_settings.config": config}) as zf:
  568. assert extract_embedded_presets_from_3mf(zf) == {
  569. "printer": "Bambu Lab X1 Carbon 0.4 nozzle",
  570. "process": "0.20mm Standard @BBL X1C",
  571. }
  572. def test_settings_id_as_list_takes_first(self):
  573. # Some exports write *_settings_id as a per-extruder list.
  574. config = json.dumps(
  575. {
  576. "printer_settings_id": ["Bambu Lab A1 0.4 nozzle"],
  577. "print_settings_id": ["0.16mm Optimal @BBL A1", "0.20mm @BBL A1"],
  578. }
  579. )
  580. with _make_3mf_with({"Metadata/project_settings.config": config}) as zf:
  581. result = extract_embedded_presets_from_3mf(zf)
  582. assert result["printer"] == "Bambu Lab A1 0.4 nozzle"
  583. assert result["process"] == "0.16mm Optimal @BBL A1"
  584. def test_missing_config_returns_none_values(self):
  585. with _make_3mf_with({"3D/3dmodel.model": "<model/>"}) as zf:
  586. assert extract_embedded_presets_from_3mf(zf) == {
  587. "printer": None,
  588. "process": None,
  589. }
  590. def test_malformed_json_returns_none_values(self):
  591. with _make_3mf_with({"Metadata/project_settings.config": "not json"}) as zf:
  592. assert extract_embedded_presets_from_3mf(zf) == {
  593. "printer": None,
  594. "process": None,
  595. }
  596. def test_blank_and_absent_keys_yield_none(self):
  597. config = json.dumps({"printer_settings_id": " ", "other": "x"})
  598. with _make_3mf_with({"Metadata/project_settings.config": config}) as zf:
  599. assert extract_embedded_presets_from_3mf(zf) == {
  600. "printer": None,
  601. "process": None,
  602. }