OpenCASCADE 基礎


OpenCASCADE 基礎

轉載

一直在用OCC作項目,但這方面的中文資料很少,看來OCC在中國還不是十分普及;

后來,項目中使用OCC和DirectX結合使用,取得了很好的效果;

隨着OCC6.3版本的推出,Open CASCADE在速度方面已有了很大的改變。以下為一些OCC的基礎知識,願與各位OCC愛好者共同學習;

一:OCC中的基礎類:

gp_Pnt

在OCC中,gp_Pnt表示一個頂點,gp_Vec表示一個向量,可以用兩個頂點來生成一個向量。

比如:

gp_Pnt P1(0,0,0); 

gp_Pnt P2(5,0,0); 

gp_Vec V1 (P1,P2);

向量有一個方法.IsOpposite(),可以用來測試兩個向量的方向是相對還是平行;

比如: 

gp_Pnt P3(-5,0,2);

gp_Vec V2 (P1,P3); 

Standard_Boolean result =V1.IsOpposite(V2,Precision::Angular());

另外向量還有一些重要方法:

--Standard_Real Magnitude() const;計算向量的大小;

--Standard_Real SquareMagnitude() const;計算向量的平方;

--向量的加減乘除操作;

--向量的單位化;

--通過一個點,線,面得出其鏡像的向量;

--向量的旋轉,平移,縮放;

具體的函數名稱可以看OCC的頭文件說明;

有時需要決定一組空間點是位於一個點;一條直線,或一個平面,或一個空間:

OCC中提供了相應的算法;

比如:

TColgp_Array1OfPnt array (1,5); // sizing array 

array.SetValue(1,gp_Pnt(0,0,1)); 

array.SetValue(2,gp_Pnt(1,2,2)); 

array.SetValue(3,gp_Pnt(2,3,3)); 

array.SetValue(4,gp_Pnt(4,4,4)); 

array.SetValue(5,gp_Pnt(5,5,5)); 

GProp_PEquation PE (array,1.5 ); 

if (PE.IsPoint()){   } //是否是同一個點 

gp_Lin L; 

if (PE.IsLinear()) {  L = PE.Line();  } //是否位於一條直線上; 

if (PE.IsPlanar()){   }  //是否在一個平面內; 

if (PE.IsSpace()) {   } 

gp_Dir類:

此類用來描述3D空間中的一個單位向量;

常用方法:

(1):IsEqual(const gp_Dir& Other,const Standard_Real AngularTolerance) const;兩個單位向量是否相等;

(2):IsNormal(const gp_Dir& Other,const Standard_Real AngularTolerance) const;兩個單位向量的夾角是否是PI/2;

(3):IsOpposite(const gp_Dir& Other,const Standard_Real AngularTolerance) const;兩個單位向量是否方向相反;

(4):IsParallel(const gp_Dir& Other,const Standard_Real AngularTolerance) const;兩個單位向量夾角O或PI;

(5):Angle(const gp_Dir& Other) const;求兩個向量之間的夾角;

(6):void CrossCross(const gp_Dir& V1,const gp_Dir& V2) ;計算三個向量之間的叉積;

(7):Standard_Real Dot(const gp_Dir& Other) const;計算點積;

(8):Standard_Real DotCross(const gp_Dir& V1,const gp_Dir& V2) const;計算叉積再點積;

(9):gp_Dir Reversed() const;得到反方向,

在OCC中用 gp_Lin2d 類,來生成一個二維空間的直線,有它的原點和單位向量;

gp_Ax2d類:

通過原點和X方向單位和Y方向單位建立一個二維坐標系;利用sense參數可以決定是右手系還是左手系;

可以利用平移、旋轉、縮放、鏡像來更改坐標系;

類似地,gp_Ax3類:

用來描述一個3D空間的坐標系。而gp_Ax2類用來表示一個二維空間坐標系;可以為右手系,也可以是左手系;

二、曲線類

GeomAPIGeomConvert包:

GeomAPI開發包提供了一個幾何體的可編程應用程序接口;

比如:

求點P和曲線C的距離D:

D = GeomAPI_ProjectPointOnCurve(P,C);

或者

GeomAPI_ProjectPointOnCurve PonC(P,C);

D = PonC.LowerDistance();

GeomConvert包提供了一些全局函數,可以用來實現轉化一個Geom曲線為BSpline曲線等;

比如:

Handle(Geom_BSplineSurface) aPipeSurface = 

Handle(Geom_BSplineSurface)::DownCast(aPipe.Surface()); 

Handle(Geom_BSplineSurface) anotherBSplineSurface = 

GeomConvert::SplitBSplineSurface(aPipeSurface,1,2,3,6);

OCC中三維幾何曲線的類型有:

--線

--園

--橢圓

--二次曲線

--拋物線

--Bezier曲線

--BSpline曲線

可以將一個二維的幾何曲線轉化為某個平面內的一個三維曲線:

比如:

Standard_Real radius = 5; 

gp_Ax2d ax2d(gp_Pnt2d(2,3),gp_Dir2d(1,0)); 

//生成一個二維園 

Handle(Geom2d_Circle) circ2d = new Geom2d_Circle(ax2d,radius); 

gp_Ax2d circ2dXAxis = circ2d->XAxis(); 

// 然后,在這個平面里轉化為三維曲線; 

Handle(Geom_Curve) C3D = GeomAPI::To3d(circ2d,gp_Pln(gp_Ax3(gp::XOY()))); 

Handle(Geom_Circle) C3DCircle = Handle(Geom_Circle)::DownCast(C3D); 

gp_Ax1 C3DCircleXAxis = C3DCircle->XAxis(); 

另外,可以以將一個三維曲線,投影到一個平面內,從而生成一個二維曲線

gp_Pln ProjectionPlane(gp_Pnt(1,1,0),gp_Dir( 1,1,1 )); 

Handle(Geom2d_Curve) C2D = GeomAPI::To2d(C3D,ProjectionPlane); 

Handle(Geom2d_Circle) C2DCircle =Handle(Geom2d_Circle)::DownCast(C2D); 

gp_Ax2d C2DCircleXAxis = C2DCircle->XAxis();

將一個基本幾何圖形進行空間變換可以使用它自帶的函數:

比如:

Handle(Geom_Geometry) aRotatedEntity  = circle->Rotated(gp::OZ(),PI/4);

如果想獲取圖形的類型名稱: 

Standard_CString aRotatedEntityTypeName = aRotatedEntity->DynamicType()->Name();

gp_Parab2d類:

描述一個平面內的拋物線;

示例:

gp_Pnt2d P(2,3); 

gp_Dir2d D(4,5); 

gp_Ax22d A(P,D); 

gp_Parab2d Para(A,6);

GCE2d_MakeParabola類:

生成一個拋物線圖形;

Geom2d_BSplineCurve類:

描述樣條曲線;

Geom2dAPI_Interpolate類:

通過一組點來修改一個樣條曲線;

FairCurve_Batten類:

用一個常量或線性增加的值來構造曲線;可以用來設計木紋或塑料板條;圖形為二維的,可以模擬物理樣條或板條.

Geom2d_TrimmedCurve類:

此類通過兩個值,定義曲線的一部分,

--可以用來計算曲線的參數值和點坐標;

--可以得到曲線的一般特征,比如連續的等級,封閉特點,周期性,邊界參數;

--當用一個矩陣應用於曲線或原始曲線轉化后進行相應參數的改變;

所有的曲線必須幾何連續,曲線至少一階可導。一般來說,在生成一個曲線時,要先檢查一下所應用的參數是否可以生成一個光滑曲線;否則會出現錯誤;

另外注意一點:不可以構造空長度的曲線或自相交的曲線;

此類的基類是Geom2d_BoundedCurve類:

它是一個抽象類;描述二維空間中的邊界曲線的一般行為;除了Geom2d_TrimmedCurve是它的一個派生類外,它還有二個派生類:

- Geom2d_BezierCurve

- Geom2d_BSplineCurve

Geom2d_BoundedCurve類的基類是Geom2d_Curve類:

Geom2d_Curve:抽象類;此抽象類描述了2D空間的曲線的一般特征;派生出的類有多個:包括直線,園,二次曲線,Bizier,BSpline曲線等;這些曲線的特點是可以參數化;

Geom2d_Curve類的基類是Geom2d_Geometry類;

此抽象類主要定義了曲線的變換,平移,旋轉,縮放及拷貝等方法;

Geom2d_Geometry類的基類是MMgt_TShared類;

此抽象類為管理對象的基類,可以引用計數,及刪除方法;

Standard_Transient:此抽象類為所有類共同的基類;

Geom2dAPI_InterCurveCurve類:

此類用來實現二維曲線的相交;

一種情況是曲線與曲線的相交,另外一種情況是曲線自身的相交;

主要方法有:

--Standard_Integer NbPoints() const;相交點數;

--Standard_Integer NbSegments() const;切線相交數;

--void Segment(const Standard_Integer Index,Handle(Geom2d_Curve)& Curve1,Handle(Geom2d_Curve)& Curve2)

const;返回其中一個線段;

下面的示例是兩個曲線相交的例子:

