]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConnectorPlugin/ConnectorPlugin_ExportFeature.py
Salome HOME
7902a8dddcc86b6895a943d8d2c79abc3e0e4013
[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             # chcking of concealment removed because of #1799, remark #13 "aContext.isConcealed()"
136             if aContext is None or aContext.isDisabled():
137                 continue
138
139             anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, aSelection.value())
140             if anID == 0:
141                 #it may be a compound of objects if movement of the group to the end
142                 # splits the original element to several (issue 1146)
143                 anExp = GeomAPI.GeomAPI_ShapeExplorer(aSelection.value(), GeomAPI.GeomAPI_Shape.SHAPE)
144                 while anExp.more():
145                     anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
146                     if anID != 0:
147                         Ids.append(anID)
148                         groupType = self.shapeType(anExp.current())
149                     anExp.next()
150             else:
151                 Ids.append(anID)
152                 groupType = self.shapeType(aSelection.value())
153
154         if len(Ids) <> 0:
155           aGroup = self.geompy.CreateGroup(self.brep, self.geompy.ShapeType[groupType])
156           self.geompy.UnionIDs(aGroup,Ids)
157           self.geompy.addToStudyInFather(self.brep, aGroup, theGroupName)
158
159     ## Exports all fields
160     def exportFields(self):
161         # iterate all features to find fields
162         aFeaturesNum = self.Part.size("Features")
163         fieldIndex = 0
164         for anIndex in range(0, aFeaturesNum):
165             aFeature = self.Part.object("Features", anIndex)
166             aSelectionList = aFeature.data().selectionList("selected")
167             # if a field has been found
168             if aSelectionList:
169                 aFeature = ModelAPI.objectToFeature(aFeature)
170                 if aFeature.firstResult() is not None:
171                   aName = aFeature.firstResult().data().name()
172                 fieldIndex = fieldIndex + 1
173                 self.createFieldFromFeature(aFeature, aName)
174
175     ## Returns a type of the shape type in the old GEOM representation
176     def selectionDim(self, theSelectionType):
177         selType=theSelectionType.lower() # more or less independed approach
178         if selType== "vertex":
179             return 0
180         if selType== "edge":
181             return 1
182         if selType== "face":
183             return 2
184         if selType== "solid":
185             return 3
186         return -1
187
188     ## Returns a type of the shape type in the GeomAPI_Shape representation
189     def geomAPISelectionDim(self, theSelectionType):
190         selType=theSelectionType.lower() # more or less independed approach
191         if selType== "vertex":
192             return GeomAPI.GeomAPI_Shape.VERTEX
193         if selType== "edge":
194             return GeomAPI.GeomAPI_Shape.EDGE
195         if selType== "face":
196             return GeomAPI.GeomAPI_Shape.FACE
197         if selType== "solid":
198             return GeomAPI.GeomAPI_Shape.SOLID
199         return GeomAPI.GeomAPI_Shape.SHAPE
200
201     ## Creates a field by the field feature and the name
202     #  @param theField: the field feature
203     #  @param theFieldName: name of the field to create
204     def createFieldFromFeature(self, theField, theFieldName):
205         # iterate on all selected entities of the field
206         # and get the corresponding ID
207         aTables = theField.tables("values")
208         aSelection = theField.selectionList("selected")
209
210         # set component names
211         aComps = theField.stringArray("components_names")
212         aCompNames = []
213         aCompNum = aComps.size()
214         for aCompIndex in range(0, aCompNum):
215           aCompNames.append(aComps.value(aCompIndex))
216
217         #if len(Ids) <> 0:
218         aDim = self.selectionDim(aSelection.selectionType())
219         aResField = self.geompy.CreateField(self.brep, theFieldName, aTables.type(), aDim, aCompNames)
220         #self.geompy.UnionIDs(theField,Ids)
221         self.geompy.addToStudyInFather(self.brep, aResField, theFieldName)
222
223         # set default values to all not filled sub-shapes (fields in GEOM support only full set of subs)
224         Ids={}
225         anExp = GeomAPI.GeomAPI_ShapeExplorer(self.shape, self.geomAPISelectionDim(aSelection.selectionType()))
226         while anExp.more():
227           anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
228           if anID != 0:
229             Ids[anID]=anExp.current()
230           anExp.next()
231
232         SelectedIds={}
233         for aSelIndex in range(aSelection.size()):
234           selShape = aSelection.value(aSelIndex).value()
235           # searching for this shape in Ids
236           for a in Ids.items():
237             if (a[1].isSame(selShape)):
238               SelectedIds[a[0]] = aSelIndex
239
240         # values here are in the same order as in field
241         listOfValues = Ids.items()
242         listOfValues.sort()
243         # set steps
244         aStepsNum = aTables.tables()
245         for aStepIndex in range(0, aStepsNum):
246           aStamp = theField.intArray("stamps").value(aStepIndex)
247           aValues = []
248           for aValId in listOfValues:
249             aRow = 0 # default value if not from selection
250             if SelectedIds.has_key(aValId[0]): # take the value from the table
251               aRow = SelectedIds[aValId[0]] + 1 # plus one to avoid default string
252             aCols = aTables.columns()
253             for aCol in range(0, aCols):
254               aVal = aTables.valueStr(aRow, aCol, aStepIndex)
255               if aTables.type() == 0: # bool
256                 if aVal == "True":
257                   aVal = True
258                 else:
259                   aVal = False
260               elif aTables.type() == 1: # int
261                 aVal = int(aVal)
262               elif aTables.type() == 2: # double
263                 aVal = float(aVal)
264               aValues.append(aVal)
265           aStep = aResField.addStep(aStepIndex + 1, aStamp, aValues)
266           if aStep:
267             self.geompy.addToStudyInFather( aResField, aStep, aStep.GetName() )
268
269     ## Exports all shapes and groups into the GEOM module.
270     def execute(self):
271         aSession = ModelAPI.ModelAPI_Session.get()
272         ## Get active document
273         self.Part = aSession.activeDocument()
274         ## List of objects created in the old geom for later use
275         self.geomObjects = []
276         ## geomBuilder tool
277         salome.salome_init(0,1)
278         self.geompy = geomBuilder.New(salome.myStudy)
279
280         # Export bodies and groups
281         self.exportBodies()
282         self.exportGroups()
283         self.exportFields()
284         pass