opencascade AIS_ColoredShape 源码学习 多颜色交互式对象

类AIS_ColoredShape

形状的呈现具有可自定义的子形状属性。
在这里插入图片描述
此类可以将topods的子拓扑分别设置不同的颜色
然后作为一个整体显示

成员类型定义文档

◆ DataMapOfDrawerCompd
typedef NCollection_IndexedDataMap<Handle<AIS_ColoredDrawer>, TopoDS_Compound, TColStd_MapTransientHasher> AIS_ColoredShape::DataMapOfDrawerCompd protected

考虑子形状颜色映射计算呈现。


构造函数与析构函数文档

◆ AIS_ColoredShape() [1/2]
AIS_ColoredShape::AIS_ColoredShape(const TopoDS_Shape & theShape)

默认构造函数。

◆ AIS_ColoredShape() [2/2]
AIS_ColoredShape::AIS_ColoredShape(const Handle<AIS_Shape> & theShape)

拷贝构造函数。


成员函数文档

◆ addShapesWithCustomProps()
void AIS_ColoredShape::addShapesWithCustomProps(const Handle<Prs3d_Presentation> & thePrs, const DataMapOfDrawerCompd * theDrawerOpenedShapePerType, const DataMapOfDrawerCompd & theDrawerClosedFaces, const Standard_Integer theMode) protected

将形状添加到呈现中。

参数:

  • thePrs:呈现
  • theDrawerOpenedShapePerType:具有独特属性的形状映射
  • theDrawerClosedFaces:封闭面的属性映射
  • theMode:显示模式
◆ bindSubShapes()
void AIS_ColoredShape::bindSubShapes(AIS_DataMapOfShapeDrawer & theShapeDrawerMap, const TopoDS_Shape & theKeyShape, const Handle<AIS_ColoredDrawer> & theDrawer) const protected

解析 theKeyShape 成子形状,在其中搜索 theBaseShape,将所有解析的子形状与 theOriginKeyShape 绑定,并将所有绑定存储在 theShapeDrawerMap 中。

参数:

  • theShapeDrawerMap:形状映射:解析并找到的 theBaseShape 子形状 -> theOriginKeyShape
  • theKeyShape:要解析(解析)为较小(在拓扑意义上)子形状的新绑定循环的形状
  • theDrawer:分配的绘制器
◆ ChangeCustomAspectsMap()
AIS_DataMapOfShapeDrawer & AIS_ColoredShape::ChangeCustomAspectsMap() inline

返回自定义外观的映射。

◆ ClearCustomAspects()
virtual void AIS_ColoredShape::ClearCustomAspects() virtual

重置自定义子形状外观的映射。

◆ Compute()
virtual void AIS_ColoredShape::Compute(const Handle<PrsMgr_PresentationManager> & thePrsMgr, const Handle<Prs3d_Presentation> & thePrs, const Standard_Integer theMode) overrideprotectedvirtual

考虑子形状颜色映射计算呈现。

从 AIS_Shape 重实现。

在 XCAFPrs_AISObject 中重新实现。

◆ ComputeSelection()
virtual void AIS_ColoredShape::ComputeSelection(const Handle<SelectMgr_Selection> & theSelection, const Standard_Integer theMode) overrideprotectedvirtual

考虑子形状隐藏状态计算选择。

从 AIS_Shape 重实现。

◆ computeSubshapeSelection()
void AIS_ColoredShape::computeSubshapeSelection(const Handle<AIS_ColoredDrawer> & theParentDrawer, const AIS_DataMapOfShapeDrawer & theShapeDrawerMap, const TopoDS_Shape & theShape, const Handle<StdSelect_BRepOwner> & theOwner, const Handle<SelectMgr_Selection> & theSelection, const TopAbs_ShapeEnum theTypOfSel, const Standard_Integer thePriority, const Standard_Real theDeflection, const Standard_Real theDeflAngle) protected

考虑隐藏状态将子形状添加到选择中(递归)。

参数:

  • theParentDrawer:父形状的绘制器
  • theShapeDrawerMap:形状映射
  • theShape:计算敏感实体的形状
  • theOwner:可选择的所有者对象
  • theSelection:要附加新敏感实体的选择
  • theTypOfSel:选择类型
  • theDeflection:线性偏差
  • theDeflAngle:角偏差
