Opencascad(C++)-建模-创建有界直线段


1、 前言

在Opencascad开发时,经常会遇到创建直线的情况,采用gp_Line创建的直线段是无界的,如果想创建一条直线段,就需要用其他的方法了。本文介绍一种创建直线段的方法。


2、用gp_Lin创建一条直线

描述三维空间中的线条。一条线位于空间中,轴(gp_Ax3对象)为其提供原点和单位向量。直线和轴是相似的对象,因此,我们可以将一个转换为另一个。通过一条线,可以直接访问其定位轴上可用的大多数编辑和查询功能。但是,此外,线还具有用于计算距离和位置的特定功能。另请参阅gce_MakeLin,它为更复杂的线结构提供了函数Geom_Line它提供了用于构造线的附加函数,特别是使用线的参数方程。
其包括的方法有:

2.1 gp_Lin类成员函数

1. gp_Lin ()
创建一条与参照坐标系的 Z 轴对应的线。

2. gp_Lin (常量 gp_Ax1 和 A1)
创建由轴 A1 定义的线。

3. gp_Lin (const gp_Pnt &theP, const gp_Dir &theV)
创建一条穿过点 theP 并平行于矢量 theV 的线(P 和 V 分别是线定位轴的原点和单位矢量)。

4. void Reverse ()

5. gp_Lin Reversed () const
反转直线的方向。

6. void SetDirection (const gp_Dir &theV)
更改线条的方向。

7. void SetLocation (const gp_Pnt &theP)
更改线的位置点(原点)。

8. void SetPosition (const gp_Ax1 &theA1)
完全重新定义生产线。的“位置”点是线的原点。的“方向”是直线的方向。

9. const gp_Dir & Direction () const
返回线条的方向。

10. const gp_Pnt & Location () const
返回线的位置点(原点)。

11. const gp_Ax1 & Position () const
返回与 具有相同位置和方向的一个轴的轴放置。

12. Standard_Real Angle (const gp_Lin &theOther) const
以弧度为单位计算两条线之间的角度。

13. Standard_Boolean Contains (const gp_Pnt &theP, const Standard_Real theLinearTolerance) const
如果此行包含点 theP,即,如果点 theP 和这条线之间的距离小于或等于线性容差,则返回 true。

14. Standard_Real Distance (const gp_Pnt &theP) const
计算 和点 theP 之间的距离。

15. Standard_Real Distance (const gp_Lin &theOther) const
计算两条线之间的距离。

16. Standard_Real SquareDistance (const gp_Pnt &theP) const
计算 和点 theP 之间的平方距离。

17. Standard_Real SquareDistance (const gp_Lin &theOther) const
计算两条线之间的平方距离。

18. gp_Lin Normal (const gp_Pnt &theP) const
计算垂直于 方向的线,穿过点 theP。如果 和点 theP 之间的距离小于或等于 gp 的分辨率,则引发构造误差,因为 3D 空间中存在无限个解。

19. void Mirror (const gp_Pnt &theP)

20. gp_Lin Mirrored (const gp_Pnt &theP) const
执行直线相对于作为对称中心的点 theP 的对称变换。

21. void Mirror (const gp_Ax1 &theA1)

22. gp_Lin Mirrored (const gp_Ax1 &theA1) const
相对于作为对称轴的轴放置执行直线的对称变换。

23. void Mirror (const gp_Ax2 &theA2)

24. gp_Lin Mirrored (const gp_Ax2 &theA2) const
执行直线相对于平面的对称变换。轴放置定位对称平面:(位置,XDirection,YDirection)。

25. void Rotate (const gp_Ax1 &theA1, const Standard_Real theAng)

26. gp_Lin Rotated (const gp_Ax1 &theA1, const Standard_Real theAng) const
旋转线条。A1 是旋转轴。Ang 是以弧度为单位的旋转角度值。

27. void Scale (const gp_Pnt &theP, const Standard_Real theS)

28. gp_Lin Scaled (const gp_Pnt &theP, const Standard_Real theS) const
缩放线条。S 是缩放值。修改线的“位置”点(原点)。如果比例为负,则“方向”反转。

29. void Transform (const gp_Trsf &theT)

