1 # Copyright (C) 2014-2022 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # ExportFeature class definition
30 from salome.shaper import model
32 import SHAPERSTUDY_ORB
33 import SHAPERSTUDY_utils
36 # Feature to export all shapes and groups into the GEOM module
37 class PublishToStudyFeature(ModelAPI.ModelAPI_Feature):
41 ModelAPI.ModelAPI_Feature.__init__(self)
45 ## Export kind. Static.
47 return "PublishToStudy"
49 ## Returns the kind of a feature.
51 return PublishToStudyFeature.ID()
53 ## This feature is action: has no property panel and executes immediately.
57 ## This feature has no attributes, as it is action.
58 def initAttributes(self):
61 ## Exports all shapes and groups into the GEOM module.
63 aSession = ModelAPI.ModelAPI_Session.get()
64 aPartSet = aSession.moduleDocument()
65 # check that the PartSet document current feature is the last to avoid problems with all
67 if aPartSet.size(model.ModelAPI_Feature_group()) > 0:
68 aLastFeature = ModelAPI.objectToFeature(aPartSet.object(model.ModelAPI_Feature_group(), aPartSet.size(model.ModelAPI_Feature_group()) - 1))
69 aCurrentFeature = aPartSet.currentFeature(True)
70 if aLastFeature.data().featureId() != aCurrentFeature.data().featureId():
71 EventsAPI.Events_InfoMessage("PublishToStudy", "Not all PartSet parts are up-to-date, nothing is published. Please, make the last PartSet feature as current.", self).send()
73 # find a shaper-study component
75 aComponent = SHAPERSTUDY_utils.findOrCreateComponent()
76 anEngine = SHAPERSTUDY_utils.getEngine()
77 # collect all processed internal entries to break the link of unprocessed later
80 # iterate all parts and all results to publish them in SHAPER_STUDY
81 for aPartId in range(aPartSet.size(model.ModelAPI_ResultPart_group())):
82 aPartObject = aPartSet.object(model.ModelAPI_ResultPart_group(), aPartId)
83 aPartRes = ModelAPI.modelAPI_ResultPart(ModelAPI.modelAPI_Result(aPartObject))
84 aPartDoc = aPartRes.partDoc()
85 if aPartDoc is None and aPartObject is not None:
86 EventsAPI.Events_InfoMessage("PublishToStudy", "To publish to SHAPER-STUDY, activate Parts in SHAPER", self).send()
88 aPartFeatureId = aPartSet.feature(aPartRes.original()).data().featureId()
89 # Collects all features of exported results to find results of the same features and extend id.
90 # Map from feature index to index of result. If index is zero (initial), no surrfix to entry is added.
92 for aResId in range(aPartDoc.size(model.ModelAPI_ResultBody_group())):
93 aResObject = aPartDoc.object(model.ModelAPI_ResultBody_group(), aResId)
94 aRes = model.objectToResult(aResObject)
96 if aRes.hasTexture() is True:
98 aResFeatureId = str(aPartDoc.feature(aRes).data().featureId())
99 if aResFeatureId in aFeaturesIndices:
100 aFeaturesIndices[aResFeatureId] += 1
101 aResFeatureId += ":" + str(aFeaturesIndices[aResFeatureId])
103 aFeaturesIndices[aResFeatureId] = 0
104 aSSEntry = str(aPartFeatureId) + ":" + aResFeatureId
105 aSShape = anEngine.FindOrCreateShape(aSSEntry)
106 aSShape.SetShapeByStream(aRes.shape().getShapeStream(False))
107 if not aSShape.GetSO(): # publish in case it is a new shape
108 anEngine.AddInStudy(aSShape, aRes.data().name(), None)
109 else: # restore a red reference if it was deleted
110 aDone, aSO2 = aSShape.GetSO().FindSubObject(1)
112 aDone, aRef = aSO2.ReferencedObject()
114 aBuilder = SHAPERSTUDY_utils.getStudy().NewBuilder()
115 aBuilder.Addreference(aSO2, aSShape.GetSO())
116 allProcessed.append(aSSEntry)
118 self.processGroups(aRes, anEngine, aPartFeatureId, aSShape, False)
120 self.processGroups(aRes, anEngine, aPartFeatureId, aSShape, True)
122 # process all SHAPER-STUDY shapes to find dead
123 aSOIter = SHAPERSTUDY_utils.getStudy().NewChildIterator(aComponent)
124 while aSOIter.More():
125 aSO = aSOIter.Value()
126 aSOIter.Next() # here because there is continue inside the loop
129 anObj = salome.orb.string_to_object(anIOR)
130 if isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object):
131 anEntry = anObj.GetEntry()
132 if len(anEntry) == 0:
134 elif anEntry not in allProcessed: # found a removed shape: make it dead for the moment
135 # remove the reference - red node
136 aRes, aSO2 = aSO.FindSubObject(1)
138 aRes, aRef = aSO2.ReferencedObject()
140 aBuilder = SHAPERSTUDY_utils.getStudy().NewBuilder()
141 aBuilder.RemoveReference(aSO2)
142 # if the object is not marked as dead, mark it (#3201) to make all entries unique
143 aDeadEntry = anObj.GetEntry()
144 if not aDeadEntry.startswith("dead"):
146 anObj.SetEntry("dead0" + str(anIndex) + "_" + aDeadEntry)
149 aGrSOIter = SHAPERSTUDY_utils.getStudy().NewChildIterator(aSO)
150 while aGrSOIter.More():
151 aGroupSO = aGrSOIter.Value()
153 anIOR = aGroupSO.GetIOR()
155 aGroup = salome.orb.string_to_object(anIOR)
156 if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group) or \
157 isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field):
158 if not aGroup.GetEntry().startswith("dead"):
159 aDeadGroupEntry = "dead0" + str(anIndex) + "_" + aGroup.GetEntry()
160 aGroup.SetEntry(aDeadGroupEntry)
162 # Part of the "execute" method: processes the Groups of theRes result publication.
163 # If theFields is true, the same is performed for Fields.
164 def processGroups(self, theRes, theEngine, thePartFeatureId, theStudyShape, theFields):
165 allGroupsProcessed = []
167 aRefGroups = ModelAPI.referencedFeatures(theRes, "Field", True)
169 aRefGroups = ModelAPI.referencedFeatures(theRes, "Group", True)
170 for aRef in aRefGroups:
173 aResShape = theRes.shape()
175 aSelList = aRef.selectionList("selected")
177 aSelList = aRef.selectionList("group_list")
178 aSelType = GeomAPI_Shape.shapeTypeByStr(aSelList.selectionType())
179 for aGroupRes in aRef.results():
180 aShape = aGroupRes.shape()
181 anExplorer = GeomAPI_ShapeExplorer(aShape, aSelType)
182 while anExplorer.more():
183 anId = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(aResShape, anExplorer.current())
184 if anId > 0 and not anId in aGroupHasIndex:
185 aGroupIndices.append(anId)
186 aGroupHasIndex[anId] = 0
188 if len(aGroupIndices): # create group
189 aGroupFeatureId = aRef.data().featureId()
191 aFieldOp = theEngine.GetIFieldOperations()
192 aGroupEntry = "field" + str(thePartFeatureId) + ":" + str(aGroupFeatureId)
193 aGroup = aFieldOp.FindField(theStudyShape, aGroupEntry)
195 aGroupOp = theEngine.GetIGroupOperations()
196 aGroupEntry = "group" + str(thePartFeatureId) + ":" + str(aGroupFeatureId)
197 aGroup = aGroupOp.FindGroup(theStudyShape, aGroupEntry)
198 if not aGroup: # create a new
200 aGroup = aFieldOp.CreateFieldByType(theStudyShape, aSelType)
202 aGroup = aGroupOp.CreateGroup(theStudyShape, aSelType)
203 aGroup.SetEntry(aGroupEntry)
204 theEngine.AddInStudy(aGroup, aRef.firstResult().data().name(), theStudyShape)
205 aGroup.SetSelection(aGroupIndices)
207 self.fillField(aGroup, aRef, theEngine, aGroupIndices)
208 # a group takes shape from the main result
209 #aGroup.SetShapeByStream(aRef.firstResult().shape().getShapeStream(False)) # group shape
210 allGroupsProcessed.append(aGroupEntry)
211 # check all existing groups: if some does not processed, remove it from the tree
212 aSOIter = SHAPERSTUDY_utils.getStudy().NewChildIterator(theStudyShape.GetSO())
213 while aSOIter.More():
214 aSO = aSOIter.Value()
217 anObj = salome.orb.string_to_object(anIOR)
218 if (theFields and isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Field)) or \
219 (not theFields and type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Group):
220 anEntry = anObj.GetEntry()
221 if anEntry not in allGroupsProcessed: # found a removed group => remove
222 aBuilder = SHAPERSTUDY_utils.getStudy().NewBuilder()
223 aBuilder.RemoveObject(anObj.GetSO())
226 # Part of the "execute" method: theFiled fields filling.
227 def fillField(self, theField, theFeature, theEngine, theSelectionIndices):
228 aTables = theFeature.tables("values")
229 aValType = aTables.type() # type of the values
230 aValTypeToSet = aValType
231 if aValType == 3: # strings do not supported by SMESH, so, make them empty boolean table
233 theField.SetValuesType(aValTypeToSet)
234 aNumSteps = aTables.tables() # number of steps is number of tables
236 for aVal in range(aNumSteps):
237 aSteps.append(aVal + 1)
238 theField.SetSteps(aSteps)
240 aCompNamesAttr = theFeature.stringArray("components_names")
241 for anIndex in range(aCompNamesAttr.size()):
242 aCompNames.append(aCompNamesAttr.value(anIndex))
243 theField.SetComponents(aCompNames)
244 # prepare the sub-shapes indices: all values for all sub-shapes must be defined
245 aShapeOp = theEngine.GetIShapesOperations()
246 allIndices = aShapeOp.GetAllSubShapesIDs(theField.GetShape(), theField.GetSelectionType(), False)
248 theField.ClearFieldSteps()
249 for aVal in range(aNumSteps):
251 for aCol in range(aTables.columns()):
252 #for aRow in range(aTables.rows()):
253 for anIndex in allIndices:
254 if anIndex in theSelectionIndices:
255 aRow = theSelectionIndices.index(anIndex) + 1 # starting from the first line
257 aRow = 0 # default value
258 aStrVal = aTables.valueStr(aRow, aCol, aVal)
259 if aValType == 0: # boolean
260 if aStrVal == "True":
264 elif aValType == 1: # int
265 aVals.append(int(aStrVal))
266 elif aValType == 2: # double
267 aVals.append(float(aStrVal))
268 theField.AddFieldStep(theFeature.intArray("stamps").value(aVal), aVal + 1, aVals)