2 # ExportFeature class definition
3 # Copyright (C) 2014-20xx CEA/DEN, EDF R&D
11 from salome.geom import geomBuilder
13 def getObjectIndex(theName):
14 aStudy = salome.myStudy
16 aObj = aStudy.FindObjectByName(theName, "GEOM")
19 aName = theName + '_' + str(aId)
20 aObj = aStudy.FindObjectByName(aName, "GEOM")
24 # Feature to export all shapes and groups into the GEOM module
25 class ExportFeature(ModelAPI.ModelAPI_Feature):
29 ModelAPI.ModelAPI_Feature.__init__(self)
30 ## Shape that will be exported (the compound if there are several exported bodies)
32 ## BRep representation of the exported shape (a stream that will be sent to GEOM and converted to GEOM object)
36 ## Export kind. Static.
40 ## Returns the kind of a feature.
42 return ExportFeature.ID()
44 ## This feature is action: has no property pannel and executes immideately.
48 ## This feature has no attributes, as it is action.
49 def initAttributes(self):
53 def exportBodies(self):
55 kResultBodyType = "Bodies"
56 aPartSize = self.Part.size(kResultBodyType)
58 EventsAPI.Events_InfoMessage("ExportFeature","No results in the active document").send()
61 anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]
62 aShapesList = GeomAlgoAPI.ShapeList()
64 for idx, anObject in enumerate(anObjList):
65 aResult = ModelAPI.modelAPI_Result(anObject)
66 aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
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()
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]
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)
88 aId = getObjectIndex(aName)
90 aName = aName + '_' + str(aId)
92 self.geompy.addToStudy(aBrep, aName)
96 def exportGroups(self):
97 # iterate all features to find groups
98 aFeaturesNum = self.Part.size("Features")
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
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)
111 ## Returns a type of the shape in the old GEOM representation
112 def shapeType(self, shape):
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()
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():
139 anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, aSelection.value())
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)
145 anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
148 groupType = self.shapeType(anExp.current())
152 groupType = self.shapeType(aSelection.value())
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)
159 ## Exports all fields
160 def exportFields(self):
161 # iterate all features to find fields
162 aFeaturesNum = self.Part.size("Features")
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
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)
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":
184 if selType== "solid":
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
194 return GeomAPI.GeomAPI_Shape.EDGE
196 return GeomAPI.GeomAPI_Shape.FACE
197 if selType== "solid":
198 return GeomAPI.GeomAPI_Shape.SOLID
199 return GeomAPI.GeomAPI_Shape.SHAPE
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")
210 # set component names
211 aComps = theField.stringArray("components_names")
213 aCompNum = aComps.size()
214 for aCompIndex in range(0, aCompNum):
215 aCompNames.append(aComps.value(aCompIndex))
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)
223 # set default values to all not filled sub-shapes (fields in GEOM support only full set of subs)
225 anExp = GeomAPI.GeomAPI_ShapeExplorer(self.shape, self.geomAPISelectionDim(aSelection.selectionType()))
227 anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
229 Ids[anID]=anExp.current()
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
240 # values here are in the same order as in field
241 listOfValues = Ids.items()
244 aStepsNum = aTables.tables()
245 for aStepIndex in range(0, aStepsNum):
246 aStamp = theField.intArray("stamps").value(aStepIndex)
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
260 elif aTables.type() == 1: # int
262 elif aTables.type() == 2: # double
265 aResField.addStep(aStepIndex + 1, aStamp, aValues)
267 ## Exports all shapes and groups into the GEOM module.
269 aSession = ModelAPI.ModelAPI_Session.get()
270 ## Get active document
271 self.Part = aSession.activeDocument()
272 ## List of objects created in the old geom for later use
273 self.geomObjects = []
275 salome.salome_init(0,1)
276 self.geompy = geomBuilder.New(salome.myStudy)
278 # Export bodies and groups