30. gp_Lin Transformed (const gp_Trsf &theT) const
使用类 Trsf 的转换转换线。

31. void Translate (const gp_Vec &theV)

32. gp_Lin Translated (const gp_Vec &theV) const
沿向量 theV 的方向平移一条线。平移的大小是矢量的大小。

33. void Translate (const gp_Pnt &theP1, const gp_Pnt &theP2)

34. gp_Lin Translated (const gp_Pnt &theP1, const gp_Pnt &theP2) const
将一条线从点 P1 转换为点 P2。

2.2 创建一条直线

我们采用一个点和轴向来创建一条直线,其代码如下:

//直线通过的点
    gp_Pnt origionPnt(0.0,0.0,0.0);
    //直线矢量采用两个点控制
    gp_Pnt startPnt(0.0,0.0,0.0);
    gp_Pnt endPnt(100,100,100);
    //创建矢量
    gp_Vec vec(startPnt,endPnt);
    //创建轴向
    gp_Dir dir(vec);
    //创建直线
    gp_Lin line(origionPnt,dir);
    //转换直线到边
    TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(line);
    //显示对象
    Handle(AIS_Shape) aisEdge = new AIS_Shape(edge);
    //在视图区中显示对象
    myOccModel->GetAISContext()->Display(aisEdge,true);

2.3 运行结果

在结果中,可以看出,这是一条无线长度的直线
在这里插入图片描述

3、创建一条有界的直线段

我们采用GC_MakeSegment来实现,首先看GC_MakeSegment的说明。

3.1 功能说明

实现 3D 空间中线段的构造算法。从 2 个点 和 开始制作一段线。结果是一条Geom_TrimmedCurve曲线。

3.2 函数说明

1. GC_MakeSegment (const gp_Pnt &P1, const gp_Pnt &P2)
从 2 个点 和 开始制作一段线。如果和混淆,则返回NullObject。
2. GC_MakeSegment (const gp_Lin &Line, const Standard_Real U1, const Standard_Real U2)
从两个参数 U1 和 U1 之间的行 中生成一段线。如果 等于 ,则返回 NullObject。

3. GC_MakeSegment (const gp_Lin &Line, const gp_Pnt &Point, const Standard_Real Ulast)
从点<点>和参数 Ulast 之间的线 中生成一段线。如果 等于 ,则返回 NullObject。
4. GC_MakeSegment (const gp_Lin &Line, const gp_Pnt &P1, const gp_Pnt &P2)
从两点 和 之间的线 中制作一段线。如果 等于 ,则返回 NullObject。更多。。。

3.2 创建直线段的代码

//    //直线通过的点
//    gp_Pnt origionPnt(0.0,0.0,0.0);
    //直线矢量采用两个点控制
    gp_Pnt startPnt(0.0,0.0,0.0);
    gp_Pnt endPnt(100,100,100);
//    //创建矢量
//    gp_Vec vec(startPnt,endPnt);
//    //创建轴向
//    gp_Dir dir(vec);
//    //创建直线
//    gp_Lin line(origionPnt,dir);
//    //转换直线到边
//    TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(line);
//    //显示对象
//    Handle(AIS_Shape) aisEdge = new AIS_Shape(edge);
//    //在视图区中显示对象
//    myOccModel->GetAISContext()->Display(aisEdge,true);


//    gp_Pnt startPnt(0,0,0);
//    gp_Pnt endPnt(100,100,100);
    Handle(Geom_TrimmedCurve) aSegment =GC_MakeSegment(startPnt, endPnt);
    TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge(aSegment);

 
    Standard_Real s1 = 0;
    Standard_Real s2= 1;
    Handle(Geom_Curve) curve = BRep_Tool::Curve(aEdge,s1,s2);
    Handle(Geom_Line)line;
    if(curve->IsKind(STANDARD_TYPE(Geom_Line))){
        line = Handle(Geom_Line)::DownCast(curve);
    }

    Handle(AIS_Shape)aisEdge = new AIS_Shape(aEdge);
    myOccModel->GetAISContext()->Display(aisEdge,true);

3.3 测试效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GimiGimmy

感谢打赏,需要交流学习的,私信

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值