opencscade 基础1

1.1 基础类:点
gp_Pnt aPnt1(0,0,0);
Handle(Geom_CartesianPoint) aPnt2 = new Geom_CartesianPoint(0 , 0 , 0);
Standard_Real xValue1 = aPnt1.X();
Standard_Real xValue2 = aPnt2->X();
1.2 轮廓:几何体
轮廓概念:指边缘;物体的外周或图形的外框。
Geom 开发包:实现基本曲线和曲面,仅仅提供几何对象的数据结构。
1.2.1 Geom 开发包
Geom_AxisPlacement 、Geom_Axis1Placement、Geom_Axis2Placement、
Geom_BezierCurve、Geom_BezierSurface、Geom_BoundedCurve、
Geom_BoundedSurface、Geom_BSplineCurve、Geom_BSplineSurface、
Geom_CartesianPoint、Geom_Circle、Geom_Conic、Geom_ConicalSurface、
Geom_Curve、Geom_CylindricalSurface、Geom_Direction、
Geom_ElementarySurface、Geom_Ellipse、Geom_Geometry、
Geom_HSequenceOfBSplineSurface、Geom_Hyperbola、Geom_Line、
Geom_OffsetCurve、Geom_OffsetSurface、Geom_OsculatingSurface、
Geom_Parabola、Geom_Plane、Geom_Point、
Geom_RectangularTrimmedSurface、
Geom_SequenceOfBSplineSurface、Geom_SphericalSurface、Geom_Surface
Geom_SurfaceOfLinearExtrusion、Geom_SurfaceOfRevolution、
Geom_SweptSurface、Geom_ToroidalSurface、Geom_Transformation、
Geom_TrimmedCurve、Geom_UndefinedDerivative、Geom_UndefinedValue、
Geom_Vector、Geom_VectorWithMagnitude

使用:
Geom_BezierCurve:
Handle(Geom_Curve) geomCurve=circleForPolygon.Value();
Geom_BSplineCurve:
Geom_Curve:
Geom_TrimmedCurve:
Handle(Geom_TrimmedCurve) geomLine=GC_MakeSegment(pnt1,pnt2);
Geom_Point:
1.2.2 GC 开发·包
GC_MakeArcOfCircle、GC_MakeArcOfEllipse、GC_MakeArcOfHyperbola、
GC_MakeArcOfParabola、GC_MakeCircle、GC_MakeConicalSurface、
GC_MakeCylindricalSurface、GC_MakeEllipse、GC_MakeHyperbola、
GC_MakeLine、GC_MakeMirror、GC_MakePlane、GC_MakeRotation、
GC_MakeScale、GC_MakeSegment、GC_MakeTranslation、
GC_MakeTrimmedCone、GC_MakeTrimmedCylinder、GC_Root

