进行了edge创建,wire创建,以及通过wire来偏置曲线功能,及显示 功能
void ViewWindow::OnGen2dCurve()
{
// Clear All
_ctrlUi->listView->Clear();
try
{
//构造一个线段
GC_MakeSegment Segment1(gp_Pnt(20, 0, 0), gp_Pnt(30, 0, 0));
//根据线段构造一个边
BRepBuilderAPI_MakeEdge anEdge(Segment1.Value());
BRepBuilderAPI_MakeEdge anEdge2(gp_Pnt(30, 0, 0), gp_Pnt(30, 20, 0));
BRepBuilderAPI_MakeEdge anEdge3(gp_Pnt(30, 20, 0), gp_Pnt(20, 0, 0));
//aWire.Add(anEdge3.Edge());
GC_MakeArcOfCircle arc4(gp_Pnt(30, 20, 0), gp_Pnt(34.651, 31.229, 0), gp_Pnt(45.88, 35.88, 0));
BRepBuilderAPI_MakeEdge anEdge4(arc4.Value());
构造一个圆
double dr5 = 49.4178;
double dAngelS5 = 98.82;
double dAngelE5 = 94.4815;
double dCenterX5 = 41.2769;
double dCenterY5 = -12.4703;
double dx5 = dr5 * cos(((dAngelE5 - dAngelS5) / 2 + dAngelS5) * M_PI / 180) + dCenterX5;
double dy5 = dr5 * sin(((dAngelE5 - dAngelS5) / 2 + dAngelS5) * M_PI / 180) + dCenterY5;
double dxs5 = dr5 * cos(dAngelS5 * M_PI / 180) + dCenterX5;
double dys5 = dr5 * sin(dAngelS5 * M_PI / 180) + dCenterY5;
double dxe5 = dr5 * cos(dAngelE5 * M_PI / 180) + dCenterX5;
double dye5 = dr5 * sin(dAngelE5 * M_PI / 180) + dCenterY5;
//GC_MakeArcOfCircle arc5(gp_Pnt(33.7, 36.363, 0), gp_Pnt(35.554, 36.615, 0),gp_Pnt(37.416, 36.796, 0));
GC_MakeArcOfCircle arc5(gp_Pnt(dxs5, dys5, 0), gp_Pnt(dx5, dy5, 0), gp_Pnt(dxe5, dye5, 0));
BRepBuilderAPI_MakeEdge anEdge5(arc5.Value());
// gp_Circ circle55(gp_Ax2(gp_Pnt(41.277, -12.470, 0), gp_Dir(0, 0, 1)), 49.418);
// //构造一个圆弧边
// GC_MakeArcOfCircle arc55(circle55, 94.481*M_PI/180, 98.82 * M_PI / 180,Standard_True);
// const Handle(Geom_TrimmedCurve) TrimmedCurve5 = arc55.Value();
// //求中间点位置
// Standard_Real first5 = TrimmedCurve5->FirstParameter();
// Standard_Real Last5 = TrimmedCurve5->LastParameter();
// Standard_Real mid5 = (Last5 - first5) / 2+ first5;
//GC_MakeArcOfCircle arc5(TrimmedCurve5->Value(first5),
// TrimmedCurve5->Value(mid5),
// TrimmedCurve5->Value(Last5));
//BRepBuilderAPI_MakeEdge anEdge5(arc5.Value());
Standard_Real fBegin5 = 0, fEnd5 = 0;
Handle(Geom_Curve) hCurveEdge5 = BRep_Tool::Curve(anEdge5, fBegin5, fEnd5);
Handle(Geom_Circle) circle5 = Handle_Geom_Circle::DownCast(hCurveEdge5);
// 在这里处理圆对象
qDebug() << "Circle5";
//读取圆弧起点和终点坐标
qDebug() << "start5:[" << circle5->Value(fBegin5).X() << "," << circle5->Value(fBegin5).Y() << ","
<< circle5->Value(fBegin5).Z() << "]";
qDebug() << "end5:[" << circle5->Value(fEnd5).X() << "," << circle5->Value(fEnd5).Y() << ","
<< circle5->Value(fEnd5).Z() << "]";
qDebug() << "mid5:[" << circle5->Value((fEnd5- fBegin5)/2+ fBegin5).X() << "," << circle5->Value((fEnd5 - fBegin5) / 2 + fBegin5).Y() << ","
<< circle5->Value((fEnd5 - fBegin5) / 2 + fBegin5).Z() << "]";
qDebug() << "R5:" << circle5->Radius();
const gp_Circ& circ5 = circle5->Circ();
const gp_Pnt& center5 = circ5.Location();
// 获取圆心坐标
qDebug() << "Center5:[" << center5.X() << "," << center5.Y() << ","
<< center5.Z() << "]";
//gp_Circ circle66(gp_Ax2(gp_Pnt(41.329, -13.129, 0), gp_Dir(0, 0, 1)), 50.078);
构造一个圆弧边
//GC_MakeArcOfCircle arc66(circle66, 90.199 * M_PI / 180,94.481 * M_PI / 180, Standard_True);
//const Handle(Geom_TrimmedCurve) TrimmedCurve6 = arc66.Value();
求中间点位置
//Standard_Real first6 = TrimmedCurve6->FirstParameter();
//Standard_Real Last6 = TrimmedCurve6->LastParameter();
//Standard_Real mid6 = (Last6 - first6) / 2 + first6;
//GC_MakeArcOfCircle arc6(TrimmedCurve6->Value(first6),
// TrimmedCurve6->Value(mid6),
// TrimmedCurve6->Value(Last6));
double dr6 = 50.0781;
double dAngelS6 = 94.4815 ;
double dAngelE6 = 90.1993;
double dCenterX6 = 41.3285;
double dCenterY6 = -13.1286;
double dx6 = dr6 * cos(((dAngelE6 - dAngelS6) / 2 + dAngelS6) * M_PI / 180) + dCenterX6;
double dy6 = dr6 * sin(((dAngelE6 - dAngelS6) / 2 + dAngelS6) * M_PI / 180) + dCenterY6;
double dxs6 = dr6 * cos(dAngelS6 * M_PI / 180) + dCenterX6;
double dys6 = dr6 * sin(dAngelS6 * M_PI / 180) + dCenterY6;
double dxe6 = dr6 * cos(dAngelE6 * M_PI / 180) + dCenterX6;
double dye6 = dr6 * sin(dAngelE6 * M_PI / 180) + dCenterY6;
//GC_MakeArcOfCircle arc6(gp_Pnt(37.416, 36.796, 0), gp_Pnt(39.283, 36.908, 0), gp_Pnt(41.154, 36.949, 0));
GC_MakeArcOfCircle arc6(gp_Pnt(dxe5, dye5, 0), gp_Pnt(dx6, dy6, 0), gp_Pnt(dxe6, dye6, 0));
BRepBuilderAPI_MakeEdge anEdge6(arc6.Value());
Standard_Real fBegin6 = 0, fEnd6 = 0;
Handle(Geom_Curve) hCurveEdge6 = BRep_Tool::Curve(anEdge6, fBegin6, fEnd6);
Handle(Geom_Circle) circle6 = Handle_Geom_Circle::DownCast(hCurveEdge6);
// 在这里处理圆对象
qDebug() << "Circle6";
//读取圆弧起点和终点坐标
qDebug() << "start6:[" << circle6->Value(fBegin6).X() << "," << circle6->Value(fBegin6).Y() << ","
<< circle6->Value(fBegin6).Z() << "]";
qDebug() << "end6:[" << circle6->Value(fEnd6).X() << "," << circle6->Value(fEnd6).Y() << ","
<< circle6->Value(fEnd6).Z() << "]";
qDebug() << "R6:" << circle6->Radius();
const gp_Circ& circ6 = circle6->Circ();
const gp_Pnt& center6 = circ6.Location();
// 获取圆心坐标
qDebug() << "Center6:[" << center6.X() << "," << center6.Y() << ","
<< center6.Z() << "]";
Handle(Geom2d_Circle) circle = new Geom2d_Circle(gp_Ax2d(gp_Pnt2d(center6.X(), center6.Y()) , gp_Dir2d(1, 0)), dr6);
Handle(Geom2d_TrimmedCurve) trimmedCurve61 = new Geom2d_TrimmedCurve(circle, dAngelS6*M_PI/180, dAngelE6 * M_PI / 180);
Geom2d_OffsetCurve OffsetCurve61(trimmedCurve61, -0.2);
// 获取起始点和结束点
gp_Pnt2d startPoint66 = OffsetCurve61.Value(OffsetCurve61.FirstParameter());
gp_Pnt2d endPoint66 = OffsetCurve61.Value(OffsetCurve61.LastParameter());
qDebug() << "startPoint66:(" << startPoint66.X() << "," << startPoint66.Y() << ")";
qDebug() << "endPoint66:(" << endPoint66.X() << "," << endPoint66.Y() << ")";
GC_MakeSegment Segment7(gp_Pnt(dxe6, dye6, 0), gp_Pnt(41.901, 37.697, 0));
BRepBuilderAPI_MakeEdge anEdge7(Segment7.Value());
gp_Pnt2d piont71(dxe6, dye6);
gp_Pnt2d piont72(41.901, 37.697);
GCE2d_MakeSegment seg71(piont71, piont72);
Geom2d_OffsetCurve OffsetCurve71(seg71.Value(), 0.2);
// 获取起始点和结束点
gp_Pnt2d startPoint77 = OffsetCurve71.Value(OffsetCurve71.FirstParameter());
gp_Pnt2d endPoint77 = OffsetCurve71.Value(OffsetCurve71.LastParameter());
qDebug() << "startPoint77:(" << startPoint77.X() << "," << startPoint77.Y() << ")";
qDebug() << "endPoint77:(" << endPoint77.X() << "," << endPoint77.Y() << ")";
//GC_MakeSegment Segment8(gp_Pnt(41.901, 37.697, 0), gp_Pnt(41.901, 40.162, 0));
GC_MakeSegment Segment8(Segment7.Value()->EndPoint(), gp_Pnt(Segment7.Value()->EndPoint().X(), Segment7.Value()->EndPoint().Y()+10, 0));
BRepBuilderAPI_MakeEdge anEdge8(Segment8.Value());
GC_MakeSegment Segment9(Segment8.Value()->EndPoint(), gp_Pnt(dxs5, Segment8.Value()->EndPoint().Y(), 0));
BRepBuilderAPI_MakeEdge anEdge9(Segment9.Value());
GC_MakeSegment Segment10(gp_Pnt(dxs5, Segment8.Value()->EndPoint().Y(), 0), gp_Pnt(dxs5, dys5, 0));
BRepBuilderAPI_MakeEdge anEdge10(Segment10.Value());
//
TopTools_ListOfShape listEdge;
listEdge.Append(anEdge5.Edge());
listEdge.Append(anEdge6.Edge());
listEdge.Append(anEdge7.Edge());
listEdge.Append(anEdge8.Edge());
listEdge.Append(anEdge9.Edge());
listEdge.Append(anEdge10.Edge());
//根据一个边构造一个线框
BRepBuilderAPI_MakeWire aWire;
//aWire.Add(anEdge.Edge());
//aWire.Add(anEdge2.Edge());
//添加到线框中
//aWire.Add(anEdge4.Edge());
aWire.Add(listEdge);
// aWire.Build();
if (!aWire.IsDone())
{
if (aWire.Error()== BRepBuilderAPI_WireError::BRepBuilderAPI_EmptyWire)
{
qDebug() << "Wire failed-EmptyWire!\n";
}
else if (aWire.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_DisconnectedWire)
{
qDebug() << "Wire failed-DisconnectedWire!\n";
}
else if (aWire.Error() == BRepBuilderAPI_WireError::BRepBuilderAPI_NonManifoldWire)
{
qDebug() << "Wire failed-NonManifoldWire!\n";
}
else
{
qDebug() << "Wire failed!\n";
}
//return;
}
// 创建 ShapeFix_Wireframe 对象并传入线框
// Handle(ShapeFix_Wireframe) aFixWire = new ShapeFix_Wireframe(aWire.Shape());
aFixWire->SetPrecision(thePrec);
//aFixWire->SetMaxTolerance(0.01);
// aFixWire->FixWireGaps();
Handle(AIS_Shape) wire_shape = new AIS_Shape(aWire.Shape());
_ctrlUi->listView->RecordModel(wire_shape, QString("wire shape"));
wire_shape->SetColor(Quantity_NOC_YELLOW);
//offset shape
//线框做偏置距离
BRepOffsetAPI_MakeOffset mk_offset;
mk_offset.AddWire(aWire.Wire());
//mk_offset.SetApprox(true);
mk_offset.Init(GeomAbs_Arc, Standard_False);
Standard_Real offset = _ctrlUi->doubleSpinBox_offsetDist->value();
mk_offset.Perform(offset);
if (!mk_offset.IsDone())
{
qDebug() << "Offset failed!\n";
}
else
{
const TopoDS_Shape& offsetShape = mk_offset.Shape();
if (offsetShape.IsNull())
{
qDebug() << "offsetShape is null!\n";
return;
}
Handle(AIS_Shape) offset_shape = new AIS_Shape(offsetShape);
_ctrlUi->listView->RecordModel(offset_shape, QString("offset shape"));
offset_shape->SetColor(Quantity_NOC_RED);
//将拓扑数据转为几何数据
TopTools_IndexedMapOfShape aEdgeMap;
TopExp::MapShapes(offsetShape, TopAbs_EDGE, aEdgeMap);
qDebug() << "edge count is " << aEdgeMap.Extent();
//遍历结果
for (int i = 1; i <= aEdgeMap.Extent(); ++i)
{
const TopoDS_Edge& aEdge = TopoDS::Edge(aEdgeMap(i));
const TopoDS_Vertex& aFirstVertex = TopExp::FirstVertex(aEdge);
const TopoDS_Vertex& aLastVertex = TopExp::LastVertex(aEdge);
Standard_Real fBegin = 0, fEnd = 0;
Handle(Geom_Curve) hCurve = BRep_Tool::Curve(aEdge, fBegin, fEnd);
if (!hCurve.IsNull())
{
if (hCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
// 是直线
Handle(Geom_Line) line = Handle_Geom_Line::DownCast(hCurve);
// 在这里处理直线对象
qDebug() << "Line";
//读取直线起点和终点坐标
qDebug() << "start:[" << line->Value(fBegin).X() << "," << line->Value(fBegin).Y() << ","
<< line->Value(fBegin).Z() << "]";
qDebug() << "end:[" << line->Value(fEnd).X() << "," << line->Value(fEnd).Y() << ","
<< line->Value(fEnd).Z() << "]";
}
else if (hCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
// 是圆
Handle(Geom_Circle) circle = Handle_Geom_Circle::DownCast(hCurve);
// 在这里处理圆对象
qDebug() << "Circle";
//读取圆弧起点和终点坐标
qDebug() << "start:[" << circle->Value(fBegin).X() << "," << circle->Value(fBegin).Y() << ","
<< circle->Value(fBegin).Z() << "]";
qDebug() << "end:[" << circle->Value(fEnd).X() << "," << circle->Value(fEnd).Y() << ","
<< circle->Value(fEnd).Z() << "]";
qDebug() << "R:" << circle->Radius();
const gp_Circ& circ = circle->Circ();
const gp_Pnt& center = circ.Location();
// 获取圆心坐标
qDebug() << "Center:[" << center.X() << "," << center.Y() << ","
<< center.Z() << "]";
}
else if (hCurve->IsKind(STANDARD_TYPE(Geom_BezierCurve))) {
// 是贝塞尔曲线
Handle(Geom_BezierCurve) bezierCurve = Handle_Geom_BezierCurve::DownCast(hCurve);
// 在这里处理贝塞尔曲线对象
qDebug() << "BezierCurve";
}
else if (hCurve->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
// 是B样条曲线
Handle(Geom_BSplineCurve) bsplineCurve = Handle_Geom_BSplineCurve::DownCast(hCurve);
// 在这里处理B样条曲线对象
qDebug() << "SplineCurve";
}
else {
// 其他类型的曲线
// 在这里处理其他类型的曲线对象
qDebug() << "OtherCurve";
}
}
}
}
_view->fitAll();
//_context->Activate(ViewHelper::SM_FACE);
//_context->Activate(ViewHelper::SM_WIRE);
}
catch (Standard_Failure const& theFailure)
{
qDebug() << "info: " << theFailure.GetMessageString() << "\n";
}
}