Files
Web_FreeCAD_Bitbybit/scripts/freecad-ordered-operation-pair-resave.py
wangdequan 83cbc80971
Some checks failed
real-verification / chrome (push) Has been cancelled
real-verification / freecad-oracle (push) Has been cancelled
real-verification / wasm (push) Has been cancelled
feat: advance TSN drift and ordered pair evidence
2026-08-15 04:44:23 -04:00

114 lines
4.4 KiB
Python

import hashlib
import json
import os
import FreeCAD as App
import Import
import Part
FREECAD_COMMIT = "0108fd4b4850cc46e625b60e53cea7a7bbe69f8d"
def version_text():
return ".".join(str(value) for value in App.Version()[:3])
def shape_snapshot(feature):
shape = feature.Shape
if shape.isNull():
raise RuntimeError("ordered pair produced a null Shape")
bounds = shape.BoundBox
return {
"valid": bool(shape.isValid()),
"solids": len(shape.Solids),
"faces": len(shape.Faces),
"edges": len(shape.Edges),
"vertices": len(shape.Vertexes),
"volume": round(float(shape.Volume), 7),
"area": round(float(shape.Area), 7),
"bounds": [round(float(value), 7) for value in (bounds.XMin, bounds.YMin, bounds.ZMin, bounds.XMax, bounds.YMax, bounds.ZMax)],
}
def phase_snapshot(document):
feature = document.getObject("PairResult")
if feature is None:
raise RuntimeError("PairResult is absent after FCStd persistence")
evidence = str(feature.NamingEvidence)
return {
"pair": str(feature.OrderedPair),
"namingEvidenceBytes": len(evidence.encode("utf-8")),
"namingEvidenceSha256": hashlib.sha256(evidence.encode("utf-8")).hexdigest(),
"shape": shape_snapshot(feature),
}
def collect():
step_path = os.environ["FREECAD_PAIR_STEP_PATH"]
evidence_path = os.environ["FREECAD_PAIR_EVIDENCE_PATH"]
pair = os.environ["FREECAD_ORDERED_PAIR"]
output_directory = os.environ["FREECAD_PAIR_OUTPUT_DIRECTORY"]
with open(evidence_path, "r", encoding="utf-8") as evidence_file:
evidence = evidence_file.read()
document = App.newDocument("FreeCadOrderedPairResave")
try:
Import.insert(step_path, document.Name)
document.recompute()
source_shapes = [obj.Shape for obj in document.Objects if hasattr(obj, "Shape") and not obj.Shape.isNull()]
if not source_shapes:
raise RuntimeError("STEP import produced no Shape")
result = document.addObject("Part::Feature", "PairResult")
result.Shape = source_shapes[0].copy() if len(source_shapes) == 1 else Part.makeCompound([shape.copy() for shape in source_shapes])
result.addProperty("App::PropertyString", "OrderedPair", "Parity")
result.addProperty("App::PropertyString", "NamingEvidence", "Parity")
result.OrderedPair = pair
result.NamingEvidence = evidence
document.recompute()
initial = phase_snapshot(document)
initial_path = os.path.join(output_directory, "ordered-pair.FCStd")
resaved_path = os.path.join(output_directory, "ordered-pair-resaved.FCStd")
document.saveAs(initial_path)
App.closeDocument(document.Name)
reopened = App.openDocument(initial_path)
reopened.recompute()
reopened_snapshot = phase_snapshot(reopened)
reopened.saveAs(resaved_path)
App.closeDocument(reopened.Name)
resaved = App.openDocument(resaved_path)
resaved.recompute()
resaved_snapshot = phase_snapshot(resaved)
App.closeDocument(resaved.Name)
return {
"schemaVersion": 1,
"baselineId": "freecad-1.1.1-ordered-operation-pair-resave",
"freecadVersion": version_text(),
"gitCommit": FREECAD_COMMIT,
"pair": pair,
"phases": {"initial": initial, "reopened": reopened_snapshot, "resaved": resaved_snapshot},
"checks": {
"shapeStable": initial["shape"] == reopened_snapshot["shape"] and initial["shape"] == resaved_snapshot["shape"],
"namingEvidenceStable": initial["namingEvidenceSha256"] == reopened_snapshot["namingEvidenceSha256"] and initial["namingEvidenceSha256"] == resaved_snapshot["namingEvidenceSha256"],
},
"status": "pass",
}
finally:
for name in list(App.listDocuments().keys()):
App.closeDocument(name)
try:
report = collect()
except Exception as error:
report = {
"schemaVersion": 1,
"baselineId": "freecad-1.1.1-ordered-operation-pair-resave",
"freecadVersion": version_text(),
"gitCommit": FREECAD_COMMIT,
"status": "failed",
"errorType": type(error).__name__,
"error": str(error),
}
print("FREECAD_ORDERED_OPERATION_PAIR_RESAVE_RESULT=" + json.dumps(report, sort_keys=True, separators=(",", ":")))