GC_MakeArcOfCircle:
Handle(Geom_TrimmedCurve) geomArc=GC_MakeArcOfCircle(pnt1, pnt2, pnt3);
GC_MakeCircle:
GC_MakeCircle circleForPolygon(gp_Circ(ax2,radius));
GC_MakeEllipse:
GC_MakeSegment:
Handle(Geom_TrimmedCurve) segOfPaintPolygon1=
GC_MakeSegment(centerOfCircle, radiusOfCircle);
示例:
Handle(Geom_TrimmedCurve) aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4);
Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1 , aPnt2);
Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4 , aPnt5);
通过 IsDone 和 Value 方法来保证更安全的使用这些类:
GC_MakeSegment mkSeg (aPnt1 , aPnt2);
Handle(Geom_TrimmedCurve) aSegment1;
if(mkSegment.IsDone())
{ aSegment1 = mkSeg.Value(); … }
1.3 轮廓:拓扑结构
TopoDS 开发包:这个开发包可以将几何对象关联在一起,让它们生成一个
复合对象。
TopoDS 开发包的所有对象都是由 TopoDS_Shape 类派生而来。
图形 Open CASCADE Class 描述
Vertex (顶点) TopoDS_Vertex 表示几何体上的一个点
Edge (边) TopoDS_Edge 表示一个曲线和一个有边界的向量
Wire (网格) TopoDS_Wire 由顶点连起来的一系列边
Face (面) TopoDS_Face 由闭合的网格组成的边界平面
Shell (壳) TopoDS_Shell 通过边连接起起来一组面
Solid (体) TopoDS_Solid 由壳组成的有边界的三维空间
CompSolid (复合体) TopoDS_CompSolid 通过面连接的一组体
Compound (复合对象) TopoDS_Compound 由上面各种图形形成的一个
集合
TopoDS 包中只提供了拓扑实体的数据结构。在 BRepBuilderAPI 包中,可以
找到计算标准拓朴对象的算法类。
为了创建一个边,通过前面得出曲线,使用 BRepBuilderAPI_MakeEdge 类
来完成:
TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);
TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle);
TopoDS_Edge aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2);
TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aPnt1 , aPnt3);
TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aPnt4 , aPnt5);
为了将边链接起来,需要通过 BRepBuilderAPI_MakeWire 创建网格。
TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(aEdge1 , aEdge2 , aEdge3);
OCC 可以根据一个形状和一个方向生成实体:
形状 生成
Vertex 顶点 Edge 边
Edge 边 Face 表面
Wire 网格 Shell 壳
Face 表面 Solid 体
Shell 壳 Compound of Solids 组合体
当前的轮廓是一个 wire,根据上面的表,应该通过 wire 获得 face,再通过 face 生成 solid。
用 BRepBuilderAPI_MakeFace 类创建 face。如前面所说, face 是一个由闭合 wire 组
成的表面轮廓的一部分。通常,BRepBuilderAPI_MakeFace 类可以将一个或多个 wire 生成
face。
如果 wire 在一个平面上,则 surface 将自动生成。
TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);
BRepPrimAPI 开发包提供了很多类来创建拓扑图元:boxes, cones, cylinders, spheres, 等
等。 其中 BRepPrimAPI_MakePrism 类。 上面的柱体可以这个创建:
gp_Vec aPrismVec(0 , 0 , myHeight);
TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile , aPrismVec);

opencscade 对topo线进行遍历 获取所有线转化为几何类型 并存储为裁剪几何曲线

QVector<Handle_Geom_TrimmedCurve>
tool_contact_domain_extraction::explore_curve(TopoDS_Shape &contact_curve)
{
//将 topo 线转换为 Geom 线
QVector<Handle_Geom_TrimmedCurve> store_trimmed_curve;
TopExp_Explorer ex_contact_curve;//存储遍历出的所有交线
for (ex_contact_curve.Init(contact_curve, TopAbs_EDGE);
ex_contact_curve.More(); ex_contact_curve.Next())
{
TopoDS_Edge topo_contact_part_Edge = TopoDS::Edge(ex_contact_curve.Current());
Standard_Real first, last;
Handle(Geom_Curve)geom_contact_part_curve = BRep_Tool::Curve(topo_contact_part_Edge, first, last);

//需要转换为裁剪曲线,然后参与计算,不然会出错
Handle(Geom_TrimmedCurve)geom_contact_trimmed_curve = new
Geom_TrimmedCurve(geom_contact_part_curve, first, last);
store_trimmed_curve.push_back(geom_contact_trimmed_curve);
}
return store_trimmed_curve;
}

opencascade去重复线

opencascade去重复线采用布尔运算去重复线 但是比较耗时
代码如下:

Handle(Geom_TrimmedCurve) l1= GC_MakeSegment(gp_Pnt(0,0,0),gp_Pnt(100,0,0));
Handle(Geom_TrimmedCurve) l2= GC_MakeSegment(gp_Pnt(0,0,0),gp_Pnt(100,0,0));
TopoDS_Edge l11=BRepBuilderAPI_MakeEdge(l1);
TopoDS_Edge l22=BRepBuilderAPI_MakeEdge(l2);
Handle(AIS_Shape) l111 = new AIS_Shape(l11);
H_AisContext->SetColor(l111,Quantity_NOC_YELLOW,1);
H_AisContext->Display(l111, Standard_True);
Handle(AIS_Shape) l222 = new AIS_Shape(l22);
H_AisContext->SetColor(l222,Quantity_NOC_YELLOW,1);
H_AisContext->Display(l222, Standard_True);
BRepAlgoAPI_Common anOp;
anOp.SetFuzzyValue (1);
TopTools_ListOfShape anArg1, anArg2;
anArg1.Append (l11);
anArg2.Append (l22);
anOp.SetArguments (anArg1);
anOp.SetTools (anArg2);
anOp.Build();
TopoDS_Shape aRes = anOp.Shape();
Handle(AIS_Shape) l3 = new AIS_Shape(aRes);
H_AisContext->SetColor(l3,Quantity_NOC_RED,1);
H_AisContext->Display(l3, Standard_True);

