geom/src/GEOMImpl/GEOMImpl_BooleanDriver.cxx

573 lines
18 KiB
C++
Raw Normal View History

2013-04-01 18:25:01 +06:00
// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
//
2012-08-09 13:58:02 +06:00
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
2012-08-09 13:58:02 +06:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
2013-04-01 18:25:01 +06:00
//
2012-08-09 13:58:02 +06:00
2005-08-11 10:43:57 +06:00
#include <GEOMImpl_BooleanDriver.hxx>
#include <GEOMImpl_IBoolean.hxx>
#include <GEOMImpl_Types.hxx>
#include <GEOMImpl_GlueDriver.hxx>
2005-08-11 10:43:57 +06:00
#include <GEOM_Function.hxx>
2013-03-15 20:28:11 +06:00
#include <GEOMUtils.hxx>
2004-12-01 15:39:14 +05:00
2012-08-09 13:58:02 +06:00
#include <TNaming_CopyShape.hxx>
#include <ShapeFix_ShapeTolerance.hxx>
#include <ShapeFix_Shape.hxx>
#include <BRep_Builder.hxx>
2004-12-01 15:39:14 +05:00
#include <BRepAlgo.hxx>
#include <BRepAlgoAPI_Common.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRepAlgoAPI_Section.hxx>
2012-08-09 13:58:02 +06:00
#include <BRepCheck_Analyzer.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
2012-08-09 13:58:02 +06:00
#include <TColStd_IndexedDataMapOfTransientTransient.hxx>
#include <Precision.hxx>
2004-12-01 15:39:14 +05:00
#include <Standard_ConstructionError.hxx>
#include <StdFail_NotDone.hxx>
//=======================================================================
//function : GetID
//purpose :
//=======================================================================
const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
{
static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
return aBooleanDriver;
}
//=======================================================================
//function : GEOMImpl_BooleanDriver
//purpose :
//=======================================================================
GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
{
}
//=======================================================================
//function : Execute
//purpose :
//=======================================================================
2012-08-09 13:58:02 +06:00
Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
2004-12-01 15:39:14 +05:00
{
if (Label().IsNull()) return 0;
Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
GEOMImpl_IBoolean aCI (aFunction);
Standard_Integer aType = aFunction->GetType();
TopoDS_Shape aShape;
switch (aType) {
case BOOLEAN_COMMON:
case BOOLEAN_CUT:
case BOOLEAN_FUSE:
case BOOLEAN_SECTION:
{
Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
TopoDS_Shape aShape1 = aRefShape1->GetValue();
TopoDS_Shape aShape2 = aRefShape2->GetValue();
if (!aShape1.IsNull() && !aShape2.IsNull()) {
// check arguments for Mantis issue 0021019
BRepCheck_Analyzer ana (aShape1, Standard_True);
if (!ana.IsValid())
StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
ana.Init(aShape2);
if (!ana.IsValid())
StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
aShape = performOperation (aShape1, aShape2, aType);
if (aShape.IsNull())
return 0;
}
}
break;
case BOOLEAN_COMMON_LIST:
case BOOLEAN_FUSE_LIST:
{
Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
const Standard_Integer nbShapes = aShapes->Length();
Standard_Integer i;
Handle(GEOM_Function) aRefShape;
TopoDS_Shape aShape2;
Standard_Integer aSimpleType =
(aType == BOOLEAN_FUSE_LIST ? BOOLEAN_FUSE : BOOLEAN_COMMON);
if (nbShapes > 0) {
aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
aShape = aRefShape->GetValue();
if (!aShape.IsNull()) {
BRepCheck_Analyzer anAna (aShape, Standard_True);
if (!anAna.IsValid()) {
StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
}
for (i = 2; i <= nbShapes; i++) {
aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
aShape2 = aRefShape->GetValue();
anAna.Init(aShape2);
if (!anAna.IsValid()) {
StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
}
aShape = performOperation (aShape, aShape2, aSimpleType);
if (aShape.IsNull()) {
return 0;
}
}
}
2004-12-01 15:39:14 +05:00
}
}
break;
case BOOLEAN_CUT_LIST:
{
Handle(GEOM_Function) aRefObject = aCI.GetShape1();
aShape = aRefObject->GetValue();
if (!aShape.IsNull()) {
// check arguments for Mantis issue 0021019
BRepCheck_Analyzer anAna (aShape, Standard_True);
if (!anAna.IsValid()) {
StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
}
Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetShapes();
const Standard_Integer nbShapes = aTools->Length();
Standard_Integer i;
Handle(GEOM_Function) aRefTool;
TopoDS_Shape aTool;
for (i = 1; i <= nbShapes; i++) {
aRefTool = Handle(GEOM_Function)::DownCast(aTools->Value(i));
aTool = aRefTool->GetValue();
anAna.Init(aTool);
if (!anAna.IsValid()) {
StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
}
aShape = performOperation (aShape, aTool, BOOLEAN_CUT);
if (aShape.IsNull()) {
return 0;
}
}
2004-12-01 15:39:14 +05:00
}
}
break;
default:
break;
}
aFunction->SetValue(aShape);
log.SetTouched(Label());
return 1;
}
//=======================================================================
//function : performOperation
//purpose :
//=======================================================================
TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
(const TopoDS_Shape theShape1,
const TopoDS_Shape theShape2,
const Standard_Integer theType)const
{
TopoDS_Shape aShape;
// perform COMMON operation
if (theType == BOOLEAN_COMMON) {
BRep_Builder B;
TopoDS_Compound C;
B.MakeCompound(C);
TopTools_ListOfShape listShape1, listShape2;
GEOMUtils::AddSimpleShapes(theShape1, listShape1);
GEOMUtils::AddSimpleShapes(theShape2, listShape2);
Standard_Boolean isCompound =
(listShape1.Extent() > 1 || listShape2.Extent() > 1);
TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
for (; itSub1.More(); itSub1.Next()) {
TopoDS_Shape aValue1 = itSub1.Value();
TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
for (; itSub2.More(); itSub2.Next()) {
TopoDS_Shape aValue2 = itSub2.Value();
BRepAlgoAPI_Common BO (aValue1, aValue2);
if (!BO.IsDone()) {
StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
}
if (isCompound) {
TopoDS_Shape aStepResult = BO.Shape();
// check result of this step: if it is a compound (boolean operations
// allways return a compound), we add all sub-shapes of it.
// This allows to avoid adding empty compounds,
// resulting from COMMON on two non-intersecting shapes.
if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
TopoDS_Iterator aCompIter (aStepResult);
for (; aCompIter.More(); aCompIter.Next()) {
// add shape in a result
B.Add(C, aCompIter.Value());
}
}
else {
// add shape in a result
B.Add(C, aStepResult);
}
}
else
aShape = BO.Shape();
}
}
if (isCompound) {
// As GlueFaces has been improved to keep all kind of shapes
TopExp_Explorer anExp (C, TopAbs_VERTEX);
if (anExp.More())
aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
else
aShape = C;
}
}
// perform CUT operation
else if (theType == BOOLEAN_CUT) {
BRep_Builder B;
TopoDS_Compound C;
B.MakeCompound(C);
TopTools_ListOfShape listShapes, listTools;
GEOMUtils::AddSimpleShapes(theShape1, listShapes);
GEOMUtils::AddSimpleShapes(theShape2, listTools);
Standard_Boolean isCompound = (listShapes.Extent() > 1);
TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
for (; itSub1.More(); itSub1.Next()) {
TopoDS_Shape aCut = itSub1.Value();
// tools
TopTools_ListIteratorOfListOfShape itSub2 (listTools);
for (; itSub2.More(); itSub2.Next()) {
TopoDS_Shape aTool = itSub2.Value();
BRepAlgoAPI_Cut BO (aCut, aTool);
if (!BO.IsDone()) {
StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
}
aCut = BO.Shape();
}
if (isCompound) {
// check result of this step: if it is a compound (boolean operations
// allways return a compound), we add all sub-shapes of it.
// This allows to avoid adding empty compounds,
// resulting from CUT of parts
if (aCut.ShapeType() == TopAbs_COMPOUND) {
TopoDS_Iterator aCompIter (aCut);
for (; aCompIter.More(); aCompIter.Next()) {
// add shape in a result
B.Add(C, aCompIter.Value());
}
}
else {
// add shape in a result
B.Add(C, aCut);
}
}
else
aShape = aCut;
}
if (isCompound) {
// As GlueFaces has been improved to keep all kind of shapes
TopExp_Explorer anExp (C, TopAbs_VERTEX);
if (anExp.More())
aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
else
aShape = C;
}
}
// perform FUSE operation
else if (theType == BOOLEAN_FUSE) {
// Perform
BRepAlgoAPI_Fuse BO (theShape1, theShape2);
if (!BO.IsDone()) {
StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
}
aShape = BO.Shape();
}
// perform SECTION operation
else if (theType == BOOLEAN_SECTION) {
BRep_Builder B;
TopoDS_Compound C;
B.MakeCompound(C);
TopTools_ListOfShape listShape1, listShape2;
GEOMUtils::AddSimpleShapes(theShape1, listShape1);
GEOMUtils::AddSimpleShapes(theShape2, listShape2);
Standard_Boolean isCompound =
(listShape1.Extent() > 1 || listShape2.Extent() > 1);
TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
for (; itSub1.More(); itSub1.Next()) {
TopoDS_Shape aValue1 = itSub1.Value();
TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
for (; itSub2.More(); itSub2.Next()) {
TopoDS_Shape aValue2 = itSub2.Value();
BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
// Set approximation to have an attached 3D BSpline geometry to each edge,
// where analytic curve is not possible. Without this flag in some cases
// we obtain BSpline curve of degree 1 (C0), which is slowly
// processed by some algorithms (Partition for example).
BO.Approximation(Standard_True);
//modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
BO.ComputePCurveOn1(Standard_True);
BO.ComputePCurveOn2(Standard_True);
//modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
BO.Build();
if (!BO.IsDone()) {
StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
}
if (isCompound) {
TopoDS_Shape aStepResult = BO.Shape();
// check result of this step: if it is a compound (boolean operations
// allways return a compound), we add all sub-shapes of it.
// This allows to avoid adding empty compounds,
// resulting from SECTION on two non-intersecting shapes.
if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
TopoDS_Iterator aCompIter (aStepResult);
for (; aCompIter.More(); aCompIter.Next()) {
// add shape in a result
B.Add(C, aCompIter.Value());
}
}
else {
// add shape in a result
B.Add(C, aStepResult);
}
}
else
aShape = BO.Shape();
}
}
if (isCompound) {
// As GlueFaces has been improved to keep all kind of shapes
TopExp_Explorer anExp (C, TopAbs_VERTEX);
if (anExp.More())
aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
else
aShape = C;
2004-12-01 15:39:14 +05:00
}
}
// UNKNOWN operation
else {
}
if (aShape.IsNull()) return aShape;
2009-02-13 17:16:39 +05:00
2012-08-09 13:58:02 +06:00
// as boolean operations always produce compound, lets simplify it
// for the case, if it contains only one sub-shape
TopTools_ListOfShape listShapeRes;
2013-03-15 20:28:11 +06:00
GEOMUtils::AddSimpleShapes(aShape, listShapeRes);
2012-08-09 13:58:02 +06:00
if (listShapeRes.Extent() == 1) {
aShape = listShapeRes.First();
if (aShape.IsNull()) return aShape;
2012-08-09 13:58:02 +06:00
}
2009-02-13 17:16:39 +05:00
// 08.07.2008 skl for bug 19761 from Mantis
BRepCheck_Analyzer ana (aShape, Standard_True);
ana.Init(aShape);
if (!ana.IsValid()) {
ShapeFix_ShapeTolerance aSFT;
aSFT.LimitTolerance(aShape, Precision::Confusion(),
Precision::Confusion(), TopAbs_SHAPE);
Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
aSfs->Perform();
aShape = aSfs->Shape();
ana.Init(aShape);
if (!ana.IsValid())
Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
2004-12-01 15:39:14 +05:00
}
2012-08-09 13:58:02 +06:00
// BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
// 1. Get shape parameters for comparison
int nbTypes [TopAbs_SHAPE];
{
for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
nbTypes[iType] = 0;
nbTypes[aShape.ShapeType()]++;
TopTools_MapOfShape aMapOfShape;
aMapOfShape.Add(aShape);
TopTools_ListOfShape aListOfShape;
aListOfShape.Append(aShape);
TopTools_ListIteratorOfListOfShape itL (aListOfShape);
for (; itL.More(); itL.Next()) {
TopoDS_Iterator it (itL.Value());
for (; it.More(); it.Next()) {
TopoDS_Shape s = it.Value();
if (aMapOfShape.Add(s)) {
aListOfShape.Append(s);
nbTypes[s.ShapeType()]++;
}
}
}
}
// 2. Limit tolerance
TopoDS_Shape aShapeCopy;
TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
ShapeFix_ShapeTolerance aSFT;
aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
aSfs->Perform();
aShapeCopy = aSfs->Shape();
// 3. Check parameters
ana.Init(aShapeCopy);
if (ana.IsValid()) {
int iType, nbTypesCopy [TopAbs_SHAPE];
for (iType = 0; iType < TopAbs_SHAPE; ++iType)
nbTypesCopy[iType] = 0;
nbTypesCopy[aShapeCopy.ShapeType()]++;
TopTools_MapOfShape aMapOfShape;
aMapOfShape.Add(aShapeCopy);
TopTools_ListOfShape aListOfShape;
aListOfShape.Append(aShapeCopy);
TopTools_ListIteratorOfListOfShape itL (aListOfShape);
for (; itL.More(); itL.Next()) {
TopoDS_Iterator it (itL.Value());
for (; it.More(); it.Next()) {
TopoDS_Shape s = it.Value();
if (aMapOfShape.Add(s)) {
aListOfShape.Append(s);
nbTypesCopy[s.ShapeType()]++;
}
}
}
bool isEqual = true;
for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
if (nbTypes[iType] != nbTypesCopy[iType])
isEqual = false;
}
if (isEqual)
aShape = aShapeCopy;
}
// END: Mantis issue 0021060
return aShape;
2004-12-01 15:39:14 +05:00
}
//================================================================================
/*!
* \brief Returns a name of creation operation and names and values of creation parameters
*/
//================================================================================
bool GEOMImpl_BooleanDriver::
GetCreationInformation(std::string& theOperationName,
std::vector<GEOM_Param>& theParams)
2004-12-01 15:39:14 +05:00
{
if (Label().IsNull()) return 0;
Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
2004-12-01 15:39:14 +05:00
GEOMImpl_IBoolean aCI (function);
Standard_Integer aType = function->GetType();
2004-12-01 15:39:14 +05:00
switch ( aType ) {
case BOOLEAN_COMMON:
theOperationName = "COMMON";
AddParam( theParams, "Object 1", aCI.GetShape1() );
AddParam( theParams, "Object 2", aCI.GetShape2() );
break;
case BOOLEAN_CUT:
theOperationName = "CUT";
AddParam( theParams, "Main Object", aCI.GetShape1() );
AddParam( theParams, "Tool Object", aCI.GetShape2() );
break;
case BOOLEAN_FUSE:
theOperationName = "FUSE";
AddParam( theParams, "Object 1", aCI.GetShape1() );
AddParam( theParams, "Object 2", aCI.GetShape2() );
break;
case BOOLEAN_SECTION:
theOperationName = "SECTION";
AddParam( theParams, "Object 1", aCI.GetShape1() );
AddParam( theParams, "Object 2", aCI.GetShape2() );
break;
case BOOLEAN_COMMON_LIST:
theOperationName = "COMMON";
AddParam( theParams, "Selected objects", aCI.GetShapes() );
break;
case BOOLEAN_FUSE_LIST:
theOperationName = "FUSE";
AddParam( theParams, "Selected objects", aCI.GetShapes() );
break;
case BOOLEAN_CUT_LIST:
theOperationName = "CUT";
AddParam( theParams, "Main Object", aCI.GetShape1() );
AddParam( theParams, "Tool Objects", aCI.GetShapes() );
break;
default:
return false;
2004-12-01 15:39:14 +05:00
}
return true;
2004-12-01 15:39:14 +05:00
}
IMPLEMENT_STANDARD_HANDLE (GEOMImpl_BooleanDriver,GEOM_BaseDriver);
IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BooleanDriver,GEOM_BaseDriver);