Salome HOME
Implementation of task "3.1. Ability to export the PartSet 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 import EventsAPI
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     if os.name == "nt":
40         tmp_filename.replace("\\", "/")
41     return tmp_filename
42
43 ## @ingroup Plugins
44 #  Feature to export all shapes and groups into the GEOM module
45 class ExportFeature(ModelAPI.ModelAPI_Feature):
46
47     ## The constructor.
48     def __init__(self):
49         ModelAPI.ModelAPI_Feature.__init__(self)
50         pass
51
52     @staticmethod
53     ## Export kind. Static.
54     def ID():
55         return "ExportToGEOM"
56
57     ## Returns the kind of a feature.
58     def getKind(self):
59         return ExportFeature.ID()
60
61     ## This feature is action: has no property panel and executes immediately.
62     def isAction(self):
63         return True
64
65     ## This feature has no attributes, as it is action.
66     def initAttributes(self):
67       pass
68
69     ## Export the results, groups and fields via XAO
70     def exportViaXAO(self):
71         # if part-set is active, iterate also parts
72         for isPart in (True, False):
73           aResultType = model.ModelAPI_ResultBody_group()
74           if isPart:
75             aResultType = model.ModelAPI_ResultPart_group()
76           # iterate all results of Part, export one by one due to issue 2882
77           for aResIndex in range(self.Part.size(aResultType)):
78             anObject = self.Part.object(aResultType, aResIndex)
79             aResult = model.objectToResult(anObject)
80             if not aResult is None:
81               if (not aResult.shape() or aResult.shape().isNull()) and isPart:
82                 aPart = model.modelAPI_ResultPart(aResult)
83                 aPartDoc = aPart.partDoc()
84                 if not aPartDoc or not aPartDoc.isOpened():
85                   EventsAPI.Events_InfoMessage("ExportToGEOM", "For export to GEOM some Part is not activated", self).send()
86                   return
87
88               if  not aResult.shape() or aResult.shape().isNull():
89                 continue
90               tmpXAOFile = getTmpFileName("xao")
91               self.tmpXAOFile = tmpXAOFile
92               #print "Export to %s"%tmpXAOFile
93               exportXAO = ExchangeAPI.exportToXAO(self.Part, tmpXAOFile, model.selection(aResult), "automatic_shaper_export_to_XAO")
94               if not os.path.exists(tmpXAOFile) or os.stat(tmpXAOFile).st_size == 0:
95                   exportXAO.feature().setError("Error in exportToXAO. No XAO file has been created.")
96                   return
97               imported, shape, subShapes, groups, fields = self.geompy.ImportXAO(tmpXAOFile)
98               self.geompy.addToStudy( shape, shape.GetName() )
99               # add sub-shapes and groups to the object browser
100               for obj in subShapes + groups:
101                   name = obj.GetName()
102                   self.geompy.addToStudyInFather(shape, obj, name)
103               # add fields to the object browser
104               for field in fields:
105                   name = field.GetName()
106                   self.geompy.addToStudyInFather(shape, field, name)
107                   # add steps to the object browser
108                   steps = field.getSteps()
109                   for i_step in steps:
110                       step = field.getStep(i_step)
111                       i_stamp = step.GetStamp()
112                       step_name = "Step %i %i"%(i_step, i_stamp)
113                       self.geompy.addToStudyInFather( field, step, step_name )
114               # Remove the temporary file
115               os.remove(tmpXAOFile)
116             pass
117           pass
118         pass
119
120     ## Exports all shapes and groups into the GEOM module.
121     def execute(self):
122         aSession = ModelAPI.ModelAPI_Session.get()
123         ## Get active document
124         self.Part = aSession.activeDocument()
125         ## List of objects created in the old geom for later use
126         self.geomObjects = []
127         ## geomBuilder tool
128         salome.salome_init(1)
129         self.geompy = geomBuilder.New()
130
131         self.exportViaXAO()
132
133         pass