拓扑 topo 转几何 geom

Standard_Real First=1, Last=3;
gp_Pnt P1(0, 0, 0), P2(10, 0, 0);
TopoDS_Edge edge;
edge=BRepBuilderAPI_MakeEdge(P1,P2);
Handle(Geom_Curve) curve;
curve = BRep_Tool::Curve(edge, First, Last);
gp_Pnt Milieu = curve->Value(First);

opencscade 二维曲线求交

Standard_Real tolerance = Precision::Confusion();
Geom2dAPI_InterCurveCurve ICC (SPL,TC1,tolerance); –得到交点
Standard_Integer NbPoints =ICC.NbPoints();
gp_Pnt2d PK;
for (Standard_Integer k = 1;k<=NbPoints;k++)
{
PK = ICC.Point(k);
// 针对每个交点,进行相应处理;
}–BR

–BRepBuilderAPI_NurbsConvert:用来将一个图形转化为 NURBS 几何体;
在这里插入图片描述

opencscade 缩放

gp_Vec x(firstPnt,lastPnt);
gp_Trsf trsf;
trsf.SetTranslation(x);
BRepBuilderAPI_Transform transform(trsf);
transform.Perform(shape);
Handle_AIS_Shape aisShape = new
AIS_Shape(transform.Shape());

opencascade 获取选择对象

H_AisContext->InitSelected();
while(H_AisContext->MoreSelected()){
if(H_AisContext->HasSelectedShape()) {
TopoDS_Shape shape= H_AisContext->SelectedShape();
QTexPaint->append("获取到 Shape");
}else{
auto obj = H_AisContext->SelectedInteractive();
QTexPaint->append("获取到交互对象");
}
H_AisContext->NextSelected();
}

交互式转拓扑

AIS_Shape 转 topo
AIS_ListOfInteractive aList;
myAISContext->DisplayedObjects(aList);
AIS_ListIteratorOfListOfInteractive aListIterator;
for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next())
{
myAISContext->SetSelected(aListIterator.Value());
TopoDS_Shape S =myAISContext->SelectShape(); this step will have error. }

全选

AIS_ListOfInteractive objects;
H_AisContext->DisplayedObjects (objects);
AIS_ListIteratorOfListOfInteractive iobject (objects);
while (iobject.More ())
{
H_AisContext->SetSelected(iobject.Value(),Standard_True);
//myAISContext()->Redisplay(iobject.Value());
//H_AisContext->Display(iobject.Value(), 1, -1, Standard_True, Standard_True);
Handle(StdSelect_FaceFilter) Fil1 = new
StdSelect_FaceFilter(StdSelect_Revol);
Handle(StdSelect_FaceFilter) Fil2 = new
StdSelect_FaceFilter(StdSelect_Plane);
H_AisContext->AddFilter(Fil1);
H_AisContext->AddFilter(Fil2);
iobject.Next ();
}

拾取图形

myView.get();
myMainSel->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
qDebug()<<"theXPMin"<<theXPMin<<"theYPMin"<<theYPMin
<<"theXPMax"<<theXPMax<<"theYPMax"<<theYPMax;
AIS_MapOfInteractive map;
for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked();
++aPickIter)
{
Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked
(aPickIter);
//if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk
(anOwner))
{
// continue;
// mySelection->Select (anOwner);
//myContext->ShiftSelect(true);
Handle(AIS_InteractiveObject) obj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
qDebug()<<"myMainSel->NbPicked()"<<myMainSel->NbPicked();
obj->SetHilightMode(1);
map.Add(obj);
myContext->Display(obj,Standard_True);
// myContext->SetColor(obj,Quantity_NOC_RED,1);
// myContext->SetWidth(obj,2,1);
}
}

