File size: 7,127 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 | from __future__ import annotations
from pathlib import Path
import cadquery as cq
from OCP.BRepAdaptor import BRepAdaptor_Surface
from OCP.GeomAbs import GeomAbs_Cylinder, GeomAbs_Plane
OUTPUT_ROOT = Path(r"C:\Users\User\Desktop")
GUI_BYPASS_FORBIDDEN_EXTENSIONS = {
".py", ".pyw", ".ipynb", ".sh", ".bash", ".zsh", ".bat", ".cmd",
".ps1", ".psm1", ".psd1", ".vbs", ".js", ".mjs", ".ts", ".rb",
".lua", ".tcl", ".ahk", ".scr",
}
GUI_BYPASS_ALLOWED_FILENAMES = {"eval.py"}
GUI_BYPASS_OUTPUT_TOKENS = (
"result.ifc", "result.pdf", "result.osm", "workflow.osw", "summary.txt",
"report.csv", "result.csv",
".dxf", ".dwg", ".step", ".stp", ".fcstd", ".scad", ".stl", ".obj",
".db", ".rst", ".rth", ".wbpj", ".odb", ".cae", ".inp",
".nc", ".gcode", ".slb", ".ipt", ".sldprt", ".sldasm",
"autocad_result", "apdl_", "wb_",
)
GUI_BYPASS_COMMAND_TOKENS = (
"python", "python3", "py ", "powershell", "pwsh", "cmd.exe", "cmd /c",
"bash", " sh ", "zsh", "node", "ruby", "perl",
"ifcopenshell", "openstudio", "energyplus",
"blender --background", "revitbatchprocessor",
"ansys", "mapdl", "fluent", "abaqus", "cae noGUI",
"freecad", "freecadcmd", "openscad", "librecad",
"ezdxf", "cadquery", "accoreconsole", "autolisp",
"solidworks", "solvespace",
)
def _read_text_safe(path):
try:
return path.read_text(encoding="utf-8", errors="ignore")
except Exception:
return ""
def _desktop_script_artifacts(root):
if not root.exists() or not root.is_dir():
return True
try:
candidates = list(root.iterdir())
for directory in list(candidates):
if directory.is_dir() and directory.name not in {"__pycache__", "_runtime"}:
try:
candidates.extend(directory.iterdir())
except Exception:
pass
for path in candidates:
if not path.is_file():
continue
if path.name in GUI_BYPASS_ALLOWED_FILENAMES:
continue
if path.suffix.lower() in GUI_BYPASS_FORBIDDEN_EXTENSIONS:
return True
except Exception:
return True
return False
def _history_paths(root):
home = Path.home()
return [
home / ".bash_history",
home / ".zsh_history",
home / ".python_history",
home / ".local/share/fish/fish_history",
home / "AppData/Roaming/Microsoft/Windows/PowerShell/PSReadLine/ConsoleHost_history.txt",
home / "AppData/Roaming/Microsoft/Windows/PowerShell/PSReadLine/Visual Studio Code Host_history.txt",
root / ".bash_history",
root / ".zsh_history",
]
def _history_contains_bypass(root):
for path in _history_paths(root):
if not path.is_file():
continue
text = _read_text_safe(path).lower()
if not text:
continue
for raw_line in text.splitlines():
line = raw_line.strip()
if not line or "eval.py" in line:
continue
touches_output = any(token in line for token in GUI_BYPASS_OUTPUT_TOKENS)
runs_command = any(token in line for token in GUI_BYPASS_COMMAND_TOKENS)
writes_file = any(
token in line for token in (">", "tee ", "cat ", "set-content", "out-file", "new-item")
)
if touches_output and (runs_command or writes_file):
return True
if ("/desktop/" in line or "\\desktop\\" in line) and any(
ext in line for ext in GUI_BYPASS_FORBIDDEN_EXTENSIONS
) and runs_command:
return True
return False
def check_no_gui_bypass(root):
root = Path(root)
if _desktop_script_artifacts(root):
return False
if _history_contains_bypass(root):
return False
return True
OUTPUT_STEP = "gui_013_clamp_vjaw_out.step"
BBOX_TOL = 0.15
POS_TOL = 0.30
RADIUS_TOL = 0.08
VOLUME_REL_TOL = 0.03
EXPECTED_VOLUME = 53112.97614
def close(actual: float, expected: float, tol: float) -> bool:
return abs(float(actual) - float(expected)) <= tol
def import_single_solid(path: Path):
if not path.exists() or path.stat().st_size <= 0:
return None
solids = cq.importers.importStep(str(path)).solids().vals()
if len(solids) != 1 or any(not solid.isValid() for solid in solids):
return None
return solids[0]
def bbox_matches(solid) -> bool:
bbox = solid.BoundingBox()
return (
close(bbox.xmin, -40.0, BBOX_TOL)
and close(bbox.xmax, 40.0, BBOX_TOL)
and close(bbox.ymin, -15.0, BBOX_TOL)
and close(bbox.ymax, 15.0, BBOX_TOL)
and close(bbox.zmin, 0.0, BBOX_TOL)
and close(bbox.zmax, 25.0, BBOX_TOL)
)
def cylinder_faces(solid):
for face in solid.Faces():
surface = BRepAdaptor_Surface(face.wrapped, True)
if surface.GetType() == GeomAbs_Cylinder:
cylinder = surface.Cylinder()
yield face, cylinder, cylinder.Axis().Direction(), cylinder.Location()
def holes_and_fillets_match(solid) -> bool:
holes = []
outside_fillets = 0
for face, cylinder, axis, location in cylinder_faces(solid):
radius = cylinder.Radius()
bbox = face.BoundingBox()
if close(radius, 3.25, RADIUS_TOL) and abs(abs(axis.Z()) - 1.0) <= 0.01:
if close(bbox.zlen, 25.0, BBOX_TOL):
holes.append((location.X(), location.Y()))
if close(radius, 1.0, RADIUS_TOL):
outside_fillets += 1
return (
any(close(x, -25.0, POS_TOL) and close(y, 0.0, POS_TOL) for x, y in holes)
and any(close(x, 25.0, POS_TOL) and close(y, 0.0, POS_TOL) for x, y in holes)
and outside_fillets >= 8
)
def v_groove_matches(solid) -> bool:
upper = lower = False
for face in solid.Faces():
surface = BRepAdaptor_Surface(face.wrapped, True)
if surface.GetType() != GeomAbs_Plane:
continue
bbox = face.BoundingBox()
center = face.Center()
if close(bbox.xlen, 80.0, 1.0) and close(bbox.ylen, 8.0, POS_TOL) and close(bbox.zlen, 8.0, POS_TOL):
if close(center.y, -11.0, POS_TOL) and close(center.z, 8.5, POS_TOL):
lower = True
if close(center.y, -11.0, POS_TOL) and close(center.z, 16.5, POS_TOL):
upper = True
return upper and lower
def volume_matches(solid) -> bool:
return abs(solid.Volume() - EXPECTED_VOLUME) / EXPECTED_VOLUME <= VOLUME_REL_TOL
def evaluate() -> bool:
if not check_no_gui_bypass(Path(r"C:\Users\User\Desktop")):
return False
solid = import_single_solid(OUTPUT_ROOT / OUTPUT_STEP)
if solid is None:
return False
return bbox_matches(solid) and holes_and_fillets_match(solid) and v_groove_matches(solid) and volume_matches(solid)
if __name__ == "__main__":
try:
if not check_no_gui_bypass(OUTPUT_ROOT):
ok = False
else:
ok = evaluate()
except Exception:
ok = False
print(True if ok else False)
|