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>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Wire.hxx>
45 #include <TopTools_MapOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TColStd_ListIteratorOfListOfInteger.hxx>
49 #include <TColStd_ListOfInteger.hxx>
50 #include <Standard_NullObject.hxx>
51 #include <Precision.hxx>
54 //=======================================================================
57 //=======================================================================
58 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
60 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
61 return aPartitionDriver;
65 //=======================================================================
66 //function : GEOMImpl_PartitionDriver
68 //=======================================================================
69 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
73 //=======================================================================
76 //=======================================================================
77 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
79 if (Label().IsNull()) return 0;
80 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
82 GEOMImpl_IPartition aCI (aFunction);
83 Standard_Integer aType = aFunction->GetType();
86 //sklNMTAlgo_Splitter1 PS;
89 if (aType == PARTITION_PARTITION) {
90 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
91 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
92 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
93 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
94 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
95 //sklStandard_Boolean DoRemoveWebs = !aMaterials.IsNull();
97 unsigned int ind, nbshapes = 0;
98 nbshapes += aShapes->Length() + aTools->Length();
99 nbshapes += aKeepIns->Length() + aRemIns->Length();
101 TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
103 // add object shapes that are in ListShapes;
104 for (ind = 1; ind <= aShapes->Length(); ind++) {
105 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
106 TopoDS_Shape aShape_i = aRefShape->GetValue();
107 if (aShape_i.IsNull()) {
108 Standard_NullObject::Raise("In Partition a shape is null");
110 if (ShapesMap.Add(aShape_i)) {
111 PS.AddShape(aShape_i);
112 //skl if (DoRemoveWebs) {
113 //skl if (aMaterials->Length() >= ind)
114 //skl PS.SetMaterial(aShape_i, aMaterials->Value(ind));
119 // add tool shapes that are in ListTools and not in ListShapes;
120 for (ind = 1; ind <= aTools->Length(); ind++) {
121 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
122 TopoDS_Shape aShape_i = aRefShape->GetValue();
123 if (aShape_i.IsNull()) {
124 Standard_NullObject::Raise("In Partition a tool shape is null");
126 if (!ShapesMap.Contains(aShape_i) && ToolsMap.Add(aShape_i)) {
127 PS.AddTool(aShape_i);
131 // add shapes that are in ListKeepInside, as object shapes;
132 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
133 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
134 TopoDS_Shape aShape_i = aRefShape->GetValue();
135 if (aShape_i.IsNull()) {
136 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
138 if (!ToolsMap.Contains(aShape_i) && ShapesMap.Add(aShape_i))
139 PS.AddShape(aShape_i);
142 // add shapes that are in ListRemoveInside, as object shapes;
143 for (ind = 1; ind <= aRemIns->Length(); ind++) {
144 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
145 TopoDS_Shape aShape_i = aRefShape->GetValue();
146 if (aShape_i.IsNull()) {
147 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
149 if (!ToolsMap.Contains(aShape_i) && ShapesMap.Add(aShape_i))
150 PS.AddShape(aShape_i);
153 PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
157 //sklPS.SetRemoveWebs(!DoRemoveWebs);
158 //sklPS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
160 // suppress result outside of shapes in KInsideMap
161 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
162 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
163 TopoDS_Shape aShape_i = aRefShape->GetValue();
164 PS.KeepShapesInside(aShape_i);
167 // suppress result inside of shapes in RInsideMap
168 for (ind = 1; ind <= aRemIns->Length(); ind++) {
169 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
170 TopoDS_Shape aShape_i = aRefShape->GetValue();
171 PS.RemoveShapesInside(aShape_i);
175 else if (aType == PARTITION_HALF) {
176 Handle(GEOM_Function) aRefShape = aCI.GetShape();
177 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
178 TopoDS_Shape aShapeArg = aRefShape->GetValue();
179 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
181 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
182 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
185 // add object shapes that are in ListShapes;
186 PS.AddShape(aShapeArg);
188 // add tool shapes that are in ListTools and not in ListShapes;
189 PS.AddTool(aPlaneArg);
193 //PS.SetRemoveWebs(Standard_False);
194 //PS.Build(aShapeArg.ShapeType());
200 if (aShape.IsNull()) return 0;
202 if (!BRepAlgo::IsValid(aShape)) {
203 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
206 aFunction->SetValue(aShape);
208 // Fill history to be used by GetInPlace functionality
209 TopTools_IndexedMapOfShape aResIndices;
210 TopExp::MapShapes(aShape, aResIndices);
212 // history for all argument shapes
213 TDF_LabelSequence aLabelSeq;
214 aFunction->GetDependency(aLabelSeq);
215 Standard_Integer nbArg = aLabelSeq.Length();
217 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
219 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
221 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
222 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
224 TopTools_IndexedMapOfShape anArgumentIndices;
225 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
226 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
228 // Find corresponding label in history
229 TDF_Label anArgumentHistoryLabel =
230 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
232 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
233 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
234 const TopTools_ListOfShape& aModified = PS.Modified(anEntity);
235 Standard_Integer nbModified = aModified.Extent();
237 if (nbModified > 0) {
238 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
239 Handle(TDataStd_IntegerArray) anAttr =
240 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
242 TopTools_ListIteratorOfListOfShape itM (aModified);
243 for (int im = 1; itM.More(); itM.Next(), ++im) {
244 int id = aResIndices.FindIndex(itM.Value());
245 anAttr->SetValue(im, id);
251 log.SetTouched(Label());
257 //=======================================================================
258 //function : GEOMImpl_PartitionDriver_Type_
260 //=======================================================================
261 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
264 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
265 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
266 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
267 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
268 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
269 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
272 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
273 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PartitionDriver",
274 sizeof(GEOMImpl_PartitionDriver),
276 (Standard_Address)_Ancestors,
277 (Standard_Address)NULL);
282 //=======================================================================
283 //function : DownCast
285 //=======================================================================
286 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
288 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
290 if (!AnObject.IsNull()) {
291 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
292 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
296 return _anOtherObject ;