]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConnectorPlugin/ConnectorPlugin_ExportFeature.py
Salome HOME
Issue #398: Export shape to GEOM with the same name as it has in NewGeom
[modules/shaper.git] / src / ConnectorPlugin / ConnectorPlugin_ExportFeature.py
1 ## @package Plugins
2 #  ExportFeature class definition
3 #  Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4
5 import EventsAPI
6 import ModelAPI
7
8 import salome
9 from salome.geom import geomBuilder
10
11
12 ## @ingroup Plugins
13 #  Feature to export all shapes and groups into the GEOM module
14 class ExportFeature(ModelAPI.ModelAPI_Feature):
15
16     ## The constructor.
17     def __init__(self):
18         ModelAPI.ModelAPI_Feature.__init__(self)
19
20     @staticmethod
21     ## Export kind. Static.
22     def ID():
23         return "ExportToGEOM"
24
25     ## Returns the kind of a feature.
26     def getKind(self):
27         return ExportFeature.ID()
28
29     ## This feature is action: has no property pannel and executes immideately.
30     def isAction(self):
31         return True
32
33     # The action is not placed into the history anyway
34     #def isInHistory(self):
35     #    return False
36
37     ## This feature has no attributes, as it is action.
38     def initAttributes(self):
39       pass
40
41     ## Exports all bodies
42     def exportBodies(self):
43         global ShapeIndex
44         kResultBodyType = "Bodies"
45         aPartSize = self.Part.size(kResultBodyType)
46         if aPartSize == 0:
47             EventsAPI.Events_Error_send("No results in the active document")
48             return
49           
50         anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]     
51         for idx, anObject in enumerate(anObjList):
52             aResult = ModelAPI.modelAPI_Result(anObject)
53             aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
54             if not aBodyResult:
55                 continue
56             aShape = aBodyResult.shape()
57             aDump = aShape.getShapeStream()
58             # Load shape to SALOME Geom
59             aBrep = self.geompy.RestoreShape(aDump)
60             self.geompy.addToStudy(aBrep, aBodyResult.data().name())
61             self.geomObjects.append([aShape, aBrep])
62
63     ## Exports all groups
64     def exportGroups(self):
65         # iterate all features to find groups
66         aFeaturesNum = self.Part.size("Features")
67         groupIndex = 0
68         for anIndex in range(0, aFeaturesNum):
69             aFeature = self.Part.object("Features", anIndex)
70             aSelectionList = aFeature.data().selectionList("group_list")
71             # if a group has been found
72             if aSelectionList:
73                 groupIndex = groupIndex + 1
74                 self.createGroupFromList(aSelectionList, "NewGeomGroup_{0}".format(groupIndex))
75                      
76     ## Creates a group by given list of selected objects and the name
77     #  @param theSelectionList: list of selected objects
78     #  @param theGroupName: name of the group to create
79     def createGroupFromList(self, theSelectionList, theGroupName):
80         # iterate on all selected entities of the group
81         # and get the corresponding ID
82         aSelectionNum = theSelectionList.size()
83         Ids = []
84         for aSelIndex in range(0, aSelectionNum):
85             aSelection = theSelectionList.value(aSelIndex)
86             aSelectionContext = aSelection.context()
87             anID = aSelection.Id()
88             Ids.append(anID)
89             if aSelection.value().isVertex():
90                 groupType = "VERTEX"
91             elif aSelection.value().isEdge():
92                 groupType = "EDGE" 
93             elif aSelection.value().isFace():
94                 groupType = "FACE"
95             else:
96                 groupType = "SOLID"
97
98         # iterate on exported objects and check if the current
99         # group refers to this object
100         for obj in self.geomObjects: 
101             if aSelectionContext.shape().isEqual(obj[0]):
102                 aGroup = self.geompy.CreateGroup(obj[1], self.geompy.ShapeType[groupType])
103                 self.geompy.UnionIDs(aGroup,Ids)
104                 self.geompy.addToStudyInFather(obj[1], aGroup, theGroupName)
105           
106     ## Exports all shapes and groups into the GEOM module.
107     def execute(self):
108         aSession = ModelAPI.ModelAPI_Session.get()
109         ## Get active document
110         self.Part = aSession.activeDocument()
111         ## List of objects created in the old geom for later use
112         self.geomObjects = []
113         ## geomBuilder tool
114         self.geompy = geomBuilder.New(salome.myStudy)
115        
116         # Export bodies and groups
117         self.exportBodies()
118         self.exportGroups()
119         pass