diff --git a/app.py b/app.py index 0543bc0..158d310 100755 --- a/app.py +++ b/app.py @@ -26,6 +26,8 @@ import sys import shutil import traceback +import re + if sys.platform == 'win32': import _winreg else: @@ -270,6 +272,9 @@ def parse_comand_line(): action="store", dest="dicom_dir") + parser.add_option("--import-all", + action="store") + parser.add_option("-s", "--save", help="Save the project after an import.") @@ -296,7 +301,7 @@ def use_cmd_optargs(options, args): # If import DICOM argument... if options.dicom_dir: import_dir = options.dicom_dir - Publisher.sendMessage('Import directory', import_dir) + Publisher.sendMessage('Import directory', {'directory': import_dir, 'gui': not options.no_gui}) if options.save: Publisher.sendMessage('Save project', os.path.abspath(options.save)) @@ -305,6 +310,14 @@ def use_cmd_optargs(options, args): check_for_export(options) return True + elif options.import_all: + import invesalius.reader.dicom_reader as dcm + for patient in dcm.GetDicomGroups(options.import_all): + for group in patient.GetGroups(): + Publisher.sendMessage('Import group', {'group': group, 'gui': not options.no_gui}) + check_for_export(options, suffix=group.title, remove_surfaces=False) + Publisher.sendMessage('Remove masks', [0]) + return True # Check if there is a file path somewhere in what the user wrote # In case there is, try opening as it was a inv3 @@ -320,13 +333,29 @@ def use_cmd_optargs(options, args): return False -def check_for_export(options): +def sanitize(text): + text = str(text).strip().replace(' ', '_') + return re.sub(r'(?u)[^-\w.]', '', text) + + +def check_for_export(options, suffix='', remove_surfaces=False): + suffix = sanitize(suffix) + if options.export: if not options.threshold: print("Need option --threshold when using --export.") exit(1) - threshold_range = tuple([int(n) for n in options.threshold.split('-')]) - export(options.export, threshold_range) + threshold_range = tuple([int(n) for n in options.threshold.split(',')]) + + if suffix: + if options.export.endswith('.stl'): + path_ = '{}-{}.stl'.format(options.export[:-4], suffix) + else: + path_ = '{}-{}.stl'.format(options.export, suffix) + else: + path_ = options.export + + export(path_, threshold_range, remove_surface=remove_surfaces) elif options.export_to_all: # noinspection PyBroadException try: @@ -334,15 +363,15 @@ def check_for_export(options): for threshold_name, threshold_range in Project().presets.thresh_ct.iteritems(): if isinstance(threshold_range[0], int): - path_ = u'{}-{}.stl'.format(options.export_to_all, threshold_name) - export(path_, threshold_range) + path_ = u'{}-{}-{}.stl'.format(options.export_to_all, suffix, threshold_name) + export(path_, threshold_range, remove_surface=True) except: traceback.print_exc() finally: exit(0) -def export(path_, threshold_range): +def export(path_, threshold_range, remove_surface=False): import invesalius.constants as const Publisher.sendMessage('Set threshold values', threshold_range) @@ -362,6 +391,8 @@ def export(path_, threshold_range): } Publisher.sendMessage('Create surface from index', surface_options) Publisher.sendMessage('Export surface to file', (path_, const.FILETYPE_STL)) + if remove_surface: + Publisher.sendMessage('Remove surfaces', [0]) def print_events(data): diff --git a/invesalius/control.py b/invesalius/control.py index f81222b..2bd0059 100644 --- a/invesalius/control.py +++ b/invesalius/control.py @@ -61,6 +61,7 @@ class Controller(): def __bind_events(self): Publisher.subscribe(self.OnImportMedicalImages, 'Import directory') + Publisher.subscribe(self.OnImportGroup, 'Import group') Publisher.subscribe(self.OnShowDialogImportDirectory, 'Show import directory dialog') Publisher.subscribe(self.OnShowDialogImportOtherFiles, @@ -440,10 +441,11 @@ class Controller(): #----------- to import by command line --------------------------------------------------- def OnImportMedicalImages(self, pubsub_evt): - directory = pubsub_evt.data - self.ImportMedicalImages(directory) + directory = pubsub_evt.data['directory'] + gui = pubsub_evt.data['gui'] + self.ImportMedicalImages(directory, gui) - def ImportMedicalImages(self, directory): + def ImportMedicalImages(self, directory, gui=True): patients_groups = dcm.GetDicomGroups(directory) name = directory.rpartition('\\')[-1].split('.') print "patients: ", patients_groups @@ -451,7 +453,7 @@ class Controller(): if len(patients_groups): # OPTION 1: DICOM group = dcm.SelectLargerDicomGroup(patients_groups) - matrix, matrix_filename, dicom = self.OpenDicomGroup(group, 0, [0, 0], gui=True) + matrix, matrix_filename, dicom = self.OpenDicomGroup(group, 0, [0, 0], gui=gui) self.CreateDicomProject(dicom, matrix, matrix_filename) else: # OPTION 2: NIfTI, Analyze or PAR/REC @@ -474,6 +476,18 @@ class Controller(): self.LoadProject() Publisher.sendMessage("Enable state project", True) + def OnImportGroup(self, pubsub_evt): + group = pubsub_evt.data['group'] + gui = pubsub_evt.data['gui'] + self.ImportGroup(group, gui) + + def ImportGroup(self, group, gui=True): + matrix, matrix_filename, dicom = self.OpenDicomGroup(group, 0, [0, 0], gui=gui) + self.CreateDicomProject(dicom, matrix, matrix_filename) + + self.LoadProject() + Publisher.sendMessage("Enable state project", True) + #------------------------------------------------------------------------------------- def LoadProject(self): @@ -788,7 +802,7 @@ class Controller(): n_slices = len(filelist) resolution_percentage = utils.calculate_resizing_tofitmemory(int(sx), int(sy), n_slices, bits/8) - if resolution_percentage < 1.0: + if resolution_percentage < 1.0 and gui: re_dialog = dialog.ResizeImageDialog() re_dialog.SetValue(int(resolution_percentage*100)) re_dialog_value = re_dialog.ShowModal() -- libgit2 0.21.2