Salome HOME
Fix for the issue #1146 : ability to export groups that has been created on the previ...
[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 import GeomAPI
8 import GeomAlgoAPI
9
10 import salome
11 from salome.geom import geomBuilder
12
13 def getObjectIndex(theName):
14     aStudy = salome.myStudy
15     aId = 0
16     aObj = aStudy.FindObjectByName(theName, "GEOM")
17     while len(aObj) != 0:
18         aId = aId + 1
19         aName = theName + '_' + str(aId)
20         aObj = aStudy.FindObjectByName(aName, "GEOM")
21     return aId
22
23 ## @ingroup Plugins
24 #  Feature to export all shapes and groups into the GEOM module
25 class ExportFeature(ModelAPI.ModelAPI_Feature):
26
27     ## The constructor.
28     def __init__(self):
29         ModelAPI.ModelAPI_Feature.__init__(self)
30
31     @staticmethod
32     ## Export kind. Static.
33     def ID():
34         return "ExportToGEOM"
35
36     ## Returns the kind of a feature.
37     def getKind(self):
38         return ExportFeature.ID()
39
40     ## This feature is action: has no property pannel and executes immideately.
41     def isAction(self):
42         return True
43
44     ## This feature has no attributes, as it is action.
45     def initAttributes(self):
46       pass
47
48     ## Exports all bodies
49     def exportBodies(self):
50         global ShapeIndex
51         kResultBodyType = "Bodies"
52         aPartSize = self.Part.size(kResultBodyType)
53         if aPartSize == 0:
54             EventsAPI.Events_Error_send("No results in the active document")
55             return
56
57         anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]
58         aShapesList = GeomAlgoAPI.ShapeList()
59         aName = ""
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             if aShape is not None and not aShape.isNull():
67               aShapesList.append(aShape)
68               if len(aShapesList) == 1:
69                 aName = aBodyResult.data().name()
70
71         # issue 1045: create compound if there are more than one shape
72         if len(aShapesList) > 1:
73           self.shape = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.compound(aShapesList)
74           aName = "ShaperResults"
75         elif len(aShapesList) == 1:
76           self.shape = aShapesList[0]
77
78         # so, only one shape is always in the result
79         aDump = self.shape.getShapeStream()
80         # Load shape to SALOME Geom
81         aBrep = self.geompy.RestoreShape(aDump)
82
83         # Make unique name
84         aId = getObjectIndex(aName)
85         if aId != 0:
86             aName = aName + '_' + str(aId)
87
88         self.geompy.addToStudy(aBrep, aName)
89         self.brep = aBrep
90
91     ## Exports all groups
92     def exportGroups(self):
93         # iterate all features to find groups
94         aFeaturesNum = self.Part.size("Features")
95         groupIndex = 0
96         for anIndex in range(0, aFeaturesNum):
97             aFeature = self.Part.object("Features", anIndex)
98             aSelectionList = aFeature.data().selectionList("group_list")
99             # if a group has been found
100             if aSelectionList:
101                 aFeature = ModelAPI.objectToFeature(aFeature)
102                 if aFeature.firstResult() is not None:
103                   aName = aFeature.firstResult().data().name()
104                 groupIndex = groupIndex + 1
105                 self.createGroupFromList(aSelectionList, aName)
106
107     ## Returns a type of the shape in the old GEOM representation
108     def shapeType(self, shape):
109         if shape.isVertex():
110             return "VERTEX"
111         elif shape.isEdge():
112             return "EDGE"
113         elif shape.isFace():
114             return "FACE"
115         
116         return "SOLID"
117
118     ## Creates a group by given list of selected objects and the name
119     #  @param theSelectionList: list of selected objects
120     #  @param theGroupName: name of the group to create
121     def createGroupFromList(self, theSelectionList, theGroupName):
122         # iterate on all selected entities of the group
123         # and get the corresponding ID
124         aSelectionNum = theSelectionList.size()
125         Ids = []
126         groupType = ""
127         for aSelIndex in range(0, aSelectionNum):
128             aSelection = theSelectionList.value(aSelIndex)
129             anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, aSelection.value())
130             if anID == 0:
131                 #it may be a compound of objects if movement of the group to the end
132                 # splits the original element to several (issue 1146)
133                 anExp = GeomAPI.GeomAPI_ShapeExplorer(aSelection.value(), GeomAPI.GeomAPI_Shape.SHAPE)
134                 while anExp.more():
135                     anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
136                     if anID != 0:
137                         Ids.append(anID)
138                         groupType = self.shapeType(anExp.current())
139                     anExp.next()
140             else:
141                 Ids.append(anID)
142                 groupType = self.shapeType(aSelection.value())
143
144
145         aGroup = self.geompy.CreateGroup(self.brep, self.geompy.ShapeType[groupType])
146         self.geompy.UnionIDs(aGroup,Ids)
147         self.geompy.addToStudyInFather(self.brep, aGroup, theGroupName)
148
149     ## Exports all shapes and groups into the GEOM module.
150     def execute(self):
151         aSession = ModelAPI.ModelAPI_Session.get()
152         ## Get active document
153         self.Part = aSession.activeDocument()
154         ## List of objects created in the old geom for later use
155         self.geomObjects = []
156         ## geomBuilder tool
157         self.geompy = geomBuilder.New(salome.myStudy)
158
159         # Export bodies and groups
160         self.exportBodies()
161         self.exportGroups()
162         pass