# re: OpenCascade BRep 格式描述之二 回復 更多評論
2014-10-28 12:01 by
TopoDS_Face WhiteFace, BrownFace, RedFace, PinkFace;
TopoDS_Edge Edge1, Edge2, Edge3, Edge4, Edge5, Edge6, Edge7;
TopoDS_Wire Wire1;
gp_Pnt P1, P2, P3, P4, P5, P6, P7;
gp_Sphere sphere (gp_Ax3(gp_Pnt(0,0,0),gp_Dir(1,0,0)),150);
WhiteFace = BRepBuilderAPI_MakeFace(sphere,0.1,0.7,0.2,0.9);
//////////////////////////////////
P1.SetCoord(-15,200,10);
P2.SetCoord(5,204,0);
P3.SetCoord(15,200,0);
P4.SetCoord(-15,20,15);
P5.SetCoord(-5,20,0);
P6.SetCoord(15,20,35);
TColgp_Array2OfPnt array(1,3,1,2);
array.SetValue(1,1,P1);
array.SetValue(2,1,P2);
array.SetValue(3,1,P3);
array.SetValue(1,2,P4);
array.SetValue(2,2,P5);
array.SetValue(3,2,P6);
Handle (Geom_BSplineSurface) curve = GeomAPI_PointsToBSplineSurface(array,3,8,GeomAbs_C2,0.001);
RedFace = BRepBuilderAPI_MakeFace(curve);
////////////////////
gp_Circ circle(gp_Ax2(gp_Pnt(0,0,0),gp_Dir(1,0,0)),80);
Edge1 = BRepBuilderAPI_MakeEdge(circle,0,PI);
Edge2 = BRepBuilderAPI_MakeEdge(gp_Pnt(0,0,-80),gp_Pnt(0,-10,40));
Edge3 = BRepBuilderAPI_MakeEdge(gp_Pnt(0,-10,40),gp_Pnt(0,0,80));
TopoDS_Wire YellowWire;
BRepBuilderAPI_MakeWire MW1(Edge1,Edge2,Edge3);
if (MW1.IsDone()) {
YellowWire = MW1;
}
BrownFace = BRepBuilderAPI_MakeFace(YellowWire);
/////////////
P1.SetCoord(35,-200,40);
P2.SetCoord(50,-204,30);
P3.SetCoord(65,-200,30);
P4.SetCoord(35,-20,45);
P5.SetCoord(45,-20,30);
P6.SetCoord(65,-20,65);
TColgp_Array2OfPnt array2(1,3,1,2);
array2.SetValue(1,1,P1);
array2.SetValue(2,1,P2);
array2.SetValue(3,1,P3);
array2.SetValue(1,2,P4);
array2.SetValue(2,2,P5);
array2.SetValue(3,2,P6);
Handle (Geom_BSplineSurface) BSplineSurf = GeomAPI_PointsToBSplineSurface(array2,3,8,GeomAbs_C2,0.001);
TopoDS_Face aFace = BRepBuilderAPI_MakeFace(BSplineSurf);
//2d lines
gp_Pnt2d P12d(0.9,0.1);
gp_Pnt2d P22d(0.2,0.7);
gp_Pnt2d P32d(0.02,0.1);
Handle (Geom2d_Line) line1 = new Geom2d_Line(P12d,gp_Dir2d((0.2-0.9),(0.7-0.1)));
Handle (Geom2d_Line) line2 = new Geom2d_Line(P22d,gp_Dir2d((0.02-0.2),(0.1-0.7)));
Handle (Geom2d_Line) line3 = new Geom2d_Line(P32d,gp_Dir2d((0.9-0.02),(0.1-0.1)));
//Edges are on the BSpline surface
Edge1 = BRepBuilderAPI_MakeEdge(line1,BSplineSurf,0,P12d.Distance(P22d));
Edge2 = BRepBuilderAPI_MakeEdge(line2,BSplineSurf,0,P22d.Distance(P32d));
Edge3 = BRepBuilderAPI_MakeEdge(line3,BSplineSurf,0,P32d.Distance(P12d));
Wire1 = BRepBuilderAPI_MakeWire(Edge1,Edge2,Edge3);
Wire1.Reverse();
PinkFace = BRepBuilderAPI_MakeFace(aFace,Wire1);
TopExp_Explorer ex;
TopoDS_Shape s;
for(ex.Init(PinkFace,TopAbs_EDGE);ex.More(); ex.Next())
{
s=ex.Current();
Edge4=TopoDS::Edge(s);
if(Edge4.IsEqual(Edge2))
Edge3=Edge3;
}
這是在mfc例2中的部分代碼,for循環式我加的,問題是PinkFace 明明由edge1、2、3 生成,為什么explorer PinkFace 中的edge就找不到與edge2相同的邊呢?僅能找到與edge1相同的邊,這種不確定的結果讓人無法捉摸。
另外博主有沒有好辦法,在不改occ庫代碼的情況下,給每個topods_shape一個全局唯一標識號或標識名稱。我試用hashcode,但不知如何正確設置它的上限,似乎它有可能重復,不能當作唯一標識符。
# re: OpenCascade BRep 格式描述之二 回復 更多評論
2014-10-28 18:16 by
@佚名
inline Standard_Boolean TopoDS_Shape::IsEqual (const TopoDS_Shape& other) const
{
return (myTShape == other.myTShape) &&
(myLocation == other.myLocation) &&
(myOrient == other.myOrient);
}
函數IsEqual()的作用是:
Returns True if two shapes are partners, i.e. if they share the same TShape. Locations and Orientations may differ.
根據上面的代碼可知只是判斷是不是共享了相同的TShape,如果朝向Orientation不同,返回值也是不同的。看你前面將wire1.Reverse(),可能朝向會不同。
==================================
TopoDS_Shape的HashCode產生函數代碼如下:
Standard_Integer TopoDS_Shape::HashCode(const Standard_Integer Upper) const
{
//PKV
const Standard_Integer aI = (Standard_Integer) ptrdiff_t(myTShape.operator->());
const Standard_Integer aHS = ::HashCode(aI,Upper);
const Standard_Integer aHL = myLocation.HashCode(Upper);
return (aHS^aHL)%Upper;
}
應該取個大一點的Upper。
也可參考Singleton模式,使用一個static的std::map<std::string, TopoDS_Shape>自己來根據名字映射TopoDS_Shape,這就像Draw Test Harness中一樣了。
# re: OpenCascade BRep 格式描述之二 回復 更多評論
2014-10-29 10:17 by
謝謝博主!
我試過了,即使不reverse,用issame也找不到與原來對應的邊,我原以為頂點、邊、面、體自底而上構建模型會逐級引用,這樣便于建立共享邊界的圖元,現在看來這一規則不成立,也許它僅在一個shape內成立。
我現在使用的是static的std::map<std::int, TopoDS_Shape>的問題,似乎它也不能解決反向檢索的問題,比如你可以指定繪制一個編號為1的shape,但反過來,你在繪圖區感知到了一個shape,不能確定它的編號是1還是其它。以前采用給topods_shape增加公共成員變量的方法來識別。后來為了避免修改occ庫保持原庫的完整性放棄了。
# re: OpenCascade BRep 格式描述之二 回復 更多評論
2014-11-10 22:47 by
@佚名
哦。
那可以看看
SetOwner (const Handle< Standard_Transient > &ApplicativeEntity);
//Allows you to attribute the owner ApplicativeEntity to
//an Interactive Object. This can be a shape for a set of
//sub-shapes or a sub-shape for sub-shapes which it
//is composed of. The owner takes the form of a transient.
SetOwner把一個指針數據放到AIS_InteractiveObject中去了,所以你也可以將ID放進去。