首先,生成第一個曲線,在這里,應用點數組來生成一個曲線;

--定義數組

Handle(TColgp_HArray1OfPnt2d) harray =  new TColgp_HArray1OfPnt2d (1,5); // sizing harray 

--輸入點數組的值

harray->SetValue(1,gp_Pnt2d (0,0)); 

harray->SetValue(2,gp_Pnt2d (-3,1)); 

harray->SetValue(3,gp_Pnt2d (-2,5)); 

harray->SetValue(4,gp_Pnt2d (2,9)); 

harray->SetValue(5,gp_Pnt2d (-4,14)); 

--檢測一下點與點之間是否為同一點;0.01為公差值,依實際需要可以更改此參數; 

Geom2dAPI_Interpolate anInterpolation(harray,Standard_False,0.01); 

--生成曲線

anInterpolation.Perform(); 

Handle(Geom2d_BSplineCurve) SPL = anInterpolation.Curve(); 

--第二個曲線用兩點來生成 

gp_Pnt2d P1(-1,-2);gp_Pnt2d P2(0,15);gp_Dir2d V1 = gp::DY2d(); 

Handle(Geom2d_TrimmedCurve) TC1=  GCE2d_MakeSegment(P1,V1,P2); 

--下面進行曲線的求交 

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); 

// 針對每個交點,進行相應處理; 

Geom2d_OffsetCurve類:

此類用來實現偏移曲線;

比如:

--生成一個曲線

TColgp_Array1OfPnt2d array (1,5); // sizing array 

array.SetValue(1,gp_Pnt2d (-4,0)); array.SetValue(2,gp_Pnt2d (-7,2)); 

array.SetValue(3,gp_Pnt2d (-6,3)); array.SetValue(4,gp_Pnt2d (-4,3)); 

array.SetValue(5,gp_Pnt2d (-3,5)); 

Handle(Geom2d_BSplineCurve) SPL1 = Geom2dAPI_PointsToBSpline(array); 

--生成一個偏移曲線 

Standard_Real dist = 1; 

Handle(Geom2d_OffsetCurve) OC = 

new Geom2d_OffsetCurve(SPL1,dist); 

Standard_Boolean result = OC->IsCN(2); 

GccAna_Pnt2dBisec類

此類實現兩點之間的等分線.

示例:

gp_Pnt2d P1(1,2); 

gp_Pnt2d P2(4,5); 

gp_Lin2d L; 

GccAna_Pnt2dBisec B(P1,P2); 

if (B.IsDone()) 

{ L = B.ThisSolution(); }

因為所生成的為直線,所以顯示時要轉化為線段:

if (B.IsDone())

{

Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment(L,-8,8);

Handle(ISession2D_Curve) aCurve = new ISession2D_Curve(aLine);

aDoc->GetISessionContext()->Display(aCurve, Standard_False);

}

gce_MakeCirc2d類

用來創建園:創建園的方法很多,主要構造方法有:

--園心和通過的一點;

--通過一個園和一個距離值,創建一個同心園;

--三點決定一個園;

--園心和半徑;

gp_Elips2d類:

可以生成一個橢園,也可以生成橢園上的一段園弧;

比如:

Standard_Real major = 12; 

Standard_Real minor = 4; 

gp_Ax2d axis = gp::OX2d(); 

gp_Elips2d EE(axis,major,minor);; 

Handle(Geom2d_TrimmedCurve) arc = GCE2d_MakeArcOfEllipse(EE,0.0,PI/4);
上面是利用長短軸的方法構造橢圓,也可以用二次方程的方式來構造橢園;

其中橢園類中方法可以求出焦點1和焦點2的位置,兩焦點之間的位置,離心率;旋轉,平移,縮放等操作.

三、關於面的類

gp_Pln:

定義一個平面,構造的方法可以是點法式,或通過ABCD系數;

另外,還提供了一些常用的方法,比如:

--求點到平面,線到平面,平面與平面的距離及平方距離;

--點是否在平面內,線是否在平面內;

--通過一個點,一個軸的鏡像平面;

--平面的旋轉,縮放與平移;

Geom_ElementarySurface類:

此類用來描述一個表面,此類的派生類有:

平面;園柱面;錐面;球面;園環面;

它的基類是Geom_Surface,是一個抽象類;

Geom_Surface類的基類是Geom_Geometry類;

Geom_RectangularTrimmedSurface類:

用來生成一個有邊界的平面;

比如:

Handle(Geom_Plane) aProjectionPlane = GC_MakePlane(ProjectionPlane).Value();

Handle(Geom_RectangularTrimmedSurface) aProjectionPlaneSurface=

new Geom_RectangularTrimmedSurface(aProjectionPlane,-8.,8.,-12.,12.); 

DisplaySurface(aDoc,aProjectionPlaneSurface);

此類的基類是Geom_BoundedSurface類;

此類的兄弟類還有

- Geom_BezierSurface,

- Geom_BSplineSurface

ConicalSurface類:用來創建一個園錐表面;

構造表面的方法有:

--已知一個園錐表面,和空間一點,過此點的平行於已知園錐表面;

--已知一個園錐表面,和一個距離,創建一個平行於已知園錐表面的園錐表面;

--通過四個點構造一個園錐表面;

--通過一個軸和兩個點;

--通過兩個點和兩個半徑;

GeomAPI_IntCS類:

此類用來計算一個園弧和和一個表面的交點或相交線段;

GeomFill_BSplineCurves類:

此類用來構造一個可以填充的BSpline表面,構造它可以用兩個三個或四個BSpline曲線作為邊界;

填充類型有三種:

enum GeomFill_FillingStyle {

GeomFill_StretchStyle,

GeomFill_CoonsStyle,

GeomFill_CurvedStyle

};

以下示例為用兩個樣條曲線生成一個表面:

GeomFill_FillingStyle Type = GeomFill_StretchStyle; 

GeomFill_BSplineCurves aGeomFill1(SPL1,SPL2,Type); 

Handle(Geom_BSplineSurface)  aBSplineSurface1 = aGeomFill1.Surface();

GeomFill_Pipe類:

此類用來構造一個pipe,沿着一個路徑sweep一個截面,這兩個都是曲線類型;一般來說,結果是一個BSpline表面;

常見的有幾種方法:

--給定一個路徑和一個半徑,截面是個園,位置是路徑的第一個點,

比如:

GeomFill_Pipe aPipe(SPL1,1); 

aPipe.Perform(); 

Handle(Geom_Surface) aSurface= aPipe.Surface(); 

Standard_CString aSurfaceEntityTypeName="Not Computed"; 

if (!aSurface.IsNull()) 

aSurfaceEntityTypeName = aSurface->DynamicType()->Name();

--給定一個路徑和一個截面。

比如:

Handle(Geom_Ellipse) E = GC_MakeEllipse( gp::XOY() ,3,1).Value(); 

GeomFill_Pipe aPipe2(SPL1,E); 

aPipe2.Perform(); 

Handle(Geom_Surface) aSurface2= aPipe2.Surface(); 

Standard_CString aSurfaceEntityTypeName2="Not Computed"; 

if (!aSurface2.IsNull())  { 

aSurfaceEntityTypeName2 = aSurface2->DynamicType()->Name(); 

aSurface2->Translate(gp_Vec(5,0,0));  }

--給定一個路徑和兩個截面,中間截面為過度線;

示例:

Handle(Geom_TrimmedCurve) TC1 = 

GC_MakeSegment(gp_Pnt(1,1,1),gp_Pnt(5,5,5)); 

Handle(Geom_TrimmedCurve) TC2 = 

GC_MakeSegment(gp_Pnt(1,1,0),gp_Pnt(4,5,6)); 

GeomFill_Pipe aPipe3(SPL1,TC1,TC2); 

aPipe3.Perform(); 

Handle(Geom_Surface) aSurface3 = aPipe3.Surface(); 

Standard_CString aSurfaceEntityTypeName3="Not Computed"; 

if (!aSurface3.IsNull()) 

aSurfaceEntityTypeName3 = aSurface3->DynamicType()->Name(); 

aSurface3->Translate(gp_Vec(10,0,0)); 

--給定一個路徑和N個截面,中間為過渡線;

一般情況下,所生結果為:NURBS,但是,在一些特殊的情況下,可以生成平面,園柱,球,園錐等;

參數,U,沿着截面的方向,V沿着路徑方向;

Geom_BezierSurface類:

生成一個Bezier表面;

Geom_OffsetSurface類:

用來偏移一個表面;

比如:

Standard_Real offset = 1; 

Handle(Geom_OffsetSurface) GOS = new Geom_OffsetSurface(aGeomSurface, offset);

Geom_SweptSurface類:

有兩個派生類,分別用來生成一個回轉體表面和一個延展體表面;

Geom_SurfaceOfLinearExtrusion:用來描述一個線性延展表面;

它的基類是:Geom_Surface類

比如:

Handle(Geom_BSplineCurve) aCurve =GeomAPI_PointsToBSpline(array).Curve(); 

gp_Dir aDir(1,2,3); 

