1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_PartitionDriver.hxx>
24 #include <GEOMImpl_IPartition.hxx>
25 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Object.hxx>
28 #include <GEOM_Function.hxx>
30 //#include <NMTAlgo_Splitter1.hxx>
31 #include <GEOMAlgo_Splitter.hxx>
32 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <TDataStd_IntegerArray.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRepAlgo.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <ShapeFix_ShapeTolerance.hxx>
50 #include <ShapeFix_Shape.hxx>
52 #include <TColStd_ListIteratorOfListOfInteger.hxx>
53 #include <TColStd_ListOfInteger.hxx>
54 #include <Standard_NullObject.hxx>
55 #include <Precision.hxx>
58 //=======================================================================
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
64 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
65 return aPartitionDriver;
69 //=======================================================================
70 //function : GEOMImpl_PartitionDriver
72 //=======================================================================
73 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
77 //=======================================================================
78 //function : SimplifyCompound
80 //=======================================================================
81 static void PrepareShapes (const TopoDS_Shape& theShape,
82 Standard_Integer theType,
83 TopTools_ListOfShape& theSimpleList)
85 if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
86 theShape.ShapeType() != TopAbs_COMPOUND) {
87 theSimpleList.Append(theShape);
91 // explode compound on simple shapes to allow their intersections
92 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
93 TopTools_MapOfShape mapShape;
94 for (; It.More(); It.Next()) {
95 if (mapShape.Add(It.Value())) {
96 TopoDS_Shape curSh = It.Value();
97 PrepareShapes(curSh, theType, theSimpleList);
102 //=======================================================================
105 //=======================================================================
106 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
108 if (Label().IsNull()) return 0;
109 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
111 GEOMImpl_IPartition aCI (aFunction);
112 Standard_Integer aType = aFunction->GetType();
115 //sklNMTAlgo_Splitter1 PS;
116 GEOMAlgo_Splitter PS;
118 if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
120 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
121 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
122 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
123 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
124 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
125 //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
128 //unsigned int ind, nbshapes = 0;
129 //nbshapes += aShapes->Length() + aTools->Length();
130 //nbshapes += aKeepIns->Length() + aRemIns->Length();
131 //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
132 TopTools_MapOfShape ShapesMap, ToolsMap;
134 // add object shapes that are in ListShapes;
135 for (ind = 1; ind <= aShapes->Length(); ind++) {
136 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
137 TopoDS_Shape aShape_i = aRefShape->GetValue();
138 if (aShape_i.IsNull()) {
139 Standard_NullObject::Raise("In Partition a shape is null");
142 TopTools_ListOfShape aSimpleShapes;
143 PrepareShapes(aShape_i, aType, aSimpleShapes);
144 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
145 for (; aSimpleIter.More(); aSimpleIter.Next()) {
146 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
147 if (ShapesMap.Add(aSimpleSh)) {
148 PS.AddShape(aSimpleSh);
149 //skl if (DoRemoveWebs) {
150 //skl if (aMaterials->Length() >= ind)
151 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
157 // add tool shapes that are in ListTools and not in ListShapes;
158 for (ind = 1; ind <= aTools->Length(); ind++) {
159 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
160 TopoDS_Shape aShape_i = aRefShape->GetValue();
161 if (aShape_i.IsNull()) {
162 Standard_NullObject::Raise("In Partition a tool shape is null");
165 TopTools_ListOfShape aSimpleShapes;
166 PrepareShapes(aShape_i, aType, aSimpleShapes);
167 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
168 for (; aSimpleIter.More(); aSimpleIter.Next()) {
169 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
170 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
171 PS.AddTool(aSimpleSh);
176 // add shapes that are in ListKeepInside, as object shapes;
177 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
178 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
179 TopoDS_Shape aShape_i = aRefShape->GetValue();
180 if (aShape_i.IsNull()) {
181 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
184 TopTools_ListOfShape aSimpleShapes;
185 PrepareShapes(aShape_i, aType, aSimpleShapes);
186 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
187 for (; aSimpleIter.More(); aSimpleIter.Next()) {
188 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
189 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
190 PS.AddShape(aSimpleSh);
194 // add shapes that are in ListRemoveInside, as object shapes;
195 for (ind = 1; ind <= aRemIns->Length(); ind++) {
196 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
197 TopoDS_Shape aShape_i = aRefShape->GetValue();
198 if (aShape_i.IsNull()) {
199 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
202 TopTools_ListOfShape aSimpleShapes;
203 PrepareShapes(aShape_i, aType, aSimpleShapes);
204 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
205 for (; aSimpleIter.More(); aSimpleIter.Next()) {
206 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
207 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
208 PS.AddShape(aSimpleSh);
212 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
213 PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
217 //skl PS.SetRemoveWebs(!DoRemoveWebs);
218 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
220 // suppress result outside of shapes in KInsideMap
221 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
222 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
223 TopoDS_Shape aShape_i = aRefShape->GetValue();
224 PS.KeepShapesInside(aShape_i);
227 // suppress result inside of shapes in RInsideMap
228 for (ind = 1; ind <= aRemIns->Length(); ind++) {
229 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
230 TopoDS_Shape aShape_i = aRefShape->GetValue();
231 PS.RemoveShapesInside(aShape_i);
235 else if (aType == PARTITION_HALF)
237 Handle(GEOM_Function) aRefShape = aCI.GetShape();
238 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
239 TopoDS_Shape aShapeArg = aRefShape->GetValue();
240 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
242 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
243 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
246 // add object shapes that are in ListShapes;
247 PS.AddShape(aShapeArg);
249 // add tool shapes that are in ListTools and not in ListShapes;
250 PS.AddTool(aPlaneArg);
254 //PS.SetRemoveWebs(Standard_False);
255 //PS.Build(aShapeArg.ShapeType());
261 if (aShape.IsNull()) return 0;
263 if (!BRepAlgo::IsValid(aShape)) {
264 // 08.07.2008 added by skl during fixing bug 19761 from Mantis
265 ShapeFix_ShapeTolerance aSFT;
266 aSFT.LimitTolerance(aShape, Precision::Confusion(),
267 Precision::Confusion(), TopAbs_SHAPE);
268 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
270 aShape = aSfs->Shape();
271 if (!BRepAlgo::IsValid(aShape))
272 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
275 aFunction->SetValue(aShape);
277 // Fill history to be used by GetInPlace functionality
278 TopTools_IndexedMapOfShape aResIndices;
279 TopExp::MapShapes(aShape, aResIndices);
281 // Map: source_shape/images of source_shape in Result
282 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
284 // history for all argument shapes
285 TDF_LabelSequence aLabelSeq;
286 aFunction->GetDependency(aLabelSeq);
287 Standard_Integer nbArg = aLabelSeq.Length();
289 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
291 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
293 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
294 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
296 TopTools_IndexedMapOfShape anArgumentIndices;
297 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
298 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
300 // Find corresponding label in history
301 TDF_Label anArgumentHistoryLabel =
302 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
304 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
305 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
306 if (!aMR.Contains(anEntity)) continue;
308 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
309 Standard_Integer nbModified = aModified.Extent();
311 if (nbModified > 0) {
312 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
313 Handle(TDataStd_IntegerArray) anAttr =
314 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
317 TopTools_ListIteratorOfListOfShape itM (aModified);
318 for (; itM.More(); itM.Next(), ++ih) {
319 int id = aResIndices.FindIndex(itM.Value());
320 anAttr->SetValue(ih, id);
326 log.SetTouched(Label());
332 //=======================================================================
333 //function : GEOMImpl_PartitionDriver_Type_
335 //=======================================================================
336 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
339 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
340 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
341 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
342 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
343 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
344 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
347 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
348 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PartitionDriver",
349 sizeof(GEOMImpl_PartitionDriver),
351 (Standard_Address)_Ancestors,
352 (Standard_Address)NULL);
357 //=======================================================================
358 //function : DownCast
360 //=======================================================================
361 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
363 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
365 if (!AnObject.IsNull()) {
366 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
367 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
371 return _anOtherObject ;