Wire 是一种复合形状,不是由几何形状构建的,而是由边缘的装配构建的。BRepBuilderAPI_MakeWire类可以从一个或多个Edge构建Wire,或将新Edge连接到现有Wire。
BRepBuilderAPI_MakeWire 类将Edge连接到Wire。添加新Edge时,如果其顶点之一与Wire共享,则认为它已连接到Wire。如果没有共享顶点,则算法会搜索边的一个顶点和Wire的一个顶点,这两个顶点位于同一位置(使用顶点的公差来测试它们是否具有相同的位置)。如果找到这样的一对顶点,则将Edge与Wire的顶点一起复制,以替换原始顶点。Edge的所有顶点都可以与Wire的顶点交换。如果找不到连接,则认为Wire已断开连接。这是一个错误。
BRepBuilderAPI_MakeWire 类可以返回添加到Wire上的最后一条边(Edge 方法)。如果复制了这条Edge,则它可能与原始Edge不同。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"Viewer.h"
#include
#include
#include
#include
#include
#include
#include
#include"GEOMAlgo_Splitter.h"
#include
#include
#include
int main(int argc, char* argv[])
{
gp_Dir Z(0.0, 0.0, 1.0);
gp_Pnt center(0, 0, 0.0);
gp_Pnt xr(0.5, 0, 0.0);
gp_Pnt yr(0.0, 1.0, 0.0);
gp_Pnt zr(0.0, 0.0, 7.0);
gp_Ax2 wb(center, Z);
gp_Circ wbcircle(wb, 0.125 / 2);
TopoDS_Edge wbe = BRepBuilderAPI_MakeEdge(wbcircle);
TopoDS_Edge xline = BRepBuilderAPI_MakeEdge(center, xr);
TopoDS_Edge yline = BRepBuilderAPI_MakeEdge(center, yr);
TopoDS_Edge zline = BRepBuilderAPI_MakeEdge(center, zr);
Standard_Real smallR = 0.112;
Standard_Real bigR = 0.1975;
Standard_Real xb = 0.0;
Standard_Real yb = bigR;
Standard_Real xs = 0.0105;
Standard_Real ys = 0.1023;
gp_Pnt p1(-0.0998, 0.0828, 0.0);
gp_Pnt p2(-0.0456, 0.0053, 0.0);
gp_Pnt p3(0.1637, 0.0871, 0.0);
gp_Pnt p4(0.2237, 0.1471, 0.0);
gp_Pnt scenter(xs, ys, 0.0);
gp_Pnt bcenter(xb, yb, 0.0);
gp_Ax2 sr(scenter, Z);
gp_Ax2 br(bcenter, Z);
gp_Circ scircle(sr, smallR);
gp_Circ bcircle(br, bigR);
TopoDS_Vertex v1 = BRepBuilderAPI_MakeVertex(p1);
TopoDS_Vertex v2 = BRepBuilderAPI_MakeVertex(p2);
TopoDS_Vertex v3 = BRepBuilderAPI_MakeVertex(p3);
TopoDS_Vertex v4 = BRepBuilderAPI_MakeVertex(p4);
Handle(Geom_TrimmedCurve) sc = GC_MakeArcOfCircle(scircle, p1, p2, 1);
Handle(Geom_TrimmedCurve) bc = GC_MakeArcOfCircle(bcircle, p2, p3, 1);
TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(sc);
TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(bc);
TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(v3, v4);
TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge(v4, v1);
//TopoDS_Wire out = BRepBuilderAPI_MakeWire(anEdge1,anEdge2);
//TopoDS_Shape out1 = BRepAlgoAPI_Fuse(anEdge1, anEdge2);
//TopoDS_Shape out = BRepAlgoAPI_Fuse(out1, anEdge3);
TopTools_ListOfShape listEdge;
listEdge.Append(anEdge4);
listEdge.Append(anEdge3);
listEdge.Append(anEdge2);
//listEdge.Append(anEdge1);
BRepBuilderAPI_MakeWire mw;
mw.Add(listEdge);
mw.Build();
if (!mw.IsDone())
{
if (mw.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_EmptyWire)
{
std::cout << "Wire failed-EmptyWire!\n";
}
else if (mw.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_DisconnectedWire)
{
std::cout << "Wire failed-DisconnectedWire!\n";
}
else if (mw.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_NonManifoldWire)
{
std::cout << "Wire failed-NonManifoldWire!\n";
}
else
{
std::cout << "Wire failed!\n";
}
}
//mw.Add(anEdge3);
//mw.Add(anEdge2);
//mw.Add(anEdge1);
//TopoDS_Shape bcut = BRepBuilderAPI_MakeFace(aCylinder->Cylinder(), TopoDS::Wire(seccut), Standard_True);
//std::cout << "ok" << std::endl;
Viewer vout(50, 50, 500, 500);
vout << wbe;
vout << xline;
vout << yline;
vout << zline;
vout << mw;
//vout << anEdge1;
//vout << anEdge2;
vout.StartMessageLoop();
return 0;
}
Error 方法返回 BRepBuilderAPI_WireError 枚举中的一个术语:WireDone – 没有发生错误。
EmptyWire – 未初始化算法,使用了空构造函数。
DisconnectedWire – 最后添加的边未连接到Wire。
NonManifoldWire – 具有某种奇异性的Wire。
上述代码返回结果:Wire failed-DisconnectedWire!
表明后面添加的边未连接到Wire,也就是两个圆弧未连接线
通过以下代码输出圆弧的端点坐标,发现圆弧之间的交点距离过大,超出了容差,将距离过大的用直线连接起来,成功生成Wire。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"Viewer.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
gp_Dir Z(0.0, 0.0, 1.0);
gp_Pnt center(0, 0, 0.0);
gp_Pnt xr(0.5, 0, 0.0);
gp_Pnt yr(0.0, 1.0, 0.0);
gp_Pnt zr(0.0, 0.0, 7.0);
gp_Ax2 wb(center, Z);
gp_Circ wbcircle(wb, 0.125 / 2);
TopoDS_Edge wbe = BRepBuilderAPI_MakeEdge(wbcircle);
TopoDS_Edge xline = BRepBuilderAPI_MakeEdge(center, xr);
TopoDS_Edge yline = BRepBuilderAPI_MakeEdge(center, yr);
TopoDS_Edge zline = BRepBuilderAPI_MakeEdge(center, zr);
Standard_Real smallR = 0.112;
Standard_Real bigR = 0.1975;
Standard_Real xb = 0.0;
Standard_Real yb = bigR;
Standard_Real xs = 0.0105;
Standard_Real ys = 0.1023;
gp_Pnt p1(-0.0998, 0.0828, 0.0);
gp_Pnt p2(-0.0456, 0.0053, 0.0);
gp_Pnt p3(0.1637, 0.0871, 0.0);
gp_Pnt p4(0.2237, 0.1471, 0.0);
gp_Pnt scenter(xs, ys, 0.0);
gp_Pnt bcenter(xb, yb, 0.0);
gp_Ax2 sr(scenter, Z);
gp_Ax2 br(bcenter, Z);
gp_Circ scircle(sr, smallR);
gp_Circ bcircle(br, bigR);
TopoDS_Vertex v1 = BRepBuilderAPI_MakeVertex(p1);
TopoDS_Vertex v2 = BRepBuilderAPI_MakeVertex(p2);
TopoDS_Vertex v3 = BRepBuilderAPI_MakeVertex(p3);
TopoDS_Vertex v4 = BRepBuilderAPI_MakeVertex(p4);
Handle(Geom_TrimmedCurve) sc = GC_MakeArcOfCircle(scircle, p1, p2, 1);
Standard_Real scfirstParam = sc->FirstParameter();
Standard_Real sclastParam = sc->LastParameter();
gp_Pnt scpf,scpl;
sc->D0(scfirstParam, scpf);
sc->D0(sclastParam, scpl);
Handle(Geom_TrimmedCurve) bc = GC_MakeArcOfCircle(bcircle, p2, p3, 1);
Standard_Real bcfirstParam = bc->FirstParameter();
Standard_Real bclastParam = bc->LastParameter();
gp_Pnt bcpf, bcpl;
bc->D0(bcfirstParam, bcpf);
bc->D0(bclastParam, bcpl);
std::cout << "sc first:(" << scpf.X() << "," << scpf.Y() << "," << scpf.Z() << ")" << std::endl;
std::cout << "sc last:(" << scpl.X() << "," << scpl.Y() << "," << scpl.Z() << ")" << std::endl;
std::cout << "bc first:(" << bcpf.X() << "," << bcpf.Y() << "," << bcpf.Z() << ")" << std::endl;
std::cout << "bc last:(" << bcpl.X() << "," << bcpl.Y() << "," << bcpl.Z() << ")" << std::endl;
TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(sc);
TopoDS_Edge anEdge12 = BRepBuilderAPI_MakeEdge(scpl, bcpf);
TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(bc);
TopoDS_Edge anEdge23 = BRepBuilderAPI_MakeEdge(bcpl,p3);
TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(v3, v4);
TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge(v4, v1);
TopoDS_Edge anEdge41 = BRepBuilderAPI_MakeEdge(p1, scpf);
//TopoDS_Wire out = BRepBuilderAPI_MakeWire(anEdge1,anEdge2);
//TopoDS_Shape out1 = BRepAlgoAPI_Fuse(anEdge1, anEdge2);
//TopoDS_Shape out = BRepAlgoAPI_Fuse(out1, anEdge3);
TopTools_ListOfShape listEdge;
listEdge.Append(anEdge1);
listEdge.Append(anEdge12);
listEdge.Append(anEdge2);
listEdge.Append(anEdge23);
listEdge.Append(anEdge3);
listEdge.Append(anEdge4);
listEdge.Append(anEdge41);
//listEdge.Append(anEdge2);
//listEdge.Append(anEdge1);
BRepBuilderAPI_MakeWire mw;
mw.Add(listEdge);
mw.Build();
if (!mw.IsDone())
{
if (mw.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_EmptyWire)
{
std::cout << "Wire failed-EmptyWire!\n";
}
else if (mw.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_DisconnectedWire)
{
std::cout << "Wire failed-DisconnectedWire!\n";
}
else if (mw.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_NonManifoldWire)
{
std::cout << "Wire failed-NonManifoldWire!\n";
}
else
{
std::cout << "Wire failed!\n";
}
}
//mw.Add(anEdge3);
//mw.Add(anEdge2);
//mw.Add(anEdge1);
//TopoDS_Shape bcut = BRepBuilderAPI_MakeFace(aCylinder->Cylinder(), TopoDS::Wire(seccut), Standard_True);
//std::cout << "ok" << std::endl;
Viewer vout(50, 50, 500, 500);
vout << wbe;
vout << xline;
vout << yline;
vout << zline;
vout << mw;
//vout << anEdge1;
//vout << anEdge2;
vout.StartMessageLoop();
return 0;
}
sc first:(-0.0997897,0.0828018,0)
sc last:(-0.0455727,0.00534717,0)
bc first:(-0.0455918,0.00533436,0)
bc last:(0.163743,0.0870711,0)