Segementation mask file (.nii format) convert .stl format

I am able to convert my data ‘.nii’ Segementation mask files to Model ‘.stl’ files.
Segementation mask files is for 3D medical images,which consists of multiple 2D slices.
My Code

nii_pathname =  ...   
seg = slicer.util.loadSegmentation(nii_pathname)
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsClosedSurfaceRepresentationToFiles(out, segmentationNode, None, "STL")

I think the conversion of mask to stl is a 3D reconstruction process, I would like to know what went through in the process and what functions were used?
Looking forward to your reply.