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 <TColStd_ListIteratorOfListOfInteger.hxx>
50 #include <TColStd_ListOfInteger.hxx>
51 #include <Standard_NullObject.hxx>
52 #include <Precision.hxx>
55 //=======================================================================
58 //=======================================================================
59 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
61 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
62 return aPartitionDriver;
66 //=======================================================================
67 //function : GEOMImpl_PartitionDriver
69 //=======================================================================
70 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
74 //=======================================================================
75 //function : SimplifyCompound
77 //=======================================================================
78 static void PrepareShapes (const TopoDS_Shape& theShape,
79 Standard_Integer theType,
80 TopTools_ListOfShape& theSimpleList)
82 if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
83 theShape.ShapeType() != TopAbs_COMPOUND) {
84 theSimpleList.Append(theShape);
88 // explode compound on simple shapes to allow their intersections
89 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
90 TopTools_MapOfShape mapShape;
91 for (; It.More(); It.Next()) {
92 if (mapShape.Add(It.Value())) {
93 TopoDS_Shape curSh = It.Value();
94 PrepareShapes(curSh, theType, theSimpleList);
99 //=======================================================================
102 //=======================================================================
103 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
105 if (Label().IsNull()) return 0;
106 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
108 GEOMImpl_IPartition aCI (aFunction);
109 Standard_Integer aType = aFunction->GetType();
112 //sklNMTAlgo_Splitter1 PS;
113 GEOMAlgo_Splitter PS;
115 if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
117 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
118 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
119 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
120 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
121 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
122 //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
125 //unsigned int ind, nbshapes = 0;
126 //nbshapes += aShapes->Length() + aTools->Length();
127 //nbshapes += aKeepIns->Length() + aRemIns->Length();
128 //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
129 TopTools_MapOfShape ShapesMap, ToolsMap;
131 // add object shapes that are in ListShapes;
132 for (ind = 1; ind <= aShapes->Length(); ind++) {
133 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
134 TopoDS_Shape aShape_i = aRefShape->GetValue();
135 if (aShape_i.IsNull()) {
136 Standard_NullObject::Raise("In Partition a shape is null");
139 TopTools_ListOfShape aSimpleShapes;
140 PrepareShapes(aShape_i, aType, aSimpleShapes);
141 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
142 for (; aSimpleIter.More(); aSimpleIter.Next()) {
143 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
144 if (ShapesMap.Add(aSimpleSh)) {
145 PS.AddShape(aSimpleSh);
146 //skl if (DoRemoveWebs) {
147 //skl if (aMaterials->Length() >= ind)
148 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
154 // add tool shapes that are in ListTools and not in ListShapes;
155 for (ind = 1; ind <= aTools->Length(); ind++) {
156 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
157 TopoDS_Shape aShape_i = aRefShape->GetValue();
158 if (aShape_i.IsNull()) {
159 Standard_NullObject::Raise("In Partition a tool shape is null");
162 TopTools_ListOfShape aSimpleShapes;
163 PrepareShapes(aShape_i, aType, aSimpleShapes);
164 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
165 for (; aSimpleIter.More(); aSimpleIter.Next()) {
166 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
167 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
168 PS.AddTool(aSimpleSh);
173 // add shapes that are in ListKeepInside, as object shapes;
174 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
175 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
176 TopoDS_Shape aShape_i = aRefShape->GetValue();
177 if (aShape_i.IsNull()) {
178 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
181 TopTools_ListOfShape aSimpleShapes;
182 PrepareShapes(aShape_i, aType, aSimpleShapes);
183 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
184 for (; aSimpleIter.More(); aSimpleIter.Next()) {
185 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
186 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
187 PS.AddShape(aSimpleSh);
191 // add shapes that are in ListRemoveInside, as object shapes;
192 for (ind = 1; ind <= aRemIns->Length(); ind++) {
193 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
194 TopoDS_Shape aShape_i = aRefShape->GetValue();
195 if (aShape_i.IsNull()) {
196 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
199 TopTools_ListOfShape aSimpleShapes;
200 PrepareShapes(aShape_i, aType, aSimpleShapes);
201 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
202 for (; aSimpleIter.More(); aSimpleIter.Next()) {
203 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
204 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
205 PS.AddShape(aSimpleSh);
209 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
210 PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
214 //skl PS.SetRemoveWebs(!DoRemoveWebs);
215 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
217 // suppress result outside of shapes in KInsideMap
218 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
219 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
220 TopoDS_Shape aShape_i = aRefShape->GetValue();
221 PS.KeepShapesInside(aShape_i);
224 // suppress result inside of shapes in RInsideMap
225 for (ind = 1; ind <= aRemIns->Length(); ind++) {
226 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
227 TopoDS_Shape aShape_i = aRefShape->GetValue();
228 PS.RemoveShapesInside(aShape_i);
232 else if (aType == PARTITION_HALF)
234 Handle(GEOM_Function) aRefShape = aCI.GetShape();
235 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
236 TopoDS_Shape aShapeArg = aRefShape->GetValue();
237 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
239 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
240 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
243 // add object shapes that are in ListShapes;
244 PS.AddShape(aShapeArg);
246 // add tool shapes that are in ListTools and not in ListShapes;
247 PS.AddTool(aPlaneArg);
251 //PS.SetRemoveWebs(Standard_False);
252 //PS.Build(aShapeArg.ShapeType());
258 if (aShape.IsNull()) return 0;
260 if (!BRepAlgo::IsValid(aShape)) {
261 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
264 aFunction->SetValue(aShape);
266 // Fill history to be used by GetInPlace functionality
267 TopTools_IndexedMapOfShape aResIndices;
268 TopExp::MapShapes(aShape, aResIndices);
270 // Map: source_shape/images of source_shape in Result
271 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
273 // history for all argument shapes
274 TDF_LabelSequence aLabelSeq;
275 aFunction->GetDependency(aLabelSeq);
276 Standard_Integer nbArg = aLabelSeq.Length();
278 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
280 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
282 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
283 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
285 TopTools_IndexedMapOfShape anArgumentIndices;
286 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
287 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
289 // Find corresponding label in history
290 TDF_Label anArgumentHistoryLabel =
291 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
293 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
294 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
295 if (!aMR.Contains(anEntity)) continue;
297 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
298 Standard_Integer nbModified = aModified.Extent();
300 if (nbModified > 0) {
301 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
302 Handle(TDataStd_IntegerArray) anAttr =
303 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
306 TopTools_ListIteratorOfListOfShape itM (aModified);
307 for (; itM.More(); itM.Next(), ++ih) {
308 int id = aResIndices.FindIndex(itM.Value());
309 anAttr->SetValue(ih, id);
315 log.SetTouched(Label());
321 //=======================================================================
322 //function : GEOMImpl_PartitionDriver_Type_
324 //=======================================================================
325 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
328 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
329 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
330 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
331 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
332 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
333 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
336 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
337 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PartitionDriver",
338 sizeof(GEOMImpl_PartitionDriver),
340 (Standard_Address)_Ancestors,
341 (Standard_Address)NULL);
346 //=======================================================================
347 //function : DownCast
349 //=======================================================================
350 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
352 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
354 if (!AnObject.IsNull()) {
355 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
356 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
360 return _anOtherObject ;