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