2 Boolean geometry group of solids.
6 from __future__ import absolute_import
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
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'
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()))
28 def getNewDerivation(elementNode):
30 return ImportDerivation(elementNode)
32 def getXMLFromCarvingFileName(fileName):
33 'Get xml text from xml text.'
34 carving = fabmetheus_interpret.getCarving(fileName)
37 output = xml_simple_writer.getBeginGeometryXMLOutput()
38 carving.addXML(0, output)
39 return xml_simple_writer.getEndGeometryXMLString(output)
41 def processElementNode(elementNode):
42 "Process the xml element."
43 processElementNodeByDerivation(None, elementNode)
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:
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:')
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.')
62 if derivation.fileName.endswith('.xml'):
63 xmlText = archive.getFileText(absoluteFileName)
65 xmlText = getXMLFromCarvingFileName(absoluteFileName)
66 print('The import tool is opening the file:')
67 print(absoluteFileName)
69 print('The file %s could not be found by processElementNode in import.' % derivation.fileName)
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)
84 class ImportDerivation(object):
85 "Class to hold import variables."
86 def __init__(self, elementNode):
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')