绘制点

void Draw2DPoint()
{
gp_Pnt pnt(1, 2, 0);
TopoDS_Shape edge= BRepBuilderAPI_MakeVertex(pnt);
Handle(AIS_Shape) hAisShape = new AIS_Shape(edge);
H_AisContext->Display(hAisShape, Standard_True);
H_AisContext->SetColor(hAisShape,Quantity_NOC_BLACK,1);
}

绘制直线

gp_Pnt aPnt1(100, 0, 0);
gp_Pnt aPnt2(200, 0, 0);
Handle(Geom_TrimmedCurve) line= GC_MakeSegment(aPnt1, aPnt2);
TopoDS_Edge lineEdge= BRepBuilderAPI_MakeEdge(line);
Handle(AIS_Shape) hAisShape = new AIS_Shape(lineEdge);
H_AisContext->Display(hAisShape, Standard_True)

绘制圆

gp_Pnt aPnt1(100, 30, 0);
gp_Dir aPnt2(0, 0, 1);
gp_Ax2 dd(aPnt1,aPnt2);
gp_Circ circle(dd,30);
TopoDS_Edge circleEdge= BRepBuilderAPI_MakeEdge(circle);
Handle(AIS_Shape) hAisShape = new AIS_Shape(circleEdge);
H_AisContext->Display(hAisShape, Standard_True);

等分椭圆

gp_Pnt aPnt1(-350, 30, 0);
gp_Dir aPnt2(0, 0, 1);
gp_Ax2 axis2(aPnt1,aPnt2);
GC_MakeEllipse ellips(gp_Elips(axis2,300,150));
Handle(Geom_Curve) geomCurve=ellips.Value();
//曲线的起始参数
double firstParameter = geomCurve->FirstParameter();
//曲线的终止参数
double lastParameter = geomCurve->LastParameter();
//计算整段曲线的参数
double Delta = lastParameter - firstParameter;
//计算等分后每一段的参数跨度
Delta = Delta / 6;
int i = 0;
double u = 0.0;
qDebug()<<firstParameter<<lastParameter<<Delta;
TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(geomCurve);
Handle(AIS_Shape) hAisShape = new AIS_Shape(anEdge1);
H_AisContext->Display(hAisShape, Standard_True);
for (u = firstParameter, i = 1; i <= 7; i++, u += Delta)
{
//计算每个等分点的世界坐标
gp_Pnt point = geomCurve->Value(u);
TopoDS_Shape anEdge1 = BRepBuilderAPI_MakeVertex(point);
Handle(AIS_Shape) hAisShape = new AIS_Shape(anEdge1);
H_AisContext->Display(hAisShape, Standard_True);
H_AisContext->SetColor(hAisShape,Quantity_NOC_BLACK,1);
}

绘制椭圆弧

BRepBuilderAPI_MakeEdge brepEdge(gpElips, angleStart, angleEnd);
TopoDS_Edge edge = brepEdge.Edge();

绘制圆弧

 gce_MakeCirc gcCircle (Pnt, Dir, radius);
 gp_Circ gpCircle = gcCircle.Value();
 GC_MakeArcOfCircle gcArcCircle = GC_MakeArcOfCircle(gpCircle, start.GpPnt, end.GpPnt, true);
 BRepBuilderAPI_MakeEdge brepEdge = BRepBuilderAPI_MakeEdge(gcArcCircle.Value());
 TopoDS_Edge edge = brepEdge.Edge();

创建多段线

MakePolygon

#点集绘制面的方法1
GeomAPI_PointsToBSplineSurface

	auto p1 = gp_Pnt(0, 0, 0);
	auto	p2 = gp_Pnt(100, 0, 0);
	auto	p3 = gp_Pnt(100, 100, 0);
	auto	p4 = gp_Pnt(0, 100, 0);
	auto	array = TColgp_Array2OfPnt(1, 2, 1, 2);
	array.SetValue(1, 1, p1);
	array.SetValue(2, 1, p2);
	array.SetValue(1, 2, p4);
	array.SetValue(2, 2, p3);
	auto curve = GeomAPI_PointsToBSplineSurface(array, 3, 8, GeomAbs_C2, 0.001).Surface();
	auto face = BRepBuilderAPI_MakeFace(curve, 1e-6).Face();

