import slicer import os import subprocess import re dicomModify=os.getenv("HOME") if not dicomModify==None: dicomModify+="/software/install/" dicomModify+="dicomModify/bin/dicomModify" class loadDicom(slicer.ScriptedLoadableModule.ScriptedLoadableModule): def __init__(self,parent): slicer.ScriptedLoadableModule.ScriptedLoadableModule.__init__(self, parent) self.className="loadDicom" self.parent.title="loadDicom" self.parent.categories = ["Examples"] self.parent.dependencies = [] self.parent.contributors = ["Andrej Studen (UL/FMF)"] # replace with "Firstname Lastname (Organization)" self.parent.helpText = """ utilities for parsing dicom entries """ self.parent.acknowledgementText = """ Developed within the medical physics research programme of the Slovenian research agency. """ # replace with organization, grant and thanks. class loadDicomWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget): """Uses ScriptedLoadableModuleWidget base class, available at: https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py """ def setup(self): slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget.setup(self) class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic): def __init__(self,parent): slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic.__init__(self, parent) def load(self,sNet,dir,doRemove=True): print("Loading dir {}").format(dir) dicomFiles=sNet.listDir(dir) #filelist=[os.path.join(dir,f) for f in os.listdir(dir)] filelist=[] for f in dicomFiles: localPath=sNet.GetFile(f) f0=localPath f1=f0+"1" if not dicomModify==None: subprocess.call(dicomModify+" "+f0+" "+f1+" && mv "+f1+" "+f0+";", shell=True) filelist.append(localPath) try: loadables=self.volumePlugin.examineForImport([filelist]) except AttributeError: self.volumePlugin=slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']() loadables=self.volumePlugin.examineForImport([filelist]) for loadable in loadables: #TODO check if it makes sense to load a particular loadable if loadable.name.find('imageOrientationPatient')>-1: continue volumeNode=self.volumePlugin.load(loadable) if volumeNode != None: vName='Series'+dicomValue(loadable.files[0],"0020,0011") volumeNode.SetName(vName) try: loadableRTs=self.RTPlugin.examineForImport([filelist]) except: self.RTPlugin=plugin=slicer.modules.dicomPlugins['DicomRtImportExportPlugin']() loadableRTs=self.RTPlugin.examineForImport([filelist]) for loadable in loadableRTs: segmentationNode=self.RTPlugin.load(loadable) #if segmentationNode!= None: # segmentationNode.SetName('SegmentationBR') if not doRemove: return for f in filelist: os.remove(f) def dicomValue(file,tag): dcmdump=os.path.join(os.environ['SLICER_HOME'],"bin") dcmdump=os.path.join(dcmdump,"dcmdump") try: out=subprocess.check_output([dcmdump,'+P',tag,file]) out=re.sub(r'^.*\[(.*)\].*\n$',r'\1',out) return out except: return None def clearNodes(): nodes=[] nodes.extend(slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode")) nodes.extend(slicer.util.getNodesByClass("vtkMRMLScalarVolumeDisplayNode")) nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationNode")) nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationDisplayNode")) for node in nodes: slicer.mrmlScene.RemoveNode(node)