Handle(Geom_SurfaceOfLinearExtrusion) SOLE =new Geom_SurfaceOfLinearExtrusion(aCurve,aDir); 

Handle(Geom_RectangularTrimmedSurface) aTrimmedSurface =new Geom_RectangularTrimmedSurface(SOLE,-10,10,false);

Geom_SurfaceOfRevolution類,表示一個回轉體表面;

比如:

Handle(Geom_BSplineCurve) aCurve = GeomAPI_PointsToBSpline(array).Curve(); 

Handle(Geom_SurfaceOfRevolution) SOR =new Geom_SurfaceOfRevolution(aCurve,gp::OX()); 

1:利用一個二維數組來生成曲面的方法:

TColgp_Array2OfPnt array3 (1,5,1,5); 

array3.SetValue(1,1,gp_Pnt (-4,-4,5));

...

array3.SetValue(2,1,gp_Pnt (-2,-4,4));

...

Handle(Geom_BSplineSurface) aSurf2 =GeomAPI_PointsToBSplineSurface(array3).Surface();

2:GeomAPI_ExtremaSurfaceSurface類:

計算兩個表面之間的極值點;

主要方法:

(1):Quantity_Length LowerDistance() const;計算兩個表面的最短距離;

(2):Standard_EXPORT  void LowerDistanceParameters(Quantity_Parameter& U1,Quantity_Parameter& V1,Quantity_Parameter& U2,Quantity_Parameter& V2) const;

得到第一個表面上的極值點的UV參數和第二個表面上的極值點的UV參數;

(3):void NearestPoints(gp_Pnt& P1,gp_Pnt& P2) const;得到第一個表面上的極值點和第二個表面上的極值點;

(4): Quantity_Length Distance(const Standard_Integer Index) const;得到第N個極值點的距離;

(5):Standard_Integer NbExtrema() const;極值的數目;

......

示例:

GeomAPI_ExtremaSurfaceSurface ESS(aSurf1,aSurf2);

Quantity_Length dist = ESS.LowerDistance();

gp_Pnt P1,P2;

ESS.NearestPoints(P1,P2);

gp_Pnt P3,P4;

Handle(Geom_Curve) aCurve;

Standard_Integer NbExtrema = ESS.NbExtrema();

for(Standard_Integer k=1;k<=NbExtrema;k++){

ESS.Points(k,P3,P4); 

aCurve= GC_MakeSegment(P3,P4).Value();

DisplayCurve(aDoc,aCurve,Quantity_NOC_YELLOW3,false);

}

一些OCC的基礎知識,願與各位OCC愛好者共同學習;mail:tongabcd@yeah.net

一:關於體的類

BRepBuilderAPI_MakeVertex

創建點;

BRepBuilderAPI_MakeEdge

此類用來創建邊;

比如,由直線生成邊:

gp_Lin line(gp_Ax1(gp_Pnt(10,10,10),gp_Dir(1,0,0)));

WhiteEdge = BRepBuilderAPI_MakeEdge(line,-20,10);

下面為生成四分之一園邊:

gp_Elips Elips(gp_Ax2(gp_Pnt(10,0,0),gp_Dir(1,1,1)),60,30);

RedEdge = BRepBuilderAPI_MakeEdge(Elips,0,PI/2);

下面是由曲線生成邊:

Handle (Geom_BezierCurve) curve = new Geom_BezierCurve(array);

BRepBuilderAPI_MakeEdge ME (curve);

GreenEdge = ME;

V3 = ME.Vertex1();

V4 = ME.Vertex2();

BRepBuilderAPI_MakeWire

用來創建一個Wire類;

用一個Wire和一個邊來生成一個新的Wire:

ExistingWire = BRepBuilderAPI_MakeWire(Edge2);

Edge3 = BRepBuilderAPI_MakeEdge(gp_Pnt(-300,0,-80),gp_Pnt(-90,20,-30));

BRepBuilderAPI_MakeWire MW1(ExistingWire,Edge3);

if (MW1.IsDone()) {YellowWire = MW1;}

用一個Wire和添加邊的方法來生成Wire:

BRepBuilderAPI_MakeWire MW;

MW.Add(ExistingWire2);

MW.Add(Edge5);

MW.Add(Edge6);

MW.Add(Edge7);

if (MW.IsDone()) {

WhiteWire = MW.Wire();

LastEdge = MW.Edge();

LastVertex = MW.Vertex();

}

BRepBuilderAPI_MakeFace

生成一個面;有多種生成面的方法;

--通過一個封閉曲線生成面:

BRepBuilderAPI_MakeFace(curve);

--通過一個Wire生成面:

BrownFace = BRepBuilderAPI_MakeFace(YellowWire);

Bnd_Box2d:

定義一個二維空間的邊界盒,可以得出邊界盒各個點的值,有時,在某個方向是無限大,這種情況下,稱為在此方向上是開放的;

示例:

Bnd_Box2d aCBox; 

Geom2dAdaptor_Curve GACC (C); 

BndLib_Add2dCurve::Add (GACC,Precision::Approximation(),aCBox);

Bnd_Box:

定義一個三維空間的邊界盒,可以擴大或縮小邊界盒,也可以合並兩個軸對齊邊界盒;

BRepPrimAPI_MakeBox

用來生成一個立方體;

構造一個立方體可以是兩個對角點,一個角點及三個方向長度,可以是非軸對稱的:

TopoDS_Shape B2 = BRepPrimAPI_MakeBox (gp_Ax2(gp_Pnt(-200.,-80.,-70.), gp_Dir(1.,2.,1.)),  80.,90.,120.);

使用方法

TopoDS_Face& BottomFace() ;.可以得到立方體的底面;同樣,用其它類似的方法可以獲得頂面等;

方法TopoDS_Solid& Solid() ;可以將box轉化為一個Solid;

方法TopoDS_Shell& Shell() ;可以將box轉化為一個shell;

BRepPrimAPI_MakeCylinder

用來生成一個園柱體或園柱體的一部分;

比如: 

TopoDS_Shape C2 = BRepPrimAPI_MakeCylinder (gp_Ax2(gp_Pnt(200.,0.,200.), gp_Dir(0.,1.,0.)),40.,110.,210.*PI180);

BRepPrimAPI_MakeCone

生成一個園錐或園錐的一部分;

BRepPrimAPI_MakeSphere

生成球體或球體的一部分,可以是U方向切一部分或V方向切一部分;

BRepPrimAPI_MakeTorus

生成環或環的一部分;

BRepPrimAPI_MakeWedge

生成一個楔塊或楔塊的一部分;

BRepPrimAPI_MakePrism

生成一個線性的swept,稱為Prisms;它的基類是BRepPrimAPI_MakeSweep類;BRepPrimAPI_MakeSweep類的基類是

BRepBuilderAPI_MakeShape類

注意,原始基本圖形不可以包含任何實體:

應用此類時:

--頂點“推移”成邊:

TopoDS_Vertex V1 = BRepBuilderAPI_MakeVertex(gp_Pnt(-200.,-200.,0.));

Handle(AIS_Shape) ais1 = new AIS_Shape(V1); 

TopoDS_Shape S1 = BRepPrimAPI_MakePrism(V1,gp_Vec(0.,0.,100.));

Handle(AIS_Shape) ais2 = new AIS_Shape(S1);

--邊“推移”成面:.

TopoDS_Edge E = BRepBuilderAPI_MakeEdge(gp_Pnt(-150.,-150,0.), gp_Pnt(-50.,-50,0.));

Handle(AIS_Shape) ais3 = new AIS_Shape(E);

myAISContext->Display(ais3,Standard_False);

TopoDS_Shape S2 = BRepPrimAPI_MakePrism(E,gp_Vec(0.,0.,100.));

--Wires “推移”成Shells.

TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge(gp_Pnt(0.,0.,0.), gp_Pnt(50.,0.,0.));

TopoDS_Edge E2 = BRepBuilderAPI_MakeEdge(gp_Pnt(50.,0.,0.), gp_Pnt(50.,50.,0.));

TopoDS_Edge E3 = BRepBuilderAPI_MakeEdge(gp_Pnt(50.,50.,0.), gp_Pnt(0.,0.,0.));

TopoDS_Wire W = BRepBuilderAPI_MakeWire(E1,E2,E3);

TopoDS_Shape S3 = BRepPrimAPI_MakePrism(W,gp_Vec(0.,0.,100.));

--Faces “推移”成Solids.

TopoDS_Face F = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()),Wc);

Handle(AIS_Shape) ais7 = new AIS_Shape(F);

myAISContext->Display(ais7,Standard_False);

TopoDS_Shape S4 = BRepPrimAPI_MakePrism(F,gp_Vec(0.,0.,100.));

--Shells “推移”成復合實體

BRepPrimAPI_MakeRevol

一個回轉sweep體;

類繼承關系和前面類似:BRepBuilderAPI_MakeShape--〉BRepPrimAPI_MakeSweep-->BRepPrimAPI_MakeRevol

,對於角度而言,范圍是[0,2PI],默認值是2PI,生成規則:

- Vertex -> Edge.

- Edge -> Face.

- Wire -> Shell.

