#include "lzzcad.h"
#include "view.h"
#include <QToolBar>
#include <QTreeView>
#include <QMessageBox>
#include <QDockWidget>
#include <gp_Circ.hxx>
#include <gp_Elips.hxx>
#include <gp_Pln.hxx>
#include <gp_Lin2d.hxx>
#include <Geom_ConicalSurface.hxx>
#include <Geom_ToroidalSurface.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <TopoDS.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <BRepLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepBuilderAPI_MakePolygon.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeCone.hxx>
#include <BRepPrimAPI_MakeSphere.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepPrimAPI_MakeTorus.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepPrimAPI_MakeRevol.hxx>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <BRepFilletAPI_MakeChamfer.hxx>
#include <BRepOffsetAPI_MakePipe.hxx>
#include <BRepOffsetAPI_ThruSections.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRepAlgoAPI_Common.hxx>
#include <AIS_Shape.hxx>
lzzcad::lzzcad(QWidget* parent): QMainWindow(parent)//头文件中已经把类定义好,这里是在写他的构造函数。这种写法是C++中的构造函数初始化列表。
//在C++中,类的构造函数可以使用初始化列表来对成员变量进行初始化。初始化列表位于构造函数的参数列表之后,使用冒号( : )分隔。
{
ui.setupUi(this);// 调用ui对象的setupUi方法,初始化用户界面,也就是帮你生成界面,需要一点qt基础才能看懂这些
myOccView = new OccView(this);//没有这两个,没有中心部件,会出问题,需要qt基础才能看懂
setCentralWidget(myOccView);
createActions();//给所有行为定义
createMenus();//以前用,现在好像不用了,可有可无吧,注释后依旧正常运行
createToolBars();//创造工具栏一行,注释它工具栏就不显示了
}
lzzcad::~lzzcad()//析构函数,没有给出具体实现方式
{
}
void lzzcad::createActions(void)//设置信号与槽的连接,所有行为都对应一个信号与槽函数,但是不清楚为什么有的连this,有的连myoccview
{
// File
connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));
// View
connect(ui.actionZoom, SIGNAL(triggered()), myOccView, SLOT(zoom()));//这些槽函数都是已经被定义好的
connect(ui.actionPan, SIGNAL(triggered()), myOccView, SLOT(pan()));
connect(ui.actionRotate, SIGNAL(triggered()), myOccView, SLOT(rotate()));
connect(ui.actionReset, SIGNAL(triggered()), myOccView, SLOT(reset()));
connect(ui.actionFitAll, SIGNAL(triggered()), myOccView, SLOT(fitAll()));//对应view.cpp里的槽函数
// Primitive
connect(ui.actionBox, SIGNAL(triggered()), this, SLOT(makeBox()));
connect(ui.actionCone, SIGNAL(triggered()), this, SLOT(makeCone()));
connect(ui.actionSphere, SIGNAL(triggered()), this, SLOT(makeSphere()));
connect(ui.actionCylinder, SIGNAL(triggered()), this, SLOT(makeCylinder()));
connect(ui.actionTorus, SIGNAL(triggered()), this, SLOT(makeTorus()));
// Modeling
connect(ui.actionFillet, SIGNAL(triggered()), this, SLOT(makeFillet()));
connect(ui.actionChamfer, SIGNAL(triggered()), this, SLOT(makeChamfer()));
connect(ui.actionExtrude, SIGNAL(triggered()), this, SLOT(makeExtrude()));
connect(ui.actionRevolve, SIGNAL(triggered()), this, SLOT(makeRevol()));
connect(ui.actionLoft, SIGNAL(triggered()), this, SLOT(makeLoft()));
connect(ui.actionCut, SIGNAL(triggered()), this, SLOT(testCut()));
connect(ui.actionFuse, SIGNAL(triggered()), this, SLOT(testFuse()));
connect(ui.actionCommon, SIGNAL(triggered()), this, SLOT(testCommon()));
connect(ui.actionHelix, SIGNAL(triggered()), this, SLOT(testHelix()));
// Help
connect(ui.actionAbout, SIGNAL(triggered()), this, SLOT(about()));
}
void lzzcad::createMenus(void)// 这个函数目前是空的,这可能意味着这个类在早期的版本中有创建菜单的功能,但在当前的版本中已经不使用了
{
}
void lzzcad::createToolBars(void)//该成员函数功能是创建occqt的工具栏,它的标题没什么用,给自己看的。同时,他把上面信号与槽中的action都可视化出来了
{
QToolBar* aToolBar = addToolBar(tr("&Navigate"));
aToolBar->addAction(ui.actionZoom);//atoolbar是指针,用->
aToolBar->addAction(ui.actionPan);
aToolBar->addAction(ui.actionRotate);
aToolBar = addToolBar(tr("&View"));//注释一行后,工具栏上面就会少一行
aToolBar->addAction(ui.actionReset);//action连接着别的槽函数,相应地,点击该东西就会触发槽函数
aToolBar->addAction(ui.actionFitAll);
aToolBar = addToolBar(tr("&Primitive"));
aToolBar->addAction(ui.actionBox);//都是连着槽函数的,在上面的信号与槽中已经定义过了
aToolBar->addAction(ui.actionCone);
aToolBar->addAction(ui.actionSphere);
aToolBar->addAction(ui.actionCylinder);
aToolBar->addAction(ui.actionTorus);
aToolBar = addToolBar(tr("&Modeling"));
aToolBar->addAction(ui.actionFillet);
aToolBar->addAction(ui.actionChamfer);
aToolBar->addAction(ui.actionExtrude);
aToolBar->addAction(ui.actionRevolve);
aToolBar->addAction(ui.actionLoft);
aToolBar->addSeparator();
aToolBar->addAction(ui.actionCut);
aToolBar->addAction(ui.actionFuse);
aToolBar->addAction(ui.actionCommon);
aToolBar->addSeparator();
aToolBar->addAction(ui.actionHelix);
aToolBar = addToolBar(tr("Help"));
aToolBar->addAction(ui.actionAbout);
}
void lzzcad::about()//点击actionabout时触发的槽函数
{
QMessageBox::about(this, tr("About lzzcad"),
tr("<h2>occQt 2.0</h2>"
"<p>Copyright © 2014 eryar@163.com"
"<p>occQt is a demo applicaton about Qt and OpenCASCADE."));
}
void lzzcad::makeBox()//点击对应action触发的槽函数
{
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(3.0, 4.0, 5.0).Shape();
Handle(AIS_Shape) anAisBox = new AIS_Shape(aTopoBox);
anAisBox->SetColor(Quantity_NOC_AZURE);
myOccView->getContext()->Display(anAisBox, Standard_True);
}
void lzzcad::makeCone()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 10.0, 0.0));
TopoDS_Shape aTopoReducer = BRepPrimAPI_MakeCone(anAxis, 3.0, 1.5, 5.0).Shape();
Handle(AIS_Shape) anAisReducer = new AIS_Shape(aTopoReducer);
anAisReducer->SetColor(Quantity_NOC_BISQUE);
anAxis.SetLocation(gp_Pnt(8.0, 10.0, 0.0));
TopoDS_Shape aTopoCone = BRepPrimAPI_MakeCone(anAxis, 3.0, 0.0, 5.0).Shape();
Handle(AIS_Shape) anAisCone = new AIS_Shape(aTopoCone);
anAisCone->SetColor(Quantity_NOC_CHOCOLATE);
myOccView->getContext()->Display(anAisReducer, Standard_True);
myOccView->getContext()->Display(anAisCone, Standard_True);
}
void lzzcad::makeSphere()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 20.0, 0.0));
TopoDS_Shape aTopoSphere = BRepPrimAPI_MakeSphere(anAxis, 3.0).Shape();
Handle(AIS_Shape) anAisSphere = new AIS_Shape(aTopoSphere);
anAisSphere->SetColor(Quantity_NOC_BLUE1);
myOccView->getContext()->Display(anAisSphere, Standard_True);
}
void lzzcad::makeCylinder()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 30.0, 0.0));
TopoDS_Shape aTopoCylinder = BRepPrimAPI_MakeCylinder(anAxis, 3.0, 5.0).Shape();
Handle(AIS_Shape) anAisCylinder = new AIS_Shape(aTopoCylinder);
anAisCylinder->SetColor(Quantity_NOC_RED);
anAxis.SetLocation(gp_Pnt(8.0, 30.0, 0.0));
TopoDS_Shape aTopoPie = BRepPrimAPI_MakeCylinder(anAxis, 3.0, 5.0, M_PI_2 * 3.0).Shape();
Handle(AIS_Shape) anAisPie = new AIS_Shape(aTopoPie);
anAisPie->SetColor(Quantity_NOC_TAN);
myOccView->getContext()->Display(anAisCylinder, Standard_True);
myOccView->getContext()->Display(anAisPie, Standard_True);
}
void lzzcad::makeTorus()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 40.0, 0.0));
TopoDS_Shape aTopoTorus = BRepPrimAPI_MakeTorus(anAxis, 3.0, 1.0).Shape();
Handle(AIS_Shape) anAisTorus = new AIS_Shape(aTopoTorus);
anAisTorus->SetColor(Quantity_NOC_YELLOW);
anAxis.SetLocation(gp_Pnt(8.0, 40.0, 0.0));
TopoDS_Shape aTopoElbow = BRepPrimAPI_MakeTorus(anAxis, 3.0, 1.0, M_PI_2).Shape();
Handle(AIS_Shape) anAisElbow = new AIS_Shape(aTopoElbow);
anAisElbow->SetColor(Quantity_NOC_THISTLE);
myOccView->getContext()->Display(anAisTorus, Standard_True);
myOccView->getContext()->Display(anAisElbow, Standard_True);
}
void lzzcad::makeFillet()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 50.0, 0.0));
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(anAxis, 3.0, 4.0, 5.0).Shape();
BRepFilletAPI_MakeFillet MF(aTopoBox);
// Add all the edges to fillet.
for (TopExp_Explorer ex(aTopoBox, TopAbs_EDGE); ex.More(); ex.Next())
{
MF.Add(1.0, TopoDS::Edge(ex.Current()));
}
Handle(AIS_Shape) anAisShape = new AIS_Shape(MF.Shape());
anAisShape->SetColor(Quantity_NOC_VIOLET);
myOccView->getContext()->Display(anAisShape, Standard_True);
}
void lzzcad::makeChamfer()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(8.0, 50.0, 0.0));
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(anAxis, 3.0, 4.0, 5.0).Shape();
BRepFilletAPI_MakeChamfer MC(aTopoBox);
TopTools_IndexedDataMapOfShapeListOfShape aEdgeFaceMap;
TopExp::MapShapesAndAncestors(aTopoBox, TopAbs_EDGE, TopAbs_FACE, aEdgeFaceMap);
for (Standard_Integer i = 1; i <= aEdgeFaceMap.Extent(); ++i)
{
TopoDS_Edge anEdge = TopoDS::Edge(aEdgeFaceMap.FindKey(i));
TopoDS_Face aFace = TopoDS::Face(aEdgeFaceMap.FindFromIndex(i).First());
MC.Add(0.6, 0.6, anEdge, aFace);
}
Handle(AIS_Shape) anAisShape = new AIS_Shape(MC.Shape());
anAisShape->SetColor(Quantity_NOC_TOMATO);
myOccView->getContext()->Display(anAisShape, Standard_True);
}
void lzzcad::makeExtrude()
{
// prism a vertex result is an edge.
TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex(gp_Pnt(0.0, 60.0, 0.0));
TopoDS_Shape aPrismVertex = BRepPrimAPI_MakePrism(aVertex, gp_Vec(0.0, 0.0, 5.0));
Handle(AIS_Shape) anAisPrismVertex = new AIS_Shape(aPrismVertex);
// prism an edge result is a face.
TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(gp_Pnt(5.0, 60.0, 0.0), gp_Pnt(10.0, 60.0, 0.0));
TopoDS_Shape aPrismEdge = BRepPrimAPI_MakePrism(anEdge, gp_Vec(0.0, 0.0, 5.0));
Handle(AIS_Shape) anAisPrismEdge = new AIS_Shape(aPrismEdge);
// prism a wire result is a shell.
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(16.0, 60.0, 0.0));
TopoDS_Edge aCircleEdge = BRepBuilderAPI_MakeEdge(gp_Circ(anAxis, 3.0));
TopoDS_Wire aCircleWire = BRepBuilderAPI_MakeWire(aCircleEdge);
TopoDS_Shape aPrismCircle = BRepPrimAPI_MakePrism(aCircleWire, gp_Vec(0.0, 0.0, 5.0));
Handle(AIS_Shape) anAisPrismCircle = new AIS_Shape(aPrismCircle);
// prism a face or a shell result is a solid.
anAxis.SetLocation(gp_Pnt(24.0, 60.0, 0.0));
TopoDS_Edge aEllipseEdge = BRepBuilderAPI_MakeEdge(gp_Elips(anAxis, 3.0, 2.0));
TopoDS_Wire aEllipseWire = BRepBuilderAPI_MakeWire(aEllipseEdge);
TopoDS_Face aEllipseFace = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()), aEllipseWire);
TopoDS_Shape aPrismEllipse = BRepPrimAPI_MakePrism(aEllipseFace, gp_Vec(0.0, 0.0, 5.0));
Handle(AIS_Shape) anAisPrismEllipse = new AIS_Shape(aPrismEllipse);
anAisPrismVertex->SetColor(Quantity_NOC_PAPAYAWHIP);
anAisPrismEdge->SetColor(Quantity_NOC_PEACHPUFF);
anAisPrismCircle->SetColor(Quantity_NOC_PERU);
anAisPrismEllipse->SetColor(Quantity_NOC_PINK);
myOccView->getContext()->Display(anAisPrismVertex, Standard_True);
myOccView->getContext()->Display(anAisPrismEdge, Standard_True);
myOccView->getContext()->Display(anAisPrismCircle, Standard_True);
myOccView->getContext()->Display(anAisPrismEllipse, Standard_True);
}
void lzzcad::makeRevol()
{
gp_Ax1 anAxis;
// revol a vertex result is an edge.
anAxis.SetLocation(gp_Pnt(0.0, 70.0, 0.0));
TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex(gp_Pnt(2.0, 70.0, 0.0));
TopoDS_Shape aRevolVertex = BRepPrimAPI_MakeRevol(aVertex, anAxis);
Handle(AIS_Shape) anAisRevolVertex = new AIS_Shape(aRevolVertex);
// revol an edge result is a face.
anAxis.SetLocation(gp_Pnt(8.0, 70.0, 0.0));
TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(gp_Pnt(6.0, 70.0, 0.0), gp_Pnt(6.0, 70.0, 5.0));
TopoDS_Shape aRevolEdge = BRepPrimAPI_MakeRevol(anEdge, anAxis);
Handle(AIS_Shape) anAisRevolEdge = new AIS_Shape(aRevolEdge);
// revol a wire result is a shell.
anAxis.SetLocation(gp_Pnt(20.0, 70.0, 0.0));
anAxis.SetDirection(gp::DY());
TopoDS_Edge aCircleEdge = BRepBuilderAPI_MakeEdge(gp_Circ(gp_Ax2(gp_Pnt(15.0, 70.0, 0.0), gp::DZ()), 1.5));
TopoDS_Wire aCircleWire = BRepBuilderAPI_MakeWire(aCircleEdge);
TopoDS_Shape aRevolCircle = BRepPrimAPI_MakeRevol(aCircleWire, anAxis, M_PI_2);
Handle(AIS_Shape) anAisRevolCircle = new AIS_Shape(aRevolCircle);
// revol a face result is a solid.
anAxis.SetLocation(gp_Pnt(30.0, 70.0, 0.0));
anAxis.SetDirection(gp::DY());
TopoDS_Edge aEllipseEdge = BRepBuilderAPI_MakeEdge(gp_Elips(gp_Ax2(gp_Pnt(25.0, 70.0, 0.0), gp::DZ()), 3.0, 2.0));
TopoDS_Wire aEllipseWire = BRepBuilderAPI_MakeWire(aEllipseEdge);
TopoDS_Face aEllipseFace = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()), aEllipseWire);
TopoDS_Shape aRevolEllipse = BRepPrimAPI_MakeRevol(aEllipseFace, anAxis, M_PI_4);
Handle(AIS_Shape) anAisRevolEllipse = new AIS_Shape(aRevolEllipse);
anAisRevolVertex->SetColor(Quantity_NOC_LIMEGREEN);
anAisRevolEdge->SetColor(Quantity_NOC_LINEN);
anAisRevolCircle->SetColor(Quantity_NOC_MAGENTA1);
anAisRevolEllipse->SetColor(Quantity_NOC_MAROON);
myOccView->getContext()->Display(anAisRevolVertex, Standard_True);
myOccView->getContext()->Display(anAisRevolEdge, Standard_True);
myOccView->getContext()->Display(anAisRevolCircle, Standard_True);
myOccView->getContext()->Display(anAisRevolEllipse, Standard_True);
}
void lzzcad::makeLoft()
{
// bottom wire.
TopoDS_Edge aCircleEdge = BRepBuilderAPI_MakeEdge(gp_Circ(gp_Ax2(gp_Pnt(0.0, 80.0, 0.0), gp::DZ()), 1.5));
TopoDS_Wire aCircleWire = BRepBuilderAPI_MakeWire(aCircleEdge);
// top wire.
BRepBuilderAPI_MakePolygon aPolygon;
aPolygon.Add(gp_Pnt(-3.0, 77.0, 6.0));
aPolygon.Add(gp_Pnt(3.0, 77.0, 6.0));
aPolygon.Add(gp_Pnt(3.0, 83.0, 6.0));
aPolygon.Add(gp_Pnt(-3.0, 83.0, 6.0));
aPolygon.Close();
BRepOffsetAPI_ThruSections aShellGenerator;
BRepOffsetAPI_ThruSections aSolidGenerator(true);
aShellGenerator.AddWire(aCircleWire);
aShellGenerator.AddWire(aPolygon.Wire());
aSolidGenerator.AddWire(aCircleWire);
aSolidGenerator.AddWire(aPolygon.Wire());
// translate the solid.
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(18.0, 0.0, 0.0));
BRepBuilderAPI_Transform aTransform(aSolidGenerator.Shape(), aTrsf);
Handle(AIS_Shape) anAisShell = new AIS_Shape(aShellGenerator.Shape());
Handle(AIS_Shape) anAisSolid = new AIS_Shape(aTransform.Shape());
anAisShell->SetColor(Quantity_NOC_OLIVEDRAB);
anAisSolid->SetColor(Quantity_NOC_PEACHPUFF);
myOccView->getContext()->Display(anAisShell, Standard_True);
myOccView->getContext()->Display(anAisSolid, Standard_True);
}
void lzzcad::testCut()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 90.0, 0.0));
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(anAxis, 3.0, 4.0, 5.0).Shape();
TopoDS_Shape aTopoSphere = BRepPrimAPI_MakeSphere(anAxis, 2.5).Shape();
TopoDS_Shape aCuttedShape1 = BRepAlgoAPI_Cut(aTopoBox, aTopoSphere);
TopoDS_Shape aCuttedShape2 = BRepAlgoAPI_Cut(aTopoSphere, aTopoBox);
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(8.0, 0.0, 0.0));
BRepBuilderAPI_Transform aTransform1(aCuttedShape1, aTrsf);
aTrsf.SetTranslation(gp_Vec(16.0, 0.0, 0.0));
BRepBuilderAPI_Transform aTransform2(aCuttedShape2, aTrsf);
Handle(AIS_Shape) anAisBox = new AIS_Shape(aTopoBox);
Handle(AIS_Shape) anAisSphere = new AIS_Shape(aTopoSphere);
Handle(AIS_Shape) anAisCuttedShape1 = new AIS_Shape(aTransform1.Shape());
Handle(AIS_Shape) anAisCuttedShape2 = new AIS_Shape(aTransform2.Shape());
anAisBox->SetColor(Quantity_NOC_SPRINGGREEN);
anAisSphere->SetColor(Quantity_NOC_STEELBLUE);
anAisCuttedShape1->SetColor(Quantity_NOC_TAN);
anAisCuttedShape2->SetColor(Quantity_NOC_SALMON);
myOccView->getContext()->Display(anAisBox, Standard_True);
myOccView->getContext()->Display(anAisSphere, Standard_True);
myOccView->getContext()->Display(anAisCuttedShape1, Standard_True);
myOccView->getContext()->Display(anAisCuttedShape2, Standard_True);
}
void lzzcad::testFuse()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 100.0, 0.0));
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(anAxis, 3.0, 4.0, 5.0).Shape();
TopoDS_Shape aTopoSphere = BRepPrimAPI_MakeSphere(anAxis, 2.5).Shape();
TopoDS_Shape aFusedShape = BRepAlgoAPI_Fuse(aTopoBox, aTopoSphere);
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(8.0, 0.0, 0.0));
BRepBuilderAPI_Transform aTransform(aFusedShape, aTrsf);
Handle(AIS_Shape) anAisBox = new AIS_Shape(aTopoBox);
Handle(AIS_Shape) anAisSphere = new AIS_Shape(aTopoSphere);
Handle(AIS_Shape) anAisFusedShape = new AIS_Shape(aTransform.Shape());
anAisBox->SetColor(Quantity_NOC_SPRINGGREEN);
anAisSphere->SetColor(Quantity_NOC_STEELBLUE);
anAisFusedShape->SetColor(Quantity_NOC_ROSYBROWN);
myOccView->getContext()->Display(anAisBox, Standard_True);
myOccView->getContext()->Display(anAisSphere, Standard_True);
myOccView->getContext()->Display(anAisFusedShape, Standard_True);
}
void lzzcad::testCommon()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 110.0, 0.0));
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(anAxis, 3.0, 4.0, 5.0).Shape();
TopoDS_Shape aTopoSphere = BRepPrimAPI_MakeSphere(anAxis, 2.5).Shape();
TopoDS_Shape aCommonShape = BRepAlgoAPI_Common(aTopoBox, aTopoSphere);
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(8.0, 0.0, 0.0));
BRepBuilderAPI_Transform aTransform(aCommonShape, aTrsf);
Handle(AIS_Shape) anAisBox = new AIS_Shape(aTopoBox);
Handle(AIS_Shape) anAisSphere = new AIS_Shape(aTopoSphere);
Handle(AIS_Shape) anAisCommonShape = new AIS_Shape(aTransform.Shape());
anAisBox->SetColor(Quantity_NOC_SPRINGGREEN);
anAisSphere->SetColor(Quantity_NOC_STEELBLUE);
anAisCommonShape->SetColor(Quantity_NOC_ROYALBLUE);
myOccView->getContext()->Display(anAisBox, Standard_True);
myOccView->getContext()->Display(anAisSphere, Standard_True);
myOccView->getContext()->Display(anAisCommonShape, Standard_True);
}
void lzzcad::testHelix()
{
makeCylindricalHelix();
makeConicalHelix();
makeToroidalHelix();
}
void lzzcad::makeCylindricalHelix()
{
Standard_Real aRadius = 3.0;
Standard_Real aPitch = 1.0;
// the pcurve is a 2d line in the parametric space.
gp_Lin2d aLine2d(gp_Pnt2d(0.0, 0.0), gp_Dir2d(aRadius, aPitch));
Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(aLine2d, 0.0, M_PI * 2.0).Value();
Handle(Geom_CylindricalSurface) aCylinder = new Geom_CylindricalSurface(gp::XOY(), aRadius);
TopoDS_Edge aHelixEdge = BRepBuilderAPI_MakeEdge(aSegment, aCylinder, 0.0, 6.0 * M_PI).Edge();
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(0.0, 120.0, 0.0));
BRepBuilderAPI_Transform aTransform(aHelixEdge, aTrsf);
Handle(AIS_Shape) anAisHelixCurve = new AIS_Shape(aTransform.Shape());
myOccView->getContext()->Display(anAisHelixCurve, Standard_True);
// sweep a circle profile along the helix curve.
// there is no curve3d in the pcurve edge, so approx one.
BRepLib::BuildCurve3d(aHelixEdge);
gp_Ax2 anAxis;
anAxis.SetDirection(gp_Dir(0.0, 4.0, 1.0));
anAxis.SetLocation(gp_Pnt(aRadius, 0.0, 0.0));
gp_Circ aProfileCircle(anAxis, 0.3);
TopoDS_Edge aProfileEdge = BRepBuilderAPI_MakeEdge(aProfileCircle).Edge();
TopoDS_Wire aProfileWire = BRepBuilderAPI_MakeWire(aProfileEdge).Wire();
TopoDS_Face aProfileFace = BRepBuilderAPI_MakeFace(aProfileWire).Face();
TopoDS_Wire aHelixWire = BRepBuilderAPI_MakeWire(aHelixEdge).Wire();
BRepOffsetAPI_MakePipe aPipeMaker(aHelixWire, aProfileFace);
if (aPipeMaker.IsDone())
{
aTrsf.SetTranslation(gp_Vec(8.0, 120.0, 0.0));
BRepBuilderAPI_Transform aPipeTransform(aPipeMaker.Shape(), aTrsf);
Handle(AIS_Shape) anAisPipe = new AIS_Shape(aPipeTransform.Shape());
anAisPipe->SetColor(Quantity_NOC_CORAL);
myOccView->getContext()->Display(anAisPipe, Standard_True);
}
}
/**
* make conical helix, it is the same as the cylindrical helix,
* the only different is the surface.
*/
void lzzcad::makeConicalHelix()
{
Standard_Real aRadius = 3.0;
Standard_Real aPitch = 1.0;
// the pcurve is a 2d line in the parametric space.
gp_Lin2d aLine2d(gp_Pnt2d(0.0, 0.0), gp_Dir2d(aRadius, aPitch));
Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(aLine2d, 0.0, M_PI * 2.0).Value();
Handle(Geom_ConicalSurface) aCylinder = new Geom_ConicalSurface(gp::XOY(), M_PI / 6.0, aRadius);
TopoDS_Edge aHelixEdge = BRepBuilderAPI_MakeEdge(aSegment, aCylinder, 0.0, 6.0 * M_PI).Edge();
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(18.0, 120.0, 0.0));
BRepBuilderAPI_Transform aTransform(aHelixEdge, aTrsf);
Handle(AIS_Shape) anAisHelixCurve = new AIS_Shape(aTransform.Shape());
myOccView->getContext()->Display(anAisHelixCurve, Standard_True);
// sweep a circle profile along the helix curve.
// there is no curve3d in the pcurve edge, so approx one.
BRepLib::BuildCurve3d(aHelixEdge);
gp_Ax2 anAxis;
anAxis.SetDirection(gp_Dir(0.0, 4.0, 1.0));
anAxis.SetLocation(gp_Pnt(aRadius, 0.0, 0.0));
gp_Circ aProfileCircle(anAxis, 0.3);
TopoDS_Edge aProfileEdge = BRepBuilderAPI_MakeEdge(aProfileCircle).Edge();
TopoDS_Wire aProfileWire = BRepBuilderAPI_MakeWire(aProfileEdge).Wire();
TopoDS_Face aProfileFace = BRepBuilderAPI_MakeFace(aProfileWire).Face();
TopoDS_Wire aHelixWire = BRepBuilderAPI_MakeWire(aHelixEdge).Wire();
BRepOffsetAPI_MakePipe aPipeMaker(aHelixWire, aProfileFace);
if (aPipeMaker.IsDone())
{
aTrsf.SetTranslation(gp_Vec(28.0, 120.0, 0.0));
BRepBuilderAPI_Transform aPipeTransform(aPipeMaker.Shape(), aTrsf);
Handle(AIS_Shape) anAisPipe = new AIS_Shape(aPipeTransform.Shape());
anAisPipe->SetColor(Quantity_NOC_DARKGOLDENROD);
myOccView->getContext()->Display(anAisPipe, Standard_True);
}
}
void lzzcad::makeToroidalHelix()
{
Standard_Real aRadius = 1.0;
Standard_Real aSlope = 0.05;
// the pcurve is a 2d line in the parametric space.
gp_Lin2d aLine2d(gp_Pnt2d(0.0, 0.0), gp_Dir2d(aSlope, 1.0));
Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(aLine2d, 0.0, M_PI * 2.0).Value();
Handle(Geom_ToroidalSurface) aCylinder = new Geom_ToroidalSurface(gp::XOY(), aRadius * 5.0, aRadius);
TopoDS_Edge aHelixEdge = BRepBuilderAPI_MakeEdge(aSegment, aCylinder, 0.0, 2.0 * M_PI / aSlope).Edge();
gp_Trsf aTrsf;
aTrsf.SetTranslation(gp_Vec(45.0, 120.0, 0.0));
BRepBuilderAPI_Transform aTransform(aHelixEdge, aTrsf);
Handle(AIS_Shape) anAisHelixCurve = new AIS_Shape(aTransform.Shape());
myOccView->getContext()->Display(anAisHelixCurve, Standard_True);
// sweep a circle profile along the helix curve.
// there is no curve3d in the pcurve edge, so approx one.
BRepLib::BuildCurve3d(aHelixEdge);
gp_Ax2 anAxis;
anAxis.SetDirection(gp_Dir(0.0, 0.0, 1.0));
anAxis.SetLocation(gp_Pnt(aRadius * 6.0, 0.0, 0.0));
gp_Circ aProfileCircle(anAxis, 0.3);
TopoDS_Edge aProfileEdge = BRepBuilderAPI_MakeEdge(aProfileCircle).Edge();
TopoDS_Wire aProfileWire = BRepBuilderAPI_MakeWire(aProfileEdge).Wire();
TopoDS_Face aProfileFace = BRepBuilderAPI_MakeFace(aProfileWire).Face();
TopoDS_Wire aHelixWire = BRepBuilderAPI_MakeWire(aHelixEdge).Wire();
BRepOffsetAPI_MakePipe aPipeMaker(aHelixWire, aProfileFace);
if (aPipeMaker.IsDone())
{
aTrsf.SetTranslation(gp_Vec(60.0, 120.0, 0.0));
BRepBuilderAPI_Transform aPipeTransform(aPipeMaker.Shape(), aTrsf);
Handle(AIS_Shape) anAisPipe = new AIS_Shape(aPipeTransform.Shape());
anAisPipe->SetColor(Quantity_NOC_CORNSILK1);
myOccView->getContext()->Display(anAisPipe, Standard_True);
}
}