Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.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             aName = aFeature.data().name()
88             # if a group has been found
89             if aSelectionList:
90                 groupIndex = groupIndex + 1
91                 self.createGroupFromList(aSelectionList, aName)
92                      
93     ## Creates a group by given list of selected objects and the name
94     #  @param theSelectionList: list of selected objects
95     #  @param theGroupName: name of the group to create
96     def createGroupFromList(self, theSelectionList, theGroupName):
97         # iterate on all selected entities of the group
98         # and get the corresponding ID
99         aSelectionNum = theSelectionList.size()
100         Ids = []
101         for aSelIndex in range(0, aSelectionNum):
102             aSelection = theSelectionList.value(aSelIndex)
103             aSelectionContext = aSelection.context()
104             anID = aSelection.Id()
105             Ids.append(anID)
106             if aSelection.value().isVertex():
107                 groupType = "VERTEX"
108             elif aSelection.value().isEdge():
109                 groupType = "EDGE" 
110             elif aSelection.value().isFace():
111                 groupType = "FACE"
112             else:
113                 groupType = "SOLID"
114
115         # iterate on exported objects and check if the current
116         # group refers to this object
117         for obj in self.geomObjects: 
118             if aSelectionContext.shape().isEqual(obj[0]):
119                 aGroup = self.geompy.CreateGroup(obj[1], self.geompy.ShapeType[groupType])
120                 self.geompy.UnionIDs(aGroup,Ids)
121                 self.geompy.addToStudyInFather(obj[1], aGroup, theGroupName)
122           
123     ## Exports all shapes and groups into the GEOM module.
124     def execute(self):
125         aSession = ModelAPI.ModelAPI_Session.get()
126         ## Get active document
127         self.Part = aSession.activeDocument()
128         ## List of objects created in the old geom for later use
129         self.geomObjects = []
130         ## geomBuilder tool
131         self.geompy = geomBuilder.New(salome.myStudy)
132        
133         # Export bodies and groups
134         self.exportBodies()
135         self.exportGroups()
136         pass