opencascade TopoDS_Shape源码学习【重中之重】

opencascade TopoDS_Shape
在这里插入图片描述

前言

描述了一个形状,它 引用了一个基础形状,该基础形状有可能被赋予一个位置和方向 为基础形状提供了一个位置,定义了它在本地坐标系中的位置为基础形状提供了一个方向,这是从几何学的角度(而不是相对于其他形状的方向)来定义的。注意:如果一个形状引用的基础形状的形状列表为空,则该形状被视为空。

1

创建一个不引用任何内容的空形状
TopoDS_Shape() : myOrient(TopAbs_EXTERNAL);

2

通用移动构造函数,也接受子类(TopoDS_Shape层次结构仅声明了没有额外字段的假子类)。
template
TopoDS_Shape(T2&& theOther, typename std::enable_if<opencascade::std::is_base_of<TopoDS_Shape, T2>::value>::type* = 0)
: myTShape(std::forward(theOther).myTShape),
myLocation(std::forward(theOther).myLocation),
myOrient(std::forward(theOther).myOrient) ;

3

通用移动赋值运算符。
template
typename std::enable_if<opencascade::std::is_base_of<TopoDS_Shape, T2>::value, TopoDS_Shape>::type&
operator=(T2&& theOther) ;

4

如果此形状为空,则返回true。换句话说,它不引用任何可能被赋予位置和方向的基础形状。
IsNull() ;

5

销毁存储在此形状中的对基础形状的引用。结果,此形状变为空。
void Nullify() ;

6

返回形状的局部坐标系。
const TopLoc_Location& Location() const { return myLocation; }

7

设置形状的局部坐标系。
void Location(const TopLoc_Location& theLoc, const Standard_Boolean theRaiseExc = Standard_True) ;

8

返回一个与相似但局部坐标系设置为的形状。
TopoDS_Shape Located(const TopLoc_Location& theLoc, const Standard_Boolean theRaiseExc = Standard_True) ;

9

返回形状的方向。
TopAbs_Orientation Orientation() const { return myOrient; }

10

设置形状的方向。
void Orientation(TopAbs_Orientation theOrient) { myOrient = theOrient; }

11

返回一个与相似但方向设置为的形状。
TopoDS_Shape Oriented(TopAbs_Orientation theOrient) const
{
TopoDS_Shape aShape(*this);
aShape.Orientation(theOrient);
return aShape;
}
#### 12
返回一个指向实际形状实现的句柄。
const Handle(TopoDS_TShape)& TShape() const { return myTShape; }
#### 13
返回与此形状对应的TopAbs_ShapeEnum枚举值,
例如VERTEX、EDGE等。 异常 如果此形状为空,则抛出Standard_NullObject异常。
TopAbs_ShapeEnum ShapeType() const { return myTShape->ShapeType(); }
#### 14
返回自由标志的值。
Standard_Boolean Free() const { return myTShape->Free(); }
#### 15
设置自由标志。
void Free(Standard_Boolean theIsFree) { myTShape->Free(theIsFree); }
#### 16
返回锁定标志的值。
Standard_Boolean Locked() const { return myTShape->Locked(); }
#### 17
设置锁定标志。
void Locked(Standard_Boolean theIsLocked) { myTShape->Locked(theIsLocked); }
#### 18
返回修改标志的值。
Standard_Boolean Modified() const { return myTShape->Modified(); }
#### 19
设置修改标志。
void Modified(Standard_Boolean theIsModified) { myTShape->Modified(theIsModified); }
####20
返回检查标志的值。
Standard_Boolean Checked() const { return myTShape->Checked(); }
#### 21
设置检查标志。
void Checked(Standard_Boolean theIsChecked) { myTShape->Checked(theIsChecked); }

22

返回可定向性标志的值。
Standard_Boolean Orientable() const { return myTShape->Orientable(); }

23

设置可定向性标志。
void Orientable(const Standard_Boolean theIsOrientable) { myTShape->Orientable(theIsOrientable); }

24

返回封闭性标志的值。
Standard_Boolean Closed() const { return myTShape->Closed(); }

25

设置封闭性标志。
void Closed(Standard_Boolean theIsClosed) { myTShape->Closed(theIsClosed); }

