fix tool import

This commit is contained in:
Ross
2025-06-02 09:59:51 +01:00
parent 1fd3020f0c
commit b408ba96b3
+40 -2
View File
@@ -1198,7 +1198,27 @@ window.importViewerState = (json: string) => {
// Export all annotations using cornerstoneTools.annotation.state.getAllAnnotations()
window.exportAnnotations = () => {
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);
} catch (e) {
alert("Failed to export annotations: " + e);
@@ -1210,7 +1230,25 @@ window.importViewerState = (json: string) => {
window.importAnnotations = (json: string) => {
try {
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) {
renderingEngineRef.current.render();
}