@sienci/gviewer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +480 -0
  2. package/dist/GCodeSVGRenderer-DaLc9_L-.cjs +1 -0
  3. package/dist/GCodeSVGRenderer-DnFLLJmH.js +1487 -0
  4. package/dist/gviewer.cjs +1 -0
  5. package/dist/gviewer.js +654 -0
  6. package/dist/react.cjs +1 -0
  7. package/dist/react.js +141 -0
  8. package/dist/types/geometry.d.ts +92 -0
  9. package/dist/types/index.d.ts +4 -0
  10. package/dist/types/parser.d.ts +6 -0
  11. package/dist/types/react/GCodeSVGVisualizer.d.ts +20 -0
  12. package/dist/types/react/GCodeVisualizer.d.ts +10 -0
  13. package/dist/types/react/index.d.ts +4 -0
  14. package/dist/types/types.d.ts +71 -0
  15. package/dist/types/viewer/GCodeViewer.d.ts +89 -0
  16. package/dist/types/viewer/ViewCube.d.ts +16 -0
  17. package/dist/types/viewer/bbox/boundingBox.d.ts +4 -0
  18. package/dist/types/viewer/bit/bit.d.ts +13 -0
  19. package/dist/types/viewer/bit/drill-stl-data.d.ts +1 -0
  20. package/dist/types/viewer/camera/camera.d.ts +21 -0
  21. package/dist/types/viewer/grid/grid.d.ts +21 -0
  22. package/dist/types/viewer/index.d.ts +7 -0
  23. package/dist/types/viewer/render/textSprite.d.ts +12 -0
  24. package/dist/types/viewer/simulation/heightmap.d.ts +36 -0
  25. package/dist/types/viewer/simulation/materialSlab.d.ts +12 -0
  26. package/dist/types/viewer/svg/GCodeSVGRenderer.d.ts +53 -0
  27. package/dist/types/viewer/svg/types.d.ts +10 -0
  28. package/dist/types/viewer/themes.d.ts +3 -0
  29. package/dist/types/viewer/toolpath/streams.d.ts +51 -0
  30. package/dist/types/viewer/types.d.ts +171 -0
  31. package/dist/types/virtualizer.d.ts +26 -0
  32. package/dist/viewcube.css +54 -0
  33. package/dist/viewer.cjs +1 -0
  34. package/dist/viewer.js +109 -0
  35. package/package.json +81 -0