◆ CustomAspects()
virtual Handle<AIS_ColoredDrawer> AIS_ColoredShape::CustomAspects(const TopoDS_Shape & theShape) virtual

自定义指定子形状的属性。如果形状不是主形状的子形状,则将其存储在映射中但忽略。此方法可用于标记具有可自定义属性的子形状。

◆ CustomAspectsMap()
const AIS_DataMapOfShapeDrawer & AIS_ColoredShape::CustomAspectsMap() const inline

返回自定义外观的映射。

◆ dispatchColors()
static Standard_Boolean AIS_ColoredShape::dispatchColors(const Handle<AIS_ColoredDrawer> & theParentDrawer, const TopoDS_Shape & theShapeToParse, const AIS_DataMapOfShapeDrawer & theShapeDrawerMap, const TopAbs_ShapeEnum theParentType, const Standard_Boolean theIsParentClosed, DataMapOfDrawerCompd * theDrawerOpenedShapePerType, DataMapOfDrawerCompd & theDrawerClosedFaces) staticprotected

递归函数以映射形状。

参数:

  • theParentDrawer:用于未详细说明形状的绘制器(默认颜色)
  • theShapeToParse:要递归解析的子形状
  • theShapeDrawerMap:形状映射 子形状(在基础形状中) -> 绘制器
  • theParentType:父子形状类型
  • theIsParentClosed:指示指定形状是否为封闭实心部分的标志
  • theDrawerOpenedShapePerType:要填充的形状类型数组
  • theDrawerClosedFaces:封闭面映射
◆ fillSubshapeDrawerMap()
void AIS_ColoredShape::fillSubshapeDrawerMap(AIS_DataMapOfShapeDrawer & theSubshapeDrawerMap) const protected

将 myShapeColors 映射(KeyshapeColored -> Color)提取到子形状映射(Subshape -> Color)。当彩色形状不是基础形状的一部分(但子形状是)并且实际上是子形状的容器时,这需要。

◆ isShapeEntirelyVisible()
Standard_Boolean AIS_ColoredShape::isShapeEntirelyVisible() const protected

检查 myShapeColors 中的所有形状的可见性。

◆ SetColor()
virtual void AIS_ColoredShape::SetColor(const Quantity_Color & theColor) overridevirtual

设置整个形状的颜色。

从 AIS_Shape 重实现。

◆ SetCustomColor()
void AIS_ColoredShape::SetCustomColor(const TopoDS_Shape & theShape, const Quantity_Color & theColor)

自定义指定子形状的颜色。

◆ SetCustomTransparency()
void AIS_ColoredShape::SetCustomTransparency(const TopoDS_Shape & theShape, Standard_Real theTransparency)

自定义指定子形状的透明度。

◆ SetCustomWidth()
void AIS_ColoredShape::SetCustomWidth(const TopoDS_Shape & theShape, const Standard_Real theLineWidth)

自定义指定子形状的线宽。

◆ SetMaterial()
virtual void AIS_ColoredShape::SetMaterial(const Graphic3d_MaterialAspect & theAspect) overridevirtual

设置材质外观。

从 AIS_Shape 重实现。

在 XCAFPrs_AISObject 中重新实现。

◆ SetTransparency()
virtual void AIS_ColoredShape::SetTransparency(const Standard_Real theValue) overridevirtual

设置透明度值。

从 AIS_Shape 重实现。

◆ SetWidth()
virtual void AIS_ColoredShape::SetWidth(const Standard_Real theLineWidth) overridevirtual

设置整个形状的线宽。

从 AIS_Shape 重实现。

◆ UnsetCustomAspects()
void AIS_ColoredShape::UnsetCustomAspects(const TopoDS_Shape & theShape, const Standard_Boolean theToUnregister = Standard_False)

重置指定子形状的自定义属性。

参数:

  • theToUnregister:是否从映射中取消注册子形状
◆ UnsetTransparency()
virtual void AIS_ColoredShape::UnsetTransparency() overridevirtual

删除重建复合形状中的透明度设置。

从 AIS_Shape 重实现。