- Face-> Solid.

- Shell-> CompSolid.

BRepOffsetAPI_MakePipe

可以生成一個管道

類繼承關系是:BRepBuilderAPI_MakeShape--〉BRepPrimAPI_MakeSweep-->BRepOffsetAPI_MakePipe

以下為生成一個管道的示例過程:

--利用生成一個WIRE,作為管道的路徑:

Handle(Geom_BezierCurve) curve = new Geom_BezierCurve(CurvePoles);

TopoDS_Edge E = BRepBuilderAPI_MakeEdge(curve);

TopoDS_Wire W = BRepBuilderAPI_MakeWire(E);

--生成一個面,作為生成管道的截面:

gp_Circ c = gp_Circ(gp_Ax2(gp_Pnt(0.,0.,0.),gp_Dir(0.,1.,0.)),10.);

TopoDS_Edge Ec = BRepBuilderAPI_MakeEdge(c);

TopoDS_Wire Wc = BRepBuilderAPI_MakeWire(Ec);

TopoDS_Face F = BRepBuilderAPI_MakeFace(gp_Pln(gp::ZOX()),Wc);

--利用前兩步生成的路徑和截面來生成pipe:

TopoDS_Shape S = BRepOffsetAPI_MakePipe(W,F);

Handle(AIS_Shape) ais2 = new AIS_Shape(S);

BRepOffsetAPI_ThruSections

此類繼承自BRepBuilderAPI_MakeShape:創建一個loft,通過一組給定的sections,生成一個shell或一個solid;通常,section是wire;但是第一個和最后一個section可以是

vertices;

比如:

BRepOffsetAPI_ThruSections generator(Standard_False,Standard_True);

generator.AddWire(W1);

generator.AddWire(W2);

generator.AddWire(W3);

generator.AddWire(W4);

generator.Build();

TopoDS_Shape S1 = generator.Shape();

Handle(AIS_Shape) ais1 = new AIS_Shape(S1);

BRepBuilderAPI_MakePolygon

創建一個polygonal wires,可以通過一組點或向量生成,也可以先生成一個空的對象,再添加點。

示例1:

BRepBuilderAPI_MakePolygon P;

P.Add(gp_Pnt(0.,0.,0.));

P.Add(gp_Pnt(200.,0.,0.));

P.Add(gp_Pnt(200.,200.,0.));

P.Add(gp_Pnt(0.,200.,0.));

P.Add(gp_Pnt(0.,0.,0.));

TopoDS_Wire W = P.Wire();

示例2:

TopoDS_Wire wprof = BRepBuilderAPI_MakePolygon(gp_Pnt(0.,0.,0.),gp_Pnt(-60.,-60.,-200.));

BRepOffsetAPI_MakeEvolved

創建一個可展圖形,它是通過一個planar spine (face or wire)和一個rofile (wire)來生成的,它是一個非循環的sweep (pipe),用profile沿着spline;自相交點將被移除;

比如:

--沿着一個spline,sweep一個profile;

Standard_EXPORT BRepOffsetAPI_MakeEvolved(const TopoDS_Face& Spine,const TopoDS_Wire& Profil,const GeomAbs_JoinType Join = GeomAbs_Arc,const Standard_Boolean

AxeProf = Standard_True,const Standard_Boolean Solid = Standard_False,const Standard_Boolean ProfOnSpine = Standard_False,const Standard_Real Tol = 0.0000001);

AxeProf參數如果為true,R是0,X,Y,Z;如果solid為真,結果為一個solid或復合的solids;

示例:

TopoDS_Shape

S = BRepOffsetAPI_MakeEvolved(W,wprof,GeomAbs_Arc,Standard_True,Standard_False,Standard_True,0.0001);

BRepBuilderAPI_ModifyShape

當使用BRepTools來創建一個修改類,主要有以下派生類:

--BRepBuilderAPI_Copy:處理一個圖形的拷貝;

--BRepBuilderAPI_Transform 和BRepBuilderAPI_GTransform:用來對一個圖形應用幾何變形;

--BRepBuilderAPI_NurbsConvert:用來將一個圖形轉化為NURBS幾何體;

--BRepOffsetAPI_DraftAngle:創建一個tapered圖形;

BRepOffsetAPI_DraftAngle

創建一個tapered圖形;一般過程是:

--初始化構造算法;

--輸入要taper的特征面;

--實現算法;

--生成結果;

示例:

TopoDS_Shape S = BRepPrimAPI_MakeBox(200.,300.,150.);

BRepOffsetAPI_DraftAngle adraft(S);

TopExp_Explorer Ex;

for (Ex.Init(S,TopAbs_FACE); Ex.More(); Ex.Next()) {

TopoDS_Face F = TopoDS::Face(Ex.Current());

Handle(Geom_Plane) surf = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(F));

gp_Pln apln = surf->Pln();

gp_Dir dirF = apln.Axis().Direction();

if (dirF.IsNormal(gp_Dir(0.,0.,1.),Precision::Angular()))

adraft.Add(F, gp_Dir(0.,0.,1.), 15.*PI180, gp_Pln(gp::XOY()));

}

ais1->Set(adraft.Shape());

二:關於布爾等實體修改操作相關

BRepAlgoAPI_BooleanOperation

此類的基類是BRepBuilderAPI_MakeShape類,它是一個抽象類;

可以應用的操作有:BOP_SECTION 、BOP_COMMON、BOP_FUSE、BOP_CUT、BOP_CUT21

有時會產生錯誤,無法達到想要的結果,根據返回值,可以得到錯誤信息,含義是:

0:OK

1: 對象創建完成,但結果為空;

2:源圖形為空;

3:參數類型檢查錯誤;

4:不能為DSFiller分配內存;

5:此種類型參數的Builder無法工作;

6:不允許的操作;

7:不能為Builder分配內存;

>100 參見Builder錯誤信息;

相關的方法介紹:

--TopTools_ListOfShape& SectionEdges()方法:返回一組截面的邊,它們在布爾操作過程中生成;

--Standard_Boolean HasDeleted()方法:如果至少一個圖形對象被刪除了,返回為真;

--Standard_Boolean HasGenerated()方法:如果至少生成了一個圖形,返回為真;

--Standard_Boolean HasModified()方法:如果至少一個圖形被修改了,返回為真;

--TopTools_ListOfShape& Generated(const TopoDS_Shape& S) 方法:返回生成以后的圖形的集合;

--TopTools_ListOfShape& Modified2(const TopoDS_Shape& aS)方法:返回修改后的圖形的集合;

--Standard_Boolean IsDeleted(const TopoDS_Shape& aS)方法:如果圖形S已經被刪除,返回為真,即結果圖形中不包括圖形S;

-BOP_Operation Operation()方法:返回布爾操作的類型;

布爾操作類

包括有BRepAlgoAPI_Cut類, BRepAlgoAPI_Fuse類,BRepAlgoAPI_Common類:布爾交集;

BRepAlgoAPI_Section

計算兩個圖形或幾何體的截面,幾何對象可以是平面的表面,轉化為face.

示例:

給定兩個圖形S1和S2,計算在S1和S2上的邊,在新曲線上生成近似值,結果在第一部分上而不在第二部分上:

Standard_Boolean PerformNow = Standard_False;

BRepBoolAPI_Section S(S1,S2,PerformNow);

S.ComputePCurveOn1(Standard_True);

S.Approximation(Standard_True);

S.Build();

TopoDS_Shape R = S.Shape();

如果結果為空,調用NotDone();

常見方法:

--BRepAlgoAPI_Section(const Handle(Geom_Surface)& Sf1,const Handle(Geom_Surface)& Sf2,const Standard_Boolean

PerformNow = Standard_True);

用來生成線:

--兩個圖形SH1和SH2;

--圖形SH和平面P1;

--表面SF和圖形SH;

--兩個表面SF1和SF2;

參數PerformNow如果為真,將直接計算結果,如果為假,表示后面將通過Build()這個函數來計算結果;

生成后的圖形是由方法Shape()得出的;

這些相交的邊是獨立的,不在一個鏈表上,也不在一個wire上,如果不存在一個相交邊,返回結果為空;

示例:

--計算相交的基本邊--利用這些基本邊創建一個相交線--決定相交線在兩個圖形的哪個圖形的參數空間;

TopoDS_Shape S1 = ... , S2 = ... ;

Standard_Boolean PerformNow = Standard_False;

BRepAlgoAPI_Section S ( S1, S2, PerformNow );

S.ComputePCurveOn1 (Standard_True);

S.Approximation (Standard_True);

S.Build();

TopoDS_Shape R = S.Shape();

BRepFilletAPI_LocalOperation

基類是BRepBuilderAPI_MakeShape;

構造在一個shell的邊的園角;常用方法有

--void Add(const TopoDS_Edge& E)  = 0;在builder上添加一個輪廓線;

--void ResetContour(const Standard_Integer IC)  = 0;重置索引為IC的輪廓線;

--Standard_Integer NbContours() const = 0;返回輪廓線的數目;

--Standard_Integer Contour(const TopoDS_Edge& E) const = 0;返回邊E的輪廓線的索引,如果邊E不在輪廓線內,返回為O;

