1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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 #include "GEOMImpl_IBaseIEOperations.hxx"
22 #include "GEOMImpl_IGroupOperations.hxx"
23 #include "GEOMImpl_IFieldOperations.hxx"
24 #include "GEOMImpl_IShapesOperations.hxx"
27 #include <NCollection_DataMap.hxx>
28 #include <TDF_ChildIDIterator.hxx>
29 #include <TNaming_NamedShape.hxx>
30 #include <TDataStd_Comment.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
34 typedef NCollection_DataMap< TCollection_ExtendedString, NCollection_List<TopoDS_Shape> >
35 DataMapOfStringListOfShape;
37 //=============================================================================
41 //=============================================================================
42 GEOMImpl_IBaseIEOperations::GEOMImpl_IBaseIEOperations(GEOM_Engine* theEngine, int theDocID)
43 : GEOM_IOperations(theEngine, theDocID)
45 myGroupOperations = new GEOMImpl_IGroupOperations( GetEngine(), GetDocID() );
46 myFieldOperations = new GEOMImpl_IFieldOperations( GetEngine(), GetDocID() );
47 myShapesOperations = new GEOMImpl_IShapesOperations( GetEngine(), GetDocID() );
50 //=============================================================================
54 //=============================================================================
55 GEOMImpl_IBaseIEOperations::~GEOMImpl_IBaseIEOperations()
57 delete myGroupOperations;
58 delete myFieldOperations;
59 delete myShapesOperations;
62 //=============================================================================
64 * This method creates material groups for an imported object.
65 * \param theObject the imported object.
67 //=============================================================================
68 void GEOMImpl_IBaseIEOperations::MakeMaterialGroups
69 (const Handle(GEOM_Object) &theObject,
70 const Handle(TColStd_HSequenceOfTransient) &theSeq)
72 TopoDS_Shape aResShape = theObject->GetValue();
74 if (aResShape.IsNull() == Standard_False) {
75 // Group shapes by material names.
76 Handle(GEOM_Function) aFunction = theObject->GetLastFunction();
77 DataMapOfStringListOfShape aMapMaterialShapes;
79 // check all named shapes using iterator
80 TDF_ChildIDIterator anIt (aFunction->GetNamingEntry(),
81 TNaming_NamedShape::GetID(), Standard_True);
83 for (; anIt.More(); anIt.Next()) {
84 Handle(TNaming_NamedShape) anAttr =
85 Handle(TNaming_NamedShape)::DownCast(anIt.Value());
87 if (anAttr.IsNull() == Standard_False) {
88 TDF_Label aLabel = anAttr->Label();
89 Handle(TDataStd_Comment) aComment;
91 if (aLabel.FindAttribute(TDataStd_Comment::GetID(), aComment)) {
92 TCollection_ExtendedString aMatName = aComment->Get();
93 TopoDS_Shape aShape = anAttr->Get();
95 if (aMapMaterialShapes.IsBound(aMatName) == Standard_False) {
96 NCollection_List<TopoDS_Shape> anEmptyList;
98 aMapMaterialShapes.Bind(aMatName, anEmptyList);
101 aMapMaterialShapes(aMatName).Append(aShape);
106 if (aMapMaterialShapes.IsEmpty() == Standard_False) {
108 TopAbs_ShapeEnum aType = aResShape.ShapeType();
110 DataMapOfStringListOfShape::Iterator aMapIter;
112 // Check each shape type.
113 for(i = aType; i <= TopAbs_VERTEX; i++) {
114 DataMapOfStringListOfShape::Iterator aMapIter(aMapMaterialShapes);
116 for (; aMapIter.More(); aMapIter.Next()) {
117 NCollection_List<TopoDS_Shape> &aShList = aMapIter.ChangeValue();
118 NCollection_List<TopoDS_Shape>::Iterator aShIter(aShList);
119 NCollection_List<TopoDS_Shape> aShListSameType;
121 while (aShIter.More()) {
122 const TopoDS_Shape &aShape = aShIter.Value();
124 if (i == aShape.ShapeType()) {
125 // Treat this element.
126 aShListSameType.Append(aShape);
127 aShList.Remove(aShIter);
129 // Go to the next element.
134 if (aShListSameType.IsEmpty() == Standard_False) {
135 // Construct a group.
136 Handle(GEOM_Object) aGroup =
137 MakeGroup(theObject, aMapIter.Key(), aShListSameType);
139 if (aGroup.IsNull() == Standard_False) {
140 theSeq->Append(aGroup);
150 //=============================================================================
152 * This method creates a group of shapes of certain type.
153 * \param theObject the imported object.
154 * \param theName the material name.
155 * \param theShapes the list of shapes to be added to this group.
156 * \return the created group.
158 //=============================================================================
159 Handle(GEOM_Object) GEOMImpl_IBaseIEOperations::MakeGroup
160 (const Handle(GEOM_Object) &theObject,
161 const TCollection_ExtendedString &theName,
162 const NCollection_List<TopoDS_Shape> &theShapes)
164 Handle(GEOM_Object) aGroup;
165 TopTools_IndexedMapOfShape anIndices;
166 Handle(TColStd_HSequenceOfInteger) aSeqIDs = new TColStd_HSequenceOfInteger;
167 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapes);
169 TopExp::MapShapes(theObject->GetValue(), anIndices);
171 // Compose shape IDs.
172 for (; anIter.More(); anIter.Next()) {
173 const TopoDS_Shape &aShape = anIter.Value();
174 const Standard_Integer anIndex = anIndices.FindIndex(aShape);
177 aSeqIDs->Append(anIndex);
181 if (aSeqIDs->IsEmpty() == Standard_False) {
183 const TopAbs_ShapeEnum aType = theShapes.First().ShapeType();
185 aGroup = myGroupOperations->CreateGroup(theObject, aType);
187 if (aGroup.IsNull() == Standard_False) {
188 aGroup->GetLastFunction()->SetDescription("");
189 myGroupOperations->UnionIDs(aGroup, aSeqIDs);
190 aGroup->GetLastFunction()->SetDescription("");
192 // Compose the group name.
193 TCollection_AsciiString aGroupName(theName);
197 aGroupName += "_VERTEX";
200 aGroupName += "_EDGE";
203 aGroupName += "_WIRE";
206 aGroupName += "_FACE";
209 aGroupName += "_SHELL";
212 aGroupName += "_SOLID";
214 case TopAbs_COMPSOLID:
215 aGroupName += "_COMPSOLID";
217 case TopAbs_COMPOUND:
218 aGroupName += "_COMPOUND";
221 aGroupName += "_SHAPE";
225 aGroup->SetName(aGroupName.ToCString());