119 lines
8.5 KiB
Python
119 lines
8.5 KiB
Python
"""Development fixtures authored by the current Codex session from drawing images.
|
||
No network/model client; all geometry passes through CADmind AgentSession.
|
||
Not loaded into the production capability library or Luna prompts.
|
||
"""
|
||
import argparse
|
||
import json
|
||
import math
|
||
import sys
|
||
import time
|
||
from pathlib import Path
|
||
ROOT=Path(__file__).resolve().parents[2]
|
||
sys.path.insert(0,str(ROOT))
|
||
from engine.agent_session import AgentSession
|
||
from engine.registry import load_registry
|
||
from engine.inspection import inspect_geometry
|
||
from engine.render import render_shape_png
|
||
from engine.job import write_json
|
||
|
||
|
||
def rounded_polygon(points, radius):
|
||
"""Derive analytic tangent points/bulges, then use the shared arc-profile tool."""
|
||
result=[]; bulges=[]
|
||
for i,p in enumerate(points):
|
||
a=points[i-1]; b=points[(i+1)%len(points)]
|
||
incoming=[p[j]-a[j] for j in range(2)]; outgoing=[b[j]-p[j] for j in range(2)]
|
||
li=math.hypot(*incoming); lo=math.hypot(*outgoing)
|
||
di=[v/li for v in incoming]; do=[v/lo for v in outgoing]
|
||
turn=math.atan2(di[0]*do[1]-di[1]*do[0],di[0]*do[0]+di[1]*do[1])
|
||
trim=min(radius*math.tan(abs(turn)/2),.4*min(li,lo))
|
||
result.extend([[p[j]-di[j]*trim for j in range(2)],[p[j]+do[j]*trim for j in range(2)]])
|
||
bulges.extend([math.tan(turn/4),0])
|
||
return {'kind':'profile','points':result,'bulges':bulges}
|
||
|
||
|
||
class Record:
|
||
def __init__(self,case,revision):
|
||
self.case=case; self.path=ROOT/'output/codex_luna_comparison'/f'{case}_codex_{revision}'
|
||
self.path.mkdir(parents=True,exist_ok=True)
|
||
self.session=AgentSession(load_registry(ROOT/'capabilities')); self.start=time.monotonic(); self.stage='base'
|
||
self.events=[]; self.snapshots=[]
|
||
def op(self,name,id,target=None,**params):
|
||
answer=self.session.apply_op(name,params,target,id)
|
||
event={'stage':self.stage,'elapsed_s':round(time.monotonic()-self.start,3),'elapsed_scope':'Python execution only; excludes Codex reading, authoring and interaction','name':name,'arguments':{'id':id,'target':target,'params':params},'result':answer}
|
||
self.events.append(event); write_json(self.path/'tools.json',self.events)
|
||
print(json.dumps(event,ensure_ascii=False),flush=True)
|
||
if not answer.get('ok'): raise RuntimeError(answer)
|
||
return id
|
||
def checkpoint(self,stage,id,notes,points=None):
|
||
path=self.path/stage; path.mkdir(exist_ok=True)
|
||
report=self.session.finish(id,[],1,path)
|
||
if not report.get('ok'): raise RuntimeError(report)
|
||
report.update({'part_name':f'{self.case}_codex','execution':'Current Codex session; project GPT-6 API calls = 0','stage':stage,'notes':notes})
|
||
write_json(path/'report.json',report)
|
||
write_json(path/'model.json',{'name':f'{self.case}_codex','operations':self.session.trace,'output':id,'expected_solid_count':1})
|
||
shape=self.session.context[id]; write_json(path/'geometry.json',inspect_geometry(shape,points))
|
||
for view in ['iso','front','top','side','back','bottom']:
|
||
(path/f'{view}.png').write_bytes(render_shape_png(shape,view,700))
|
||
self.snapshots.append({'stage':stage,'output':id,'notes':notes,'operations':len(self.session.trace),'volume_mm3':shape.volume})
|
||
write_json(self.path/'stages.json',self.snapshots)
|
||
return id
|
||
|
||
|
||
def build_c(r):
|
||
body=r.op('tube_with_lands','shell',outer_radius=29.5,inner_radius=28,height=58,land_width=39.5,length=55,shoulder_radius=2,origin=[0,0,0],x_axis=[1,0,0],normal=[0,-1,0])
|
||
r.checkpoint('base',body,'59×55×58 mm;轴向沿Y。内半径28、平台39.5与过渡R2为图像估计,非已标尺寸。')
|
||
r.stage='features'
|
||
windows=[]
|
||
for sign in [-1,1]:
|
||
windows.append({'kind':'slot','u':sign*10.5,'v':0,'length':39,'width':10.5,'rotation':90})
|
||
for y in [-13.5,13.5]: windows.append({'kind':'circle','u':sign*15.2,'v':y,'diameter':3.8})
|
||
body=r.op('cut_openings','platform_windows',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,0,1],openings=windows,allow_merge=True)
|
||
body=r.op('cut_openings','platform_holes',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,0,1],openings=[{'kind':'circle','u':x,'v':y,'diameter':2.5} for x in [-15,15] for y in [-22.5,22.5]])
|
||
slots=[{'kind':'slot','u':y,'v':z,'length':14,'width':6} for y in [-12.5,12.5] for z in [-19,19]]
|
||
slots += [{'kind':'slot','u':0,'v':z,'length':13,'width':5.7} for z in [-8,8]]
|
||
body=r.op('cut_openings','six_slots_per_side',body,origin=[0,0,0],x_axis=[0,1,0],normal=[1,0,0],openings=slots)
|
||
notch=rounded_polygon([[-32,-18],[-25,-14],[-12,-10],[-12,10],[-25,14],[-32,18]],3)
|
||
body=r.op('cut_openings','same_end_notches',body,origin=[0,0,0],x_axis=[0,1,0],normal=[1,0,0],openings=[notch])
|
||
r.checkpoint('features',body,'上下各两个带外侧耳部的长窗与四小孔;两侧各6水平槽,2-1-1-2分层;同一Y负端开放缺口。未把对壁斜线添加为本面连接条。')
|
||
r.stage='detail'
|
||
probes=[[0,0,0],[0,0,28.6],[10.5,0,28.6],[-10.5,0,28.6],[28.8,0,0],[28.5,-24,0]]
|
||
r.checkpoint('detail',body,'几何有效性及材料点核验;未标孔径/壁厚/圆角仍是估计。没有将未逐项核对的细节声明为通过。',probes)
|
||
|
||
|
||
def build_b(r):
|
||
body=r.op('bent_plate','bent_local',points=[[50.34,0],[50.34,76],[44.84,88],[44.84,146],[37,146]],thickness=2,width=40,bend_radius=2)
|
||
body=r.op('place_on_plane','bent_world',body,origin=[-20,0,0],x_axis=[0,1,0],normal=[1,0,0])
|
||
# Planar C flange traced as a line/arc boundary; heights/diameters other than
|
||
# overall dimensions and stem width are image estimates, not STEP extraction.
|
||
flange_points=[[-20,44.84],[20,44.84],[20,38.5],[22.8,31.3],[24.65,0],[17.75,4],[-17.75,24.5],[-24.65,28.5],[-20,38.5]]
|
||
# Outer arc bulge solves max X=28.28 with min X=-24.65, total 52.93.
|
||
flange_bulges=[0,0,.15,-.28777092530986625,0,1,0,-.12,0]
|
||
f=r.op('sketch_arc_profile','flange_profile',points=flange_points,bulges=flange_bulges)
|
||
# This clockwise Face has a -Z normal: extrude descends from 146 to 144.
|
||
f=r.op('place_on_plane','flange_plane',f,origin=[0,0,146],x_axis=[1,0,0],normal=[0,0,1])
|
||
f=r.op('extrude','flange',f,amount=2)
|
||
body=r.op('boolean_combine','connected_bracket',body,with_id=f,operation='union')
|
||
bottom={'kind':'profile','points':[[-21,-2],[21,-2],[21,8],[20,8],[-20,8],[-21,8]],'bulges':[0,0,0,-.4,0,0]}
|
||
body=r.op('cut_openings','rounded_bottom',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,-1,0],openings=[bottom])
|
||
r.checkpoint('base',body,'Z高146、板带宽40、Y包络50.34;板厚2、偏置5.5、折弯R2、C形曲线是图像估计。法兰整体宽度需在导出后核对52.93。')
|
||
r.stage='features'
|
||
windows=[rounded_polygon([[-8,133],[12,133],[12,106]],2),rounded_polygon([[-12,125],[8,98],[-12,98]],2)]
|
||
body=r.op('cut_openings','upper_two_windows',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,-1,0],openings=windows)
|
||
lower=[rounded_polygon([[-11,68],[11,68],[11,51],[4,56],[2,61]],2),rounded_polygon([[-11,62],[-4,57],[-2,52],[8,47],[-11,47]],2),{'kind':'circle','u':0,'v':55,'diameter':4}]
|
||
body=r.op('cut_openings','lower_two_windows_and_hole',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,-1,0],openings=lower)
|
||
holes=[{'kind':'circle','u':0,'v':26,'diameter':27}]
|
||
holes += [{'kind':'circle','u':x,'v':z,'diameter':3.2} for x in [-15,15] for z in [139.5,131,94,75,9]]
|
||
holes += [{'kind':'circle','u':0,'v':3.4,'diameter':2.5}]
|
||
body=r.op('cut_openings','front_holes',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,-1,0],openings=holes)
|
||
body=r.op('cut_openings','flange_four_holes',body,origin=[0,0,0],x_axis=[1,0,0],normal=[0,0,1],openings=[{'kind':'circle','u':x,'v':y,'diameter':4} for x,y in [(-12.3,34.9),(12.3,34.9),(17.6,31),(24.3,14.2)]],through_all=False,start_depth=143,depth=4)
|
||
r.checkpoint('features',body,'两上窗保留斜连接条;下方两个异形窗及小孔、大圆孔、周边孔和法兰4孔。所有未标局部尺寸是图像估计。')
|
||
r.stage='detail'
|
||
r.checkpoint('detail',body,'修复首版法兰拉伸法向导致的盲孔;校正总宽52.93。孔轴上在厚度内取点确认贯穿,未标细节仍为估计。',[[0,49.3,26],[0,49.3,38],[0,43.8,116],[0,43.8,140]]+[[x,y,z] for x,y in [(-12.3,34.9),(12.3,34.9),(17.6,31),(24.3,14.2)] for z in [144.2,145,145.8]])
|
||
|
||
|
||
if __name__=='__main__':
|
||
parser=argparse.ArgumentParser(); parser.add_argument('case',choices=['3B','3C']); parser.add_argument('--revision',default='v1'); args=parser.parse_args()
|
||
r=Record(args.case,args.revision)
|
||
(build_b if args.case=='3B' else build_c)(r)
|