Skip to content

Commit c7971eb

Browse files
dakerfloryst
authored andcommitted
feat(vtkGCodeReader): add vtkGCodeReader
Fixes #3149
1 parent 4691eea commit c7971eb

File tree

7 files changed

+516
-0
lines changed

7 files changed

+516
-0
lines changed
6.07 KB
Loading

Documentation/content/examples/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ This will allow you to see the some live code running in your browser. Just pick
184184
[![HttpDataSetSeriesReader Example][HttpDataSetSeriesReaderWithIcon]](./HttpDataSetSeriesReader.html "Import a VTK dataset with time support.")
185185
[![HttpSceneLoader Example][HttpSceneLoaderWithIcon]](./HttpSceneLoader.html "Import a VTK scene (data + representation)")
186186
[![OfflineLocalView Example][OfflineLocalViewWithIcon]](./OfflineLocalView.html "Load a serialized scene (VTKSZ)")
187+
[![G-Code Example][GCodeReaderWithIcon]](./GCodeReader.html "G-Code reader(gcode)")
187188

188189
</div>
189190

@@ -203,6 +204,7 @@ This will allow you to see the some live code running in your browser. Just pick
203204
[HttpDataSetSeriesReaderWithIcon]: ../docs/gallery/HttpDataSetSeriesReaderWithIcon.gif
204205
[HttpSceneLoaderWithIcon]: ../docs/gallery/HttpSceneLoaderWithIcon.jpg
205206
[OfflineLocalViewWithIcon]: ../docs/gallery/OfflineLocalViewWithIcon.jpg
207+
[GCodeReaderWithIcon]: ../docs/gallery/GCodeReaderWithIcon.jpg
206208

