Split Shape by Plane in OpenCASCADE

简介: Split Shape by Plane in OpenCASCADE eryar@163.com Abstract. Sometimes you want to split a shape by plane or even split a shape by a B Spline surfac...

Split Shape by Plane in OpenCASCADE

eryar@163.com

Abstract. Sometimes you want to split a shape by plane or even split a shape by a B Spline surface, OpenCASCADE provide a feature class BRepFeat_SplitShape to implement the function. The paper give a sample code to split a cylinder by plane.

Key Words. Split Shape, BRep Feature Algorithms.

1. Introduction

OpenCASCADE提供了Boolean Operation实现了任意两个形状的交、并、差的布尔操作。但是如何实现用一个面将一个形状切割成两半呢?其实Boolean Operation中已经有求交分割的算法,但是没有直接提供一个分割的功能类,而是在BRepFeat_SplitShape提供了分割功能。

wps69BE.tmp

Figure 1. Split Cylinder by Plane

2. Code Demo

使用类BRepAlgoAPI_Section和类BRepFeat_SplitShape相结合来实现分割Split形状的功能。完整代码示例如下:

/*
Copyright(C) 2017 Shing Liu(eryar@163.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//  Visual Studio 2013 & OpenCASCADE7.1.0

#include <gp_Pln.hxx>

#include <TopoDS.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>

#include <BRepTools.hxx>

#include <BRepPrimAPI_MakeCylinder.hxx>

#include <BRepAlgoAPI_Section.hxx>

#include <BRepFeat_SplitShape.hxx>

#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")

#pragma comment(lib, "TKG2d.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib")
#pragma comment(lib, "TKGeomAlgo.lib")

#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKTopAlgo.lib")

#pragma comment(lib, "TKBO.lib")
#pragma comment(lib, "TKPrim.lib")
#pragma comment(lib, "TKFeat.lib")

// ! Test split a cylinder by plane.
// ! You can use the algorithm to split other shapes.
void testSplit()
{
    BRepPrimAPI_MakeCylinder aCylinderMaker(10.0, 20.0);
    TopoDS_Shape aCylinder = aCylinderMaker.Shape();

     //  Build section by the split plane for the cylinder.
    BRepAlgoAPI_Section aSection(aCylinder, gp_Pln(gp_Pnt(0.0, 0.0, 15.0), gp::DZ()), Standard_False);
    aSection.ComputePCurveOn1(Standard_True);
    aSection.Approximation(Standard_True);
    aSection.Build();

     //  Split the cylinder shape.
    BRepFeat_SplitShape aShapeSpliter(aCylinder);

     for (TopExp_Explorer i(aSection.Shape(), TopAbs_EDGE); i.More(); i.Next())
    {
        TopoDS_Shape anEdge = i.Current();
        TopoDS_Shape aFace;

         if (aSection.HasAncestorFaceOn1(anEdge, aFace))
        {
            TopoDS_Edge E = TopoDS::Edge(anEdge);
            TopoDS_Face F = TopoDS::Face(aFace);

            aShapeSpliter.Add(E, F);
        }
    }

    aShapeSpliter.Build();

     //  Rebuild left and right shape.
    BRep_Builder aBuilder;
    TopoDS_Compound aLeftCompound;
    TopoDS_Compound aRightCompound;

    aBuilder.MakeCompound(aLeftCompound);
    aBuilder.MakeCompound(aRightCompound);

     //  Left shape.
    TopTools_MapOfShape aLeftShapeMap;
     const TopTools_ListOfShape& aLeftShapes = aShapeSpliter.Left();
     for (auto i = aLeftShapes.cbegin(); i != aLeftShapes.cend(); i++)
    {
        aLeftShapeMap.Add(*i);

        aBuilder.Add(aLeftCompound, *i);
    }

     //  Right shape.
    TopTools_IndexedMapOfShape aShapeMap;
    TopExp::MapShapes(aShapeSpliter.Shape(), TopAbs_FACE, aShapeMap);

     for (auto i = aShapeMap.cbegin(); i != aShapeMap.cend(); i++)
    {
         if (!aLeftShapeMap.Contains(*i))
        {
            aBuilder.Add(aRightCompound, *i);
        }
    }

     //  Output left and right shape.
    BRepTools::Write(aLeftCompound, "d:/left.brep");
    BRepTools::Write(aRightCompound, "d:/right.brep");
}

int main( int argc,  char* argv[])
{
    testSplit();

     return 0;
}

先创建一个圆柱体,再使用类BRepAlgoAPI_Section将圆柱体用平面进行分割,最后使用类BRepFeat_SplitShape进行分类,得到切割后的形状及Left()形状,把切割后形状中的Left过滤后剩下就是切割另一半的形状;最后导出被平面切割后得到的两半形状。结果用动画演示如下:

split shape

Figure 2. Split Shape animation demo

3. Conclusion

OpenCASCADE提供类BRepFeat_SplitShape来实现对一个形状进行切割的功能,但是要配合BRepAlgoAPI_Section使用。因为Boolean Operation中已经实现了求交、分类的功能,所以在最新版本的源码7.2.0中已经将分割功能集成到了Boolean Operation中。分割后如果没有被改变的面还是原来的面。

目录
相关文章
|
机器学习/深度学习
Leetcode-Easy 887. Projection Area of 3D Shapes
Leetcode-Easy 887. Projection Area of 3D Shapes
141 0
Leetcode-Easy 887. Projection Area of 3D Shapes
成功解决ValueError: Dimension 1 in both shapes must be equal, but are 1034 and 1024. Shapes are [100,103
成功解决ValueError: Dimension 1 in both shapes must be equal, but are 1034 and 1024. Shapes are [100,103
|
开发者
Intersection between a 2d line and a conic in OpenCASCADE
Intersection between a 2d line and a conic in OpenCASCADE eryar@163.com Abstract. OpenCASCADE provides the algorithm to implementation of the analy...
1535 0
|
算法
Convert BSpline Curve to Arc Spline in OpenCASCADE
Convert BSpline Curve to Arc Spline in OpenCASCADE eryar@163.com Abstract. The paper based on OpenCASCADE algorithms to approximate the NURBS curve to arc spline.
1532 0
|
人工智能 图形学
OpenCASCADE Shape Location
OpenCASCADE Shape Location eryar@163.com Abstract. The TopLoc package of OpenCASCADE gives resources to handle 3D local coordinate systems called Locations.
1612 0
|
算法框架/工具 索引 Caffe
Faster RCNN 运行自己的数据,刚开始正常,后来就报错: Index exceeds matrix dimensions. Error in ori_demo (line 114) boxes_cell{i} = [boxes(:, (1+(i-1)*4):(i*4)), scores(
function script_faster_rcnn_demo() close all; clc; clear mex; clear is_valid_handle; % to clear init_key run(fullfile(filepart...
|
算法 图形学 数据可视化
OpenCASCADE Hidden Line Removal
OpenCASCADE Hidden Line Removal eryar@163.com Abstract. To provide the precision required in industrial design, drawings need to offer the possibilit...
2023 0
|
算法
IsoAlgo Split Point Issues
IsoAlgo Split Point Issues eryar@163.com   Abstract.  The impossibility to define the splitting point makes it impossible to generate the readable isometrics drawing in some cases.
1228 0
|
C++ 算法 测试技术
OpenCascade Draw Test Harness
OpenCascade Draw Test Harness eryar@163.com Abstract. Draw is a command interpreter based on Tcl/Tk and a graphical system used to test and demonstrate OpenCascade modeling libraries.
1353 0