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