Posted on 2020-06-16 11:04
eryar 閱讀(2852)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
2.OpenCASCADE
BRep Builder
eryar@163.com
1 Introduction
類BRep_Builder提供了創(chuàng)建、修改BRep模型的方法。使用這個(gè)類,你可以從底層自己構(gòu)建BRep體,前提條件是你要對(duì)BRep模型的數(shù)據(jù)結(jié)構(gòu)有一定理解。邊界表示法BRep的重點(diǎn)在邊界的定義,打開BRep_Builder的類圖:

可以看到其中重載了很多UpdateEdge函數(shù),每個(gè)UpdateEdge函數(shù)都修改了Edge中的幾何數(shù)據(jù),包括邊界的定義數(shù)據(jù)。若能理解每個(gè)UpdateEdge函數(shù),則對(duì)OpenCASCADE的BREP數(shù)據(jù)結(jié)構(gòu)就能理解了。本文主要介紹其中兩個(gè)函數(shù)的用法:Add和Remove。
2 Add Shape
BRep_Builder的Add函數(shù)的字面意思是將一個(gè)Shape添加到另外一個(gè)Shape中。因這個(gè)函數(shù)的實(shí)現(xiàn)比較簡單,把源碼列出如下:
void TopoDS_Builder::Add (TopoDS_Shape& aShape,
const TopoDS_Shape& aComponent) const
{
// From now the Component cannot be edited
aComponent.TShape()->Free(Standard_False);
// Note that freezing aComponent before testing if aShape is free
// prevents from self-insertion
// but aShape will be frozen when the Exception is raised
if (aShape.Free())
{
static const unsigned int aTb[9]=
{
//COMPOUND to:
(1<<((unsigned int)TopAbs_COMPOUND)),
//COMPSOLID to:
(1<<((unsigned int)TopAbs_COMPOUND)),
//SOLID to:
(1<<((unsigned int)TopAbs_COMPOUND)) |
(1<<((unsigned int)TopAbs_COMPSOLID)),
//SHELL to:
(1<<((unsigned int)TopAbs_COMPOUND)) |
(1<<((unsigned int)TopAbs_SOLID)),
//FACE to:
(1<<((unsigned int)TopAbs_COMPOUND)) |
(1<<((unsigned int)TopAbs_SHELL)),
//WIRE to:
(1<<((unsigned int)TopAbs_COMPOUND)) |
(1<<((unsigned int)TopAbs_FACE)),
//EDGE to:
(1<<((unsigned int)TopAbs_COMPOUND)) |
(1<<((unsigned int)TopAbs_SOLID)) |
(1<<((unsigned int)TopAbs_WIRE)),
//VERTEX to:
(1<<((unsigned int)TopAbs_COMPOUND)) |
(1<<((unsigned int)TopAbs_SOLID)) |
(1<<((unsigned int)TopAbs_FACE)) |
(1<<((unsigned int)TopAbs_EDGE)),
//SHAPE to:
0
};
//
const unsigned int iC=(unsigned int)aComponent.ShapeType();
const unsigned int iS=(unsigned int)aShape.ShapeType();
//
if ((aTb[iC] & (1<<iS)) != 0) {
TopoDS_ListOfShape& L = aShape.TShape()->myShapes;
L.Append(aComponent);
TopoDS_Shape& S = L.Last();
//
// compute the relative Orientation
if (aShape.Orientation() == TopAbs_REVERSED)
S.Reverse();
//
// and the Relative Location
const TopLoc_Location& aLoc=aShape.Location();
if (!aLoc.IsIdentity())
S.Move(aLoc.Inverted());
//
// Set the TShape as modified.
aShape.TShape()->Modified(Standard_True);
}
else {
throw TopoDS_UnCompatibleShapes("TopoDS_Builder::Add");
}
}
else {
throw TopoDS_FrozenShape("TopoDS_Buider::Add");
}
}
Add函數(shù)通過一個(gè)靜態(tài)的檢查列表,來檢查添加的Shape是不是合法的,即FACE只能添加到SHELL和COMPOUND中,EDGE只能添加到WIRE,SOLID和COMPOUND中等。添加之后還檢查了Shape的ORIENTATION及位置信息并作相應(yīng)調(diào)整。不滿足條件的情況都會(huì)拋出異常,所以對(duì)于Add函數(shù)需要增加異常處理邏輯。
使用這個(gè)函數(shù)需要注意的是這個(gè)Add只是簡單的將Shape添加到TShape的Shape表中,并沒有維護(hù)BREP的邊界信息。
3 Remove Shape
與Add對(duì)應(yīng)的有Remove函數(shù),可以從一個(gè)Shape中刪除一個(gè)子Shape。還是打開源碼,有源碼有真相:
//=======================================================================
//function : Remove
//purpose : Remove a Shape from an other one
//=======================================================================
void TopoDS_Builder::Remove (TopoDS_Shape& aShape,
const TopoDS_Shape& aComponent) const
{
// check if aShape is not Frozen
TopoDS_FrozenShape_Raise_if (!aShape.Free(),"TopoDS_Builder::Remove");
// compute the relative Orientation and Location of aComponent
TopoDS_Shape S = aComponent;
if (aShape.Orientation() == TopAbs_REVERSED)
S.Reverse();
S.Location(S.Location().Predivided(aShape.Location()));
TopoDS_ListOfShape& L = aShape.TShape()->myShapes;
TopoDS_ListIteratorOfListOfShape It(L);
while (It.More()) {
if (It.Value() == S) {
L.Remove(It);
aShape.TShape()->Modified(Standard_True);
break;
}
It.Next();
}
}
從源碼中可知,Remove實(shí)現(xiàn)的邏輯也是很簡單的:
l 檢查Shape是不是Free的,若不是則拋出異常;
l 計(jì)算要?jiǎng)h除Component的ORIENTATION和LOCATION;
l 從Shape列中查找Component,若找到將其從列表中刪除;
刪除操作比添加操作要簡單,一個(gè)是把已有的數(shù)據(jù)刪除,一個(gè)是從無到有的構(gòu)建數(shù)據(jù)。從函數(shù)實(shí)現(xiàn)代碼來看,刪除操作也是簡單的從Shape列表中刪除指定的Shape。刪除之后多余的邊界信息還會(huì)存在原來的Shape中,要確保刪除的Shape之后沒有多余信息,還需要?jiǎng)h除沒有使用的PCurves。

上圖所示為刪除一個(gè)底面的圓柱體。
4 Conclusion
類BRep_Builder的操作需要以充分理解OpenCASCADE的BREP數(shù)據(jù)結(jié)構(gòu)為前提,因?yàn)槠?/span>Add和Remove函數(shù)并沒有提供維護(hù)邊界的功能,只是將指定的Shape添加到列表中或從列表中刪除。
為了方便大家在移動(dòng)端也能看到我的博文和討論交流,現(xiàn)已注冊(cè)微信公眾號(hào),歡迎大家掃描下方二維碼關(guān)注。