{
 "commands": [
  {
   "command": "parameters.add",
   "params": {
    "name": "L",
    "expression": "60 mm",
    "comment": "hinge length"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "n",
    "expression": "5",
    "unit": "",
    "comment": "knuckle count, odd whole number 3..9"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "g",
    "expression": "0.4 mm",
    "comment": "axial gap between knuckles"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "c",
    "expression": "0.4 mm",
    "comment": "radial pin clearance"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "pin_d",
    "expression": "4 mm",
    "comment": "pin diameter"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "kd",
    "expression": "8 mm",
    "comment": "knuckle outer diameter (>= pin_d+2c+2.4)"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "t",
    "expression": "3 mm",
    "comment": "leaf thickness (< kd/2)"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "lw",
    "expression": "20 mm",
    "comment": "leaf width, hinge axis to leaf edge"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "hole_d",
    "expression": "3.5 mm",
    "comment": "screw hole diameter"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "cs_d",
    "expression": "7 mm",
    "comment": "countersink diameter"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "hx",
    "expression": "20 mm",
    "comment": "screw hole offset from centre along hinge"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "hy",
    "expression": "12 mm",
    "comment": "screw hole offset from hinge axis"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "s",
    "expression": "(L - (n-1)*g)/n",
    "comment": "knuckle segment length (derived)"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "kc",
    "expression": "kd + 2*c",
    "comment": "leaf relief diameter around a foreign knuckle (derived)"
   }
  },
  {
   "command": "parameters.add",
   "params": {
    "name": "rl",
    "expression": "s + 2*g + 0.02 mm",
    "comment": "relief length (0.01 mm extra each side avoids coplanar faces)"
   }
  },
  {
   "command": "solid.box",
   "params": {
    "length": "L",
    "width": "lw",
    "height": "t",
    "corner": [
     "-L/2",
     0,
     0
    ],
    "body_name": "LeafA",
    "name": "LeafA_Plate"
   }
  },
  {
   "command": "solid.cylinder",
   "params": {
    "diameter": "kc",
    "height": "rl",
    "axis": "X",
    "base": [
     "-L/2 + s - 0.01 mm",
     "0",
     "kd/2"
    ],
    "operation": "cut",
    "targets": [
     "LeafA"
    ],
    "name": "LeafA_Relief"
   }
  },
  {
   "command": "solid.pattern.rectangular",
   "params": {
    "features": [
     "LeafA_Relief"
    ],
    "dir1": [
     1,
     0,
     0
    ],
    "count1": "(n-1)/2",
    "spacing1": "2*(s+g)",
    "name": "LeafA_Reliefs"
   }
  },
  {
   "command": "solid.cylinder",
   "params": {
    "diameter": "kd",
    "height": "s",
    "axis": "X",
    "base": [
     "-L/2",
     "0",
     "kd/2"
    ],
    "operation": "join",
    "targets": [
     "LeafA"
    ],
    "name": "KnuckleA"
   }
  },
  {
   "command": "solid.pattern.rectangular",
   "params": {
    "features": [
     "KnuckleA"
    ],
    "dir1": [
     1,
     0,
     0
    ],
    "count1": "(n+1)/2",
    "spacing1": "2*(s+g)",
    "name": "KnucklesA"
   }
  },
  {
   "command": "solid.cylinder",
   "params": {
    "diameter": "pin_d",
    "height": "L - 2 mm",
    "axis": "X",
    "base": [
     "-L/2 + 1 mm",
     "0",
     "kd/2"
    ],
    "operation": "join",
    "targets": [
     "LeafA"
    ],
    "name": "Pin"
   }
  },
  {
   "command": "solid.box",
   "params": {
    "length": "L",
    "width": "lw",
    "height": "t",
    "corner": [
     "-L/2",
     "-lw",
     0
    ],
    "body_name": "LeafB",
    "name": "LeafB_Plate"
   }
  },
  {
   "command": "solid.cylinder",
   "params": {
    "diameter": "kc",
    "height": "rl",
    "axis": "X",
    "base": [
     "-L/2 - g - 0.01 mm",
     "0",
     "kd/2"
    ],
    "operation": "cut",
    "targets": [
     "LeafB"
    ],
    "name": "LeafB_Relief"
   }
  },
  {
   "command": "solid.pattern.rectangular",
   "params": {
    "features": [
     "LeafB_Relief"
    ],
    "dir1": [
     1,
     0,
     0
    ],
    "count1": "(n+1)/2",
    "spacing1": "2*(s+g)",
    "name": "LeafB_Reliefs"
   }
  },
  {
   "command": "solid.cylinder",
   "params": {
    "diameter": "kd",
    "height": "s",
    "axis": "X",
    "base": [
     "-L/2 + s + g",
     "0",
     "kd/2"
    ],
    "operation": "join",
    "targets": [
     "LeafB"
    ],
    "name": "KnuckleB"
   }
  },
  {
   "command": "solid.pattern.rectangular",
   "params": {
    "features": [
     "KnuckleB"
    ],
    "dir1": [
     1,
     0,
     0
    ],
    "count1": "(n-1)/2",
    "spacing1": "2*(s+g)",
    "name": "KnucklesB"
   }
  },
  {
   "command": "solid.cylinder",
   "params": {
    "diameter": "pin_d + 2*c",
    "height": "L + 2 mm",
    "axis": "X",
    "base": [
     "-L/2 - 1 mm",
     "0",
     "kd/2"
    ],
    "operation": "cut",
    "targets": [
     "LeafB"
    ],
    "name": "PinBore"
   }
  },
  {
   "command": "solid.hole",
   "params": {
    "position": [
     "-hx",
     "hy",
     "t"
    ],
    "diameter": "hole_d",
    "type": "countersink",
    "cs_diameter": "cs_d",
    "cs_angle": 90,
    "name": "Screw1"
   }
  },
  {
   "command": "solid.hole",
   "params": {
    "position": [
     "hx",
     "hy",
     "t"
    ],
    "diameter": "hole_d",
    "type": "countersink",
    "cs_diameter": "cs_d",
    "cs_angle": 90,
    "name": "Screw2"
   }
  },
  {
   "command": "solid.hole",
   "params": {
    "position": [
     "-hx",
     "-hy",
     "t"
    ],
    "diameter": "hole_d",
    "type": "countersink",
    "cs_diameter": "cs_d",
    "cs_angle": 90,
    "name": "Screw3"
   }
  },
  {
   "command": "solid.hole",
   "params": {
    "position": [
     "hx",
     "-hy",
     "t"
    ],
    "diameter": "hole_d",
    "type": "countersink",
    "cs_diameter": "cs_d",
    "cs_angle": 90,
    "name": "Screw4"
   }
  },
  {
   "command": "appearance.assign",
   "params": {
    "bodies": [
     "LeafA"
    ],
    "color": "#2f7fd8"
   }
  },
  {
   "command": "appearance.assign",
   "params": {
    "bodies": [
     "LeafB"
    ],
    "color": "#e8892a"
   }
  }
 ]
}