2 # ExportFeature class definition
3 # Copyright (C) 2014-20xx CEA/DEN, EDF R&D
10 from salome.geom import geomBuilder
12 def getObjectIndex(theName):
13 aStudy = salome.myStudy
15 aObj = aStudy.FindObjectByName(theName, "GEOM")
18 aName = theName + '_' + str(aId)
19 aObj = aStudy.FindObjectByName(aName, "GEOM")
23 # Feature to export all shapes and groups into the GEOM module
24 class ExportFeature(ModelAPI.ModelAPI_Feature):
28 ModelAPI.ModelAPI_Feature.__init__(self)
31 ## Export kind. Static.
35 ## Returns the kind of a feature.
37 return ExportFeature.ID()
39 ## This feature is action: has no property pannel and executes immideately.
43 ## This feature has no attributes, as it is action.
44 def initAttributes(self):
48 def exportBodies(self):
50 kResultBodyType = "Bodies"
51 aPartSize = self.Part.size(kResultBodyType)
53 EventsAPI.Events_Error_send("No results in the active document")
56 anObjList = [self.Part.object(kResultBodyType, idx) for idx in xrange(aPartSize)]
57 aShapesList = GeomAlgoAPI.ShapeList()
59 for idx, anObject in enumerate(anObjList):
60 aResult = ModelAPI.modelAPI_Result(anObject)
61 aBodyResult = ModelAPI.modelAPI_ResultBody(aResult)
64 aShape = aBodyResult.shape()
65 if aShape is not None and not aShape.isNull():
66 aShapesList.append(aShape)
67 if len(aShapesList) == 1:
68 aName = aBodyResult.data().name()
70 # issue 1045: create compound if there are more than one shape
71 if len(aShapesList) > 1:
72 self.shape = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.compound(aShapesList)
73 aName = "ShaperResults"
74 elif len(aShapesList) == 1:
75 self.shape = aShapesList[0]
77 # so, only one shape is always in the result
78 aDump = self.shape.getShapeStream()
79 # Load shape to SALOME Geom
80 aBrep = self.geompy.RestoreShape(aDump)
83 aId = getObjectIndex(aName)
85 aName = aName + '_' + str(aId)
87 self.geompy.addToStudy(aBrep, aName)
91 def exportGroups(self):
92 # iterate all features to find groups
93 aFeaturesNum = self.Part.size("Features")
95 for anIndex in range(0, aFeaturesNum):
96 aFeature = self.Part.object("Features", anIndex)
97 aSelectionList = aFeature.data().selectionList("group_list")
98 # if a group has been found
100 aFeature = ModelAPI.objectToFeature(aFeature)
101 if aFeature.firstResult() is not None:
102 aName = aFeature.firstResult().data().name()
103 groupIndex = groupIndex + 1
104 self.createGroupFromList(aSelectionList, aName)
106 ## Creates a group by given list of selected objects and the name
107 # @param theSelectionList: list of selected objects
108 # @param theGroupName: name of the group to create
109 def createGroupFromList(self, theSelectionList, theGroupName):
110 # iterate on all selected entities of the group
111 # and get the corresponding ID
112 aSelectionNum = theSelectionList.size()
114 for aSelIndex in range(0, aSelectionNum):
115 aSelection = theSelectionList.value(aSelIndex)
116 anID = GeomAlgoAPI.GeomAlgoAPI_CompoundBuilder.id(self.shape, aSelection.value())
118 if aSelection.value().isVertex():
120 elif aSelection.value().isEdge():
122 elif aSelection.value().isFace():
127 aGroup = self.geompy.CreateGroup(self.brep, self.geompy.ShapeType[groupType])
128 self.geompy.UnionIDs(aGroup,Ids)
129 self.geompy.addToStudyInFather(self.brep, aGroup, theGroupName)
131 ## Exports all shapes and groups into the GEOM module.
133 aSession = ModelAPI.ModelAPI_Session.get()
134 ## Get active document
135 self.Part = aSession.activeDocument()
136 ## List of objects created in the old geom for later use
137 self.geomObjects = []
139 self.geompy = geomBuilder.New(salome.myStudy)
141 # Export bodies and groups