◆ UnsetWidth()
virtual void AIS_ColoredShape::UnsetWidth() overridevirtual

设置整个形状的线宽。

从 AIS_Shape 重实现。


字段文档

◆ myShapeColors
AIS_DataMapOfShapeDrawer AIS_ColoredShape::myShapeColors protected

考虑子形状颜色映射计算呈现。

用法示例

AIS_ColoredShape 是一个用于显示具有自定义子形状属性的彩色形状的类。它允许您为特定子形状设置颜色、透明度和线宽等属性。以下是 AIS_ColoredShape 的一些常见用法示例。

初始化

  1. 创建一个 TopoDS_Shape 对象:

    TopoDS_Shape myShape = ...; // 通过加载或创建一个形状
    
  2. 使用 TopoDS_Shape 创建 AIS_ColoredShape 对象:

    Handle(AIS_ColoredShape) coloredShape = new AIS_ColoredShape(myShape);
    

设置子形状的自定义属性

  1. 设置子形状的颜色:

    TopoDS_Shape subShape = ...; // 获取子形状
    Quantity_Color customColor(Quantity_NOC_RED); // 设定红色
    coloredShape->SetCustomColor(subShape, customColor);
    
  2. 设置子形状的透明度:

    Standard_Real transparency = 0.5; // 设定透明度(0到1之间)
    coloredShape->SetCustomTransparency(subShape, transparency);
    
  3. 设置子形状的线宽:

    Standard_Real lineWidth = 2.0; // 设定线宽
    coloredShape->SetCustomWidth(subShape, lineWidth);
    

清除子形状的自定义属性

  1. 清除子形状的自定义颜色:

    coloredShape->UnsetCustomAspects(subShape);
    
  2. 清除所有子形状的自定义属性:

    coloredShape->ClearCustomAspects();
    

显示形状

  1. 将形状添加到视图中:
    Handle(V3d_View) aView = ...; // 获取视图
    Handle(AIS_InteractiveContext) aContext = ...; // 获取交互上下文
    aContext->Display(coloredShape, Standard_True);
    

例子1

以下是一个完整的示例,演示如何使用 AIS_ColoredShape 类来创建一个彩色形状并为其子形状设置自定义属性:

#include <AIS_ColoredShape.hxx>
#include <TopoDS_Shape.hxx>
#include <Quantity_Color.hxx>
#include <AIS_InteractiveContext.hxx>
#include <V3d_View.hxx>

// 创建一个形状
TopoDS_Shape myShape = ...; // 通过加载或创建一个形状

// 创建 AIS_ColoredShape 对象
Handle(AIS_ColoredShape) coloredShape = new AIS_ColoredShape(myShape);

// 获取一个子形状
TopoDS_Shape subShape = ...; // 获取子形状

// 设置子形状的颜色
Quantity_Color customColor(Quantity_NOC_RED); // 设定红色
coloredShape->SetCustomColor(subShape, customColor);

// 设置子形状的透明度
Standard_Real transparency = 0.5; // 设定透明度(0到1之间)
coloredShape->SetCustomTransparency(subShape, transparency);

// 设置子形状的线宽
Standard_Real lineWidth = 2.0; // 设定线宽
coloredShape->SetCustomWidth(subShape, lineWidth);

// 获取视图和交互上下文
Handle(V3d_View) aView = ...; // 获取视图
Handle(AIS_InteractiveContext) aContext = ...; // 获取交互上下文

// 将彩色形状添加到视图中
aContext->Display(coloredShape, Standard_True);

总结

AIS_ColoredShape 类提供了一种方便的方法来为复杂形状及其子形状设置和管理自定义属性。通过这种方式,您可以在 OpenCASCADE 中以更加细粒度的方式控制形状的外观。

示例2

为了展示 AIS_ColoredShape 类的效果,我们将创建一个简单的 OpenCASCADE 应用程序,用于显示一个彩色立方体,并为其面设置不同的颜色、透明度和线宽。

完整示例代码

#include <AIS_ColoredShape.hxx>
#include <AIS_InteractiveContext.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <Quantity_Color.hxx>
#include <TopoDS_Shape.hxx>
#include <V3d_View.hxx>
#include <Graphic3d.hxx>
#include <WNT_Window.hxx>
#include <Aspect_Handle.hxx>

