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