fix tool import

This commit is contained in:
Ross
2025-06-02 09:59:51 +01:00
parent 1fd3020f0c
commit b408ba96b3
+44 -6
View File
@@ -1073,7 +1073,7 @@ function App() {
} }
useEffect(() => { useEffect(() => {
// Export the current viewer state as JSON // Export the current viewer state as JSON
window.exportViewerState = () => { window.exportViewerState = () => {
const numActive = viewportGrid.rows * viewportGrid.cols; const numActive = viewportGrid.rows * viewportGrid.cols;
const modes = viewportModes.slice(0, numActive); const modes = viewportModes.slice(0, numActive);
const stackIdx = selectedStackIdx.slice(0, numActive); const stackIdx = selectedStackIdx.slice(0, numActive);
@@ -1121,10 +1121,10 @@ window.exportViewerState = () => {
cam, cam,
}; };
return JSON.stringify(state); return JSON.stringify(state);
}; };
// Import and restore a viewer state from JSON (using stack indices) // Import and restore a viewer state from JSON (using stack indices)
window.importViewerState = (json: string) => { window.importViewerState = (json: string) => {
try { try {
restoringStateRef.current = true; restoringStateRef.current = true;
const state = typeof json === "string" ? JSON.parse(json) : json; const state = typeof json === "string" ? JSON.parse(json) : json;
@@ -1193,12 +1193,32 @@ window.importViewerState = (json: string) => {
restoringStateRef.current = false; restoringStateRef.current = false;
alert("Failed to import viewer state: " + e); alert("Failed to import viewer state: " + e);
} }
}; };
// Export all annotations using cornerstoneTools.annotation.state.getAllAnnotations() // Export all annotations using cornerstoneTools.annotation.state.getAllAnnotations()
window.exportAnnotations = () => { window.exportAnnotations = () => {
try { try {
const allAnnotations = cornerstoneTools.annotation.state.getAllAnnotations(); let allAnnotations = cornerstoneTools.annotation.state.getAllAnnotations();
console.log("Exporting annotations:", allAnnotations);
// Remove ReferenceLines annotations (toolName may be 'ReferenceLines')
if (Array.isArray(allAnnotations)) {
// Only keep annotations with toolName in the allowed list
const allowedTools = [
LengthTool.toolName,
ArrowAnnotateTool.toolName,
RectangleROITool.toolName,
EllipticalROITool.toolName,
PlanarFreehandROITool.toolName,
PlanarFreehandContourSegmentationTool.toolName,
ProbeTool.toolName,
SculptorTool.toolName,
];
allAnnotations = allAnnotations.filter(
ann => allowedTools.includes(ann.metadata.toolName)
);
} else {
return "{}"; // If no annotations found, return empty object
}
return JSON.stringify(allAnnotations, null, 2); return JSON.stringify(allAnnotations, null, 2);
} catch (e) { } catch (e) {
alert("Failed to export annotations: " + e); alert("Failed to export annotations: " + e);
@@ -1210,7 +1230,25 @@ window.importViewerState = (json: string) => {
window.importAnnotations = (json: string) => { window.importAnnotations = (json: string) => {
try { try {
const annotationData = typeof json === "string" ? JSON.parse(json) : json; const annotationData = typeof json === "string" ? JSON.parse(json) : json;
cornerstoneTools.annotation.state.restoreAnnotations(annotationData); // Remove all existing annotations if needed
if (cornerstoneTools.annotation.state.removeAllAnnotations) {
cornerstoneTools.annotation.state.removeAllAnnotations();
}
// Add each annotation from the imported data
if (Array.isArray(annotationData)) {
annotationData.forEach(ann => {
cornerstoneTools.annotation.state.addAnnotation(ann);
});
} else if (annotationData && typeof annotationData === "object") {
// If the data is an object with tool types as keys
Object.values(annotationData).forEach((anns: any) => {
if (Array.isArray(anns)) {
anns.forEach(ann => {
cornerstoneTools.annotation.state.addAnnotation(ann);
});
}
});
}
if (renderingEngineRef.current) { if (renderingEngineRef.current) {
renderingEngineRef.current.render(); renderingEngineRef.current.render();
} }