207209
# Actors
208210

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<table>
2+
<tr>
3+
<td colspan="2">
4+
<h3 style="margin: 5px 0;">Options</h3>
5+
</td>
6+
</tr>
7+
<tr>
8+
<td class="label">Layers: </td>
9+
<td>
10+
<input type="range" class="layers" id="layers" min="0" max="0" value="0" step="1" style="width: 100%;" />
11+
</td>
12+
</tr>
13+
</table>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import '@kitware/vtk.js/favicon';
2+
3+
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
4+
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
5+
6+
import vtkMath from '@kitware/vtk.js/Common/Core/Math';
7+
8+
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
9+
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
10+
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
11+
import vtkGCodeReader from '@kitware/vtk.js/IO/Misc/GCodeReader';
12+
13+
import controlPanel from './controller.html';
14+
15+
// ----------------------------------------------------------------------------
16+
// Example code
17+
// ----------------------------------------------------------------------------
18+
19+
let renderer = null;
20+
let renderWindow = null;
21+
let layersSelector = null;
22+
let layersLabel = null;
23+
24+
const reader = vtkGCodeReader.newInstance();
25+
26+
// ----------------------------------------------------------------------------
27+
28+
function refresh() {
29+
if (renderer && renderWindow) {
30+
renderer.resetCamera();
31+
renderWindow.render();
32+
}
33+
}
34+
35+
function update() {
36+
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
37+
fullScreenRenderer.addController(controlPanel);
38+
layersSelector = document.querySelector('.layers');
39+
layersLabel = document.querySelector('.label');
40+
renderer = fullScreenRenderer.getRenderer();
41+
renderWindow = fullScreenRenderer.getRenderWindow();
42+
const numLayers = reader.getNumberOfOutputPorts();
43+
44+
layersLabel.innerHTML = `Layers(${numLayers}):`;
45+
layersSelector.max = numLayers.toString();
46+
layersSelector.value = numLayers.toString();
47+
48+
const actors = [];
49+
50+
for (let idx = 1; idx < numLayers; idx++) {
51+
const polyData = reader.getOutputData(idx);
52+
if (polyData) {
53+
const mapper = vtkMapper.newInstance();
54+
mapper.setInputData(polyData);
55+
56+
const actor = vtkActor.newInstance();
57+
actor.setMapper(mapper);
58+
59+
const hue = idx / numLayers;
60+
const saturation = 0.8;
61+
const value = 1;
62+
const hsv = [hue, saturation, value];
63+
const rgb = [];
64+
vtkMath.hsv2rgb(hsv, rgb);
65+
actor.getProperty().setColor(rgb);
66+
actor.rotateX(-90);
67+
68+
actors.push(actor);
69+
}
70+
}
71+
72+
layersSelector.addEventListener('input', (event) => {
73+
const visibleLayers = parseInt(event.target.value, 10);
74+
actors.forEach((actor, index) => {
75+
actor.setVisibility(index < visibleLayers);
76+
});
77+
renderWindow.render();
78+
});
79+
80+
actors.forEach((actor) => renderer.addActor(actor));
81+
refresh();
82+
}
83+
84+
// ----------------------------------------------------------------------------
85+
// Use a file reader to load a local file
86+
// ----------------------------------------------------------------------------
87+
88+
const myContainer = document.querySelector('body');
89+
const fileContainer = document.createElement('div');
90+
fileContainer.innerHTML =
91+
'<div>Select a gcode file.<br/><input type="file" class="file"/></div>';
92+
myContainer.appendChild(fileContainer);
93+
94+
const fileInput = fileContainer.querySelector('input');
95+
96+
function handleFile(event) {
97+
event.preventDefault();
98+
const dataTransfer = event.dataTransfer;
99+
const files = event.target.files || dataTransfer.files;
100+
myContainer.removeChild(fileContainer);
101+
const fileReader = new FileReader();
102+
fileReader.onload = function onLoad(e) {
103+
reader.parse(fileReader.result);
104+
update();
105+
};
106+
fileReader.readAsArrayBuffer(files[0]);
107+
}
108+
109+
fileInput.addEventListener('change', handleFile);
110+
111+
// ----------------------------------------------------------------------------
112+
// Use the reader to download a file
113+
// ----------------------------------------------------------------------------
114+
// reader.setUrl(`${__BASE_PATH__}/data/gcode/cube.gcode`, { binary: true }).then(update);
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
2+
import HtmlDataAccessHelper from '../../Core/DataAccessHelper/HtmlDataAccessHelper';
3+
import HttpDataAccessHelper from '../../Core/DataAccessHelper/HttpDataAccessHelper';
4+
import JSZipDataAccessHelper from '../../Core/DataAccessHelper/JSZipDataAccessHelper';
5+
import LiteHttpDataAccessHelper from '../../Core/DataAccessHelper/LiteHttpDataAccessHelper';
6+
7+
interface IGCodeReaderOptions {
8+
binary?: boolean;
9+
compression?: string;
10+
progressCallback?: any;
11+
}
12+
13+
/**
14+
*
15+
*/
16+
export interface IGCodeReaderInitialValues {}
17+
18+
type vtkGCodeReaderBase = vtkObject &
19+
Omit<
20+
vtkAlgorithm,
21+
| 'getInputData'
22+
| 'setInputData'
23+
| 'setInputConnection'
24+
| 'getInputConnection'
25+
| 'addInputConnection'
26+
| 'addInputData'
27+
>;
28+
29+
export interface vtkGCodeReader extends vtkGCodeReaderBase {
30+
/**
31+
*
32+
*/
33+
getBaseURL(): string;
34+
35+
/**
36+
*
37+
*/
38+
getDataAccessHelper():
39+
| HtmlDataAccessHelper
40+
| HttpDataAccessHelper
41+
| JSZipDataAccessHelper
42+
| LiteHttpDataAccessHelper;
43+
44+
/**
45+
* Get the url of the object to load.
46+
*/
47+
getUrl(): string;
48+
49+
/**
50+
* Load the object data.
51+
* @param {IGCodeReaderOptions} [options]
52+
*/
53+
loadData(options?: IGCodeReaderOptions): Promise<any>;
54+
55+
/**
56+
* Parse data.
57+
* @param {String | ArrayBuffer} content The content to parse.
58+
*/
59+
parse(content: string | ArrayBuffer): void;
60+
61+
/**
62+
* Parse data as ArrayBuffer.
63+
* @param {ArrayBuffer} content The content to parse.
64+
*/
65+
parseAsArrayBuffer(content: ArrayBuffer): void;
66+
67+
/**
68+
* Parse data as text.
69+
* @param {String} content The content to parse.
70+
*/
71+
parseAsText(content: string): void;
72+
73+
/**
74+
*
75+
* @param inData
76+
* @param outData
77+
*/
78+
requestData(inData: any, outData: any): void;
79+
80+
/**
81+
*
82+
* @param dataAccessHelper
83+
*/
84+
setDataAccessHelper(
85+
dataAccessHelper:
86+
| HtmlDataAccessHelper
87+
| HttpDataAccessHelper
88+
| JSZipDataAccessHelper
89+
| LiteHttpDataAccessHelper
90+
): boolean;
91+
92+
/**
93+
* Set the url of the object to load.
94+
* @param {String} url the url of the object to load.
95+
* @param {IGCodeReaderOptions} [option] The PLY reader options.
96+
*/
97+
setUrl(url: string, option?: IGCodeReaderOptions): Promise<string | any>;
98+
}
99+
100+
/**
101+
* Method used to decorate a given object (publicAPI+model) with vtkGCodeReader characteristics.
102+
*
103+
* @param publicAPI object on which methods will be bounds (public)
104+
* @param model object on which data structure will be bounds (protected)
105+
* @param {IGCodeReaderInitialValues} [initialValues] (default: {})
106+
*/
107+
export function extend(
108+
publicAPI: object,
109+
model: object,
110+
initialValues?: IGCodeReaderInitialValues
111+
): void;
112+
113+
/**
114+
* Method used to create a new instance of vtkGCodeReader
115+
* @param {IGCodeReaderInitialValues} [initialValues] for pre-setting some of its content
116+
*/
117+
export function newInstance(
118+
initialValues?: IGCodeReaderInitialValues
119+
): vtkGCodeReader;
120+
121+
/**
122+
* vtkGCodeReader is a source object that reads a GCODE file.
123+
*/
124+
export declare const vtkGCodeReader: {
125+
newInstance: typeof newInstance;
126+
extend: typeof extend;
127+
};
128+
export default vtkGCodeReader;

0 commit comments

Comments
 (0)