Open Cascade:使用鼠标画线
Open Cascade:使用鼠标画线
在View类文件中创建以下代码:
1.创建鼠标消息:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); gp_Pnt ConvertClickToPoint(Standard_Real theX, Standard_Real theY, Handle(V3d_View) theView)
2.添加消息映射宏:
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
3.实现函数:
gp_Pnt ConvertClickToPoint(Standard_Real theX, Standard_Real theY, Handle(V3d_View) theView) { Standard_Real XEye,YEye,ZEye,XAt,YAt,ZAt; theView->Eye(XEye,YEye,ZEye); theView->At(XAt,YAt,ZAt); gp_Pnt EyePoint(XEye,YEye,ZEye); gp_Pnt AtPoint(XAt,YAt,ZAt); gp_Vec EyeVector(EyePoint,AtPoint); gp_Dir EyeDir(EyeVector); gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir); Standard_Real X,Y,Z; theView->Convert(int(theX),int(theY),X,Y,Z); gp_Pnt ConvertedPoint(X,Y,Z); gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint); gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView); return ResultPoint; } void CMyOCCView::OnLButtonDown(UINT nFlags, CPoint point) { myPointStart = ConvertClickToPoint(point.x,point.y,myView); } void CMyOCCView::OnMouseMove(UINT nFlags, CPoint point) { if(nFlags & MK_LBUTTON){ gp_Pnt aPnt = ConvertClickToPoint(point.x,point.y,myView); //myView->Rotation(aPnt); CMyOCCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDoc->DrawLineByMouse(myPointStart, aPnt); } }
在Doc类文件中创建DrawLineByMouse函数代码:
Doc.h头文件中创建如下代码:
public: void DrawLineByMouse(gp_Pnt thePnt1, gp_Pnt thePnt2); private: gp_Pnt myPntStart; gp_Pnt myPntEnd; Handle(Geom_TrimmedCurve) mySegment1; TopoDS_Edge myEdge1; Handle(AIS_Shape) myAISShape; Handle(AIS_InteractiveContext) myAISContext;
在Doc.cpp文件加入实现函数代码:
void CMyOCCDoc::DrawLineByMouse(gp_Pnt thePntStart, gp_Pnt thePntEnd) { //检查传入参数 if(thePntStart.IsEqual(thePntEnd,1e-3)) return; //构建拓扑线段 Handle(Geom_TrimmedCurve) aSegment = GC_MakeSegment(thePntStart, thePntEnd); TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge(aSegment); //将构建的拓扑线段设置至AIS_Shape形状中 myAISShape->SetShape(aEdge); //移除前面绘画的旧线段, 绘制新线段。 myAISContext->Remove(myAISShape,myViewer); myAISContext->Display (myAISShape, Standard_False); //更新View myAISContext->UpdateCurrentViewer(); }
OK!!