26

返回无穷性标志的值。
Standard_Boolean Infinite() const { return myTShape->Infinite(); }

27

设置无穷性标志。
void Infinite(Standard_Boolean theIsInfinite) { myTShape->Infinite(theIsInfinite); }

28

返回凸性标志的值。
Standard_Boolean Convex() const { return myTShape->Convex(); }

29

设置凸性标志。
void Convex(Standard_Boolean theIsConvex) { myTShape->Convex(theIsConvex); }

30

将形状的位置乘以thePosition。
void Move(const TopLoc_Location& thePosition, const Standard_Boolean theRaiseExc = Standard_True) ;

31

返回一个与相似但位置乘以thePosition的形状。
TopoDS_Shape Moved(const TopLoc_Location& thePosition, const Standard_Boolean theRaiseExc = Standard_True) ;

32

使用TopAbs包中的Reverse方法反转方向。
void Reverse() { myOrient = TopAbs::Reverse(myOrient); }

33

返回一个与相似但方向使用TopAbs包中的Reverse方法反转的形状。
TopoDS_Shape Reversed() const ;

34

使用TopAbs包中的Complement方法补全方向。
void Complement() { myOrient = TopAbs::Complement(myOrient); }

35

返回一个与相似但方向使用TopAbs包中的Complement方法补全的形状。
TopoDS_Shape Complemented() const ;

36

使用TopAbs包中的Compose方法更新形状的方向,通过与theOrient组合。
void Compose(TopAbs_Orientation theOrient) ;

37

返回一个与相似但方向使用TopAbs包中的Compose方法与theOrient组合的形状。
TopoDS_Shape Composed(TopAbs_Orientation theOrient) const ;

38

返回直接子形状(子节点)的数量。
TopoDS_Iterator 用于访问子形状
Standard_Integer NbChildren() const { return myTShape.IsNull() ? 0 : myTShape->NbChildren(); }

39

//! 如果两个形状是伙伴,则返回True,即如果它们共享同一个TShape。
//! 位置和方向可能不同。
Standard_Boolean IsPartner(const TopoDS_Shape& theOther) const { return (myTShape == theOther.myTShape); }

40

//! 如果两个形状相同,则返回True,即如果它们共享同一个TShape和相同的位置。
//! 方向可能不同。
Standard_Boolean IsSame(const TopoDS_Shape& theOther) const
{
return myTShape == theOther.myTShape
&& myLocation == theOther.myLocation;
}

45

//! 如果两个形状相等,则返回True,即如果它们共享同一个TShape、相同的位置和方向。
Standard_Boolean IsEqual(const TopoDS_Shape& theOther) const
{
return myTShape == theOther.myTShape
&& myLocation == theOther.myLocation
&& myOrient == theOther.myOrient;
}

46

//! 返回一个表示的哈希值。该值在范围[1, theUpperBound]内。它是根据
//! TShape和Location计算得出的。Orientation(方向)未被使用。
//! @param theUpperBound 计算哈希码时必须位于的范围的上界
//! @return 一个计算出的哈希码,范围在[1, theUpperBound]内
Standard_EXPORT Standard_Integer HashCode(Standard_Integer theUpperBound) const;

47

//! 用一个新的Shape替换,该Shape具有相同的
//! Orientation(方向)和Location(位置),以及一个新的TShape,该TShape具有
//! 相同的几何形状但没有子形状。
void EmptyCopy() { myTShape = myTShape->EmptyCopy(); }

48

//! 返回一个新的Shape,该Shape具有相同的
//! Orientation(方向)和Location(位置),以及一个新的TShape,该TShape具有
//! 相同的几何形状但没有子形状。
TopoDS_Shape EmptyCopied() const
{
TopoDS_Shape aShape(*this);
aShape.EmptyCopy();
return aShape;
}

49

设置TShape
void TShape(const Handle(TopoDS_TShape)& theTShape) { myTShape = theTShape; }

50

将我的内容转储到流中
Standard_EXPORT void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;

用法用例

