]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConnectorPlugin/ConnectorPlugin_ExportFeature.py
Salome HOME
Issue #1865 : finalization of export of fields to GEOM
[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         ## Shape that will be exported (the compound if there are several exported bodies)
31         self.shape = None
32         ## BRep representation of the exported shape (a stream that will be sent to GEOM and converted to GEOM object)
33         self.brep = None
34
35     @staticmethod
36     ## Export kind. Static.
37     def ID():
38         return "ExportToGEOM"
39
40     ## Returns the kind of a feature.
41     def getKind(self):
42         return ExportFeature.ID()
43
44     ## This feature is action: has no property pannel and executes immideately.
45     def isAction(self):
46         return True
47
48     ## This feature has no attributes, as it is action.
49     def initAttributes(self):
50       pass
51
52     ## Exports all bodies
53     def exportBodies(self):
54         global ShapeIndex
55         kResultBodyType = "Bodies"
56         aPartSize = self.Part.size(kResultBodyType)
57         if aPartSize == 0:
58             EventsAPI.Events_InfoMessage("ExportFeature","No results in the active document").send()
59             return
60
61         anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]
62         aShapesList = GeomAlgoAPI.ShapeList()
63         aName = ""
64         for idx, anObject in enumerate(anObjList):
65             aResult = ModelAPI.modelAPI_Result(anObject)
66             aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
67             if not aBodyResult:
68                 continue
69             aShape = aBodyResult.shape()
70             if aShape is not None and not aShape.isNull():
71               aShapesList.append(aShape)
72               if len(aShapesList) == 1:
73                 aName = aBodyResult.data().name()
74
75         # issue 1045: create compound if there are more than one shape
76         if len(aShapesList) > 1:
77           self.shape = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.compound(aShapesList)
78           aName = "ShaperResults"
79         elif len(aShapesList) == 1:
80           self.shape = aShapesList[0]
81
82         # so, only one shape is always in the result
83         aDump = self.shape.getShapeStream()
84         # Load shape to SALOME Geom
85         aBrep = self.geompy.RestoreShape(aDump)
86
87         # Make unique name
88         aId = getObjectIndex(aName)
89         if aId != 0:
90             aName = aName + '_' + str(aId)
91
92         self.geompy.addToStudy(aBrep, aName)
93         self.brep = aBrep
94
95     ## Exports all groups
96     def exportGroups(self):
97         # iterate all features to find groups
98         aFeaturesNum = self.Part.size("Features")
99         groupIndex = 0
100         for anIndex in range(0, aFeaturesNum):
101             aFeature = self.Part.object("Features", anIndex)
102             aSelectionList = aFeature.data().selectionList("group_list")
103             # if a group has been found
104             if aSelectionList:
105                 aFeature = ModelAPI.objectToFeature(aFeature)
106                 if aFeature.firstResult() is not None:
107                   aName = aFeature.firstResult().data().name()
108                 groupIndex = groupIndex + 1
109                 self.createGroupFromList(aSelectionList, aName)
110
111     ## Returns a type of the shape in the old GEOM representation
112     def shapeType(self, shape):
113         if shape.isVertex():
114             return "VERTEX"
115         elif shape.isEdge():
116             return "EDGE"
117         elif shape.isFace():
118             return "FACE"
119
120         return "SOLID"
121
122     ## Creates a group by given list of selected objects and the name
123     #  @param theSelectionList: list of selected objects
124     #  @param theGroupName: name of the group to create
125     def createGroupFromList(self, theSelectionList, theGroupName):
126         # iterate on all selected entities of the group
127         # and get the corresponding ID
128         aSelectionNum = theSelectionList.size()
129         Ids = []
130         groupType = ""
131         for aSelIndex in range(0, aSelectionNum):
132             aSelection = theSelectionList.value(aSelIndex)
133             # issue 1326: bodies that are already concealed did not exported, so groups should not be invalid
134             aContext =  ModelAPI.modelAPI_Result(aSelection.context())
135             if aContext is None or aContext.isConcealed() or aContext.isDisabled():
136                 continue
137
138             anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, aSelection.value())
139             if anID == 0:
140                 #it may be a compound of objects if movement of the group to the end
141                 # splits the original element to several (issue 1146)
142                 anExp = GeomAPI.GeomAPI_ShapeExplorer(aSelection.value(), GeomAPI.GeomAPI_Shape.SHAPE)
143                 while anExp.more():
144                     anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
145                     if anID != 0:
146                         Ids.append(anID)
147                         groupType = self.shapeType(anExp.current())
148                     anExp.next()
149             else:
150                 Ids.append(anID)
151                 groupType = self.shapeType(aSelection.value())
152
153         if len(Ids) <> 0:
154           aGroup = self.geompy.CreateGroup(self.brep, self.geompy.ShapeType[groupType])
155           self.geompy.UnionIDs(aGroup,Ids)
156           self.geompy.addToStudyInFather(self.brep, aGroup, theGroupName)
157
158     ## Exports all fields
159     def exportFields(self):
160         # iterate all features to find fields
161         aFeaturesNum = self.Part.size("Features")
162         fieldIndex = 0
163         for anIndex in range(0, aFeaturesNum):
164             aFeature = self.Part.object("Features", anIndex)
165             aSelectionList = aFeature.data().selectionList("selected")
166             # if a field has been found
167             if aSelectionList:
168                 aFeature = ModelAPI.objectToFeature(aFeature)
169                 if aFeature.firstResult() is not None:
170                   aName = aFeature.firstResult().data().name()
171                 fieldIndex = fieldIndex + 1
172                 self.createFieldFromFeature(aFeature, aName)
173
174     ## Returns a type of the shape type in the old GEOM representation
175     def selectionDim(self, theSelectionType):
176         if theSelectionType == "vertex":
177             return 0
178         if theSelectionType == "edge":
179             return 1
180         if theSelectionType == "face":
181             return 2
182         if theSelectionType == "solid":
183             return 3
184         return -1
185
186     ## Creates a field by the field feature and the name
187     #  @param theField: the field feature
188     #  @param theFieldName: name of the field to create
189     def createFieldFromFeature(self, theField, theFieldName):
190         # iterate on all selected entities of the field
191         # and get the corresponding ID
192         aTables = theField.tables("values")
193         aSelection = theField.selectionList("selected")
194
195         # set component names
196         aComps = theField.stringArray("components_names")
197         aCompNames = []
198         aCompNum = aComps.size()
199         for aCompIndex in range(0, aCompNum):
200           aCompNames.append(aComps.value(aCompIndex))
201
202         #if len(Ids) <> 0:
203         aDim = self.selectionDim(aSelection.selectionType())
204         aResField = self.geompy.CreateField(self.brep, theFieldName, aTables.type(), aDim, aCompNames)
205         #self.geompy.UnionIDs(theField,Ids)
206         self.geompy.addToStudyInFather(self.brep, aResField, theFieldName)
207
208         # set steps
209         aStepsNum = aTables.tables()
210         for aStepIndex in range(0, aStepsNum):
211           aStamp = theField.intArray("stamps").value(aStepIndex)
212           aValues = []
213           aRows = aTables.rows()
214           aCols = aTables.columns()
215           for aRow in range(1, aRows): # no defaults
216             for aCol in range(0, aCols):
217               aVal = aTables.valueStr(aRow, aCol, aStepIndex)
218               if aTables.type() == 0: # bool
219                 if aVal == "True":
220                   aVal = True
221                 else:
222                   aVal = False
223               elif aTables.type() == 1: # int
224                 aVal = int(aVal)
225               elif aTables.type() == 2: # double
226                 aVal = float(aVal)
227               aValues.append(aVal)
228           aResField.addStep(aStepIndex + 1, aStamp, aValues)
229
230     ## Exports all shapes and groups into the GEOM module.
231     def execute(self):
232         aSession = ModelAPI.ModelAPI_Session.get()
233         ## Get active document
234         self.Part = aSession.activeDocument()
235         ## List of objects created in the old geom for later use
236         self.geomObjects = []
237         ## geomBuilder tool
238         salome.salome_init(0,1)
239         self.geompy = geomBuilder.New(salome.myStudy)
240
241         # Export bodies and groups
242         self.exportBodies()
243         self.exportGroups()
244         self.exportFields()
245         pass