Repository navigation
Expand file tree
/
Copy pathbuild-example.ts
More file actions
103 lines (93 loc) · 7.63 KB
/
Copy pathbuild-example.ts
File metadata and controls
103 lines (93 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Generates examples/apartment.opencalque: a small furnished flat that shows most of what a
// drawing can hold (walls, openings, rooms with floor patterns, components from the libraries,
// dimensions, annotations and a paper). Run with `pnpm example` after changing it.
import { readFileSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { applyOps, componentsOf, createDocument, importComponents, parseDocument, serializeDocument, type Document, type NodeInput, type Op } from '@opencalque/core'
const here = dirname(fileURLToPath(import.meta.url))
const library = (name: string) => parseDocument(JSON.parse(readFileSync(join(here, `../../warehouse/components/${name}.opencalque`), 'utf8')))
let doc: Document = applyOps(createDocument('Apartment'), [
{ op: 'update_node', id: 'page_1', patch: { name: 'Ground floor' } },
{ op: 'update_layer', id: 'layer_1', patch: { name: 'Walls' } },
{ op: 'add_layer', layer: { id: 'rooms', name: 'Rooms' } },
{ op: 'add_layer', layer: { id: 'furniture', name: 'Furniture', color: '#5b6472' } },
{ op: 'add_layer', layer: { id: 'notes', name: 'Dimensions and notes', color: '#1d4ed8' } },
])
const add = (node: Record<string, unknown>): Op => ({ op: 'add_node', node: { parent: 'page_1', ...node } as NodeInput })
const wall = (ax: number, ay: number, bx: number, by: number, thickness: number) => add({ type: 'wall', layer: 'layer_1', a: { x: ax, y: ay }, b: { x: bx, y: by }, thickness })
/** A door or window on a wall: `turn` is the wall's direction, 0 along the sheet and 90 down it. */
const opening = (kind: string, x: number, y: number, turn: number, props: Record<string, unknown>) => add({ type: 'parametric', layer: 'layer_1', kind, x, y, rotation: turn, props })
const room = (name: string, x: number, y: number, pattern?: Record<string, unknown>) =>
add({ type: 'room', layer: 'rooms', name, x, y, size: 220, ...(pattern ? { modifiers: [{ type: 'hatch', frame: { x: 0, y: 0 }, params: pattern }] } : {}) })
doc = applyOps(doc, [
{ op: 'set_document', info: { address: '12 rue des Lilas\n75020 Paris', author: 'OpenCalque' } },
// The sheet first: an A3 lying, at 1:50.
add({ type: 'paper', name: 'Ground floor', x: -5500, y: -4200, width: 21000, height: 14850, scale: 50, titleBlock: { date: '{date}', number: '{page-number} / {pages}' } }),
// Outside walls, then the partitions: a hall behind the entrance, serving the living room, the bedroom and the bathroom.
wall(0, 0, 10000, 0, 250),
wall(10000, 0, 10000, 7000, 250),
wall(10000, 7000, 0, 7000, 250),
wall(0, 7000, 0, 0, 250),
wall(6000, 0, 6000, 7000, 100),
wall(6000, 3800, 10000, 3800, 100),
wall(7600, 3800, 7600, 7000, 100),
// The kitchen is open on the living room: a corner of it, marked off without walls.
add({ type: 'divider', layer: 'rooms', a: { x: 0, y: 4300 }, b: { x: 3700, y: 4300 } }),
add({ type: 'divider', layer: 'rooms', a: { x: 3700, y: 4300 }, b: { x: 3700, y: 7000 } }),
opening('arch.door', 6350, 7000, 0, { width: 950, flip: true }),
opening('arch.door', 6000, 4350, 90, { width: 850 }),
opening('arch.door', 6350, 3800, 0, { width: 850, flip: true }),
opening('arch.door', 7600, 4900, 90, { width: 750, flip: true }),
opening('arch.window', 1400, 0, 0, { width: 2200, panes: 3 }),
opening('arch.window', 7200, 0, 0, { width: 1600 }),
opening('arch.window', 0, 1600, 90, { width: 1500 }),
opening('arch.window', 1200, 7000, 0, { width: 1400 }),
opening('arch.window', 10000, 5000, 90, { width: 800, panes: 1 }),
room('Living room', 3000, 3300, { pattern: 'Planks', spacing: 180 }),
room('Kitchen', 1800, 5300, { pattern: 'Tiles', spacing: 400 }),
room('Bedroom', 6950, 2500, { pattern: 'Planks', spacing: 180, angle: 90 }),
room('Bathroom', 8750, 5200, { pattern: 'Tiles', spacing: 300 }),
room('Hall', 6800, 5450),
add({ type: 'dimension', layer: 'notes', a: { x: 0, y: 0 }, b: { x: 10000, y: 0 }, offset: -900, unit: 'm', decimals: 2, showUnit: true, size: 200 }),
add({ type: 'dimension', layer: 'notes', a: { x: 0, y: 7000 }, b: { x: 0, y: 0 }, offset: -900, unit: 'm', decimals: 2, showUnit: true, size: 200 }),
add({ type: 'annotation', layer: 'notes', a: { x: 1300, y: 3700 }, b: { x: -2300, y: 4100 }, text: 'Oak floor, oiled', bend: 0.12, size: 220 }),
add({ type: 'annotation', layer: 'notes', a: { x: 2550, y: 6450 }, b: { x: 3300, y: 8300 }, text: 'Induction hob', bend: 0.15, size: 220 }),
add({ type: 'annotation', layer: 'notes', a: { x: 6825, y: 7200 }, b: { x: 8100, y: 8300 }, text: 'Entrance', bend: -0.2, size: 220 }),
add({ type: 'text', layer: 'notes', x: -4700, y: -2900, text: 'Apartment, ground floor', size: 520 }),
add({ type: 'text', layer: 'notes', x: -4700, y: -2250, text: '70 m², two rooms. Scale 1:50', size: 260 }),
])
/** Copies components out of a library and returns a function that places them by name. */
function furnish(name: string) {
const source = library(name)
const result = importComponents(doc, source)
doc = result.doc
return (component: string, x: number, y: number, rotation = 0): Op => {
const found = componentsOf(source).find((c) => c.name === component)
if (!found) throw new Error(`No component called "${component}" in the ${name} library`)
return add({ type: 'instance', layer: 'furniture', component: result.imported[found.id], x, y, rotation })
}
}
const furniture = furnish('furniture')
doc = applyOps(doc, [
add({ type: 'parametric', layer: 'furniture', kind: 'arch.sofa', x: 500, y: 500, props: { width: 1600, depth: 850, seats: 2 } }),
furniture('Coffee table, round', 850, 1650),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.armchair', x: 2700, y: 1550, rotation: 90, props: { width: 850, depth: 850 } }),
// The adjustable table and bed, which ship with the app: the table by its top's corner, its chairs around it.
add({ type: 'parametric', layer: 'furniture', kind: 'furn.table', x: 3500, y: 1130, props: { length: 1800, width: 900, seats: 6 } }),
add({ type: 'parametric', layer: 'furniture', kind: 'furn.bed', x: 7800, y: 250, props: { width: 1600, length: 2000 } }),
// A symbol's back is its top edge: these stand against the wall below them, so they are turned
// round, placed by the corner that was their top left.
add({ type: 'parametric', layer: 'furniture', kind: 'arch.wardrobe', x: 9850, y: 3700, rotation: 180, props: { width: 1800, depth: 600, doors: 4 } }),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.fridge', x: 850, y: 6850, rotation: 180, props: { width: 600, depth: 650, doors: 1 } }),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.sink', x: 2150, y: 6850, rotation: 180, props: { width: 1200, depth: 600, bowls: 1, drainer: 'Right' } }),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.hob', x: 2850, y: 6850, rotation: 180, props: { width: 600, depth: 600 } }),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.baseunit', x: 3550, y: 6850, rotation: 180, props: { width: 600, depth: 600 } }),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.washbasin', x: 7850, y: 3900, rotation: 0, props: { width: 600, depth: 450, bowls: 1 } }),
// The toilet against the partition above it, the shower tray in the corner against the outside wall below it.
add({ type: 'parametric', layer: 'furniture', kind: 'arch.toilet', x: 8950, y: 3850, props: { width: 380, depth: 700 } }),
add({ type: 'parametric', layer: 'furniture', kind: 'arch.showertray', x: 9875, y: 6875, rotation: 180, props: { width: 900, depth: 900 } }),
])
const out = join(here, '../../../examples/apartment.opencalque')
writeFileSync(out, serializeDocument(doc))
console.log(`Wrote ${Object.keys(doc.nodes).length} nodes to ${out}`)