TopoDS_Shape 是 OpenCASCADE 中表示拓扑形状的基类,它本身是一个抽象类,不能直接实例化。但是,它定义了一些方法和操作,可以通过其派生类(如 TopoDS_VertexTopoDS_EdgeTopoDS_FaceTopoDS_Solid 等)来操作和访问拓扑形状的属性和信息。

以下是一些示例,展示了如何使用 TopoDS_Shape 类中的方法及其派生类的操作:

1. 获取拓扑类型

通过 ShapeType() 方法可以获取拓扑形状的类型,它返回一个枚举值,表示形状的具体类型(顶点、边、面、实体等)。

#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Solid.hxx>
#include <TopExp.hxx>
#include <iostream>

void PrintShapeType(const TopoDS_Shape& shape) {
    switch (shape.ShapeType()) {
        case TopAbs_VERTEX:
            std::cout << "Shape is a vertex." << std::endl;
            break;
        case TopAbs_EDGE:
            std::cout << "Shape is an edge." << std::endl;
            break;
        case TopAbs_FACE:
            std::cout << "Shape is a face." << std::endl;
            break;
        case TopAbs_SOLID:
            std::cout << "Shape is a solid." << std::endl;
            break;
        default:
            std::cout << "Unknown shape type." << std::endl;
            break;
    }
}

int main() {
    // 示例:创建一个顶点
    TopoDS_Vertex vertex;
    // 在实际应用中,需要通过构造函数或者其他方法来创建顶点

    // 打印顶点的类型
    PrintShapeType(vertex);

    return 0;
}
2. 拓扑结构查询

通过 TopExp 类提供的方法可以进行更复杂的拓扑结构查询,如查找顶点、边、面之间的关系和连接。

#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopExp.hxx>
#include <iostream>

void PrintConnectedEdges(const TopoDS_Vertex& vertex) {
    // 查找连接到顶点的边
    TopTools_IndexedMapOfShape edgeMap;
    TopExp::MapShapes(vertex, TopAbs_EDGE, edgeMap);
    for (int i = 1; i <= edgeMap.Extent(); ++i) {
        const TopoDS_Edge& edge = TopoDS::Edge(edgeMap(i));
        std::cout << "Connected edge with orientation: " << (edge.Orientation() == TopAbs_FORWARD ? "Forward" : "Reversed") << std::endl;
    }
}

int main() {
    // 示例:创建一个顶点和一条边
    TopoDS_Vertex vertex;
    // 在实际应用中,需要通过构造函数或者其他方法来创建顶点

    TopoDS_Edge edge;
    // 在实际应用中,需要通过构造函数或者其他方法来创建边

    // 打印顶点连接的边
    PrintConnectedEdges(vertex);

    return 0;
}
3. 拓扑属性访问

通过 TopoDS_Shape 类及其派生类可以访问拓扑形状的特定属性,如边的起点和终点、面的法向量等。

#include <TopoDS_Shape.hxx>
#include <TopoDS_Edge.hxx>
#include <BRep_Tool.hxx>
#include <gp_Pnt.hxx>
#include <iostream>

void PrintEdgeVertices(const TopoDS_Edge& edge) {
    // 获取边的起点和终点坐标
    const TopoDS_Vertex& startVertex = TopExp::FirstVertex(edge);
    const TopoDS_Vertex& endVertex = TopExp::LastVertex(edge);

    gp_Pnt startPoint = BRep_Tool::Pnt(startVertex);
    gp_Pnt endPoint = BRep_Tool::Pnt(endVertex);

    std::cout << "Edge start point: (" << startPoint.X() << ", " << startPoint.Y() << ", " << startPoint.Z() << ")" << std::endl;
    std::cout << "Edge end point: (" << endPoint.X() << ", " << endPoint.Y() << ", " << endPoint.Z() << ")" << std::endl;
}

int main() {
    // 示例:创建一条边
    TopoDS_Edge edge;
    // 在实际应用中,需要通过构造函数或者其他方法来创建边

    // 打印边的起点和终点坐标
    PrintEdgeVertices(edge);

    return 0;
}

这些示例展示了如何利用 TopoDS_Shape 及其派生类的方法来操作和查询拓扑形状的信息。在实际应用中,根据具体的需求和场景,可以进一步组合和扩展这些方法,以实现更复杂的几何建模和拓扑操作。
参考

  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值