--Standard_Integer NbEdges(const Standard_Integer I) const = 0;返回在輪廓線I中的邊數;

--void Remove(const TopoDS_Edge& E)  = 0;移除一個邊;

--Standard_Real Length(const Standard_Integer IC) const = 0;得到某個輪廓線的長度;

--TopoDS_Vertex FirstVertex(const Standard_Integer IC) const = 0;返回某個輪廓線的第一個頂點;LastVertex方法返回最后一個頂點;

--Abscissa方法,返回某個頂點的橫坐標;

--Standard_Boolean ClosedAndTangent(const Standard_Integer IC) const如果某個輪廓線是封閉切線,返回為真;

--Standard_Boolean Closed(const Standard_Integer IC) const = 0;如果某個輪廓線是封閉,返回為真;

--Reset()  = 0;重置所有;

BRepFilletAPI_MakeFillet

創建一個園角;

示例一:

對一個BOX園角:

BRepFilletAPI_MakeFillet fillet(Box);

for (TopExp_Explorer ex(Box,TopAbs_EDGE); ex.More(); ex.Next()) {

TopoDS_Edge Edge =TopoDS::Edge(ex.Current());

fillet.Add(20,Edge);

}

示例二:

兩個BOX,合並后園角;

TopoDS_Shape fusedShape = BRepAlgoAPI_Fuse(S1,S2);

BRepFilletAPI_MakeFillet fill(fusedShape);

for (TopExp_Explorer ex1(fusedShape,TopAbs_EDGE); ex1.More(); ex1.Next()) {

TopoDS_Edge E =TopoDS::Edge(ex1.Current());

fill.Add(E);

}

for (Standard_Integer i = 1;i<=fill.NbContours();i++) {

Standard_Real longueur(fill.Length(i));

Standard_Real Rad(0.15*longueur);

fill.SetRadius(Rad,i, 1);

}

TopoDS_Shape blendedFusedSolids = fill.Shape();

Handle(AIS_Shape) aBlend = new AIS_Shape(blendedFusedSolids);

示例三:

只園角其中一條邊:

BRepFilletAPI_MakeFillet Rake(theBox);

TopExp_Explorer ex(theBox,TopAbs_EDGE);

ex.Next();

ex.Next();

ex.Next();

ex.Next();

Rake.Add(8,50,TopoDS::Edge(ex.Current()));

Rake.Build();

if (Rake.IsDone() ){

TopoDS_Shape evolvedBox = Rake.Shape();

ais1->Set(evolvedBox);

}

示例四:

園角一個園柱:

BRepFilletAPI_MakeFillet fillet(theCylinder);

TColgp_Array1OfPnt2d TabPoint2(1,20);

for (Standard_Integer i=0; i<=19; i++) {

gp_Pnt2d Point2d(i*2*PI/19,60*cos(i*PI/19-PI/2)+10);

TabPoint2.SetValue(i+1,Point2d);

}

TopExp_Explorer exp2(theCylinder,TopAbs_EDGE);

fillet.Add(TabPoint2,TopoDS::Edge(exp2.Current()));

fillet.Build();

if (fillet.IsDone() ){

TopoDS_Shape LawEvolvedCylinder = fillet.Shape();

ais3->Set(LawEvolvedCylinder);

myAISContext->Redisplay(ais3,Standard_False);

myAISContext->SetCurrentObject(ais3,Standard_False);

}

BRepFilletAPI_MakeChamfer

創建一個倒角;

基類:BRepFilletAPI_LocalOperation;

可以設置相關參數,比如倒角兩個距離,角度等參數;

示例:

BRepFilletAPI_MakeChamfer MC(theBox);

// add all the edges to chamfer

TopTools_IndexedDataMapOfShapeListOfShape M;

TopExp::MapShapesAndAncestors(theBox,TopAbs_EDGE,TopAbs_FACE,M);

for (Standard_Integer i = 1;i<=M.Extent();i++) {

TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));

TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());

MC.Add(5,5,E,F);

}

BRepBuilderAPI_MakeShell

生成一個表面的外殼,注意,一個圖形的外殼,不是一個由表面和厚度定義的實體模型,如果想要創建這種殼,需要使用BRepOffsetAPI_MakeOffsetShape,一個外殼是由一系列相互通過普通的邊連接起來的面;如果表面是C2連續的,外殼將只有一個面;如果表面不是C2連續的,將把一些面細分成所有的面都是C2連續的,結果是外殼包含所有這些面;通過一個非C2連續的表面來生成一個外殼,一般過程是:--構造一個外殼對象--實現算法--生成結果;

注意:表面分解的這些C2面並沒有縫合在一起,需要使用BRepOffsetAPI_Sewing,如果想實現帶厚度的外殼,需要使用BRepOffsetAPI_MakeOffsetShape類;

BRepBuilderAPI_Sewing

將多個鄰近圖形“縫合”成為一個圖形;同時有多個邊的情況下無法縫合;

一般操作過程是:

--創建一個空對象;

缺省的公差是1.E-06;

面分析;

縫合操作;

根據需要作剪操作;

--定義公差;

--添加要縫合的對象;

--計算生成;

--輸出結果圖形;

--如果需要可以輸出自由邊;

--如果需要可以輸出多個邊;

--輸出其它問題;

主要方法:

--構造函數:

option1 如果為假表示只控制;

option2:分析退化的圖形;

option3:為自由邊的剪操作;

option4:未復制處理;

BRepBuilderAPI_Sewing(const Standard_Real tolerance = 1.0e-06,const Standard_Boolean option1 = Standard_True,const Standard_Boolean option2 = Standard_True,const Standard_Boolean option3 = Standard_True,const Standard_Boolean option4 = Standard_False);

如果必要,可以初始化參數;

void Init(const Standard_Real tolerance = 1.0e-06,const Standard_Boolean option1 = Standard_True,const Standard_Boolean option2 = Standard_True,const Standard_Boolean option3 = Standard_True,const Standard_Boolean option4 = Standard_False) ;

--添加一個要縫合的圖形的方法是;

void Add(const TopoDS_Shape& shape) ;

--生成圖形方法是:

void Perform() ;

--得到縫合后的圖形方法是:

TopoDS_Shape& SewedShape() const;

--得到自由邊(只被一個面共享的邊)的數量方法是:

Standard_Integer NbFreeEdges() const;

--得到一個自由邊的方法是:

const TopoDS_Edge& FreeEdge(const Standard_Integer index) const;

--得到復合邊(被兩個及以上面共享的邊)的數量:

Standard_Integer NbMultipleEdges() const;

--得到其中的一個復合邊:

const TopoDS_Edge& MultipleEdge(const Standard_Integer index) const;

--得到鄰近邊的數量:

Standard_Integer NbContigousEdges() const;

--得到其中一個鄰近邊:

const TopoDS_Edge& ContigousEdge(const Standard_Integer index) const;

--得到有一個鄰近邊的邊的集合(截面);

const TopTools_ListOfShape& ContigousEdgeCouple(const Standard_Integer index) const;

--一個截面是否是有邊界的(使用SectionToBoundary方法之前):

Standard_Boolean IsSectionBound(const TopoDS_Edge& section) const;

--得到成為截面的原始邊。記住,截面是由普通邊所組成的,這個信息對於控制來說是很重要的,因為通過原始邊可以找到被附加的截面的表面;

const TopoDS_Edge& SectionToBoundary(const TopoDS_Edge& section) const;

--得到每一個退化的圖形:

const TopoDS_Shape& DegeneratedShape(const Standard_Integer index) const;

--此圖形是否是退化的圖形:

Standard_Boolean IsDegenerated(const TopoDS_Shape& shape) const;

--此圖形是否已被修改過:

Standard_Boolean IsModified(const TopoDS_Shape& shape) const;

--得到一個修改后的圖形:

const TopoDS_Shape& Modified(const TopoDS_Shape& shape) const;

--子圖形是否被修改過:

Standard_Boolean IsModifiedSubShape(const TopoDS_Shape& shape) const;

--得到一個修改過的子圖形:

TopoDS_Shape ModifiedSubShape(const TopoDS_Shape& shape) const;

--得到每一個被刪除的面:

const TopoDS_Face& DeletedFace(const Standard_Integer index) const;

--void Dump() const;打印相關信息;

--得到一個修改后的圖形:

TopoDS_Face WhichFace(const TopoDS_Edge& theEdg,const Standard_Integer index = 1) const;

示例:

BRepOffsetAPI_Sewing aMethod;

aMethod.Add(FirstShape); 

aMethod.Add(SecondShape);

aMethod.Perform();

TopoDS_Shape sewedShape = aMethod.SewedShape();

Handle(AIS_Shape) result = new AIS_Shape(sewedShape);

BRep_Tool

提供了處理BRep圖形幾何對象的一些方法;

如果S是一個Solid,Shell,或Compound.返回為真;

Standard_Boolean IsClosed(const TopoDS_Shape& S) ;

返回在位置L處的幾何表面:

Handle_Geom_Surface& Surface(const TopoDS_Face& F,TopLoc_Location& L) ;

