Salome HOME
Implementation of proposal 3 in the issue #2882 : Groups are not exported to GEOM...
[modules/shaper.git] / src / ConnectorPlugin / ConnectorPlugin_ExportFeature.py
1 # Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 #
19
20 ## @package Plugins
21 #  ExportFeature class definition
22
23 import ModelAPI
24 import ExchangeAPI
25
26 import salome
27 from salome.geom import geomBuilder
28
29 from salome.shaper import model
30
31 import os
32
33 def getTmpFileName(ext):
34     import tempfile
35     tempdir = tempfile.gettempdir()
36     tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
37     tmp_filename = tmp_file.name
38     if os.name == "nt":
39         tmp_filename.replace("\\", "/")
40     return tmp_filename
41
42 ## @ingroup Plugins
43 #  Feature to export all shapes and groups into the GEOM module
44 class ExportFeature(ModelAPI.ModelAPI_Feature):
45
46     ## The constructor.
47     def __init__(self):
48         ModelAPI.ModelAPI_Feature.__init__(self)
49         pass
50
51     @staticmethod
52     ## Export kind. Static.
53     def ID():
54         return "ExportToGEOM"
55
56     ## Returns the kind of a feature.
57     def getKind(self):
58         return ExportFeature.ID()
59
60     ## This feature is action: has no property panel and executes immediately.
61     def isAction(self):
62         return True
63
64     ## This feature has no attributes, as it is action.
65     def initAttributes(self):
66       pass
67
68     ## Export the results, groups and fields via XAO
69     def exportViaXAO(self):
70         # iterate all results of Part, export one by one due to issue 2882
71         for aResIndex in range(self.Part.size(model.ModelAPI_ResultBody_group())):
72           anObject = self.Part.object(model.ModelAPI_ResultBody_group(), aResIndex)
73           aResult = model.objectToResult(anObject)
74           if not aResult is None:
75             tmpXAOFile = getTmpFileName("xao")
76             self.tmpXAOFile = tmpXAOFile
77             #print "Export to %s"%tmpXAOFile
78             exportXAO = ExchangeAPI.exportToXAO(self.Part, tmpXAOFile, model.selection(aResult), "automatic_shaper_export_to_XAO")
79             if not os.path.exists(tmpXAOFile) or os.stat(tmpXAOFile).st_size == 0:
80                 exportXAO.feature().setError("Error in exportToXAO. No XAO file has been created.")
81                 return
82             imported, shape, subShapes, groups, fields = self.geompy.ImportXAO(tmpXAOFile)
83             self.geompy.addToStudy( shape, shape.GetName() )
84             # add sub-shapes and groups to the object browser
85             for obj in subShapes + groups:
86                 name = obj.GetName()
87                 self.geompy.addToStudyInFather(shape, obj, name)
88             # add fields to the object browser
89             for field in fields:
90                 name = field.GetName()
91                 self.geompy.addToStudyInFather(shape, field, name)
92                 # add steps to the object browser
93                 steps = field.getSteps()
94                 for i_step in steps:
95                     step = field.getStep(i_step)
96                     i_stamp = step.GetStamp()
97                     step_name = "Step %i %i"%(i_step, i_stamp)
98                     self.geompy.addToStudyInFather( field, step, step_name )
99             # Remove the temporary file
100             os.remove(tmpXAOFile)
101           pass
102         pass
103
104     ## Exports all shapes and groups into the GEOM module.
105     def execute(self):
106         aSession = ModelAPI.ModelAPI_Session.get()
107         ## Get active document
108         self.Part = aSession.activeDocument()
109         ## List of objects created in the old geom for later use
110         self.geomObjects = []
111         ## geomBuilder tool
112         salome.salome_init(1)
113         self.geompy = geomBuilder.New()
114
115         self.exportViaXAO()
116
117         pass