package/dist/react.js ADDED
@@ -0,0 +1,141 @@
1
+ import * as o from "react";
2
+ import { a as w, G as V } from "./GCodeSVGRenderer-DnFLLJmH.js";
3
+ const C = o.forwardRef(
4
+ function(u, c) {
5
+ const { id: l, options: n, callbacks: a, className: f, style: d } = u, i = o.useRef(null), t = o.useRef(null);
6
+ return o.useEffect(() => {
7
+ const e = i.current;
8
+ if (!e)
9
+ return;
10
+ const m = new w({ id: l, container: e, options: n, callbacks: a });
11
+ return t.current = m, () => {
12
+ t.current = null, m.dispose();
13
+ };
14
+ }, [l]), o.useEffect(() => {
15
+ var e;
16
+ (e = t.current) == null || e.setOptions(n ?? {});
17
+ }, [n]), o.useEffect(() => {
18
+ var e;
19
+ (e = t.current) == null || e.setCallbacks(a ?? {});
20
+ }, [a]), o.useImperativeHandle(
21
+ c,
22
+ () => {
23
+ const e = () => {
24
+ const r = t.current;
25
+ if (!r)
26
+ throw new Error("GCodeViewer is not ready.");
27
+ return r;
28
+ };
29
+ return {
30
+ get id() {
31
+ return l;
32
+ },
33
+ setCallbacks(r) {
34
+ e().setCallbacks(r);
35
+ },
36
+ snapCameraToView(r, s) {
37
+ e().snapCameraToView(r, s);
38
+ },
39
+ setBitPosition(r, s) {
40
+ e().setBitPosition(r, s);
41
+ },
42
+ setBitVisible(r) {
43
+ e().setBitVisible(r);
44
+ },
45
+ setToolpathRotationA(r) {
46
+ e().setToolpathRotationA(r);
47
+ },
48
+ hideUntilLine(r, s) {
49
+ e().hideUntilLine(r, s);
50
+ },
51
+ seekToLine(r, s) {
52
+ e().seekToLine(r, s);
53
+ },
54
+ showAll() {
55
+ e().showAll();
56
+ },
57
+ resetColors() {
58
+ e().resetColors();
59
+ },
60
+ loadFromUrl(r, s) {
61
+ return e().loadFromUrl(r, s);
62
+ },
63
+ loadFromFile(r) {
64
+ return e().loadFromFile(r);
65
+ },
66
+ loadFromText(r) {
67
+ return e().loadFromText(r);
68
+ },
69
+ loadFromLines(r) {
70
+ return e().loadFromLines(r);
71
+ },
72
+ unload() {
73
+ e().unload();
74
+ },
75
+ setOptions(r) {
76
+ e().setOptions(r);
77
+ },
78
+ getOptions() {
79
+ return e().getOptions();
80
+ },
81
+ resize() {
82
+ e().resize();
83
+ },
84
+ focusToModel() {
85
+ e().focusToModel();
86
+ },
87
+ resetCamera() {
88
+ e().resetCamera();
89
+ },
90
+ getBounds() {
91
+ return e().getBounds();
92
+ },
93
+ dispose() {
94
+ e().dispose();
95
+ }
96
+ };
97
+ },
98
+ [l]
99
+ ), o.createElement("div", { ref: i, className: f, style: d });
100
+ }
101
+ ), G = o.forwardRef(
102
+ function(u, c) {
103
+ const { id: l, options: n, className: a, style: f } = u, d = o.useRef(null), i = o.useRef(null);
104
+ return o.useEffect(() => {
105
+ const t = d.current;
106
+ if (!t) return;
107
+ const e = new V(t, n);
108
+ return i.current = e, () => {
109
+ i.current = null, e.dispose();
110
+ };
111
+ }, []), o.useEffect(() => {
112
+ var t;
113
+ n && ((t = i.current) == null || t.setOptions(n));
114
+ }, [n]), o.useImperativeHandle(c, () => {
115
+ const t = () => {
116
+ const e = i.current;
117
+ if (!e) throw new Error("GCodeSVGRenderer is not ready.");
118
+ return e;
119
+ };
120
+ return {
121
+ loadFromLines: (e) => t().loadFromLines(e),
122
+ loadFromFile: (e) => t().loadFromFile(e),
123
+ loadFromText: (e) => t().loadFromText(e),
124
+ clear: () => t().clear(),
125
+ resetView: () => t().resetView(),
126
+ setOptions: (e) => t().setOptions(e),
127
+ setProjectionMode: (e) => t().setProjectionMode(e),
128
+ getSVGElement: () => t().getSVGElement(),
129
+ dispose: () => t().dispose()
130
+ };
131
+ }, []), o.createElement("div", {
132
+ ref: d,
133
+ className: a,
134
+ style: { width: "100%", height: "100%", ...f }
135
+ });
136
+ }
137
+ );
138
+ export {
139
+ G as GCodeSVGVisualizer,
140
+ C as GCodeVisualizer
141
+ };
@@ -0,0 +1,92 @@
1
+ import { ArcMoveCallback, LinearMoveCallback, Position } from "./types";
2
+ export type VertexCollector = {
3
+ onLinearMove?: (args: Parameters<LinearMoveCallback>[0], vertices: number[]) => void;
4
+ onArcMove?: (args: Parameters<ArcMoveCallback>[0], vertices: number[]) => void;
5
+ };
6
+ export type VertexBuildOptions = {
7
+ arcSegments?: number;
8
+ collector?: VertexCollector;
9
+ };
10
+ export type BatchBuildOptions = {
11
+ onProgress?: (processed: number, total: number) => void;
12
+ everyLines?: number;
13
+ yieldEveryLines?: number;
14
+ shouldAbort?: () => boolean;
15
+ };
16
+ export type MovementVertices = {
17
+ rapid: Float32Array;
18
+ cutting: Float32Array;
19
+ };
20
+ export type LaserBucket = {
21
+ opacity: number;
22
+ vertices: Float32Array;
23
+ };
24
+ export type LaserVertices = {
25
+ rapid: Float32Array;
26
+ buckets: LaserBucket[];
27
+ minPower: number;
28
+ maxPower: number;
29
+ };
30
+ export declare function buildVerticesFromLines(lines: Iterable<string>, options?: VertexBuildOptions): Float32Array;
31
+ export declare function buildMovementVerticesFromLines(lines: Iterable<string>, options?: Omit<VertexBuildOptions, "collector">): MovementVertices;
32
+ export declare function buildMovementVerticesFromLinesBatched(lines: readonly string[], options?: Omit<VertexBuildOptions, "collector"> & {
33
+ batch?: BatchBuildOptions;
34
+ }): Promise<MovementVertices>;
35
+ export type MovementGeometryLineKind = 0 | 1 | 2 | 3;
36
+ export type MovementGeometry = {
37
+ positions: Float32Array;
38
+ lineStartVertex: Int32Array;
39
+ lineEndVertex: Int32Array;
40
+ lineKind: Uint8Array;
41
+ prefixEndVertex: Int32Array;
42
+ };
43
+ export declare function buildMovementGeometryFromLinesBatched(lines: readonly string[], options?: Omit<VertexBuildOptions, "collector"> & {
44
+ batch?: BatchBuildOptions;
45
+ }): Promise<MovementGeometry>;
46
+ export declare function buildLaserVerticesFromLines(lines: Iterable<string>, options?: {
47
+ arcSegments?: number;
48
+ bucketCount?: number;
49
+ baseOpacity?: number;
50
+ }): LaserVertices;
51
+ export declare function buildLaserVerticesFromLinesBatched(lines: readonly string[], options?: {
52
+ arcSegments?: number;
53
+ bucketCount?: number;
54
+ baseOpacity?: number;
55
+ batch?: BatchBuildOptions;
56
+ }): Promise<LaserVertices>;
57
+ export type LaserGeometryBucket = {
58
+ opacity: number;
59
+ positions: Float32Array;
60
+ prefixEndVertex: Int32Array;
61
+ };
62
+ export type LaserGeometry = {
63
+ rapidPositions: Float32Array;
64
+ rapidPrefixEndVertex: Int32Array;
65
+ buckets: LaserGeometryBucket[];
66
+ minPower: number;
67
+ maxPower: number;
68
+ };
69
+ export type ToolpathGeometryStream = {
70
+ positions: Float32Array;
71
+ prefixEndVertex: Int32Array;
72
+ };
73
+ export type ToolpathGeometry = {
74
+ rapid: ToolpathGeometryStream;
75
+ cuts: ToolpathGeometryStream[];
76
+ cutBucketCount: number;
77
+ minPower: number;
78
+ maxPower: number;
79
+ };
80
+ export declare function buildToolpathGeometryFromLinesBatched(lines: readonly string[], options?: {
81
+ arcSegments?: number;
82
+ bucketCount?: number;
83
+ laserMode?: boolean;
84
+ batch?: BatchBuildOptions;
85
+ }): Promise<ToolpathGeometry>;
86
+ export declare function buildLaserGeometryFromLinesBatched(lines: readonly string[], options?: {
87
+ arcSegments?: number;
88
+ bucketCount?: number;
89
+ baseOpacity?: number;
90
+ batch?: BatchBuildOptions;
91
+ }): Promise<LaserGeometry>;
92
+ export declare function pushXYZ(vertices: number[], position: Position): void;
@@ -0,0 +1,4 @@
1
+ export * from "./parser";
2
+ export * from "./virtualizer";
3
+ export * from "./geometry";
4
+ export * from "./types";
@@ -0,0 +1,6 @@
1
+ import { ParsedLine } from "./types";
2
+ export declare class GCodeParser {
3
+ parseLine(line: string): ParsedLine;
4
+ private stripComments;
5
+ private parseWords;
6
+ }
@@ -0,0 +1,20 @@
1
+ import * as React from "react";
2
+ import type { GCodeSVGOptions } from "../viewer/svg/types";
3
+ export type GCodeSVGRendererHandle = {
4
+ loadFromLines(lines: string[]): void;
5
+ loadFromFile(file: File): Promise<void>;
6
+ loadFromText(gcode: string): void;
7
+ clear(): void;
8
+ resetView(): void;
9
+ setOptions(opts: Partial<GCodeSVGOptions>): void;
10
+ setProjectionMode(mode: 'perspective' | 'isometric'): void;
11
+ getSVGElement(): SVGSVGElement;
12
+ dispose(): void;
13
+ };
14
+ export type GCodeSVGVisualizerProps = {
15
+ id: string;
16
+ options?: Partial<GCodeSVGOptions>;
17
+ className?: string;
18
+ style?: React.CSSProperties;
19
+ };
20
+ export declare const GCodeSVGVisualizer: React.ForwardRefExoticComponent<GCodeSVGVisualizerProps & React.RefAttributes<GCodeSVGRendererHandle>>;
@@ -0,0 +1,10 @@
1
+ import * as React from "react";
2
+ import type { GCodeViewerCallbacks, GCodeViewerHandle, GCodeViewerOptions } from "../viewer/types";
3
+ export type GCodeVisualizerProps = {
4
+ id: string;
5
+ options?: Partial<GCodeViewerOptions>;
6
+ callbacks?: GCodeViewerCallbacks;
7
+ className?: string;
8
+ style?: React.CSSProperties;
9
+ };
10
+ export declare const GCodeVisualizer: React.ForwardRefExoticComponent<GCodeVisualizerProps & React.RefAttributes<GCodeViewerHandle>>;
@@ -0,0 +1,4 @@
1
+ export { GCodeVisualizer } from "./GCodeVisualizer";
2
+ export type { GCodeVisualizerProps } from "./GCodeVisualizer";
3
+ export { GCodeSVGVisualizer } from "./GCodeSVGVisualizer";
4
+ export type { GCodeSVGVisualizerProps, GCodeSVGRendererHandle } from "./GCodeSVGVisualizer";
@@ -0,0 +1,71 @@
1
+ export type Axis = "X" | "Y" | "Z" | "A" | "B" | "C";
2
+ export type Position = Record<Axis, number>;
3
+ export type Comment = {
4
+ type: "paren" | "semicolon";
5
+ text: string;
6
+ start: number;
7
+ end: number;
8
+ };
9
+ export type GCodeWord = {
10
+ letter: string;
11
+ value: number;
12
+ raw: string;
13
+ start: number;
14
+ end: number;
15
+ };
16
+ export type ParsedLine = {
17
+ raw: string;
18
+ words: GCodeWord[];
19
+ gcodes: GCodeWord[];
20
+ params: GCodeWord[];
21
+ comments: Comment[];
22
+ };
23
+ export type MotionMode = "G0" | "G1" | "G2" | "G3";
24
+ export type DistanceMode = "G90" | "G91";
25
+ export type PlaneMode = "G17" | "G18" | "G19";
26
+ export type UnitsMode = "G20" | "G21";
27
+ export type ModalState = {
28
+ motion: MotionMode;
29
+ distance: DistanceMode;
30
+ plane: PlaneMode;
31
+ units: UnitsMode;
32
+ feedMode: "G93" | "G94";
33
+ feedRate: number | null;
34
+ spindleSpeed: number | null;
35
+ tool: number | null;
36
+ coolant: "M7" | "M8" | "M9" | null;
37
+ spindle: "M3" | "M4" | "M5" | null;
38
+ coordinateSystem: "G54" | "G55" | "G56" | "G57" | "G58" | "G59";
39
+ };
40
+ export type LinearMoveCallback = (args: {
41
+ modals: ModalState;
42
+ start: Position;
43
+ end: Position;
44
+ transformedStart: Position;
45
+ transformedEnd: Position;
46
+ }) => void;
47
+ export type ArcMoveCallback = (args: {
48
+ modals: ModalState;
49
+ start: Position;
50
+ end: Position;
51
+ max: Position;
52
+ center: Position;
53
+ plane: PlaneMode;
54
+ motion: MotionMode;
55
+ transformedStart: Position;
56
+ transformedEnd: Position;
57
+ transformedMax: Position;
58
+ transformedCenter: Position;
59
+ }) => void;
60
+ export type MovementCallbacks = {
61
+ onLinearMove?: LinearMoveCallback;
62
+ onArcMove?: ArcMoveCallback;
63
+ };
64
+ export type VirtualizeResult = {
65
+ parsed: ParsedLine;
66
+ modals: ModalState;
67
+ start: Position;
68
+ end: Position;
69
+ movement: "none" | "linear" | "arc";
70
+ arcMax?: Position;
71
+ };
@@ -0,0 +1,89 @@
1
+ import { GCodeViewerCameraView, GCodeViewerBounds, GCodeViewerCallbacks, GCodeViewerCreateArgs, GCodeViewerHandle, GCodeViewerBitPosition, GCodeViewerOptions } from "./types";
2
+ export declare class GCodeViewer implements GCodeViewerHandle {
3
+ readonly id: string;
4
+ private readonly container;
5
+ private readonly canvas;
6
+ private readonly renderer;
7
+ private readonly scene;
8
+ private readonly toolpathRoot;
9
+ private readonly camera;
10
+ private readonly controls;
11
+ private viewCube;
12
+ private readonly viewCubeCorrection;
13
+ private callbacks;
14
+ private options;
15
+ private resizeObserver;
16
+ private onWindowResize;
17
+ private animationFrameId;
18
+ private gridGroup;
19
+ private axesGroup;
20
+ private gridLabelsGroup;
21
+ private boundingBoxGroup;
22
+ private bitMarker;
23
+ private preLaserBitType;
24
+ private toolpathStreams;
25
+ private toolpathCutBucketCount;
26
+ private toolpathRotationA;
27
+ private sim3dHandle;
28
+ private currentLines;
29
+ private linePositions;
30
+ private renderSequence;
31
+ private currentBounds;
32
+ private cameraFocusTransition;
33
+ constructor(args: GCodeViewerCreateArgs);
34
+ setBitPosition(position: GCodeViewerBitPosition, options?: {
35
+ immediate?: boolean;
36
+ }): void;
37
+ setBitVisible(visible: boolean): void;
38
+ setToolpathRotationA(aDegrees: number): void;
39
+ setCallbacks(callbacks: GCodeViewerCallbacks): void;
40
+ hideUntilLine(lineIndex: number, mode?: "hide" | "grey"): void;
41
+ seekToLine(lineIndex: number, mode?: "hide" | "grey"): void;
42
+ showAll(): void;
43
+ resetColors(): void;
44
+ snapCameraToView(view: GCodeViewerCameraView, options?: {
45
+ durationMs?: number;
46
+ distance?: number;
47
+ }): void;
48
+ private startSnapToView;
49
+ loadFromUrl(url: string, args?: {
50
+ signal?: AbortSignal;
51
+ }): Promise<void>;
52
+ loadFromFile(file: File): Promise<void>;
53
+ loadFromText(gcode: string): Promise<void>;
54
+ loadFromLines(lines: readonly string[]): Promise<void>;
55
+ unload(): void;
56
+ setOptions(next: Partial<GCodeViewerOptions>): void;
57
+ getOptions(): Readonly<GCodeViewerOptions>;
58
+ resize(): void;
59
+ focusToModel(): void;
60
+ resetCamera(): void;
61
+ getBounds(): GCodeViewerBounds | null;
62
+ dispose(): void;
63
+ private emitProgress;
64
+ private emitBoundsChanged;
65
+ private startAnimationLoop;
66
+ private ensureBitMarker;
67
+ private worldSizes;
68
+ private renderGridAndAxes;
69
+ private refreshGridLabels;
70
+ private refreshBoundingBox;
71
+ private setBitMarker;
72
+ private setGridGroup;
73
+ private setAxesGroup;
74
+ private setGridLabelsGroup;
75
+ private setBoundingBoxGroup;
76
+ private setGeometryEmpty;
77
+ private setToolpathGeometry;
78
+ private refreshToolpathColors;
79
+ private refreshToolpathOpacities;
80
+ private cutBucketOpacity;
81
+ private buildLinePositions;
82
+ private renderScene;
83
+ private setSim3dHandle;
84
+ private setToolpathStreamsVisible;
85
+ private buildAndApplySim3d;
86
+ private startCameraFocus;
87
+ private updateCameraFocusTransition;
88
+ private updateViewCubeRotation;
89
+ }
@@ -0,0 +1,16 @@
1
+ import type { GCodeViewerCameraView } from "./types";
2
+ type ViewCubeArgs = {
3
+ container: HTMLElement;
4
+ onSelectView: (view: GCodeViewerCameraView) => void;
5
+ };
6
+ export declare class ViewCube {
7
+ private readonly root;
8
+ private readonly cube;
9
+ private readonly onSelectView;
10
+ private readonly faceButtons;
11
+ constructor(args: ViewCubeArgs);
12
+ setActiveFace(view: "front" | "back" | "left" | "right" | "top" | "bottom"): void;
13
+ setRotationMatrix3d(elements: readonly number[]): void;
14
+ dispose(): void;
15
+ }
16
+ export {};
@@ -0,0 +1,4 @@
1
+ import * as THREE from "three";
2
+ import type { GCodeViewerOptions } from "../types";
3
+ export declare function createBoundingBoxGroup(bounds: THREE.Box3, options: GCodeViewerOptions): THREE.Group;
4
+ export declare function disposeBoundingBoxGroup(group: THREE.Object3D): void;
@@ -0,0 +1,13 @@
1
+ import * as THREE from "three";
2
+ import type { GCodeViewerBitPosition, GCodeViewerOptions } from "../types";
3
+ export type BitMarker = {
4
+ object: THREE.Object3D;
5
+ update(nowMs: number): void;
6
+ dispose(): void;
7
+ setVisible(visible: boolean): void;
8
+ setOptions(options: GCodeViewerOptions): void;
9
+ setTarget(position: GCodeViewerBitPosition, options?: {
10
+ immediate?: boolean;
11
+ }): void;
12
+ };
13
+ export declare function createBitMarker(initialOptions: GCodeViewerOptions): BitMarker;