This directory contains the core data structures and operations for representing and manipulating weaving drafts in AdaCAD. A draft is the fundamental data structure that represents a weaving pattern, including the interlacement pattern (drawdown), material assignments (shuttles), and structural systems.
Defines all TypeScript interfaces and types used throughout the draft system:
-
Draft: The main draft interface containing:id: Unique numeric identifiergen_name: Auto-generated nameud_name: User-defined namedrawdown: 2D array of cells representing the interlacement patternrowShuttleMapping: Array mapping each weft pick to a material/shuttle IDrowSystemMapping: Array mapping each weft pick to a system IDcolShuttleMapping: Array mapping each warp end to a material/shuttle IDcolSystemMapping: Array mapping each warp end to a system IDnotes: Optional user notes
-
Cell: Represents a single intersection of warp and weft:is_set: Boolean indicating if a weft crosses this locationis_up: Boolean indicating if the heddle is lifted (true) or lowered (false)- Three valid states:
{is_set: true, is_up: true}(heddle up/black),{is_set: true, is_up: false}(heddle down/white),{is_set: false, is_up: false}(unset/no weft)
-
Drawdown: Type alias forArray<Array<Cell>>, representing a 2D grid of cells -
System: Represents a structural unit (like a layer):id: Numeric identifiername: Display namenotes: User notesvisible: Visibility flagin_use: Whether the system is currently assigned
-
Interlacement: Location within a drawdown:i: Row/weft number (0 at top)j: Column/warp number (0 at left)
-
InterlacementVal: Location with value:i,j: Position coordinatesval: Boolean value (true = heddle up, false = heddle down, null = unset)
-
InitDraftParams: Parameters for initializing drafts with specific settings -
InitSystemParams: Parameters for initializing systems -
CompressedDraft: Space-efficient draft format using byte arrays
Functions for creating and manipulating individual cells:
-
createCell(setting: boolean | null): Cell- Creates a cell from a boolean or null value
- Handles conversion to proper cell state
-
toggleHeddle(c: Cell): Cell- Toggles the heddle state (up/down)
- If unset, sets to heddle up
-
createCellFromSequenceVal(val: number): Cell- Creates cell from sequence value (0=down, 1=up, 2=unset)
-
setCellValue(c: Cell, value: boolean | null): Cell- Sets the cell value, handling null for unset
-
getCellValue(c: Cell): boolean | null- Returns the cell value (true/false/null)
-
cellToSequenceVal(c: Cell): number- Converts cell to sequence value (0/1/2)
Core functions for creating, manipulating, and querying drafts:
Draft Creation:
initDraft(): Creates an empty draft with unique IDinitDraftWithParams(params: InitDraftParams): Creates draft with specific parametersinitDraftFromDrawdown(drawdown: Drawdown): Creates draft from drawdown onlycreateDraft(...): Creates draft with all parameters specifiedcopyDraft(d: Draft): Deep copies a draft
Drawdown Operations:
createBlankDrawdown(wefts: number, warps: number): Creates empty drawdownwefts(d: Drawdown): Returns number of wefts (rows)warps(d: Drawdown): Returns number of warps (columns)hasCell(d: Drawdown, i: number, j: number): Checks if cell existsisUp(d: Drawdown, i: number, j: number): Checks if heddle is upisSet(d: Drawdown, i: number, j: number): Checks if cell is setsetHeddle(d: Drawdown, i: number, j: number, bool: boolean): Sets heddle stategetHeddle(d: Drawdown, i: number, j: number): Gets heddle stategetCol(d: Drawdown, j: number): Gets a column as array
Drawdown Transformations:
invertDrawdown(drawdown: Drawdown): Inverts all cells (up becomes down, etc.)flipDrawdown(drawdown: Drawdown, horiz: boolean): Flips horizontallyshiftDrawdown(drawdown: Drawdown, up: boolean, inc: number): Shifts rowsapplyMask(mask: Drawdown, pattern: Drawdown): Applies mask to patternpasteIntoDrawdown(...): Pastes pattern into drawdown at specified locationcropDraft(draft: Draft, top, left, width, height): Crops draft to specified region
Row/Column Manipulation:
insertDrawdownRow(d: Drawdown, i: number, row: Array<Cell>): Inserts rowdeleteDrawdownRow(d: Drawdown, i: number): Deletes rowinsertDrawdownCol(d: Drawdown, j: number, col: Array<Cell>): Inserts columndeleteDrawdownCol(d: Drawdown, j: number): Deletes columninsertMappingRow(m: Array<number>, i: number, val: number): Inserts into mapping arraydeleteMappingRow(m: Array<number>, i: number): Deletes from mapping arrayinsertMappingCol(m: Array<number>, j: number, val: number): Inserts into column mappingdeleteMappingCol(m: Array<number>, j: number): Deletes from column mapping
Draft Transformations:
flipDraft(d: Draft, horiz: boolean, vert: boolean): Flips draft horizontally/verticallygenerateMappingFromPattern(drawdown, pattern, type): Generates mapping from patternupdateWeftSystemsAndShuttles(to: Draft, from: Draft): Updates weft mappingsupdateWarpSystemsAndShuttles(to: Draft, from: Draft): Updates warp mappings
Compression/Export:
compressDraft(draft: Draft): Compresses draft to save spaceexportDrawdownToArray(drawdown: Drawdown): Exports to number arrayunpackDrawdownFromArray(compressed, warps, wefts): Unpacks from arrayexportDrawdownToBitArray(drawdown: Drawdown): Exports to byte arrayunpackDrawdownFromBitArray(arr, warps, wefts): Unpacks from byte array
Rendering:
getDraftAsImage(draft, pix_per_cell, floats, use_color, mats): Generates ImageData for renderingdrawDraftViewCell(...): Draws a single cell to image data
Utilities:
getDraftName(draft: Draft): Returns display name (user-defined or generated)
Functions for managing systems (structural units):
createSystem(systemDict: InitSystemParams): Creates a new systemsetSystemId(sys: System, id: number): Sets system ID and generates default namegetSystemChar(sys: System): Returns character representation (a-z)getSystemCharFromId(id: number): Returns character for system IDmakeSystemsUnique(systems: Array<Array<number>>): Makes system mappings unique across drafts
Exports all public functions and types from the draft directory.
import { initDraftWithParams, createCell } from './draft';
// Create a 10x10 draft with all cells set to heddle down
const draft = initDraftWithParams({
wefts: 10,
warps: 10,
drawdown: [[createCell(false)]], // Pattern repeats
ud_name: 'My Draft'
});import { setHeddle, isUp, getCellValue } from './draft';
import { toggleHeddle } from './cell';
// Set a specific cell to heddle up
const newDrawdown = setHeddle(draft.drawdown, 0, 0, true);
// Check cell state
const isHeddleUp = isUp(draft.drawdown, 0, 0);
// Toggle a cell
const cell = draft.drawdown[0][0];
const toggled = toggleHeddle(cell);import { createSystem, getSystemChar } from './system';
const system = createSystem({
id: 0,
name: 'Layer 1',
visible: true
});
const char = getSystemChar(system); // Returns 'a'Add functions to cell.ts:
/**
* Checks if a cell represents a float (heddle up)
*/
export const isFloat = (c: Cell): boolean => {
return c.is_set && c.is_up;
};
/**
* Checks if a cell represents a binding (heddle down)
*/
export const isBinding = (c: Cell): boolean => {
return c.is_set && !c.is_up;
};Add functions to draft.ts:
/**
* Rotates a drawdown 90 degrees clockwise
*/
export const rotateDrawdown = (drawdown: Drawdown): Drawdown => {
const rotated: Drawdown = [];
const weftCount = wefts(drawdown);
const warpCount = warps(drawdown);
for (let j = 0; j < warpCount; j++) {
rotated.push([]);
for (let i = weftCount - 1; i >= 0; i--) {
rotated[j].push(drawdown[i][j]);
}
}
return rotated;
};/**
* Counts the number of floats in a draft
*/
export const countFloats = (draft: Draft): number => {
let count = 0;
draft.drawdown.forEach(row => {
row.forEach(cell => {
if (cell.is_set && cell.is_up) count++;
});
});
return count;
};-
Immutability: Most functions return new objects rather than mutating inputs. Follow this pattern when extending.
-
Bounds Checking: Always check array bounds before accessing cells:
if (i >= 0 && i < wefts(drawdown) && j >= 0 && j < warps(drawdown)) { // Safe to access }
-
Cell State Handling: Remember the three valid cell states and handle null/unset appropriately.
-
Size Limits: Check
defaults.max_areabefore creating large drafts to prevent rendering issues. -
Mapping Arrays: When manipulating drafts, remember to update corresponding mapping arrays (rowShuttleMapping, etc.) to maintain consistency.
- Loom: Drafts can be converted to/from loom configurations (threading, tieup, treadling)
- Materials: Drafts reference materials through shuttle mappings
- Operations: Drafts are the primary input/output for all operations
- Simulation: Drafts are used to generate 3D simulations of woven cloth