ictdesigntools.draw¶
Drawings described once and written out as SVG, PDF or DXF.
A Drawing holds shapes (Rect, Circle, Ring, Polygon, Polyline, Arc,
Line, Text, Dim, Earth) in real units, millimetres by default, with y down as
in SVG. Group moves, turns or scales shapes together; a Component is a part drawn
once and placed many times with Use. Shapes take their look from named Style\s, so
colour and line weight live in one place and a theme can restyle a drawing for a dark page
or for print. Meta marks what a part is (an id, a kind and data) so a page can make it
interactive.
Outputs:
to_svg/svg_file: the standard library only.draw.pdf.to_reportlab/draw.pdf.pdf_file: needsictdesigntools[pdf].draw.dxf.to_dxf: needsictdesigntools[dxf]; components become blocks.
to_svg(classes=True) names each styled shape's class after its style, so a page's CSS can
theme it (dark mode); to_svg(interactive=True) with interactive_script() served on the
page makes parts with a Meta id hoverable, focusable and selectable.
draw.graph draws network topologies (a networkx graph, node-link JSON or its own
Graph) in layered or tree layouts with attribute styles and highlighted routes.
draw.text estimates text widths to fit labels, and draw.layout places things in
grids, stacks and rack units and wraps outlines around circles.
>>> from ictdesigntools.draw import Drawing, Rect, Text, to_svg
>>> d = Drawing(title="Panel").add(Rect(0, 0, 482.6, 44.45, stroke="#000"),
... Text(241.3, 22.2, "24-port patch panel", 8))
>>> to_svg(d.fit(5)).startswith("<svg")
True
Drawing
dataclass
¶
Drawing(
view: tuple[float, float, float, float] = (
0.0,
0.0,
100.0,
100.0,
),
items: list = list(),
title: str = "",
desc: str = "",
units: str = "mm",
stroke: float = 0.35,
font: float = 2.5,
layers: dict[str, Layer] = dict(),
styles: dict[str, Style] = dict(),
)
A drawing in units ("mm", the default, or "in"), framed by view
(x, y, width, height; fit() sets it from the content).
style ¶
The look of a shape: its own values, then its named style (from theme first,
then the drawing's), then the defaults.
Source code in src/ictdesigntools/draw/drawing.py
components ¶
Every component placed, inner ones first, each once.
Source code in src/ictdesigntools/draw/drawing.py
walk ¶
Every shape with its transform to the drawing and its layer, components expanded.
bounds ¶
The content's extent (x0, y0, x1, y1), or None when empty.
Source code in src/ictdesigntools/draw/drawing.py
fit ¶
Frame the content with margin around it.
Source code in src/ictdesigntools/draw/drawing.py
Layer
dataclass
¶
A CAD layer: an AutoCAD colour index and an optional linetype.
Arc
dataclass
¶
Arc(
cx: float,
cy: float,
r: float,
start: float,
end: float,
stroke: str | None = None,
width: float | None = None,
dash: bool | None = None,
layer: str = "0",
style: str = "",
meta: Meta = Meta(),
)
Part of a circle from start to end degrees, counter-clockwise as seen on the
page, with 0 pointing right.
Component
dataclass
¶
A part drawn once, in its own coordinates, and placed many times with Use: a 1U
panel, a connector, a duct bundle. SVG writes it once in <defs>; DXF makes it a block.
name must be unique in the drawing.
Dim
dataclass
¶
Dim(
x1: float,
y1: float,
x2: float,
y2: float,
text: str,
offset: float,
vertical: bool = False,
layer: str = "DIM",
)
A linear dimension between two points, drawn offset to one side. Its text size is the
drawing's font.
Earth
dataclass
¶
Earth(
x: float,
y: float,
w: float,
h: float,
holes: tuple[
tuple[float, float, float, float], ...
] = (),
tile: float = 25.0,
layer: str = "EARTH",
)
Soil in the CAD "EARTH" style over a rectangle, leaving out the holes (rectangles): alternating tiles of three horizontal and three vertical strokes. In DXF it is a native EARTH hatch, so CAD users can edit it.
Group
dataclass
¶
Group(
items: tuple = (),
transform: Transform = IDENTITY,
title: str = "",
layer: str = "",
meta: Meta = Meta(),
)
Shapes moved, turned or scaled together. layer, when set, is given to children
left on layer "0".
Hatch
dataclass
¶
Hatch(
pattern: str = "SOLID",
scale: float = 1.0,
color: int | None = None,
angle: float = 0.0,
outline: bool = False,
)
How a closed shape is filled in CAD: "SOLID" or a named pattern such as
"AR-CONC", "ANSI31" or "EARTH", with its scale and an AutoCAD colour index
(None: the layer's colour).
Meta
dataclass
¶
What a part is, for interactive pages and cross-references: an id unique in the
drawing, a kind ("port", "conduit", …) and string data. The SVG output
writes them as data-id, data-kind and data-<key> attributes.
Polygon
dataclass
¶
Polygon(
points: tuple[tuple[float, float], ...],
fill: str | None = None,
stroke: str | None = None,
width: float | None = None,
dash: bool | None = None,
layer: str = "0",
title: str = "",
style: str = "",
meta: Meta = Meta(),
)
A closed outline through points.
Polyline
dataclass
¶
Polyline(
points: tuple[tuple[float, float], ...],
stroke: str | None = None,
width: float | None = None,
dash: bool | None = None,
layer: str = "0",
title: str = "",
style: str = "",
meta: Meta = Meta(),
)
An open path through points, e.g. a cable's route or a leader.
Ring
dataclass
¶
Ring(
cx: float,
cy: float,
r_outer: float,
r_inner: float,
color: str,
bore: str = "#fcfcfb",
layer: str = "0",
title: str = "",
edge: str | None = None,
cad_color: bool = False,
meta: Meta = Meta(),
style: str = "",
)
A tube wall between two radii, with its bore filled: a conduit, duct or tube in
section. edge outlines both radii so a light wall still shows; cad_color carries
the wall colour into CAD.
Style
dataclass
¶
Style(
fill: str | None = None,
stroke: str | None = None,
width: float | None = None,
dash: bool | None = None,
color: str | None = None,
hatch: Hatch | None = None,
cad_color: bool | None = None,
)
A named look. None leaves a value to the next style down (the drawing's, then the
built-in default); colours are CSS hex strings.
over ¶
This style with base filling what it leaves unset.
Use
dataclass
¶
Use(
component: Component,
transform: Transform = IDENTITY,
title: str = "",
layer: str = "",
meta: Meta = Meta(),
)
A component placed by transform.
Transform
dataclass
¶
Transform(
a: float = 1.0,
b: float = 0.0,
c: float = 0.0,
d: float = 1.0,
e: float = 0.0,
f: float = 0.0,
)
scale_factor
property
¶
scale_factor: float
How much lengths grow, for a similarity (uniform scale with rotation or mirror).
is_similarity
property
¶
is_similarity: bool
True when circles stay circles: no shear or uneven scale.
rotation
property
¶
rotation: float
The page rotation of the x axis, in degrees counter-clockwise as seen on the page.
rotate
staticmethod
¶
Counter-clockwise as seen on the page (y down), about (cx, cy).
Source code in src/ictdesigntools/draw/transform.py
then ¶
vector ¶
interactive_script ¶
interactive_script() -> str
The JavaScript (no dependencies) for interactive drawings.
circle_hull ¶
The convex outline around circles (cx, cy, r): how a sheath shrinks onto what it holds, whatever their sizes.
Source code in src/ictdesigntools/draw/layout.py
convex_hull ¶
The convex outline around points, anticlockwise (Andrew's monotone chain).
Source code in src/ictdesigntools/draw/layout.py
grid ¶
grid(
count: int,
columns: int,
pitch_x: float,
pitch_y: float,
x: float = 0.0,
y: float = 0.0,
) -> list[tuple[float, float]]
count positions filling rows of columns left to right, then top to bottom.
Source code in src/ictdesigntools/draw/layout.py
rack_y ¶
The top edge of equipment height_u high whose lowest unit is u (numbered 1 from
the bottom, as on the rails) in a rack rack_u high drawn from top down.
Source code in src/ictdesigntools/draw/layout.py
stack ¶
Where each of a run of sizes starts when laid end to end with gap between them.
Source code in src/ictdesigntools/draw/layout.py
svg_file ¶
A standalone SVG file, at full size in the drawing's units.
Source code in src/ictdesigntools/draw/svg.py
to_svg ¶
to_svg(
drawing: Drawing,
css_class: str = "",
standalone: bool = False,
theme: dict[str, Style] | None = None,
id_prefix: str = "",
classes: bool = False,
class_prefix: str = "d-",
interactive: bool = False,
) -> str
The drawing as an <svg> element.
standalone adds the namespace and a physical size for a file; id_prefix keeps
component ids unique when a page holds several drawings; theme overrides named styles.
classes also gives every shape with a named style the class <class_prefix><name>, so
a page's stylesheet can restyle it (a dark theme, a highlight) over the colours written as
attributes, which stay as the fallback. interactive marks the drawing for the optional
script (interactive_script()): hover, select, keyboard focus and a draw:select
event for parts that carry a Meta id.
Source code in src/ictdesigntools/draw/svg.py
fit_size ¶
The largest size up to size at which text fits in width, or None when it
would have to be smaller than smallest.
Source code in src/ictdesigntools/draw/text.py
text_width ¶
The width of text set at size (the em height), in the same units.
Source code in src/ictdesigntools/draw/text.py
truncate ¶
text cut short with ellipsis to fit width at size; "" if nothing fits.
Source code in src/ictdesigntools/draw/text.py
wrap ¶
text broken into lines at spaces to fit width; a word longer than the width is
left on a line of its own.