返回面的幾何表面,如果有一個位置可以是一個拷貝;

Handle_Geom_Surface Surface(const TopoDS_Face& F) ;

返回面的多邊三角形,如果沒有三角形返回一個空句柄:

const Handle_Poly_Triangulation& Triangulation(const TopoDS_Face& F,TopLoc_Location& L) ;

返加面的公差值:

Standard_Real Tolerance(const TopoDS_Face& F) ;

返回面的自然約束標志:

Standard_Boolean NaturalRestriction(const TopoDS_Face& F) ;

如果E是一個3D曲線或表面上的一個曲線,返回為真;

Standard_Boolean IsGeometric(const TopoDS_Edge& E) ;

返回邊的3D曲線,可以是NULL,返回L位置,及參數范圍;

Handle_Geom_Curve& Curve(const TopoDS_Edge& E,TopLoc_Location& L,Standard_Real& First,Standard_Real& Last) ;

返回邊的3D多邊形,返回多邊形的位置L;

Handle_Poly_Polygon3D& Polygon3D(const TopoDS_Edge& E,TopLoc_Location& L)

TopLoc_Location

一個Location 是一個復合的平移;對象類型是TopLoc_Datum3D;

常見方法:

--TopLoc_Location();

構造一個空的局部坐標系統對象;注意,這種被構造的缺省的數據為空;、

--TopLoc_Location(const gp_Trsf& T);

通過T構造一個局部坐標系統;

--TopLoc_Location(const Handle(TopLoc_Datum3D)& D);

通過3D datum D來構造一個局部坐標系統,如果平移T不能表達一個局部坐標系統,會引發構造異常;

--Standard_Boolean IsIdentity() const;如果此位置等於一個單位化平移,返回為真;

-- void Identity() ;設置位置為單位化平移;

--Handle_TopLoc_Datum3D& FirstDatum() 得到位置的第一個基礎數據;

-- const TopLoc_Location& NextLocation() const;

另外,具有加減乘除,是否相等方法;

示例:

炸開一個立方體的六個面:

for (TopExp_Explorer exp (aBox,TopAbs_FACE);exp.More();exp.Next()) {

TopoDS_Face aCurrentFace = TopoDS::Face(exp.Current());

//測試當前面的方向

TopAbs_Orientation orient = aCurrentFace.Orientation();

//重新生成幾何平面

TopLoc_Location location;

Handle (Geom_Surface) aGeometricSurface = BRep_Tool::Surface(aCurrentFace,location);

Handle (Geom_Plane) aPlane = Handle (Geom_Plane)::DownCast(aGeometricSurface);

//Build an AIS_Shape with a new color

//創建一個新的AIS_Shape

Handle(AIS_Shape) theMovingFace = new AIS_Shape(aCurrentFace);

Quantity_NameOfColor aCurrentColor = (Quantity_NameOfColor)j;

myAISContext->SetColor(theMovingFace,aCurrentColor,Standard_False);

myAISContext->SetMaterial(theMovingFace,Graphic3d_NOM_PLASTIC,Standard_False); 

//查找每個面的法向量

gp_Pln agpPlane = aPlane->Pln();

gp_Ax1 norm = agpPlane.Axis();

gp_Dir dir = norm.Direction();

gp_Vec move(dir);

TopLoc_Location aLocation;

Handle (AIS_ConnectedInteractive) theTransformedDisplay = new AIS_ConnectedInteractive();

theTransformedDisplay->Connect(theMovingFace, aLocation);

// = myAISContext->Location(theMovingFace);

Handle (Geom_Transformation) theMove = new Geom_Transformation(aLocation.Transformation());

for (Standard_Integer i=1;i<=30;i++) {

theMove->SetTranslation(move*i);

if (orient==TopAbs_FORWARD) myAISContext->SetLocation(theTransformedDisplay,TopLoc_Location(theMove->Trsf()));

else myAISContext->SetLocation(theTransformedDisplay,TopLoc_Location(theMove->Inverted()->Trsf()));

myAISContext->Redisplay(theTransformedDisplay,Standard_False);

}

j+=15;

}

BRepAlgo

BRepAlgo提供了一些布爾操作的服務;

注意,在BrepAlgoAPI包中提供了新的布爾操作,代替了舊的布爾操作;

方法:

--static  Standard_Boolean IsValid(const TopoDS_Shape& S) ;檢測圖形是否合法;

--Standard_EXPORT static  Standard_Boolean IsValid(const TopTools_ListOfShape& theArgs,const TopoDS_Shape&

theResult,const Standard_Boolean closedSolid = Standard_False,const Standard_Boolean GeomCtrl = Standard_True) ;

檢查在結果圖形中所生成和修改后的面是否合法,參數theArgs可以為空,表示所有的面都被檢查;如果closedSolid 為真,表示只有封閉的圖形合法,如果參數GeomCtrl為假,幾何體的頂點和邊不檢查,自相交的新的wire也不檢查;

--Standard_Boolean IsTopologicallyValid(const TopoDS_Shape& S) ;

也是檢查圖形是否合法,和前一個不同的是,檢查 no  geometric contols (intersection  of wires, pcurve validity) are

performed.

GProp_GProps

計算圖元的屬性;

gp_Trsf

定義一個矩陣變換的類

--可以定義平移、旋轉、縮放的矩陣;

--可以對稱於一個點,一條線,一個平面;

示例一:

對稱於一個點:

gp_Trsf theTransformation;

gp_Pnt PntCenterOfTheTransformation(110,60,60);

theTransformation.SetMirror(PntCenterOfTheTransformation);

示例二:

繞一個軸旋轉:

gp_Trsf theTransformation;

gp_Ax1 axe = gp_Ax1(gp_Pnt(200,60,60),gp_Dir(0.,1.,0.));

theTransformation.SetRotation(axe,30*PI/180);

示例三:

縮放:

gp_Trsf theTransformation;

gp_Pnt theCenterOfScale(200,60,60);

theTransformation.SetScale(theCenterOfScale,0.5);

示例四:

平移:

gp_Trsf theTransformation;

gp_Vec theVectorOfTranslation(-6,-6,6);

theTransformation.SetTranslation(theVectorOfTranslation);

示例五:

Displacement:

TopoDS_Shape S = BRepPrimAPI_MakeWedge(60.,100.,80.,20.);

gp_Trsf theTransformation;

gp_Ax3 ax3_1(gp_Pnt(0,0,0),gp_Dir(0,0,1));

gp_Ax3 ax3_2(gp_Pnt(60,60,60),gp_Dir(1,1,1));

theTransformation.SetDisplacement(ax3_1,ax3_2);

BRepBuilderAPI_Transform myBRepTransformation(S,theTransformation);

TopoDS_Shape TransformedShape = myBRepTransformation.Shape();

示例六:

變形

gp_GTrsf theTransformation;

gp_Mat rot(1, 0, 0, 0, 0.5, 0, 0, 0, 1.5);

theTransformation.SetVectorialPart(rot);

theTransformation.SetTranslationPart(gp_XYZ(5,5,5));

BRepBuilderAPI_GTransform myBRepTransformation(S,theTransformation);

TopoDS_Shape S2 = myBRepTransformation.Shape();

BuilderAPI_MakeEdge

定義一生成一個邊;此類有多個構造函數,現舉其中一個介紹如下:

Standard_EXPORT BRepBuilderAPI_MakeEdge(const Handle(Geom2d_Curve)& L,const Handle(Geom_Surface)& S,const TopoDS_Vertex& V1,const TopoDS_Vertex& V2,const Standard_Real p1,const Standard_Real p2);

其參數含義是:

頂點V1和V2用來限制曲線(定義邊的約束),值p1和p2為頂點的參數;

曲線可以定義成在一個表面的2D曲線,應用缺省的公差;

參數規則:

對於曲線來說:

--句柄不能為空;

--如果曲線是一個trimmed曲線,使用基礎曲線;

對於頂點來說:

--可以為空,表示此參數為無限大;靜態方法 Precision::Infinite()用來定義一個無限大的數;

--兩個頂點如果位於同一位置,必須一樣,當曲線是封閉時使用相同的頂點;

對於參數來說:

--參數為必須在曲線參數范圍內,如果曲線是trimmed,使用基礎曲線;如果邊的條件不滿足,返回BRepAPI_ParameterOutOfRange錯誤;

--參數值不能相等,如果條件不滿足,邊無法創建,返回BRepAPI_LineThroughIdenticPoints錯誤;

--參數值可以這樣給出C->FirstParameter()

--如果參數值需要切換,比如第一個頂點的參數為P2,第二個頂點的參數為P1,邊的方向可以“reversed”;

--對於一個周期曲線,值P1和P2可以通過加或減周期來得到;

--參數值可以無限大,在對應的方向上邊是開放的。然而,對應的頂點必須是空圖形,如果條件不滿足,邊無法創建,返回BRepAPI_PointWithInfiniteParameter錯誤;

--參數值可以被忽略,將通過曲線上的投影進行計算;

--可以給定空間三維點;

BRepFeat_MakePipe

基類為:BRepFeat_Form;

