Salome HOME
Implement ExportToGEOM via exportToXAO. Fix the issue #2198.
[modules/shaper.git] / src / ConnectorPlugin / ConnectorPlugin_ExportFeature.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 ## @package Plugins
22 #  ExportFeature class definition
23
24 import ModelAPI
25 import ExchangeAPI
26
27 import salome
28 from salome.geom import geomBuilder
29
30 from salome.shaper import model
31
32 import os
33
34 def getTmpFileName(ext):
35     import tempfile
36     tempdir = tempfile.gettempdir()
37     tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
38     tmp_filename = tmp_file.name
39     return tmp_filename
40
41 ## @ingroup Plugins
42 #  Feature to export all shapes and groups into the GEOM module
43 class ExportFeature(ModelAPI.ModelAPI_Feature):
44
45     ## The constructor.
46     def __init__(self):
47         ModelAPI.ModelAPI_Feature.__init__(self)
48         pass
49
50     @staticmethod
51     ## Export kind. Static.
52     def ID():
53         return "ExportToGEOM"
54
55     ## Returns the kind of a feature.
56     def getKind(self):
57         return ExportFeature.ID()
58
59     ## This feature is action: has no property pannel and executes immediately.
60     def isAction(self):
61         return True
62
63     ## This feature has no attributes, as it is action.
64     def initAttributes(self):
65       pass
66
67     ## Export the results, groups and fields via XAO
68     def exportViaXAO(self):
69         tmpXAOFile = getTmpFileName("xao")
70         self.tmpXAOFile = tmpXAOFile
71         #print "Export to %s"%tmpXAOFile
72         exportXAO = ExchangeAPI.exportToXAO(self.Part, tmpXAOFile, "automatic_shaper_export_to_XAO")
73         if not os.path.exists(tmpXAOFile) or os.stat(tmpXAOFile).st_size == 0:
74             exportXAO.feature().setError("Error in exportToXAO. No XAO file has been created.")
75             return
76         imported, shape, subShapes, groups, fields = self.geompy.ImportXAO(tmpXAOFile)
77         self.geompy.addToStudy( shape, shape.GetName() )
78         # add sub-shapes and groups to the object browser
79         for obj in subShapes + groups:
80             name = obj.GetName()
81             self.geompy.addToStudyInFather(shape, obj, name)
82         # add fields to the object browser
83         for field in fields:
84             name = field.GetName()
85             self.geompy.addToStudyInFather(shape, field, name)
86             # add steps to the object browser
87             steps = field.getSteps()
88             for i_step in steps:
89                 step = field.getStep(i_step)
90                 i_stamp = step.GetStamp()
91                 step_name = "Step %i %i"%(i_step, i_stamp)
92                 self.geompy.addToStudyInFather( field, step, step_name )
93         # Remove the temporary file
94         os.remove(tmpXAOFile)
95         pass
96
97     ## Exports all shapes and groups into the GEOM module.
98     def execute(self):
99         aSession = ModelAPI.ModelAPI_Session.get()
100         ## Get active document
101         self.Part = aSession.activeDocument()
102         ## List of objects created in the old geom for later use
103         self.geomObjects = []
104         ## geomBuilder tool
105         salome.salome_init(0,1)
106         self.geompy = geomBuilder.New(salome.myStudy)
107
108         self.exportViaXAO()
109
110         pass