# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Job import Job
from UM.Application import Application
from UM.Message import Message

import os.path

from UM.i18n import i18nCatalog
i18n_catalog = i18nCatalog("uranium")

##  A Job subclass that performs mesh loading.
#
#   The result of this Job is a MeshData object.
class ReadMeshJob(Job):
    def __init__(self, filename):
        super().__init__()
        self._filename = filename
        self._handler = Application.getInstance().getMeshFileHandler()
        self._device = Application.getInstance().getStorageDevice("LocalFileStorage")

    def getFileName(self):
        return self._filename

    def run(self):
        loading_message = Message(i18n_catalog.i18nc("Loading mesh message, {0} is file name", "Loading {0}").format(self._filename), lifetime = 0, dismissable = False)
        loading_message.setProgress(-1)
        loading_message.show()
        self.setResult(self._handler.read(self._filename, self._device))
        loading_message.hide()
        result_message = Message(i18n_catalog.i18nc("Finished loading mesh message, {0} is file name", "Loaded {0}").format(self._filename))
        result_message.show()

@@ -185,16 +185,100 @@
  Message
+%0Afrom UM.Math.AxisAlignedBox import AxisAlignedBox%0Afrom UM.Math.Matrix import Matrix
 %0A%0Aimport
@@ -1050,24 +1050,25 @@
 .show()%0A
+%0A
         
 self.set
@@ -1063,72 +1063,1078 @@
     
-self.setResult(self._handler.read(self._filename, self._device))
+mesh = self._handler.read(self._filename, self._device)%0A%0A        # Scale down to maximum bounds size if that is available%0A        if hasattr(Application.getInstance().getController().getScene(), %22_maximum_bounds%22):%0A            max_bounds = Application.getInstance().getController().getScene()._maximum_bounds%0A            bbox = mesh.getExtents()%0A%0A            if max_bounds.width %3C bbox.width or max_bounds.height %3C bbox.height or max_bounds.depth %3C bbox.depth:%0A                largest_dimension = max(bbox.width, bbox.height, bbox.depth)%0A%0A                scale_factor = 1.0%0A                if largest_dimension == bbox.width:%0A                    scale_factor = max_bounds.width / bbox.width%0A                elif largest_dimension == bbox.height:%0A                    scale_factor = max_bounds.height / bbox.height%0A                else:%0A                    scale_factor = max_bounds.depth / bbox.depth%0A%0A                matrix = Matrix()%0A                matrix.setByScaleFactor(scale_factor)%0A                mesh = mesh.getTransformed(matrix)%0A%0A        self.setResult(mesh)%0A
 %0A   

Scale down models when they are just loaded and very large