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中。分割后如果没有被改变的面还是原来的面。

目录
相关文章
|
Linux Docker 容器
在anolis 8.4上安装docker-ce
在anolis的软件仓库中并没有docker-ce套件,那么该如何安装docker-ce呢?
3297 1
在anolis 8.4上安装docker-ce
|
7月前
|
运维 Kubernetes Java
Koupleless 助力「人力家」实现分布式研发集中式部署,又快又省!
通过引入Koupleless框架,解决了多应用部署中资源浪费和运维成本高的问题,实现了模块瘦身、快速部署及流量控制优化,大幅降低了服务器资源占用和发布耗时,提升了系统稳定性和运维效率。最终,人力家成功实现了多应用的轻量集中部署,显著减少了运维成本。
 Koupleless 助力「人力家」实现分布式研发集中式部署,又快又省!
|
11月前
|
人工智能 安全 Cloud Native
|
11月前
|
算法 关系型数据库 MySQL
分布式唯一ID生成:深入理解Snowflake算法在Go中的实现
在分布式系统中,确保每个节点生成的 ID 唯一且高效至关重要。Snowflake 算法由 Twitter 开发,通过 64 位 long 型数字生成全局唯一 ID,包括 1 位标识位、41 位时间戳、10 位机器 ID 和 12 位序列号。该算法具备全局唯一性、递增性、高可用性和高性能,适用于高并发场景,如电商促销时的大量订单生成。本文介绍了使用 Go 语言的 `bwmarrin/snowflake` 和 `sony/sonyflake` 库实现 Snowflake 算法的方法。
498 1
分布式唯一ID生成:深入理解Snowflake算法在Go中的实现
|
分布式计算 大数据 数据处理
MaxCompute操作报错合集之在开发环境中配置MaxCompute参数进行调度,但参数解析不出来,如何解决
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
|
机器学习/深度学习 监控 算法
子图分析图片及视频
8月更文挑战第14天
282 0
|
传感器 API Android开发
Android摄像头采集选Camera1还是Camera2?
Camera1与Camera2是Android平台上的两种摄像头API。Camera1(API1)在Android 5.0后被标记为过时,新项目应优先选用Camera2(API2)。Camera2提供了更精细的控制选项,如曝光时间、ISO感光度等;支持多摄像头管理;采用异步操作提高应用响应速度;并支持RAW图像捕获及实时图像处理。此外,它还具备更好的适配性和扩展性,适用于各类应用场景,如相机应用开发、视频通话和计算机视觉等。因此,在现代Android开发中推荐使用Camera2。
396 0
|
存储 编解码 监控
QT5 QCamera摄像头
QT5 QCamera摄像头
705 0
|
域名解析 弹性计算 Linux
阿里云服务器配置教程(从购买配置到网站上线全流程)
阿里云服务器使用教程包括云服务器购买、云服务器配置选择、云服务器开通端口号、搭建网站所需Web环境、安装网站程序、域名解析到云服务器公网IP地址,最后网站上线全流程,阿小云分享阿里云服务器详细使用教程:
1252 0
|
C++ Windows
Visual Studio 安装检测内存工具-Visual Leak Detetctor。(适用于VS2013、VS2015、VS2017、VS2019、VS2022版本)
Visual Studio 安装检测内存工具-Visual Leak Detetctor。(适用于VS2013、VS2015、VS2017、VS2019、VS2022版本)
1053 0