int main(int argc, char* argv[])
{
    // 初始化 OpenCASCADE 应用程序的窗口和视图
    Handle(Aspect_DisplayConnection) aDisplayConnection = new Aspect_DisplayConnection();
    Handle(Graphic3d_GraphicDriver) aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
    
    if (aGraphicDriver.IsNull()) {
        std::cerr << "Error: Unable to create graphic driver." << std::endl;
        return 1;
    }

    Handle(V3d_Viewer) aViewer = new V3d_Viewer(aGraphicDriver, Standard_ExtString("Visu3D"), "", 1000.0, V3d_XposYnegZpos, Quantity_NOC_GRAY20, V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT);
    Handle(WNT_Window) aWindow = new WNT_Window("OpenCASCADE Demo", Handle(WNT_WClass)::DownCast(aDisplayConnection->GetDisplayConnection()), Aspect_Handle(), 0, 0, 800, 600, Aspect_FM_NONE);
    Handle(V3d_View) aView = aViewer->CreateView();
    aView->SetWindow(aWindow);

    if (!aWindow->IsMapped()) aWindow->Map();

    Handle(AIS_InteractiveContext) aContext = new AIS_InteractiveContext(aViewer);

    // 创建一个立方体形状
    TopoDS_Shape myShape = BRepPrimAPI_MakeBox(100.0, 100.0, 100.0).Shape();

    // 创建 AIS_ColoredShape 对象
    Handle(AIS_ColoredShape) coloredShape = new AIS_ColoredShape(myShape);

    // 设置立方体每个面的不同颜色、透明度和线宽
    TopExp_Explorer exp(myShape, TopAbs_FACE);
    int faceIndex = 0;
    while (exp.More()) {
        TopoDS_Shape face = exp.Current();
        
        // 设置颜色
        Quantity_Color color;
        switch (faceIndex) {
            case 0: color = Quantity_Color(Quantity_NOC_RED); break;
            case 1: color = Quantity_Color(Quantity_NOC_GREEN); break;
            case 2: color = Quantity_Color(Quantity_NOC_BLUE1); break;
            case 3: color = Quantity_Color(Quantity_NOC_YELLOW); break;
            case 4: color = Quantity_Color(Quantity_NOC_MAGENTA); break;
            case 5: color = Quantity_Color(Quantity_NOC_CYAN1); break;
            default: color = Quantity_Color(Quantity_NOC_WHITE); break;
        }
        coloredShape->SetCustomColor(face, color);

        // 设置透明度
        coloredShape->SetCustomTransparency(face, 0.3);

        // 设置线宽
        coloredShape->SetCustomWidth(face, 2.0);

        exp.Next();
        faceIndex++;
    }

    // 将彩色形状添加到视图中
    aContext->Display(coloredShape, Standard_True);
    aView->FitAll();

    // 保持窗口打开
    while (true) {
        aView->Redraw();
    }

    return 0;
}

编译和运行

要编译并运行此示例,您需要安装 OpenCASCADE 库,并设置好编译环境。这里是一个简单的 CMakeLists.txt 文件示例:

cmake_minimum_required(VERSION 3.10)
project(OpenCASCADE_ColoredShape)

find_package(OpenCASCADE REQUIRED)

add_executable(OpenCASCADE_ColoredShape main.cpp)
target_link_libraries(OpenCASCADE_ColoredShape ${OpenCASCADE_LIBRARIES})
include_directories(${OpenCASCADE_INCLUDE_DIR})

将上述代码保存为 main.cpp,并在同一目录下创建 CMakeLists.txt 文件。然后在命令行中运行以下命令来编译和运行程序:

mkdir build
cd build
cmake ..
make
./OpenCASCADE_ColoredShape

预期效果

程序运行后,将会显示一个带有彩色面的立方体。每个面将有不同的颜色,透明度为 0.3,线宽为 2.0。您可以旋转和缩放视图以查看立方体的各个面。

总结

这个示例展示了如何使用 OpenCASCADE 库中的 AIS_ColoredShape 类来创建和显示一个具有自定义子形状属性的彩色形状。通过这种方式,您可以更细致地控制复杂形状的显示效果。

  • 30
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值