通過基本圖形生成一個Pipe;

BRepFeat_MakeLinearForm

基類為:BRepFeat_RibSlot

在一個平面表面上建一個肋或開凹槽;

BRepFeat_Gluer

粘合兩個實體為一個實體;

示例:

(1):創建兩個BOX,並找到要粘合的面;

(2):創建要粘合的對象:

BRepFeat_Gluer glue2(S4,S3);

(3):用兩個面粘合對象;

glue2.Bind(F4,F3);

(4):重新生成對象:

LocOpe_FindEdges CommonEdges(F4,F3);

for (CommonEdges.InitIterator(); CommonEdges.More(); CommonEdges.Next())

glue2.Bind(CommonEdges.EdgeFrom(),CommonEdges.EdgeTo());

TopoDS_Shape res2 = glue2.Shape();

myAISContext->Erase(ais3,Standard_False,Standard_False);

ais4->Set(res2);

myAISContext->Redisplay(ais4,Standard_False);

Graphic2d_Polyline

創建一個多邊形.

常見方法:

--Length()得到線的點數;

--void Values(const Standard_Integer aRank,Quantity_Length& X,Quantity_Length& Y) const;得到序號為aRank的點;

--void DrawElement(const Handle(Graphic2d_Drawer)& aDrawer,const Standard_Integer anIndex) ;繪制多邊形的一條邊;

--void DrawVertex(const Handle(Graphic2d_Drawer)& aDrawer,const Standard_Integer anIndex) ;繪制多邊形的一個頂點;

--Standard_Boolean Pick(const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal aPrecision,const Handle(Graphic2d_Drawer)& aDrawer) ;得到此多邊形是否被拾取,注意:PickIndex()方法得到的是最后拾取的點,如果拾取點在線的內部,返回0;

Graphic2d_Line

是Polyline, Circle ... 等圖元的基類;

常見方法:

--SetWidthIndex(const Standard_Integer anIndex) ;得到在width map中的寬度的索引;設定對應的線寬值;

--SetTypeIndex(const Standard_Integer anIndex) ;設置線型;

--SetInteriorColorIndex(const Standard_Integer anIndex) ;設置顏色;

--void SetDrawEdge(const Standard_Boolean aDraw) ;設置邊是否繪出,注意,這種情況下,polygon的類型必須為:

Graphic2d_TOPF_FILLED 或者 Graphic2d_TOPF_PATTERNED;

--SetInteriorPattern(const Standard_Integer anIndex) ;定義封閉線的內部圖案,polygon的填充類型必須是:Graphic2d_TOPF_PATTERNED;

--SetTypeOfPolygonFilling(const Graphic2d_TypeOfPolygonFilling aType) ;定義封閉線的圖案,TypeOfPolygonFilling可選類型有:

- Graphic2d_TOPF_EMPTY - Graphic2d_TOPF_FILLED - Graphic2d_TOPF_PATTERNED ;

--Standard_Integer InteriorColorIndex() const;得到顏色索引;

--Standard_Integer InteriorPattern() const;得到所使用的圖案索引;

--Graphic2d_TypeOfPolygonFilling TypeOfPolygonFilling() const;得到多邊形填充模式;

Graphic2d_Primitive

是Graphic2d_Line類的基類,

常見方法:

--得到及獲取顏色索引;

--得到圖元元素的數量和頂點的數量:

Standard_Integer NumOfElemIndices() const;

Standard_Integer NumOfVertIndices() const;

--Standard_Integer PickedIndex() const;得到最后拾取的圖元元素的索引值;

--void Highlight(const Standard_Integer anIndex = 0) ;高亮顯示圖元或圖元的一部分,當anIndex=0表示所有的圖元高亮顯示,>0為當所要求的圖元元素高亮顯示時,<0為所要求的頂點高亮顯示時;

--void Unhighlight() ;禁止圖元高亮顯示;

-- Standard_Boolean IsHighlighted() const;圖元是否高亮顯示;

--Handle_TColStd_HSequenceOfInteger HighlightIndices() const;得到圖元高亮顯示的索引序列;

--void SetDisplayMode(const Standard_Integer aMode) ;設置圖元顯示的模式;

--Standard_Integer DisplayMode() const;得到圖元顯示的模式;

--Standard_Boolean Graphic2d_GraphicObject::Pick(const Standard_Real X,const Standard_Real Y,const Standard_Real aPrecision,const Handle(Graphic2d_Drawer)& aDrawer) ;

用一個矩形框拾取圖形對象,如果圖形對象被拾取,返回為真,通過方法Graphic2d_View::Pick調用;

--Standard_Boolean Graphic2d_GraphicObject::PickByCircle(const Standard_Real X,const Standard_Real Y,const Standard_Real Radius,const Handle(Graphic2d_Drawer)& aDrawer) ;

用一個園來拾取圖形對象,如果圖形對象被拾取,返回為真,通過方法Graphic2d_View::PickByCircle調用;

--Standard_Boolean Graphic2d_GraphicObject::Pick(const Standard_Real Xmin,const Standard_Real Ymin,const Standard_Real Xmax,const Standard_Real Ymax,const Handle(Graphic2d_Drawer)& aDrawer,const Graphic2d_PickMode aPickMode) ;

以下情況下返回值為真:

包括在矩形內:included in rectangle (),

不在矩形內:excluded from rectangle (),

相交於矩形框:intersected by rectangle (),

通過 Xmin, Ymin, Xmax, Ymax定義矩形框。

--得到所有在圖元內的markers的最小最大值,注意,如果me為空,或未顯示,或沒有markers返回為假,

Minx = Miny = RealFirst () ;Maxx = Maxy = RealLast ()

Standard_EXPORT  Standard_Boolean Graphic2d_GraphicObject::MarkerMinMax(Quantity_Length& Minx,Quantity_Length& Maxx,Quantity_Length& Miny,Quantity_Length& Maxy) const;

--移除圖元;Standard_EXPORT  void Graphic2d_GraphicObject::RemovePrimitive(const Handle(Graphic2d_Primitive)& aPrimitive) ;

--繪制圖元,以默認的圖元屬性繪制;void Graphic2d_TransientManager::Draw(const Handle(Graphic2d_Primitive)& aPrimitive) ;

AIS2D_InteractiveObject

使用顯示和選擇服務,來可視化和選擇機制,交互式對象常用來顯示數據,曲線,圖形,markers,尺寸標注等。

常用方法:

--獲取及設置屬性

Handle_Prs2d_Drawer Attributes() const;

void SetAttributes(const Handle(Prs2d_Drawer)& aDrawer) ;

--通過Aspect設置屬性,到所有圖元分配這個Aspect.

Standard_EXPORT  void SetAspect(const Handle(Prs2d_AspectRoot)& anAspect) ;

--通過Aspect設置屬性,到所有通過InteractiveContext被鏈接的圖元對象;

Standard_EXPORT  void SetAspect(const Handle(Prs2d_AspectRoot)& anAspect,const Handle(Graphic2d_Primitive)& aPrimitive) ;

--得到圖元的Aspect;

Standard_EXPORT  Handle_Prs2d_AspectRoot GetAspect(const Handle(Graphic2d_Primitive)& aPrimitive) const;

--如果圖元用一個aspect鏈接的話返回為真;

Standard_EXPORT  Standard_Boolean HasAspect(const Handle(Graphic2d_Primitive)& aPrimitive) const;

--指出交互對象是否有一個交互上下文設備;

Standard_EXPORT  Standard_Boolean HasInteractiveContext() const;

--得到交互對象的上下文設備; 

Standard_EXPORT  Handle_AIS2D_InteractiveContext GetContext() const;

Graphic2d_GraphicObject

是AIS2D_InteractiveObject類的基類;在一個view內創建一個圖形對象,一個圖形對象管理一系列圖元;默認值為:空,可輸出,可繪制,可拾取,不顯示,不高亮,優先權為0;

主要方法:

設置視圖,設置一個變形,設置獲取圖層,設置獲取優先權,禁用/使用輸出,是否可輸出,禁用/使用Draw.是否可顯示,Erase,高亮顯示,顏色,拾取等;

Graphic2d_ImageFile

定義一個圖像,以圖像的中心位置作為插入點,X,Y定義在模型空間的位置,adx,ady 定義在設備空間的偏移量.ascale定義一個縮放系數;

Aspect_WidthMap

定義一個WidthMap集合對象;

主要方法有,

--添加一個入口:

Standard_Integer AddEntry(const Aspect_WidthOfLine aStyle) ;

void AddEntry(const Aspect_WidthMapEntry& AnEntry) ;

Standard_Integer AddEntry(const Quantity_Length aStyle) ;

--根據索引得到一個入口:

Aspect_WidthMapEntry Entry(const Standard_Integer AnIndex) const;

示例:

--定義private :

Handle(Aspect_WidthMap) myWidthMap;

--遍歷:

for(int i =1;i<=myWidthMap->Size();i++)

{

Aspect_WidthMapEntry aWidthMapEntry = myWidthMap->Entry(i);

}

--得到一個入口:  Aspect_WidthMapEntry aWidthMapEntry = myWidthMap->Entry(CurrentSelectionIndex);

