openCASCade显示模块样例

        • 例一: 属性创建
          • 1 颜色属性
          • 2 线条属性
          • 3 马克笔属性
          • 4 面属性
          • 5 文字属性
        • 例二:创建3D Viewer
        • 例三:创建3D View
        • 例四:创建交互上下文
        • 例五:自定义交互对象
        • 例六:交互对象中几何体创建
          • 1 获取Group从Prs3d_Presentation
          • 2 更新Group属性
          • 3 创建两个三角形
          • 4 用多边形方法创建包围盒
          • 5 创建Text与Maskers

例一: 属性创建
1 颜色属性
Quantity_Color aBlack (Quantity_NOC_BLACK); 
Quantity_Color aBlue (Quantity_NOC_MATRABLUE); 
Quantity_Color aBrown (Quantity_NOC_BROWN4); 
Quantity_Color aFirebrick (Quantity_NOC_FIREBRICK); 
Quantity_Color aForest (Quantity_NOC_FORESTGREEN); 
Quantity_Color aGray (Quantity_NOC_GRAY70); 
Quantity_Color aMyColor (0.99, 0.65, 0.31, Quantity_TOC_RGB); 
Quantity_Color aBeet (Quantity_NOC_BEET); 
Quantity_Color aWhite (Quantity_NOC_WHITE);
2 线条属性
Handle(Graphic3d_AspectLine3d) anAspectBrown = new Graphic3d_AspectLine3d(); 
Handle(Graphic3d_AspectLine3d) anAspectBlue = new Graphic3d_AspectLine3d(); 
Handle(Graphic3d_AspectLine3d) anAspectWhite = new Graphic3d_AspectLine3d(); 
anAspectBrown->SetColor (aBrown); 
anAspectBlue ->SetColor (aBlue); 
anAspectWhite->SetColor (aWhite);
3 马克笔属性
Handle(Graphic3d_AspectMarker3d aFirebrickMarker = new Graphic3d_AspectMarker3d(); 
// marker attributes 
aFirebrickMarker->SetColor (Firebrick); 
aFirebrickMarker->SetScale (1.0); 
aFirebrickMarker->SetType (Aspect_TOM_BALL); 
// or this 
// it is a preferred way (supports full-color images on modern hardware). 
aFirebrickMarker->SetMarkerImage (theImage)
4 面属性
Handle(Graphic3d_AspectFillArea3d) aFaceAspect = new Graphic3d_AspectFillArea3d(); 
Graphic3d_MaterialAspect aBrassMaterial (Graphic3d_NOM_BRASS); 
Graphic3d_MaterialAspect aGoldMaterial (Graphic3d_NOM_GOLD); 
aFaceAspect->SetInteriorStyle (Aspect_IS_SOLID); 
aFaceAspect->SetInteriorColor (aMyColor); 
aFaceAspect->SetDistinguishOn (); 
aFaceAspect->SetFrontMaterial (aGoldMaterial); 
aFaceAspect->SetBackMaterial (aBrassMaterial); 
aFaceAspect->SetEdgeOn();
5 文字属性
Handle(Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d (aForest, Graphic3d_NOF_ASCII_MONO, 1.0, 0.0);
例二:创建3D Viewer
// create a default connection 
Handle(Aspect_DisplayConnection) aDisplayConnection; 
// create a graphic driver from default connection 
Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection); 
// create a viewer 
myViewer = new V3d_Viewer (aGraphicDriver); 
// set parameters for V3d_Viewer 
// defines default lights 
// positional-light 0.3 0.0 0.0 
// directional-light V3d_XnegYposZpos 
// directional-light V3d_XnegYneg 
// ambient-light 
a3DViewer->SetDefaultLights(); 
// activates all the lights defined in this viewer 
a3DViewer->SetLightOn(); 
// set background color to black 
a3DViewer->SetDefaultBackgroundColor (Quantity_NOC_BLACK);
例三:创建3D View
Handle(WNT_Window) aWNTWindow = new WNT_Window (GetSafeHwnd()); 
myView = myViewer->CreateView(); 
myView->SetWindow (aWNTWindow);
例四:创建交互上下文
myAISContext = new AIS_InteractiveContext (myViewer);
TopoDS_Shape aShape = BRepAPI_MakeBox (10, 20, 30).Solid(); 
Handle(AIS_Shape) anAISShape = new AIS_Shape (aShape); 
myAISContext->Display (anAISShape);
例五:自定义交互对象

必须重载两个方法

void MyPresentableObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsManager, const Handle(Prs3d_Presentation)& thePrs, const Standard_Integer theMode) ( //... )
void MyPresentableObject::Compute (const Handle(Prs3d_Projector)& theProjector, const Handle(Prs3d_Presentation)& thePrs) ( //... )
例六:交互对象中几何体创建
1 获取Group从Prs3d_Presentation
Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
2 更新Group属性
aGroup->SetGroupPrimitivesAspect (anAspectBlue);
3 创建两个三角形
Standard_Integer aNbTria = 2; 
Handle(Graphic3d_ArrayOfTriangles) aTriangles = new Graphic3d_ArrayOfTriangles (3 * aNbTria, 0, true); 
for (Standard_Integer aTriIter = 1; aTriIter <= aNbTria; ++aTriIter) 
{ 
    aTriangles->AddVertex (aTriIter * 5., 0., 0., 1., 1., 1.); 
    aTriangles->AddVertex (aTriIter * 5 + 2.5, 5., 0., 1., 1., 1.); 
} 
aGroup->AddPrimitiveArray (aTriangles); 
aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectFillArea3d());
4 用多边形方法创建包围盒
Standard_Real Xm, Ym, Zm, XM, YM, ZM; 
thePrs->MinMaxValues (Xm, Ym, Zm, XM, YM, ZM);
Handle(Graphic3d_ArrayOfPolylines) aPolylines = new Graphic3d_ArrayOfPolylines (16, 4); 
aPolylines->AddBound (4);
aPolylines->AddVertex (Xm, Ym, Zm); 
aPolylines->AddVertex (Xm, Ym, ZM); 
aPolylines->AddVertex (Xm, YM, ZM); 
aPolylines->AddVertex (Xm, YM, Zm); 
aPolylines->AddBound (4); 
aPolylines->AddVertex (Xm, Ym, Zm); 
aPolylines->AddVertex (XM, Ym, Zm); 
aPolylines->AddVertex (XM, Ym, ZM); 
aPolylines->AddVertex (XM, YM, ZM); 
aPolylines->AddBound (4); 
aPolylines->AddVertex (XM, YM, Zm); 
aPolylines->AddVertex (XM, Ym, Zm); 
aPolylines->AddVertex (XM, YM, Zm); 
aPolylines->AddVertex (Xm, YM, Zm); 
aPolylines->AddBound (4); 
aPolylines->AddVertex (Xm, YM, ZM); 
aPolylines->AddVertex (XM, YM, ZM); 
aPolylines->AddVertex (XM, Ym, ZM); 
aPolylines->AddVertex (Xm, Ym, ZM);
aGroup->AddPrimitiveArray(aPolylines); 
aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d());
5 创建Text与Maskers
static char* texte[3] = { "Application title", "My company", "My company address." }; 
Handle(Graphic3d_ArrayOfPoints) aPtsArr = new Graphic3d_ArrayOfPoints (2, 1); 
aPtsArr->AddVertex (-40.0, -40.0, -40.0); 
aPtsArr->AddVertex (40.0, 40.0, 40.0); 
aGroup->AddPrimitiveArray (aPtsArr); 
aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectText3d());
Graphic3d_Vertex aMarker (0.0, 0.0, 0.0); 
for (int i = 0; i <= 2; i++) 
{ 
    aMarker.SetCoord (-(Standard_Real )i * 4 + 30, (Standard_Real )i * 4, -(Standard_Real )i * 4); 
    aGroup->Text (texte[i], Marker, 20.); 
}

你可能感兴趣的:(opencascade,openCASCade,occ,demo)