Skip to content

Commit

Permalink
Command line for automatic export of skull and implant by command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulojamorim committed Sep 24, 2024
1 parent 80bf1ef commit 81748d1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
35 changes: 33 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def use_cmd_optargs(args):
Publisher.sendMessage("Save project", filepath=os.path.abspath(args.save))
exit(0)

check_for_segmentation(args)
check_for_cranioplasty(args)
check_for_export(args)

return True
Expand Down Expand Up @@ -411,10 +411,41 @@ def use_cmd_optargs(args):
return False


def check_for_segmentation(args):
def check_for_cranioplasty(args):
import invesalius.constants as const
from invesalius.i18n import tr as _

surface_options = {
"method": {
"algorithm": "Default",
"options": {},
},
"options": {
"index": 0,
"name": "",
"quality": _("Optimal *"),
"fill": False,
"keep_largest": False,
"overwrite": False,
},
}

if args.cranioplasty:
from invesalius.data import slice_
from invesalius.project import Project

# create cranium mask
Publisher.sendMessage("Update threshold limits", threshold_range=(226, 3071))
Publisher.sendMessage("Appy threshold all slices")

# create implant mask
Publisher.sendMessage("Create implant for cranioplasty")

# convert masks to surfaces and exports them.
Publisher.sendMessage(
"Export all surfaces separately", folder="./", filetype=const.FILETYPE_STL
)


def sanitize(text):
text = str(text).strip().replace(" ", "_")
Expand Down
1 change: 1 addition & 0 deletions invesalius/data/slice_.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def __bind_events(self) -> None:
Publisher.subscribe(self._fill_holes_auto, "Fill holes automatically")

Publisher.subscribe(self._set_interpolation_method, "Set interpolation method")
Publisher.subscribe(self.do_threshold_to_all_slices, "Appy threshold all slices")

def GetMaxSliceNumber(self, orientation: str) -> int:
shape: Tuple[int, int, int] = self.matrix.shape
Expand Down
56 changes: 56 additions & 0 deletions invesalius/data/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def __bind_events(self):
Publisher.subscribe(self.UpdateConvertToInvFlag, "Update convert_to_inv flag")

Publisher.subscribe(self.CreateSurfaceFromPolydata, "Create surface from polydata")
Publisher.subscribe(self.export_all_surfaces_separately, "Export all surfaces separately")

def OnDuplicate(self, surface_indexes):
proj = prj.Project()
Expand Down Expand Up @@ -1206,6 +1207,61 @@ def OnExportSurface(self, filename, filetype, convert_to_world=False):
dlg.Destroy()
os.remove(temp_file)

def export_all_surfaces_separately(self, folder, filetype):
import invesalius.data.slice_ as slc

if filetype in (
const.FILETYPE_STL,
const.FILETYPE_VTP,
const.FILETYPE_PLY,
const.FILETYPE_STL_ASCII,
):
proj = prj.Project()

for index in list(proj.mask_dict.keys()):
if index == 1:
algorithm = "Context aware smoothing"
else:
algorithm = "Default"
surface_parameters = {
"method": {
"algorithm": algorithm,
"options": {},
},
"options": {
"index": index,
"name": "",
"quality": _("Optimal *"),
"fill": False,
"keep_largest": True,
"overwrite": False,
},
}

mask = proj.mask_dict[index]
print(mask.matrix.min(), mask.matrix.max(), mask.name)
slice_ = slc.Slice()

Publisher.sendMessage(
"Create surface",
slice_=slice_,
mask=mask,
surface_parameters=surface_parameters,
)

# hide all surfaces
for index in proj.surface_dict:
proj.surface_dict[index].is_shown = False

# Displays one surface at a time and export
for index in proj.surface_dict.keys():
print(proj.surface_dict[index].name)
proj.surface_dict[index].is_shown = True
self._export_surface(
os.path.join(folder, str(index) + ".stl"), const.FILETYPE_STL, False
)
proj.surface_dict[index].is_shown = False

def _export_surface(self, filename, filetype, convert_to_world):
if filetype in (
const.FILETYPE_STL,
Expand Down

0 comments on commit 81748d1

Please sign in to comment.