Aspect_TypeMap

定義一個線型集合對象:

Aspect_MarkMap

定義一個MarkMap集合對象;

Aspect_FontMap

定義一個字體集合對象;

Aspect_ColorMap

定義一個顏色集合對象;

GGraphic2d_SetOfCurves

基類是:Graphic2d_Line;

定義一圖元為由多個curves 的集合;主要方法有添加一個curves, 得到curves的數量,得到其中一個curves等;只繪制其中一個元素,是否為拾取狀態;

示例:

Handle(Prs2d_AspectLine) aLineAspect = new Prs2d_AspectLine; 

aLineAspect->SetTypeOfFill(Graphic2d_TOPF_EMPTY);

aLineAspect->SetTypeIndex(...);

aLineAspect->SetColorIndex(...);

aLineAspect->SetWidthIndex(...);

Handle(Graphic2d_SetOfCurves) segment;

segment = new Graphic2d_SetOfCurves(this);

segment->Add(myGeom2dCurve);

將此曲線集合應用所定義的線型線寬等;

SetAspect(aLineAspect, segment);

創建交互式對象相關的類介紹

AIS_Line

此類的繼承關系是:

Standard_Transient->MMgt_TShared->PrsMgr_PresentableObject-->SelectMgr_SelectableObject-->AIS_InteractiveObject->AIS_Line

Standard_Transient:抽象類,主要定義分配空間,得到類型,引用計數等;

MMgt_TShared:抽象類,主要用來管理對象的內存;

PrsMgr_PresentableObject類:表示一個可表達的二維或三維圖形對象;主要方法有設置位置,更新,圖形類型等;

此類的派生類類型有:

-AIS_InteractiveObject

-AIS_ConnectedInteractive

-AIS_MultipleConnectedInteractive

-AIS_Shape

SelectMgr_SelectableObject類:表示一個可選擇的對象;

AIS_LineAIS_Circle等類

定義一個直線,園等;主要方法有,返回對象的類型,設置線寬,線型,顏色等;

示例:

GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.));

Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value());

myAISContext->Display(anAISCirc);

AIS_InteractiveContext

交互式設備類,可以用它來管理交互式圖形對象,可以在一個或多個視圖中。如果圖形對象已經裝入交互式設備,可以直接調用交互式對象的方法。

使用設備時必須區分兩種狀態:

-沒有打開本地設備。也稱為不確定點;

-打開了一個或多個設備;

有的方法可以使用在打開的設備中,有的方法用在關閉的設備中,有的方法與設備狀態無關;

--當想工作在一個入口類型上,應設置選項UseDisplayedObjects為假,可顯示對象可以重新可視化交互對象;

--當使用缺省的選項來打開一個設備時,注意:

:可視化的交互對象在缺省選擇模式下是活動的,必須分離那些不想使用的對象;

:交互式對象可以自動分解為子圖形;

:“臨時的”交互對象不會自動計入總數,,如果想使用它,必須手動裝載它;

使用過程是:

--用正確的選項打開設備;

--裝載/顯示對象;

--如果需要,激活標准模式;

--創建一個過濾器,添加到設備中;

--查找/選擇/重置所需的入口;

--根據索引關閉設備;

--創建一個交互設備編輯器很有用,可以設置不同的設備用不用的選擇/表達方式;

常見方法:

--如果沒有設備打開,交互對象沒有顯示模式,缺省的顯示模式是0,如果一個設備是打開的並且更新為假,對象不會更新顯示。

void Display(const Handle(AIS_InteractiveObject)& anIobj,const Standard_Integer amode,const Standard_Integer

aSelectionMode,const Standard_Boolean updateviewer = Standard_True,const Standard_Boolean allowdecomposition =

Standard_True) ;

--使用給定的選擇模式載入一個交互對象:

void Load(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Integer SelectionMode = -1,const Standard_Boolean

AllowDecomp = Standard_False) ;

--擦除一個對象:如果putinCollector為假,對象被擦除但不放入集合中;

void Erase(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Boolean updateviewer = Standard_True,const

Standard_Boolean PutInCollector = Standard_True) ;

--擦除視圖集合中的每個對象;

void EraseAll(const Standard_Boolean PutInCollector = Standard_True,const Standard_Boolean updateviewer = Standard_True) ;

--從集合中顯示所有對象;

void DisplayAll(const Standard_Boolean OnlyFromCollector = Standard_True,const Standard_Boolean updateviewer =

Standard_True) ;

--從集合中顯示一個對象;

void DisplayFromCollector(const Handle(AIS_InteractiveObject)& anIObj,const Standard_Boolean updateviewer = Standard_True)

--擦除選擇的對象;

void EraseSelected(const Standard_Boolean PutInCollector = Standard_True,const Standard_Boolean updateviewer =

Standard_True) ;

--改變臨時對象的狀態,

Standard_Boolean KeepTemporary(const Handle(AIS_InteractiveObject)& anIObj,const Standard_Integer InWhichLocal = -1) ;

--從所有的視圖中移除交互對象;

void Clear(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Boolean updateviewer = Standard_True) ;

--從每個視圖中移除對象; 

void Remove(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Boolean updateviewer = Standard_True) ;

--從所有打開的設備中移除所有對象;

void RemoveAll(const Standard_Boolean updateviewer = Standard_True) ;

--通過鼠標動態檢測,感知的圖元被高亮顯示。缺省的鼠標移過時的顏色為白色。

void Hilight(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Boolean updateviewer = Standard_True) ;

--改變視圖中線的顏色;

void HilightWithColor(const Handle(AIS_InteractiveObject)& aniobj,const Quantity_NameOfColor aCol,const Standard_Boolean updateviewer = Standard_True) ;

--從入口對象中移除高亮;更新視圖;

void Unhilight(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Boolean updateviewer = Standard_True) ;

--設置顯示的優先權;

void SetDisplayPriority(const Handle(AIS_InteractiveObject)& anIobj,const Standard_Integer aPriority) ;

--設置所看到的交互對象的顯示模式;

void SetDisplayMode(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Integer aMode,const Standard_Boolean

updateviewer = Standard_True) ;

--設置/移除交互對象的選擇模式:

void SetSelectionMode(const Handle(AIS_InteractiveObject)& aniobj,const Standard_Integer aMode) ;

void UnsetSelectionMode(const Handle(AIS_InteractiveObject)& aniobj) ;

--設置感知的精度:

void SetSensitivity(const Standard_Real aPrecision) ;

--定義當前選擇感知的像素:

void SetSensitivity(const Standard_Integer aPrecision = 4) ;

--設置/重置初始圖形的位置;如果有一個位置返回為真;

void SetLocation(const Handle(AIS_InteractiveObject)& aniobj,const TopLoc_Location& aLocation) ;

void ResetLocation(const Handle(AIS_InteractiveObject)& aniobj) ;

Standard_Boolean HasLocation(const Handle(AIS_InteractiveObject)& aniobj) const;

得到實體對象的位置;

const TopLoc_Location& Location(const Handle(AIS_InteractiveObject)& aniobj) const;

--改變當前面的模式;缺省模式是Aspect_TOFM_TWO_SIDE。意味着屬性在前面和后面都應用;

void SetCurrentFacingModel(const Handle(AIS_InteractiveObject)& aniobj,const Aspect_TypeOfFacingModel aModel =

Aspect_TOFM_BOTH_SIDE) ;

--設置/獲得三角形的尺寸,缺省值是100mm.

void SetTrihedronSize(const Standard_Real aSize,const Standard_Boolean updateviewer = Standard_True) ;

Standard_Real TrihedronSize() const;

--設置/獲取平面的尺寸:

Standard_EXPORT  void SetPlaneSize(const Standard_Real aSizeX,const Standard_Real aSizeY,const Standard_Boolean

updateviewer = Standard_True) ;

--得到實體對象的顯示狀態;

AIS_DisplayStatus DisplayStatus(const Handle(AIS_InteractiveObject)& anIobj) const;

--得到實體對象的顯示模式的列表:

const TColStd_ListOfInteger& DisplayedModes(const Handle(AIS_InteractiveObject)& aniobj) const;

--關於繪制隱藏線相關的一些函數,通過名稱就可以知道函數的意思;

EnableDrawHiddenLine();

DisableDrawHiddenLine();

Standard_Boolean DrawHiddenLine();

--設置/得到UV等高參數;等高參數是否可用;

Standard_Integer IsoNumber(const AIS_TypeOfIso WhichIsos = AIS_TOI_Both) ;

--設置/添加/移除當前對象.....

InitCurrent() ;MoreCurrent();NextCurrent();

Standard_Boolean IsCurrent(const Handle(AIS_InteractiveObject)& aniobj) const;

Handle_AIS_InteractiveObject Current() const;

Handle_AIS_InteractiveObject FirstCurrentObject() ;

void HilightCurrents(const Standard_Boolean updateviewer = Standard_True) ;

void UnhilightCurrents(const Standard_Boolean updateviewer = Standard_True) ;

void ClearCurrents(const Standard_Boolean updateviewer = Standard_True) ;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM