tscad GitHub

Sketch Flange

Pipe flange with two mounting lugs, drawn as one constraint sketch: - the right lug is drawn once (two lines tangent to the flange circle and to the lug's end arc) and mirrored with <mirrorEntities>, so both lugs stay identical as params change; - the bolt circle is one hole and a <circularPattern>, its copies tied by rotation constraints rather than computed coordinates.

Open in playground →

examples/sketch-flange.tsx on GitHub

bunx tscad dev examples/sketch-flange.tsx
/**
 * Pipe flange with two mounting lugs, drawn as one constraint sketch:
 * - the right lug is drawn once (two lines tangent to the flange circle and to the lug's end
 *   arc) and mirrored with <mirrorEntities>, so both lugs stay identical as params change;
 * - the bolt circle is one hole and a <circularPattern>, its copies tied by rotation
 *   constraints rather than computed coordinates.
 *
 *   tscad dev examples/sketch-flange.tsx
 */
import { defineParams, edges, type ParamValues } from "tscad"

export const params = defineParams({
  diameter: { type: "number", default: 80, min: 40, max: 200, step: 1, unit: "mm", label: "Flange Ø" },
  bore: { type: "number", default: 32, min: 10, max: 120, step: 1, unit: "mm", label: "Bore Ø" },
  bolts: { type: "int", default: 6, min: 3, max: 16, label: "Bolts" },
  boltCircle: { type: "number", default: 58, min: 25, max: 180, step: 1, unit: "mm", label: "Bolt circle Ø" },
  boltHole: { type: "number", default: 6.6, min: 2, max: 20, step: 0.1, unit: "mm", label: "Bolt hole Ø" },
  lugReach: { type: "number", default: 62, min: 30, max: 150, step: 1, unit: "mm", label: "Lug hole offset" },
  lugRadius: { type: "number", default: 10, min: 5, max: 30, step: 0.5, unit: "mm", label: "Lug end radius" },
  lugHole: { type: "number", default: 8.5, min: 3, max: 20, step: 0.1, unit: "mm", label: "Lug hole Ø" },
  thickness: { type: "number", default: 8, min: 3, max: 25, step: 0.5, unit: "mm", label: "Thickness" },
})

export default function Flange(p: ParamValues<typeof params>) {
  const R = p.diameter / 2
  // derived so any slider combination stays a valid flange, with at least `gap` of material
  // between features (the top-edge fillet needs room)
  const gap = 4
  const bore = Math.min(p.bore, p.diameter - 4 * gap)
  const lugR = Math.min(p.lugRadius, 0.45 * R)
  const reach = Math.max(p.lugReach, R + 0.3 * lugR) // the lug must stick out of the disc
  const lugHole = Math.max(1, Math.min(p.lugHole, 2 * (lugR - gap)))
  // bolt circle between the bore and the rim; fewer bolts if they would touch
  const hole = Math.min(p.boltHole, R - bore / 2 - 2 * gap)
  const bc = Math.min(Math.max(p.boltCircle / 2, bore / 2 + hole / 2 + gap), R - hole / 2 - gap)
  const maxBolts = hole > 1 ? Math.floor(Math.PI / Math.asin(Math.min(1, (hole + gap) / (2 * bc)))) : 0
  const bolts = Math.min(p.bolts, maxBolts)
  const withBolts = hole > 1 && bolts >= 2
  return (
    <part name="flange" material="steel">
      <fillet radius={1} edges={edges.max("z")}>
        <extrude height={p.thickness}>
          <sketch>
            <point id="o" fixed />
            <line id="axis" from="o" to="up" vertical length={R} construction />

            {/* right lug: centre on the X axis, end arc, two tangent lines into the flange */}
            <point id="lug" />
            <horizontal a="o" b="lug" />
            <dimension a="o" b="lug" dx={reach} />
            <arc id="lugEnd" center="lug" from="l1" to="l2" radius={lugR} />
            {/* rough positions: where the lug's lines meet the lug and the disc */}
            <point id="l1" x={reach} y={-lugR} />
            <point id="l2" x={reach} y={lugR} />
            <point id="m1" x={0.7 * R} y={-0.7 * R} />
            <point id="m2" x={0.7 * R} y={0.7 * R} />
            <line id="lugTop" from="l2" to="m2" />
            <line id="lugBottom" from="m1" to="l1" />

            {/* the flange circle between the lugs: top and bottom arcs */}
            <arc id="top" center="o" from="m2" to="m2'" radius={R} />
            <arc id="bottom" center="o" from="m1'" to="m1" equal="top" />
            {/* the lug's lines are outer tangents: the disc's and the lug's centres both on their
                left (`side` picks that branch whatever the sizes) */}
            <tangent a="lugTop" b="lugEnd" side="left" />
            <tangent a="lugBottom" b="lugEnd" side="left" />
            <tangent a="lugTop" b="top" side="left" />
            <tangent a="lugBottom" b="bottom" side="left" />
            <mirrorEntities about="axis" of={["lugEnd", "lugTop", "lugBottom"]} />

            {/* holes: bore, lug holes, bolt circle */}
            <circle center="o" diameter={bore} />
            <circle center="lug" diameter={lugHole} />
            <circle center="lug'" diameter={lugHole} />
            {withBolts && (
              <>
                <circle id="bolt" center="b" diameter={hole} />
                <dimension a="o" b="b" distance={bc} />
                <line from="o" to="b" angle={90 + 180 / bolts} construction />
                <circularPattern center="o" count={bolts} of={["bolt"]} />
              </>
            )}
          </sketch>
        </extrude>
      </fillet>
    </part>
  )
}