在这个示例中,我们首先创建了一个TColgp_Array2OfPnt类型的二维点集数组,并填充了四个点。然后,我们使用GeomAPI_PointsToBSplineSurface类从点集中创建了一个B样条曲面。最后,我们使用BRepBuilderAPI_MakeFace类从这个B样条曲面创建了一个TopoDS_Face对象,它代表了我们想要绘制的面。

缩放视图

void View::wheelEvent(QWheelEvent *event)
{
myView->StartZoomAtPoint(event->pos().x(),event->pos().y());
myView->ZoomAtPoint(0, 0, event->angleDelta().y(), 0);
}

平移视图

myView->Pan( point.x() - myXmax, myYmax - point.y() );
myXmax = point.x();
myYmax = point.y();

opencascade 获取坐标值

将屏幕坐标转化为occ坐标

void View::mouseMoveEvent(QMouseEvent* e)
{
onMouseMove( e->buttons(), e->pos() );
gp_Pnt P0; //定义点
gp_Vec Vp2; //定义向量
double X,Y,Z;//X,Y,Z 为鼠标转换为 occ 的 xyz 三轴的点
myView->Convert(e->pos().x(),e->pos().y(),X,Y,Z); //将鼠标的 x,y 转换为
occview 的 X,Y,Z
setXCoordinate(X);
setYCoordinate(Y);
qDebug()<<"X"<<X<<"Y"<<Y;
}

opencascade 显示坐标系

myView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.10, V3d_WIREFRAME);
myView->SetProj(V3d_Zpos); //显示 xy 轴平面

opencascade 点到线的距离 occ

类 Extrema_ExtPC

方法
Extrema_ExtPC (const gp_Pnt &P, const Adaptor3d_Curve &C, const Standard_Real Uinf, const Standard_Real Usup, const Standard_Real TolF=1.0e-10);

Extrema_ExtPC (const gp_Pnt &P, const Adaptor3d_Curve &C, const Standard_Real TolF=1.0e-10);

void Initialize (const Adaptor3d_Curve &C, const Standard_Real Uinf, const Standard_Real Usup, const Standard_Real TolF=1.0e-10);

void Perform (const gp_Pnt &P);

Standard_Boolean IsDone () const

Standard_Real SquareDistance (const Standard_Integer N) const

Standard_Integer NbExt () const

Standard_Boolean IsMin (const Standard_Integer N) const

const Extrema_POnCurv & Point (const Standard_Integer N) const

void TrimmedSquareDistances (Standard_Real &dist1, Standard_Real &dist2, gp_Pnt &P1, gp_Pnt &P2) const

示例
BRepAdaptor_Curve curve(edge);
Extrema_ExtPC ext(gp_pnt,curve);
if(ext.IsDone())
{
auto num = ext.NbExt ();
for(int i = 1;i <= num;i++)
{
if(!ext.IsMin(i)) continue;
auto dis = ext.SquareDistance(i);
}
}

Opencascade矩阵变换-BRepBuilderAPI_GTransform

OpenCASCADE中使用算法BRepBuilderAPI_Transform来实现:平移、旋转、缩放及镜像变换,该方法会使图形变形。

1:任意比例变换

BRepBuilderAPI_GTransform
示例代码:
gp_Mat mat = new gp_Mat(m11, m12, m13,
m21,m22, m23,
m31,m32, m33);
gp_GTrsf gtsf = new gp_GTrsf(mat, new gp_XYZ(0, 0, 0));
BRepBuilderAPI_GTransform newShape1 = new BRepBuilderAPI_GTransform(rec.Shape, gtsf, false);
rec .Shape = newShape1.Shape();

2:固定比例变换

BRepBuilderAPI_Transform
gp_Trsf myTrsf;
TopoDS_Shape aShape ;
myTrsf.SetScale(positionPnt, proportion);
BRepBuilderAPI_Transform bTransf(aShape, myTrsf);
TopoDS_Shape newShape=bTransf;
2023.5.25

附:交流邮箱:yzxxty39@163.com

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值