main
John Lauer Add JST GH libraries, native schematic parity and revised sensor harness study with reviewed clips 1ac01c3
"""Prepare portable JST GH candidate libraries; original off-board geometry is approximate."""
from pathlib import Path
import cadquery as cq
import json,shutil,hashlib
import sexp
R=Path(__file__).resolve().parents[1];source='https://www.jst-mfg.com/product/pdf/eng/eGH.pdf'
items=[('bm03b-ghs-tbt','BM03B-GHS-TBT',3,'top-entry SMT header'),('ghr-03v-s','GHR-03V-S',3,'off-board keyed cable housing'),('sshl-002t-p0-2','SSHL-002T-P0.2',1,'loose cable crimp contact')]
for slug,mpn,n,role in items:
 P=R/'components'/slug;P.mkdir(exist_ok=True);(P/'review').mkdir(exist_ok=True);header=slug=='bm03b-ghs-tbt'
 if header:
  stock=P/'review/JST_GH_BM03B-GHS-TBT_1x03-1MP_P1.25mm_Vertical.step';shutil.copy2(stock,P/(slug+'.step'))
  f=sexp.parse((P/'review/JST_GH_BM03B-GHS-TBT_1x03-1MP_P1.25mm_Vertical.kicad_mod').read_text());f[1]=mpn
  for p in sexp.find(f,'property'):
   if p[1]=='Value':p[2]=mpn
  sexp.find(f,'model')[0][1]='${KIPRJMOD}/'+slug+'.step';(P/(slug+'.kicad_mod')).write_text(sexp.dump(f)+'\n')
  classification='KiCad stock nominal library model; not manufacturer CAD';dims={'bodyAndRetainerWidth':7.0,'overallModelDepth':4.9504,'overallHeight':4.2,'pitch':1.25};limits=['Pin cavities and contact spring/retention detail are nominal, not manufacturer-approved mating geometry.','Two MP pads are mechanical retention solder tabs, not signal pins.','No native Altium or Fusion parity claimed.']
 else:
  a=cq.Assembly(name=mpn.replace('-','_').replace('.','_')+'_NOMINAL')
  if n==3:
   body=cq.Workplane('XY').box(5,3.0,5.7,centered=(True,True,False))
   for x in [-1.25,0,1.25]:body=body.cut(cq.Workplane('XY').box(.85,1.4,5.9,centered=(True,True,False)).translate((x,0,-.1)))
   # Illustrative latch within manufacturer overall4.15mm depth, no engagement claim.
   latch=cq.Workplane('XY').box(3,.6,3.5,centered=(True,False,False)).translate((0,2.05,1.8));bridge=cq.Workplane('XY').box(3,1.5,.4,centered=(True,False,False)).translate((0,1.15,1.8))
   a.add(body,name='Housing_Nominal',color=cq.Color(.86,.85,.79));a.add(latch.union(bridge),name='Latch_Illustrative',color=cq.Color(.86,.85,.79));dims={'width':5,'overallDepth':4.15,'height':5.7,'pitch':1.25};limits=['Socket passages and latch are illustrative, not drawing-controlled mating/retention geometry.','Empty housing contains no conductors; order contacts separately.'];w,d=5,4.15
  else:
   socket=cq.Workplane('XY').box(.8,.55,2,centered=(True,True,False)).cut(cq.Workplane('XY').box(.64,.39,2.2,centered=(True,True,False)).translate((0,0,-.1)))
   spine=cq.Workplane('XY').box(.6,.08,2.8,centered=(True,False,False)).translate((0,-.275,2))
   a.add(socket.union(spine),name='Socket_and_spine_approx',color=cq.Color(.72,.73,.75))
   for z,length in [(2.3,.8),(3.8,1.0)]:
    barrel=cq.Workplane('XY').polyline([(-.4,.45),(-.28,-.275),(.28,-.275),(.4,.45),(.32,.45),(.20,-.195),(-.20,-.195),(-.32,.45)]).close().extrude(length).translate((0,0,z));a.add(barrel,name='Loose_barrel_'+str(z).replace('.','_'),color=cq.Color(.72,.73,.75))
   dims={'overallLength':4.8,'socketSection':[.8,.55],'insulationDiameterRange':[.76,1.0],'wireAWG':[30,26]};limits=['Original loose-contact illustration; barrel/spine lengths, wall thickness, spring and latch details are not dimensioned or qualified.','Not an installed/crimped variant and not evidence of contact engagement.'];w,d=.8,.8
  a.save(str(P/(slug+'.step')));classification='Original simplified drawing-led envelope; not manufacturer CAD'
  (P/(slug+'.kicad_mod')).write_text(f'''(footprint "{mpn}_MECHANICAL_ONLY" (version 20240108) (generator "adom") (layer "F.Cu") (attr board_only exclude_from_pos_files exclude_from_bom)
(descr "Off-board envelope; zero copper pads; not a PCB solder footprint")
(fp_rect (start {-w/2} {-d/2}) (end {w/2} {d/2}) (stroke (width 0.1) (type default)) (fill none) (layer "F.Fab"))
(model "${{KIPRJMOD}}/{slug}.step" (offset (xyz 0 0 0)) (scale (xyz 1 1 1)) (rotate (xyz 0 0 0))))\n''')
 # Original passive documentation symbols, no project-specific signal assignment.
 pins=''.join(f'''(pin passive line (at -2.54 {(n-1)/2*2.54-i*2.54} 0) (length 2.54) (name "{i+1}" (effects (font (size 1 1)))) (number "{i+1}" (effects (font (size 1 1)))))'''for i in range(n))
 (P/(slug+'.kicad_sym')).write_text(f'''(kicad_symbol_lib (version 20231120) (generator "adom") (symbol "{mpn}" (in_bom yes) (on_board {'yes'if header else 'no'})
(property "Reference" "J" (at 0 5.08 0) (effects (font (size 1.27 1.27))))
(property "Value" "{mpn}" (at 0 -5.08 0) (effects (font (size 1.27 1.27))))
(symbol "{mpn}_0_1" (rectangle (start 0 3.81) (end 2.54 -3.81) (stroke (width 0) (type default)) (fill (type background))))
(symbol "{mpn}_1_1" {pins})))\n''')
 (P/'review/model-review.json').write_text(json.dumps({'classification':classification,'dimensionsMm':dims,'limits':limits,'source':source,'datasheetPages':[2,3],'drawingRevision':'Retrieved2026-10-11; document front matter has no revision identifier','visualReview':'pending','physicalQualification':False},indent=2)+'\n')
 print(slug,'prepared')