--- jupyter: description: 'Parameters shared by planex2d sketches: px.Parameters holds named values, sk.Use lets a sketch''s expressions name them, and one Update changes every sketch that uses them. A flange and its gasket that keep their bolt holes in line.' --- # Parameters shared by sketches Parts that fit together share dimensions. A flange and its gasket have the same bolt circle, and when the bolt circle changes, both have to change with it. `px.Parameters` holds such values by name. A sketch that uses the set with `sk.Use(p)` can name the values in its expressions, and `p.Update(...)` sets new values in every sketch that uses them, in one step. ```python import math import planex as px ``` ```python nbsphinx="hidden" from docs_figures import Row ``` ## A flange and its gasket The example is a round flange with an outer diameter of 120, four bolt holes of 9 on a bolt circle of 90 and a bore of 50, and its gasket, which has the same outline and holes but an opening of 70. The three shared values are parameters: ```python p = px.Parameters(od=120, pcd=90, bolt=9) print(p) ``` Both parts are drawn by one function. Each sketch dimensions its outline, its bore and one bolt hole, and three expressions tie the shared dimensions to the parameters. A circular pattern then repeats the hole four times: ```python def part(bore): sk = px.Sketch().Use(p) sk.Circle((0, 0), 58, id="outer") sk.Constrain("outer.center", px.Fix) sk.Constrain("outer", px.Diameter, 116, id="outer_d") sk.Circle((0, 0), bore / 2, id="bore") sk.Constrain("bore", "outer", px.Concentric) sk.Constrain("bore", px.Diameter, bore, id="bore_d") sk.Circle((44, 0), 4, id="hole") sk.Constrain("hole", px.Diameter, 8, id="hole_d") sk.Constrain("hole.center", "outer.center", px.Distance, 44, id="hole_r") sk.Constrain("hole.center", "outer.center", px.Horizontal) sk.Expression("outer_d = od").Expression("hole_r = pcd / 2").Expression("hole_d = bolt") sk.CircularPattern("hole", "outer.center", 4) return sk flange, gasket = part(50), part(70) print("flange", flange.status, "gasket", gasket.status) ``` The outline and the hole were drawn at rough sizes. The expressions bring them to the parameters' values, so both parts are fully constrained with an outer diameter of 120 and their holes on the bolt circle of 90 (Figure 1). Their areas are the disc less the bore and the four holes: ```python for name, sk, bore in (("flange", flange, 50), ("gasket", gasket, 70)): print(name, "area", sk.Face().mass, "exact", math.pi / 4 * (120**2 - bore**2 - 4 * 9**2)) ``` ```python nbsphinx="hide-input" Row((flange.Face(), "The flange"), (gasket.Face(), "The gasket"), caption="A flange and its gasket from one parameter set") ``` ## One update for both A larger flange, with an outer diameter of 130, a bolt circle of 100 and bolts of 11, is one call. `p.Update` sets the three values and solves both sketches again: ```python p.Update(pcd=100, od=130, bolt=11) for name, sk, bore in (("flange", flange, 50), ("gasket", gasket, 70)): print(name, "area", sk.Face().mass, "exact", math.pi / 4 * (130**2 - bore**2 - 4 * 11**2)) ``` The bolt holes of the two parts stay in line, each of them at the same place in both: ```python def holes(sk): centers = [sk.Measure(ref)["center"][:2] for ref in sk.elements["Circle"] if ref not in ("outer", "bore")] return sorted((round(x, 9), round(y, 9)) for x, y in centers) print(holes(flange)) print(holes(flange) == holes(gasket)) ``` ## The rules - A parameter is set only by `p.Update`. Setting it in a sketch, as `flange["pcd"] = 110`, raises an error, as do driving it and removing it there. - Inside a sketch, its dimensions stay its own, with their own ids. No dimension or element of a sketch may have the name of a parameter, and `h`, `v` and `t` are no parameter names, since curve formulas use them. - `p.Update` changes all sketches or none. If one of them has no solution for the new values, it raises an error that names that sketch and leaves every sketch as it was. - Curve formulas may name parameters too. See [Parametric curves](../sketch/parametric-curves.md) for curves. ```python try: flange["pcd"] = 110 except ValueError as err: print(err) ```