Salome HOME
Merge remote branch 'remotes/origin/vsr/gcc_4_9_compat' into Dev_2.1.0
[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 def getObjectIndex(theName):
12     aStudy = salome.myStudy
13     aId = 0
14     aObj = aStudy.FindObjectByName(theName, "GEOM")
15     while len(aObj) != 0:
16         aId = aId + 1
17         aName = theName + '_' + str(aId)
18         aObj = aStudy.FindObjectByName(aName, "GEOM")
19     return aId
20
21 ## @ingroup Plugins
22 #  Feature to export all shapes and groups into the GEOM module
23 class ExportFeature(ModelAPI.ModelAPI_Feature):
24
25     ## The constructor.
26     def __init__(self):
27         ModelAPI.ModelAPI_Feature.__init__(self)
28
29     @staticmethod
30     ## Export kind. Static.
31     def ID():
32         return "ExportToGEOM"
33
34     ## Returns the kind of a feature.
35     def getKind(self):
36         return ExportFeature.ID()
37
38     ## This feature is action: has no property pannel and executes immideately.
39     def isAction(self):
40         return True
41
42     # The action is not placed into the history anyway
43     #def isInHistory(self):
44     #    return False
45
46     ## This feature has no attributes, as it is action.
47     def initAttributes(self):
48       pass
49
50     ## Exports all bodies
51     def exportBodies(self):
52         global ShapeIndex
53         kResultBodyType = "Bodies"
54         aPartSize = self.Part.size(kResultBodyType)
55         if aPartSize == 0:
56             EventsAPI.Events_Error_send("No results in the active document")
57             return
58           
59         anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]     
60         for idx, anObject in enumerate(anObjList):
61             aResult = ModelAPI.modelAPI_Result(anObject)
62             aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
63             if not aBodyResult:
64                 continue
65             aShape = aBodyResult.shape()
66             aDump = aShape.getShapeStream()
67             # Load shape to SALOME Geom
68             aBrep = self.geompy.RestoreShape(aDump)
69             aName = aBodyResult.data().name()
70             
71             # Make unique name
72             aId = getObjectIndex(aName)
73             if aId != 0:
74                 aName = aName + '_' + str(aId)
75             
76             self.geompy.addToStudy(aBrep, aName)
77             self.geomObjects.append([aShape, aBrep])
78
79     ## Exports all groups
80     def exportGroups(self):
81         # iterate all features to find groups
82         aFeaturesNum = self.Part.size("Features")
83         groupIndex = 0
84         for anIndex in range(0, aFeaturesNum):
85             aFeature = self.Part.object("Features", anIndex)
86             aSelectionList = aFeature.data().selectionList("group_list")
87             # if a group has been found
88             if aSelectionList:
89                 aFeature = ModelAPI.objectToFeature(aFeature)
90                 if aFeature.firstResult() is not None:
91                   aName = aFeature.firstResult().data().name()
92                 groupIndex = groupIndex + 1
93                 self.createGroupFromList(aSelectionList, aName)
94                      
95     ## Creates a group by given list of selected objects and the name
96     #  @param theSelectionList: list of selected objects
97     #  @param theGroupName: name of the group to create
98     def createGroupFromList(self, theSelectionList, theGroupName):
99         # iterate on all selected entities of the group
100         # and get the corresponding ID
101         aSelectionNum = theSelectionList.size()
102         Ids = []
103         for aSelIndex in range(0, aSelectionNum):
104             aSelection = theSelectionList.value(aSelIndex)
105             aSelectionContext = aSelection.context()
106             anID = aSelection.Id()
107             Ids.append(anID)
108             if aSelection.value().isVertex():
109                 groupType = "VERTEX"
110             elif aSelection.value().isEdge():
111                 groupType = "EDGE" 
112             elif aSelection.value().isFace():
113                 groupType = "FACE"
114             else:
115                 groupType = "SOLID"
116
117         aContextBody = ModelAPI.modelAPI_ResultBody(aSelectionContext)
118         if aContextBody is not None:
119           # iterate on exported objects and check if the current
120           # group refers to this object
121           for obj in self.geomObjects: 
122               if aContextBody.isLatestEqual(obj[0]):
123                   aGroup = self.geompy.CreateGroup(obj[1], self.geompy.ShapeType[groupType])
124                   self.geompy.UnionIDs(aGroup,Ids)
125                   self.geompy.addToStudyInFather(obj[1], aGroup, theGroupName)
126           
127     ## Exports all shapes and groups into the GEOM module.
128     def execute(self):
129         aSession = ModelAPI.ModelAPI_Session.get()
130         ## Get active document
131         self.Part = aSession.activeDocument()
132         ## List of objects created in the old geom for later use
133         self.geomObjects = []
134         ## geomBuilder tool
135         self.geompy = geomBuilder.New(salome.myStudy)
136        
137         # Export bodies and groups
138         self.exportBodies()
139         self.exportGroups()
140         pass