Coverage for tatlin/lib/model/gcode/loader.py: 94%

31 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 05:56 +0000

1# -*- coding: utf-8 -*- 

2# Copyright (C) 2024 Denis Kobozev 

3# 

4# This program is free software; you can redistribute it and/or modify 

5# it under the terms of the GNU General Public License as published by 

6# the Free Software Foundation; either version 2 of the License, or 

7# (at your option) any later version. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU General Public License for more details. 

13# 

14# You should have received a copy of the GNU General Public License 

15# along with this program; if not, write to the Free Software Foundation, 

16# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

17 

18import logging 

19 

20from tatlin.lib.gl.gcodemodel import GcodeModel 

21from tatlin.lib.ui.gcode import GcodePanel 

22 

23from ..baseloader import BaseModelLoader, ModelFileError 

24from .parser import GcodeParser, GcodeParserError 

25 

26 

27class GcodeModelLoader(BaseModelLoader): 

28 def load(self, config, scene, progress_dlg): 

29 parser = GcodeParser() 

30 with open(self.path, "r") as gcodefile: 

31 parser.load(gcodefile) 

32 try: 

33 progress_dlg.stage("Reading file...") 

34 data = parser.parse(progress_dlg.step) 

35 

36 progress_dlg.stage("Loading file...") 

37 model = GcodeModel() 

38 model.load_data(data, progress_dlg.step) 

39 

40 scene.add_model(model) 

41 scene.mode_2d = bool(config.read("ui.gcode_2d", int)) 

42 

43 offset_x = config.read("machine.platform_offset_x", float) 

44 offset_y = config.read("machine.platform_offset_y", float) 

45 offset_z = config.read("machine.platform_offset_z", float) 

46 

47 if offset_x is None and offset_y is None and offset_z is None: 

48 scene.view_model_center() 

49 logging.info( 

50 "Platform offsets not set, showing model in the center" 

51 ) 

52 else: 

53 model.offset_x = offset_x if offset_x is not None else 0 

54 model.offset_y = offset_y if offset_y is not None else 0 

55 model.offset_z = offset_z if offset_z is not None else 0 

56 logging.info( 

57 "Using platform offsets: (%s, %s, %s)" 

58 % (model.offset_x, model.offset_y, model.offset_z) 

59 ) 

60 return model, GcodePanel 

61 except GcodeParserError as e: 

62 # rethrow as generic file error 

63 raise ModelFileError(f"Parsing error: {e}")