TPrsStd_AISPresentation with wrong location / position

I try to read step file with opencascade, there are several ways to display the bodies in step file, but when I use TPrsStd_AISPresentation, the location of bodies will go wrong some times. AND when I use method prs->GetAIS()->SetLocalTransformation(loc) to change the location of the AIS Object of TPrsStd_AISPresentation, it will not work too.

Here is my sample codes

//! this will generate wrong location
for (XCAFPrs_DocumentExplorer aLeafIter(d->doc, XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes);
    aLeafIter.More(); aLeafIter.Next())
{
    const XCAFPrs_DocumentNode& aDocNode = aLeafIter.Current();
    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aDocNode.RefLabel);
    if (!aShape.IsNull())
    {
        Handle(TPrsStd_AISPresentation) prs = TPrsStd_AISPresentation::Set(aDocNode.Label, XCAFPrs_Driver::GetID());
        prs->Display(true);
    }
}

//! this will generate right location
for (XCAFPrs_DocumentExplorer aLeafIter(d->doc, XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes);
    aLeafIter.More(); aLeafIter.Next())
{
    const XCAFPrs_DocumentNode& aDocNode = aLeafIter.Current();
    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aDocNode.RefLabel);
    if (!aShape.IsNull())
    {
        aShape.Location(aDocNode.Location);
        Handle(AIS_Shape) ais = new AIS_Shape(aShape);
        //mCtx->Display(ais, true);
    }
}

I want to know why SetLocalTransformation Method does'work AND why the location was not right?

Does anyone can help, Thank You!

gkv311 n's picture

Please use 'insert code' in editor or Markdown syntax to make shared code readable.

W. H.'s picture

sorry, I did't notice that

gkv311 n's picture

Why would you want using TPrsStd_AISPresentation instead of more straightforward XCAFPrs_AISObject?

W. H.'s picture

gkv:
Thank you for your reply!
Because I want to store the AIS Objects to ocaf. Before I use TPrsStd_AISPresentation, I have to use a map to keep the relationship of Label and AIS Object. I think may be that's not the best practice, so I want to try TPrsStd_AISPresentation.

W. H.'s picture

gkv:
Thank you for your reply!
Because I want to store the AIS Objects to ocaf. Before I use TPrsStd_AISPresentation, I have to use a map to keep the relationship of Label and AIS Object. I think may be that's not the best practice, so I want to try TPrsStd_AISPresentation.

gkv311 n's picture
    const XCAFPrs_DocumentNode& aDocNode = aLeafIter.Current();
    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aDocNode.RefLabel);
    aShape.Location(aDocNode.Location);
    Handle(AIS_Shape) ais = new AIS_Shape(aShape);

The code above works as expected, because you pass shape location from XCAFPrs_DocumentExplorer tool. XCAFPrs_DocumentExplorer combines transformation of all parent nodes in XCAF tree and provides a final location that could be applied to aDocNode.RefLabel to locate shape in the world space.

    const XCAFPrs_DocumentNode& aDocNode = aLeafIter.Current();
    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aDocNode.RefLabel);
    if (!aShape.IsNull())
    {
        Handle(TPrsStd_AISPresentation) prs = TPrsStd_AISPresentation::Set(aDocNode.Label, XCAFPrs_Driver::GetID());
        prs->Display(true);

This code doesn't use location accumulated by XCAFPrs_DocumentExplorer tool. Instead, it takes aDocNode.Label (instead of aDocNode.RefLabel). These shape labels have no information about all parents nodes (in fact, shape label may be a child of several different parent nodes having different final location). So that displaying this presentation preserves only transformation of a child relative to a single parent, which is not what you are looking for.

I'm not familiar with a proper way for setting location to TPrsStd_AISPresentation (overriding XCAFPrs_Driver::Update() in a driver subclass?), but you need somehow passing this location information to interactive object (AIS_InteractiveContext::SetLocation()) or to shape.

W. H.'s picture

Thank you very much for your explain!
I will try your suggestion.