File size: 33,923 Bytes
5154c4e | 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 | from __future__ import annotations
import hashlib
import json
import math
import os
import re
import secrets
import shlex
import stat
import subprocess
import sys
import tempfile
import traceback
import base64
import zipfile
import xml.etree.ElementTree as ET
from pathlib import Path
from pathlib import PurePosixPath
ROOT = Path("/home/user/Desktop")
FCSTD_PATH = ROOT / "task-08.FCStd"
STEP_PATH = ROOT / "rest_shape.step"
NC_PATH = ROOT / "task-08.nc"
EXPECTED_STEP_SHA256 = "6422d10ff940f6e558a26d5b1a85597e103de0a3c36b83774397d5be500abcf7"
EXPECTED_FREECAD_COMMIT = "b9bfa5c5507506e4515816414cd27f4851d00489"
FREECADCMD = Path("/usr/bin/freecadcmd")
INNER_ENV = "ENGIWORLD_TASK08_FREECAD"
MARKER_ENV = "ENGIWORLD_TASK08_MARKER"
NUMBER = r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)"
WORD_RE = re.compile(rf"([A-Z])\s*({NUMBER})", re.IGNORECASE)
TOP_Z = 18.0
FLOOR_Z = 6.0
ALLOWED_OBJECT_TYPES = {
"App::DocumentObjectGroup",
"App::FeaturePython",
"Part::Feature",
"Part::FeaturePython",
"PartDesign::Feature",
"Path::FeaturePython",
"Sketcher::SketchObject",
}
ALLOWED_PROXY_PAIRS = {
("Path.Base.SetupSheet", "SetupSheet"),
("Path.Main.Job", "ObjectJob"),
("Path.Main.Stock", "StockCreateBox"),
("Path.Main.Stock", "StockFromBase"),
("Path.Op.Adaptive", "PathAdaptive"),
("Path.Op.PocketShape", "ObjectPocket"),
("Path.Tool.Bit", "ToolBit"),
("Path.Tool.Controller", "ToolController"),
("draftobjects.clone", "Clone"),
}
ALLOWED_PROXY_STATES = {b"null", b"{}", b'"Clone"'}
def regular_file(path: Path, maximum_size: int) -> bool:
try:
info = path.lstat()
except OSError:
return False
return stat.S_ISREG(info.st_mode) and not path.is_symlink() and 0 < info.st_size <= maximum_size
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def normalized_text_sha256(path: Path) -> str:
data = path.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
return hashlib.sha256(data).hexdigest()
def safe_fcstd_container(path: Path) -> bool:
if not regular_file(path, 20 * 1024 * 1024) or not zipfile.is_zipfile(path):
return False
try:
with zipfile.ZipFile(path) as archive:
entries = archive.infolist()
names = [entry.filename for entry in entries]
if (
not 1 <= len(entries) <= 200
or len(names) != len(set(names))
or names.count("Document.xml") != 1
or sum(entry.file_size for entry in entries) > 50 * 1024 * 1024
):
return False
for entry in entries:
member = PurePosixPath(entry.filename)
mode = (entry.external_attr >> 16) & 0o170000
if (
entry.flag_bits & 0x1
or entry.compress_type not in {zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED}
or entry.file_size > 25 * 1024 * 1024
or entry.filename.startswith("/")
or "\\" in entry.filename
or ".." in member.parts
or mode == stat.S_IFLNK
or member.suffix.lower() in {".py", ".pyc", ".pyd", ".so", ".dll", ".exe", ".sh"}
):
return False
xml_data = archive.read("Document.xml")
except (OSError, KeyError, RuntimeError, zipfile.BadZipFile):
return False
if len(xml_data) > 5 * 1024 * 1024 or b"<!DOCTYPE" in xml_data.upper() or b"<!ENTITY" in xml_data.upper():
return False
try:
root = ET.fromstring(xml_data)
except ET.ParseError:
return False
objects = root.find("Objects")
object_data = root.find("ObjectData")
if objects is None or object_data is None:
return False
declarations = objects.findall("Object")
data_objects = object_data.findall("Object")
declared_names = [obj.get("name", "") for obj in declarations]
data_names = [obj.get("name", "") for obj in data_objects]
if (
not 1 <= len(declarations) <= 100
or len(declared_names) != len(set(declared_names))
or set(declared_names) != set(data_names)
or any(obj.get("type") not in ALLOWED_OBJECT_TYPES for obj in declarations)
):
return False
for extension in root.findall(".//Extension"):
if extension.get("type") not in {
"App::GroupExtension",
"App::GroupExtensionPython",
"Part::AttachExtension",
"Part::AttachExtensionPython",
}:
return False
all_python_properties = root.findall('.//*[@type="App::PropertyPythonObject"]')
audited_python_properties = []
for obj in data_objects:
proxy_pair = None
python_properties = [
prop
for prop in obj.findall("./Properties/Property")
if prop.get("type") == "App::PropertyPythonObject"
]
audited_python_properties.extend(python_properties)
for prop in python_properties:
name = prop.get("name", "")
nodes = prop.findall("Python")
if len(nodes) != 1 or name not in {"Proxy", "AdaptiveInputState", "AdaptiveOutputState"}:
return False
node = nodes[0]
if node.get("encoded") != "yes":
return False
try:
decoded = base64.b64decode(node.get("value", ""), validate=True)
except (ValueError, TypeError):
return False
module = node.get("module")
class_name = node.get("class")
if name == "Proxy":
if (module, class_name) not in ALLOWED_PROXY_PAIRS or decoded not in ALLOWED_PROXY_STATES:
return False
proxy_pair = (module, class_name)
else:
if module is not None or class_name is not None or node.get("json") != "yes":
return False
try:
json.loads(decoded.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError):
return False
state_names = {prop.get("name") for prop in python_properties if prop.get("name") != "Proxy"}
if state_names and proxy_pair is not None and proxy_pair[0] != "Path.Op.Adaptive":
return False
if len(all_python_properties) != len(audited_python_properties) or {
id(prop) for prop in all_python_properties
} != {id(prop) for prop in audited_python_properties}:
return False
return True
def close(first, second, tolerance: float = 1e-5) -> bool:
return abs(float(first) - float(second)) <= tolerance
def proxy_module(obj) -> str:
proxy = getattr(obj, "Proxy", None)
return getattr(proxy.__class__, "__module__", "") if proxy is not None else ""
def shapes_equal(first, second, tolerance: float = 1e-5) -> bool:
try:
return (
first.isValid()
and second.isValid()
and first.cut(second).Volume <= tolerance
and second.cut(first).Volume <= tolerance
)
except Exception:
return False
def object_state_clean(obj) -> bool:
try:
states = {str(value) for value in obj.State}
except Exception:
return False
return not states.intersection({"Touched", "Invalid", "Error"})
def distance_to_segment(px: float, py: float, start, end) -> float:
ax, ay = start
bx, by = end
dx = bx - ax
dy = by - ay
denominator = dx * dx + dy * dy
if denominator <= 1e-16:
return math.hypot(px - ax, py - ay)
scale = max(0.0, min(1.0, ((px - ax) * dx + (py - ay) * dy) / denominator))
return math.hypot(px - (ax + scale * dx), py - (ay + scale * dy))
def path_motions(commands):
current = {"X": None, "Y": None, "Z": None}
motions = []
for command in commands:
before = dict(current)
parameters = command.Parameters
for axis in ("X", "Y", "Z"):
if axis in parameters:
current[axis] = float(parameters[axis])
name = str(command.Name).upper().replace(" ", "")
current_complete = all(current[axis] is not None for axis in ("X", "Y", "Z"))
before_complete = all(before[axis] is not None for axis in ("X", "Y", "Z"))
if not before_complete and current_complete and name in {"G0", "G00", "G1", "G01", "G2", "G02", "G3", "G03"}:
motions.append(("G0" if name in {"G0", "G00"} else "G1", dict(current), dict(current)))
continue
if name in {"G0", "G00", "G1", "G01"} and all(
before[axis] is not None and current[axis] is not None for axis in ("X", "Y", "Z")
):
motions.append(("G0" if name in {"G0", "G00"} else "G1", before, dict(current)))
elif name in {"G2", "G02", "G3", "G03"} and all(
before[axis] is not None and current[axis] is not None for axis in ("X", "Y", "Z")
):
if "I" not in parameters and "J" not in parameters:
raise ValueError("unsupported arc without I/J center offsets")
center_x = before["X"] + float(parameters.get("I", 0.0))
center_y = before["Y"] + float(parameters.get("J", 0.0))
start_angle = math.atan2(before["Y"] - center_y, before["X"] - center_x)
end_angle = math.atan2(current["Y"] - center_y, current["X"] - center_x)
radius = math.hypot(before["X"] - center_x, before["Y"] - center_y)
end_radius = math.hypot(current["X"] - center_x, current["Y"] - center_y)
if radius <= 1e-8 or abs(radius - end_radius) > 1e-4:
raise ValueError("invalid arc radius")
clockwise = name in {"G2", "G02"}
sweep = (start_angle - end_angle) % (2.0 * math.pi) if clockwise else (end_angle - start_angle) % (2.0 * math.pi)
if sweep <= 1e-10:
sweep = 2.0 * math.pi
steps = max(2, int(math.ceil(radius * sweep / 0.25)))
previous = dict(before)
direction = -1.0 if clockwise else 1.0
for step in range(1, steps + 1):
fraction = step / steps
angle = start_angle + direction * sweep * fraction
point = {
"X": center_x + radius * math.cos(angle),
"Y": center_y + radius * math.sin(angle),
"Z": before["Z"] + (current["Z"] - before["Z"]) * fraction,
}
if step == steps:
point = dict(current)
motions.append(("G1", previous, point))
previous = point
return motions
def contact_projection(before, after):
"""Return the XY projection of only the portion at or below the stock top."""
before_z = before["Z"]
after_z = after["Z"]
if min(before_z, after_z) >= TOP_Z - 1e-4:
return None
start = dict(before)
end = dict(after)
delta_z = after_z - before_z
if abs(delta_z) > 1e-12:
fraction = (TOP_Z - before_z) / delta_z
if 0.0 < fraction < 1.0:
crossing = {
axis: before[axis] + (after[axis] - before[axis]) * fraction
for axis in ("X", "Y", "Z")
}
if before_z > TOP_Z:
start = crossing
elif after_z > TOP_Z:
end = crossing
return ((start["X"], start["Y"]), (end["X"], end["Y"]))
def operation_motions(operation):
motions = path_motions(list(operation.Path.Commands))
contact_segments = []
floor_segments = []
reached_floor = False
for mode, before, after in motions:
if min(before["Z"], after["Z"]) < FLOOR_Z - 1e-4:
return None
if mode == "G1" and abs(after["Z"] - FLOOR_Z) <= 1e-4:
reached_floor = True
if mode == "G1" and abs(before["Z"] - FLOOR_Z) <= 1e-4 and abs(after["Z"] - FLOOR_Z) <= 1e-4:
floor_segments.append(((before["X"], before["Y"]), (after["X"], after["Y"])))
contact = contact_projection(before, after)
if contact is not None:
contact_segments.append(contact)
return (contact_segments, floor_segments) if reached_floor and floor_segments else None
def selected_faces(operation, Part, allow_edges: bool = False):
faces = []
boundaries = []
try:
bases = list(operation.Base)
except Exception:
return None
if not bases:
return None
for boundary, names in bases:
if boundary.Document != operation.Document or not names:
return None
boundaries.append(boundary)
edges = []
for name in names:
name = str(name)
if name.startswith("Edge") and allow_edges:
try:
edges.append(boundary.Shape.getElement(name))
except Exception:
return None
continue
if not name.startswith("Face"):
return None
try:
face = boundary.Shape.getElement(name)
except Exception:
return None
if not isinstance(face.Surface, Part.Plane):
return None
normal = face.normalAt(0.5, 0.5)
if abs(abs(float(normal.z)) - 1.0) > 1e-5 or face.BoundBox.ZLength > 1e-5:
return None
copy = face.copy()
copy.translate(__import__("FreeCAD").Vector(0.0, 0.0, TOP_Z - face.BoundBox.ZMin))
faces.append(copy)
if edges:
try:
import DraftGeomUtils
wires = DraftGeomUtils.findWires(edges)
except Exception:
return None
if not wires:
return None
for wire in wires:
try:
if not wire.isClosed() or wire.BoundBox.ZLength > 1e-5:
return None
face = Part.Face(wire)
if not isinstance(face.Surface, Part.Plane):
return None
normal = face.normalAt(0.5, 0.5)
if abs(abs(float(normal.z)) - 1.0) > 1e-5:
return None
face.translate(__import__("FreeCAD").Vector(0.0, 0.0, TOP_Z - face.BoundBox.ZMin))
faces.append(face)
except Exception:
return None
return faces, boundaries
def fused_faces(faces):
result = faces[0]
for face in faces[1:]:
result = result.fuse(face)
try:
result = result.removeSplitter()
except Exception:
pass
return result
def expected_regions(Part, App):
main = Part.makePlane(60.0, 36.0, App.Vector(-30.0, -18.0, TOP_Z))
residuals = [
Part.makePlane(8.0, 12.0, App.Vector(-4.0, 18.0, TOP_Z)),
Part.makePlane(8.0, 12.0, App.Vector(-4.0, -30.0, TOP_Z)),
Part.makePlane(12.0, 8.0, App.Vector(-42.0, -4.0, TOP_Z)),
Part.makePlane(12.0, 8.0, App.Vector(30.0, -4.0, TOP_Z)),
]
return main, residuals
def face_matches_region(face, expected, tolerance: float = 1e-4) -> bool:
try:
return face.cut(expected).Area <= tolerance and expected.cut(face).Area <= tolerance
except Exception:
return False
def rounded_rectangle_contains(x, y, bbox, radius):
center_x = min(max(x, bbox.XMin + radius), bbox.XMax - radius)
center_y = min(max(y, bbox.YMin + radius), bbox.YMax - radius)
return math.hypot(x - center_x, y - center_y) <= radius + 1e-8
def segment_sweep(Part, App, start, end, radius: float):
start_point = App.Vector(start[0], start[1], TOP_Z)
end_point = App.Vector(end[0], end[1], TOP_Z)
def disk(center):
return Part.Face(Part.Wire([Part.makeCircle(radius, center)]))
length = math.hypot(end[0] - start[0], end[1] - start[1])
sweep = disk(start_point)
if length <= 1e-10:
return sweep
normal_x = -(end[1] - start[1]) * radius / length
normal_y = (end[0] - start[0]) * radius / length
corners = [
App.Vector(start[0] + normal_x, start[1] + normal_y, TOP_Z),
App.Vector(end[0] + normal_x, end[1] + normal_y, TOP_Z),
App.Vector(end[0] - normal_x, end[1] - normal_y, TOP_Z),
App.Vector(start[0] - normal_x, start[1] - normal_y, TOP_Z),
]
strip = Part.Face(Part.makePolygon(corners + [corners[0]]))
return sweep.fuse(strip).fuse(disk(end_point)).removeSplitter()
def validate_region_path(operation, region_faces, allowed_face, diameter: float) -> bool:
motion_sets = operation_motions(operation)
if motion_sets is None:
return False
contact_segments, floor_segments = motion_sets
radius = diameter / 2.0
for face in region_faces:
bbox = face.BoundBox
if min(bbox.XLength, bbox.YLength) <= diameter:
return False
samples = 0
x = bbox.XMin
while x <= bbox.XMax + 1e-8:
y = bbox.YMin
while y <= bbox.YMax + 1e-8:
point = __import__("FreeCAD").Vector(x, y, TOP_Z)
if face.isInside(point, 1e-6, True) and rounded_rectangle_contains(x, y, bbox, radius):
if min(distance_to_segment(x, y, start, end) for start, end in floor_segments) > radius + 0.12:
return False
samples += 1
y += 0.5
x += 0.5
if samples < 100:
return False
Part = __import__("Part")
App = __import__("FreeCAD")
stock_footprint = Part.makePlane(120.0, 80.0, App.Vector(-60.0, -40.0, TOP_Z))
for start, end in contact_segments:
sweep = segment_sweep(Part, App, start, end, radius)
material_contact = sweep.common(stock_footprint)
if material_contact.cut(allowed_face).Area > 1e-4:
return False
return True
def strip_comments(text: str):
output = []
index = 0
while index < len(text):
char = text[index]
if char == "(":
end = text.find(")", index + 1)
if end < 0 or "(" in text[index + 1 : end]:
raise ValueError("malformed parenthesis comment")
output.extend(" " for _ in range(index, end + 1))
index = end + 1
elif char == ")":
raise ValueError("unmatched parenthesis")
elif char == ";":
end = text.find("\n", index)
if end < 0:
end = len(text)
output.extend(" " for _ in range(index, end))
index = end
else:
output.append(char)
index += 1
return "".join(output)
def parse_nc(text: str):
executable = strip_comments(text)
raw_lines = executable.splitlines()
percent_lines = [index for index, line in enumerate(raw_lines) if line.strip() == "%"]
nonempty = [index for index, line in enumerate(raw_lines) if line.strip()]
if percent_lines and (len(percent_lines) != 2 or percent_lines != [nonempty[0], nonempty[-1]]):
raise ValueError("illegal percent wrapper")
blocks = []
for raw_line in raw_lines:
line = raw_line.strip().upper()
if not line or line == "%":
continue
tokens = []
cursor = 0
for match in WORD_RE.finditer(line):
if line[cursor : match.start()].strip():
raise ValueError("unparsed NC text")
tokens.append((match.group(1).upper(), float(match.group(2))))
cursor = match.end()
if line[cursor:].strip() or not tokens:
raise ValueError("unparsed NC line")
blocks.append(tokens)
if not blocks:
raise ValueError("empty NC")
return blocks
def canonical_blocks(text: str):
result = []
for block in parse_nc(text):
normalized = []
for letter, number in block:
if letter == "N":
continue
if letter in {"G", "M", "T", "H", "O"} and close(number, round(number), 1e-9):
value = str(int(round(number)))
else:
value = f"{number:.9f}".rstrip("0").rstrip(".")
if value in {"-0", "+0"}:
value = "0"
normalized.append(letter + value)
result.append(tuple(sorted(normalized)))
return result
def validate_nc_safety(text: str) -> bool:
try:
blocks = parse_nc(text)
except Exception:
return False
state = {
"units_mm": False,
"absolute": False,
"motion": None,
"feed": None,
"speed": None,
"tool": None,
"spindle": False,
"tool_changed": False,
"X": None,
"Y": None,
"Z": None,
}
changed_tools = []
bottom_tools = set()
saw_g21 = saw_g90 = ended = False
for block_index, block in enumerate(blocks):
words = {}
g_codes = []
m_codes = []
for letter, number in block:
if letter == "G":
if not close(number, round(number), 1e-9):
return False
g_codes.append(int(round(number)))
elif letter == "M":
if not close(number, round(number), 1e-9):
return False
m_codes.append(int(round(number)))
else:
if letter in words:
return False
words[letter] = number
if any(code not in {0, 1, 2, 3, 17, 21, 40, 43, 49, 54, 55, 56, 57, 58, 59, 80, 90, 94} for code in g_codes):
return False
if any(code not in {2, 3, 4, 5, 6, 7, 8, 9, 30} for code in m_codes):
return False
if 21 in g_codes:
state["units_mm"] = True
saw_g21 = True
if 90 in g_codes:
state["absolute"] = True
saw_g90 = True
for code in g_codes:
if code in (0, 1, 2, 3):
state["motion"] = code
if "T" in words:
tool = int(round(words["T"]))
if not close(words["T"], tool, 1e-9) or tool not in {1, 2}:
return False
state["tool"] = tool
if "H" in words and (state["tool"] is None or not close(words["H"], state["tool"], 1e-9)):
return False
if "S" in words:
if not close(words["S"], 7000, 1e-6):
return False
state["speed"] = words["S"]
if "F" in words:
if not close(words["F"], 500, 1e-5):
return False
state["feed"] = words["F"]
if 5 in m_codes:
state["spindle"] = False
if 6 in m_codes:
if state["spindle"] or state["tool"] is None:
return False
changed_tools.append(state["tool"])
state["tool_changed"] = True
if 3 in m_codes and 4 in m_codes:
return False
if 3 in m_codes or 4 in m_codes:
if state["speed"] is None or not close(state["speed"], 7000, 1e-6):
return False
state["spindle"] = True
before = {axis: state[axis] for axis in ("X", "Y", "Z")}
moved = False
for axis in ("X", "Y", "Z"):
if axis in words:
state[axis] = words[axis]
moved = True
if moved and state["motion"] in (0, 1, 2, 3):
if not state["units_mm"] or not state["absolute"]:
return False
if state["motion"] != 0:
if not state["tool_changed"] or not state["spindle"] or state["feed"] is None:
return False
if state["Z"] is not None and state["Z"] < FLOOR_Z - 1e-4:
return False
if state["Z"] is not None and abs(state["Z"] - FLOOR_Z) <= 1e-4:
bottom_tools.add(state["tool"])
if 2 in m_codes or 30 in m_codes:
if block_index != len(blocks) - 1 or state["spindle"]:
return False
ended = True
return saw_g21 and saw_g90 and changed_tools == [1, 2] and bottom_tools == {1, 2} and ended
def valid_postprocessor_args(value: str) -> bool:
try:
arguments = shlex.split(value)
except ValueError:
return False
flags = {
"--no-header",
"--no-comments",
"--line-numbers",
"--no-show-editor",
"--modal",
"--axis-modal",
"--no-tlo",
}
index = 0
while index < len(arguments):
argument = arguments[index]
if argument in flags:
index += 1
continue
if argument == "--precision":
index += 1
if index >= len(arguments) or not re.fullmatch(r"(?:[3-9]|1[0-5])", arguments[index]):
return False
index += 1
continue
if re.fullmatch(r"--precision=(?:[3-9]|1[0-5])", argument):
index += 1
continue
return False
return True
def evaluate_in_freecad() -> bool:
import builtins
builtins.pythonopen = open
import FreeCAD as App
import Part
import Path.Post.Command as PathPostCommand
from Path.Post.Processor import PostProcessor
if App.Version()[:4] != ["0", "21", "2", "33771 (Git)"] or App.Version()[-1] != EXPECTED_FREECAD_COMMIT:
return False
if not all(
regular_file(path, maximum)
for path, maximum in ((STEP_PATH, 5 * 1024 * 1024), (FCSTD_PATH, 20 * 1024 * 1024), (NC_PATH, 5 * 1024 * 1024))
):
return False
if normalized_text_sha256(STEP_PATH) != EXPECTED_STEP_SHA256:
return False
source_shape = Part.read(str(STEP_PATH))
if (
source_shape.ShapeType != "Solid"
or len(source_shape.Solids) != 1
or not source_shape.isValid()
or not close(source_shape.Volume, 142272.0, 1e-3)
or not close(source_shape.BoundBox.XMin, -60)
or not close(source_shape.BoundBox.XMax, 60)
or not close(source_shape.BoundBox.YMin, -40)
or not close(source_shape.BoundBox.YMax, 40)
or not close(source_shape.BoundBox.ZMin, 0)
or not close(source_shape.BoundBox.ZMax, TOP_Z)
):
return False
stock_shape = Part.makeBox(120.0, 80.0, TOP_Z, App.Vector(-60.0, -40.0, 0.0))
target_void = stock_shape.cut(source_shape).removeSplitter()
if not target_void.isValid() or len(target_void.Solids) != 1 or not close(target_void.Volume, 30528.0, 1e-3):
return False
target_void = target_void.Solids[0]
document = App.openDocument(str(FCSTD_PATH))
if document is None:
return False
jobs = [obj for obj in document.Objects if proxy_module(obj) == "Path.Main.Job"]
all_ops = [obj for obj in document.Objects if proxy_module(obj).startswith("Path.Op.")]
if len(jobs) != 1 or len(all_ops) != 2:
return False
job = jobs[0]
operations = list(job.Operations.Group)
if len(operations) != 2 or {obj.Name for obj in operations} != {obj.Name for obj in all_ops}:
return False
main, rest = operations
tracked = list(document.Objects)
for obj in tracked:
obj.touch()
document.recompute()
if not all(object_state_clean(obj) for obj in tracked):
return False
if proxy_module(main) != "Path.Op.PocketShape" or proxy_module(rest) != "Path.Op.Adaptive":
return False
if (
not close(main.ExtraOffset.Value, 0.0)
or str(rest.OperationType) != "Clearing"
or not close(rest.StockToLeave.Value, 0.0)
):
return False
if (
str(job.PostProcessor).strip().lower() != "linuxcnc"
or bool(job.SplitOutput)
or os.path.normpath(str(job.PostProcessorOutputFile)) != str(NC_PATH)
or not valid_postprocessor_args(str(job.PostProcessorArgs))
):
return False
fixtures = [str(value).strip().upper() for value in job.Fixtures]
if fixtures != ["G54"]:
return False
if len(job.Model.Group) != 1 or not shapes_equal(job.Model.Group[0].Shape, source_shape):
return False
if not shapes_equal(job.Stock.Shape, stock_shape):
return False
if len(job.Tools.Group) != 2:
return False
controllers = {int(controller.ToolNumber): controller for controller in job.Tools.Group}
if set(controllers) != {1, 2} or main.ToolController != controllers[1] or rest.ToolController != controllers[2]:
return False
for number, diameter in ((1, 10.0), (2, 4.0)):
controller = controllers[number]
if proxy_module(controller) != "Path.Tool.Controller" or proxy_module(controller.Tool) != "Path.Tool.Bit":
return False
if (
not close(controller.Tool.Diameter.Value, diameter)
or not close(controller.SpindleSpeed, 7000)
or str(controller.SpindleDir) not in {"Forward", "Reverse"}
or not close(controller.HorizFeed.getValueAs("mm/min").Value, 500)
or not close(controller.VertFeed.getValueAs("mm/min").Value, 500)
):
return False
if not controllers[2].Tool.Diameter.Value < controllers[1].Tool.Diameter.Value:
return False
for operation in operations:
if (
not close(operation.StartDepth.Value, TOP_Z)
or not close(operation.FinalDepth.Value, FLOOR_Z)
or operation.StepDown.Value <= 0
or operation.ClearanceHeight.Value <= operation.SafeHeight.Value
or operation.SafeHeight.Value <= TOP_Z
):
return False
main_selection = selected_faces(main, Part)
rest_selection = selected_faces(rest, Part, allow_edges=True)
if main_selection is None or rest_selection is None:
return False
main_faces, main_boundaries = main_selection
rest_faces, rest_boundaries = rest_selection
expected_main, expected_residuals = expected_regions(Part, App)
main_region = fused_faces(main_faces)
rest_region = fused_faces(rest_faces)
expected_rest = fused_faces(expected_residuals)
if not face_matches_region(main_region, expected_main) or not face_matches_region(rest_region, expected_rest):
return False
operation_void = main_region.extrude(App.Vector(0.0, 0.0, FLOOR_Z - TOP_Z))
operation_void = operation_void.fuse(rest_region.extrude(App.Vector(0.0, 0.0, FLOOR_Z - TOP_Z))).removeSplitter()
if not shapes_equal(operation_void, target_void, 1e-3):
return False
allowed_region = fused_faces([expected_main, *expected_residuals])
if not validate_region_path(main, [expected_main], allowed_region, 10.0):
return False
if not validate_region_path(rest, expected_residuals, allowed_region, 4.0):
return False
try:
submitted_text = NC_PATH.read_text(encoding="utf-8")
except Exception:
return False
if not validate_nc_safety(submitted_text):
return False
sections = PathPostCommand.buildPostList(job)
if len(sections) != 1:
return False
with tempfile.TemporaryDirectory(prefix="engiworld-task08-eval-") as temporary:
repost_path = Path(temporary) / "task-08.nc"
try:
PostProcessor.load("linuxcnc").export(sections[0][1], str(repost_path), str(job.PostProcessorArgs))
repost_text = repost_path.read_text(encoding="utf-8")
except BaseException:
return False
if canonical_blocks(submitted_text) != canonical_blocks(repost_text):
return False
return True
def run_outer() -> bool:
if not safe_fcstd_container(FCSTD_PATH):
return False
try:
executable = FREECADCMD.resolve(strict=True)
info = executable.stat()
except OSError:
return False
if not stat.S_ISREG(info.st_mode) or info.st_uid != 0 or info.st_mode & 0o022:
return False
marker = "TASK08_RESULT_" + secrets.token_hex(16)
evaluator = str(Path(__file__).resolve())
command = (
"import builtins; builtins.pythonopen=open; "
f"exec(compile(open({evaluator!r}, encoding='utf-8').read(), {evaluator!r}, 'exec'))"
)
try:
with tempfile.TemporaryDirectory(prefix="engiworld-task08-home-") as isolated_home:
environment = {
"HOME": isolated_home,
"LANG": "C.UTF-8",
"LC_ALL": "C.UTF-8",
"LOGNAME": "user",
"PATH": "/usr/bin:/bin",
"USER": "user",
"XDG_CACHE_HOME": str(Path(isolated_home) / ".cache"),
"XDG_CONFIG_HOME": str(Path(isolated_home) / ".config"),
INNER_ENV: "1",
MARKER_ENV: marker,
}
completed = subprocess.run(
[str(executable), "-c", command],
capture_output=True,
text=True,
timeout=240,
env=environment,
cwd=isolated_home,
check=False,
)
except Exception:
return False
marker_lines = [line.strip() for line in completed.stdout.splitlines() if line.strip().startswith(marker + "=")]
return completed.returncode == 0 and marker_lines == [marker + "=True"]
if __name__ == "__main__":
if os.environ.get(INNER_ENV) == "1":
marker = os.environ.get(MARKER_ENV, "")
ok = False
try:
ok = evaluate_in_freecad()
except BaseException:
traceback.print_exc(file=sys.stderr)
ok = False
print(f"{marker}={bool(ok)}")
else:
print(True if run_outer() else False)
|