]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConnectorPlugin/ConnectorPlugin_ExportFeature.py
Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom.git into Dev_0.7.1
[modules/shaper.git] / src / ConnectorPlugin / ConnectorPlugin_ExportFeature.py
1 """
2 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 """
4
5 import EventsAPI
6 import ModelAPI
7
8 import salome
9 from salome.geom import geomBuilder
10
11
12 class ExportFeature(ModelAPI.ModelAPI_Feature):
13
14     "Feature to create a box by drawing a sketch and extruding it"
15
16     def __init__(self):
17         ModelAPI.ModelAPI_Feature.__init__(self)
18
19     @staticmethod
20     def ID():
21         return "ExportToGEOM"
22
23     def getKind(self):
24         return ExportFeature.ID()
25
26     # This feature is action: has no property pannel and executes immideately
27     def isAction(self):
28         return True
29
30     # The action is not placed into the histiry anyway
31     #def isInHistory(self):
32     #    return False
33
34     def initAttributes(self):
35       # This feature has no attributes, it is action
36       pass
37         
38     def exportBodies(self):
39         # Get all bodies
40         kResultBodyType = "Bodies"
41         aPartSize = self.Part.size(kResultBodyType)
42         if aPartSize == 0:
43             EventsAPI.Events_Error_send("No results in the active document")
44             return
45           
46         anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]     
47         for idx, anObject in enumerate(anObjList):
48             aResult = ModelAPI.modelAPI_Result(anObject)
49             aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
50             if not aBodyResult:
51                 continue
52             aShape = aBodyResult.shape()
53             aDump = aShape.getShapeStream()
54             # Load shape to SALOME Geom
55             aBrep = self.geompy.RestoreShape(aDump)
56             self.geompy.addToStudy(aBrep, "NewGeomShape_{0}".format(idx + 1))
57             self.geomObjects.append([aShape, aBrep])
58
59     def exportGroups(self):
60         # iterate all features to find groups
61         aFeaturesNum = self.Part.size("Features")
62         groupIndex = 0
63         for anIndex in range(0, aFeaturesNum):
64             aFeature = self.Part.object("Features", anIndex)
65             aSelectionList = aFeature.data().selectionList("group_list")
66             # if a group has been found
67             if aSelectionList:
68                 groupIndex = groupIndex + 1
69                 self.createGroupFromList(aSelectionList, "NewGeomGroup_{0}".format(groupIndex))
70                      
71     def createGroupFromList(self, theSelectionList, theGroupName):     
72         # iterate on all selected entities of the group
73         # and get the corresponding ID
74         aSelectionNum = theSelectionList.size()
75         Ids = []
76         for aSelIndex in range(0, aSelectionNum):
77             aSelection = theSelectionList.value(aSelIndex)
78             aSelectionContext = aSelection.context()
79             anID = aSelection.Id()
80             Ids.append(anID)
81             if aSelection.value().isVertex():
82                 groupType = "VERTEX"
83             elif aSelection.value().isEdge():
84                 groupType = "EDGE" 
85             elif aSelection.value().isFace():
86                 groupType = "FACE"
87             else:
88                 groupType = "SOLID"
89
90         # iterate on exported objects and check if the current
91         # group refers to this object
92         for obj in self.geomObjects: 
93             if aSelectionContext.shape().isEqual(obj[0]):
94                 aGroup = self.geompy.CreateGroup(obj[1], self.geompy.ShapeType[groupType])
95                 self.geompy.UnionIDs(aGroup,Ids)
96                 self.geompy.addToStudyInFather(obj[1], aGroup, theGroupName)
97           
98     def execute(self):
99         aSession = ModelAPI.ModelAPI_Session.get()
100         self.Part = aSession.activeDocument()
101         self.geomObjects = []
102         self.geompy = geomBuilder.New(salome.myStudy)
103        
104         # Export bodies and groups
105         self.exportBodies()
106         self.exportGroups()
107         pass