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)
32 ## Export kind. Static.
36 ## Returns the kind of a feature.
38 return ExportFeature.ID()
40 ## This feature is action: has no property pannel and executes immideately.
44 ## This feature has no attributes, as it is action.
45 def initAttributes(self):
49 def exportBodies(self):
51 kResultBodyType = "Bodies"
52 aPartSize = self.Part.size(kResultBodyType)
54 EventsAPI.Events_Error_send("No results in the active document")
57 anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]
58 aShapesList = GeomAlgoAPI.ShapeList()
60 for idx, anObject in enumerate(anObjList):
61 aResult = ModelAPI.modelAPI_Result(anObject)
62 aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
65 aShape = aBodyResult.shape()
66 if aShape is not None and not aShape.isNull():
67 aShapesList.append(aShape)
68 if len(aShapesList) == 1:
69 aName = aBodyResult.data().name()
71 # issue 1045: create compound if there are more than one shape
72 if len(aShapesList) > 1:
73 self.shape = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.compound(aShapesList)
74 aName = "ShaperResults"
75 elif len(aShapesList) == 1:
76 self.shape = aShapesList[0]
78 # so, only one shape is always in the result
79 aDump = self.shape.getShapeStream()
80 # Load shape to SALOME Geom
81 aBrep = self.geompy.RestoreShape(aDump)
84 aId = getObjectIndex(aName)
86 aName = aName + '_' + str(aId)
88 self.geompy.addToStudy(aBrep, aName)
92 def exportGroups(self):
93 # iterate all features to find groups
94 aFeaturesNum = self.Part.size("Features")
96 for anIndex in range(0, aFeaturesNum):
97 aFeature = self.Part.object("Features", anIndex)
98 aSelectionList = aFeature.data().selectionList("group_list")
99 # if a group has been found
101 aFeature = ModelAPI.objectToFeature(aFeature)
102 if aFeature.firstResult() is not None:
103 aName = aFeature.firstResult().data().name()
104 groupIndex = groupIndex + 1
105 self.createGroupFromList(aSelectionList, aName)
107 ## Returns a type of the shape in the old GEOM representation
108 def shapeType(self, shape):
118 ## Creates a group by given list of selected objects and the name
119 # @param theSelectionList: list of selected objects
120 # @param theGroupName: name of the group to create
121 def createGroupFromList(self, theSelectionList, theGroupName):
122 # iterate on all selected entities of the group
123 # and get the corresponding ID
124 aSelectionNum = theSelectionList.size()
127 for aSelIndex in range(0, aSelectionNum):
128 aSelection = theSelectionList.value(aSelIndex)
129 # issue 1326: bodies that are already concealed did not exported, so groups should not be invalid
130 aContext = ModelAPI.modelAPI_Result(aSelection.context())
131 if aContext is None or aContext.isConcealed() or aContext.isDisabled():
134 anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, aSelection.value())
136 #it may be a compound of objects if movement of the group to the end
137 # splits the original element to several (issue 1146)
138 anExp = GeomAPI.GeomAPI_ShapeExplorer(aSelection.value(), GeomAPI.GeomAPI_Shape.SHAPE)
140 anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, anExp.current())
143 groupType = self.shapeType(anExp.current())
147 groupType = self.shapeType(aSelection.value())
150 aGroup = self.geompy.CreateGroup(self.brep, self.geompy.ShapeType[groupType])
151 self.geompy.UnionIDs(aGroup,Ids)
152 self.geompy.addToStudyInFather(self.brep, aGroup, theGroupName)
154 ## Exports all shapes and groups into the GEOM module.
156 aSession = ModelAPI.ModelAPI_Session.get()
157 ## Get active document
158 self.Part = aSession.activeDocument()
159 ## List of objects created in the old geom for later use
160 self.geomObjects = []
162 self.geompy = geomBuilder.New(salome.myStudy)
164 # Export bodies and groups