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