chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / cura_sf / fabmetheus_utilities / geometry / manipulation_meta / import.py
1 """
2 Boolean geometry group of solids.
3
4 """
5
6 from __future__ import absolute_import
7 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
8 import __init__
9
10 from fabmetheus_utilities.fabmetheus_tools import fabmetheus_interpret
11 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
12 from fabmetheus_utilities.geometry.solids import group
13 from fabmetheus_utilities import xml_simple_reader
14 from fabmetheus_utilities import xml_simple_writer
15 from fabmetheus_utilities import archive
16 from fabmetheus_utilities import euclidean
17 from fabmetheus_utilities import gcodec
18 from fabmetheus_utilities import settings
19 import cStringIO
20 import os
21
22
23 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
24 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
25 __date__ = '$Date: 2008/02/05 $'
26 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
27
28
29 def appendAttributes(fromElementNode, toElementNode):
30         'Append the attributes from the child nodes of fromElementNode to the attributes of toElementNode.'
31         for childNode in fromElementNode.childNodes:
32                 toElementNode.attributes.update(evaluate.removeIdentifiersFromDictionary(childNode.attributes.copy()))
33
34 def getNewDerivation(elementNode):
35         'Get new derivation.'
36         return ImportDerivation(elementNode)
37
38 def getXMLFromCarvingFileName(fileName):
39         'Get xml text from xml text.'
40         carving = fabmetheus_interpret.getCarving(fileName)
41         if carving == None:
42                 return ''
43         output = xml_simple_writer.getBeginGeometryXMLOutput()
44         carving.addXML(0, output)
45         return xml_simple_writer.getEndGeometryXMLString(output)
46
47 def processElementNode(elementNode):
48         "Process the xml element."
49         processElementNodeByDerivation(None, elementNode)
50
51 def processElementNodeByDerivation(derivation, elementNode):
52         'Process the xml element by derivation.'
53         if derivation == None:
54                 derivation = ImportDerivation(elementNode)
55         if derivation.fileName == None:
56                 return
57         parserFileName = elementNode.getOwnerDocument().fileName
58         absoluteFileName = archive.getAbsoluteFolderPath(parserFileName, derivation.fileName)
59         if 'models/' not in absoluteFileName:
60                 print('Warning, models/ was not in the absolute file path, so for security nothing will be done for:')
61                 print(elementNode)
62                 print('For which the absolute file path is:')
63                 print(absoluteFileName)
64                 print('The import tool can only read a file which has models/ in the file path.')
65                 print('To import the file, move the file into a folder called model/ or a subfolder which is inside the model folder tree.')
66                 return
67         xmlText = ''
68         if derivation.fileName.endswith('.xml'):
69                 xmlText = archive.getFileText(absoluteFileName)
70         else:
71                 xmlText = getXMLFromCarvingFileName(absoluteFileName)
72         print('The import tool is opening the file:')
73         print(absoluteFileName)
74         if xmlText == '':
75                 print('The file %s could not be found by processElementNode in import.' % derivation.fileName)
76                 return
77         if derivation.importName == None:
78                 elementNode.attributes['_importName'] = archive.getUntilDot(derivation.fileName)
79                 if derivation.basename:
80                         elementNode.attributes['_importName'] = os.path.basename(elementNode.attributes['_importName'])
81         xml_simple_reader.createAppendByText(elementNode, xmlText)
82         if derivation.appendDocumentElement:
83                 appendAttributes(elementNode, elementNode.getDocumentElement())
84         if derivation.appendElement:
85                 appendAttributes(elementNode, elementNode)
86         elementNode.localName = 'group'
87         evaluate.processArchivable(group.Group, elementNode)
88
89
90 class ImportDerivation:
91         "Class to hold import variables."
92         def __init__(self, elementNode):
93                 'Set defaults.'
94                 self.appendDocumentElement = evaluate.getEvaluatedBoolean(False, elementNode, 'appendDocumentElement')
95                 self.appendElement = evaluate.getEvaluatedBoolean(False, elementNode, 'appendElement')
96                 self.basename = evaluate.getEvaluatedBoolean(True, elementNode, 'basename')
97                 self.elementNode = elementNode
98                 self.fileName = evaluate.getEvaluatedString('', elementNode, 'file')
99                 self.importName = evaluate.getEvaluatedString(None, elementNode, '_importName')