Open Cascade:マウスで線を引く

5241 ワード

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ファイルにDrawLine ByMouse関数コードを作成します.
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