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