Removing a child shape does not remove it

Hi,

I have the next issue:

  • I create 2 cubes, one is child to the other [CViewer3dView::Addcubeandchildcube]
    • The second cube is made child to the first with hAISCubeParent->AddChild(hAISCubeChild);
  • I remove the child cube [CViewer3dView::Removechildcube]
    • The recursive routine RecurseRemove uses AIS_InteractiveContext::Remove to effectively delete the shape from the context
  • When I redisply the parent cube, the child cube is shown as wel [CViewer3dView::Togglevisibility]
    • Redisplaying is done with the recursive routine RecurseShowHide which calls AIS_InteractiveContext::Display

So I'm surprised that in the third step the child cube reappears.

A small secondary question: is there a way to see the internal structure of an object? I have seen some dumpJson functions which were made obsolete. As the objects are added to an AIS_InteractiveContext, it would be helpfull if I could dump the contents into a text file.


void AISObjectDisplay(const Handle(AIS_InteractiveContext)& theAIScontext, const Handle(AIS_InteractiveObject)& rShape, AIS_DisplayStatus DisplayStatus)
{
	for (PrsMgr_ListOfPresentableObjectsIter anIt(rShape->Children()); anIt.More(); anIt.Next())
	{
		if (Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast(anIt.Value()))
		{
			AISObjectDisplay(theAIScontext, aShape, DisplayStatus);
		}
	}

	if (rShape.IsNull())
		return;
	rShape->SetHilightMode(1);
	rShape->SetColor(Quantity_NOC_ROYALBLUE);
	rShape->SetMaterial(Graphic3d_NOM_BRONZE);
	rShape->SetDisplayMode(AIS_Shaded);
	theAIScontext->Display(rShape, 1/*theAIScontext->DisplayMode()*/, 0, Standard_False, DisplayStatus);
}

void AISObjectRedisplay(const Handle(AIS_InteractiveContext)& theAIScontext, const Handle(AIS_InteractiveObject)& rShape, bool bShow)
{
	if (rShape.IsNull())
		return;
	if (bShow)
		theAIScontext->Display(rShape, false);
	else
		theAIScontext->Erase(rShape, false);

	for (PrsMgr_ListOfPresentableObjectsIter anIt(rShape->Children()); anIt.More(); anIt.Next())
	{
		if (Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast(anIt.Value()))
		{
			AISObjectRedisplay(theAIScontext, aShape, bShow);
		}
	}
}


void RecurseAdd(const Handle(AIS_InteractiveContext)& theAIScontext, const Handle(AIS_InteractiveObject)& theShape, bool bShow)
{
	if (theShape.IsNull())
		return;

	for (PrsMgr_ListOfPresentableObjectsIter anIt(theShape->Children()); anIt.More(); anIt.Next())
	{
		if (Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast(anIt.Value()))
			RecurseAdd(theAIScontext, aShape, bShow);
	}

	if (!theShape->HasColor())
		theShape->SetColor(Quantity_NOC_ROYALBLUE);
	if (!theShape->HasMaterial())
		theShape->SetMaterial(Graphic3d_NOM_STEEL);
	if (!theShape->HasDisplayMode())
		theShape->SetDisplayMode(0);
	if (!theShape->HasHilightMode())
		theShape->SetHilightMode(0);

	AIS_DisplayStatus DisplayStatus = (bShow ? AIS_DS_Displayed : AIS_DS_Erased);
	theAIScontext->Display(theShape, theAIScontext->DisplayMode(), 0, Standard_False, DisplayStatus);

}


void RecurseRemove(const Handle(AIS_InteractiveContext)& rInteractiveContext, Handle(AIS_InteractiveObject)& theIObj)
{
	if (theIObj.IsNull())
		return;
	for (PrsMgr_ListOfPresentableObjectsIter aPrsIter(theIObj->Children()); aPrsIter.More(); aPrsIter.Next())
	{
		Handle(AIS_InteractiveObject) hChildInteractiveObject = Handle(AIS_InteractiveObject)::DownCast(aPrsIter.Value());
		if (!hChildInteractiveObject.IsNull())
			RecurseRemove(rInteractiveContext, hChildInteractiveObject);
	}
	rInteractiveContext->Remove(theIObj, Standard_False);
}

void RecurseShowHide(const Handle(AIS_InteractiveContext)& rInteractiveContext, Handle(AIS_InteractiveObject)& theIObj, bool bShow, bool bRedraw)
{
	if (theIObj.IsNull())
		return;
	if (bShow)
		rInteractiveContext->Display(theIObj, bRedraw);
	else
		rInteractiveContext->Erase(theIObj, bRedraw);
	for (PrsMgr_ListOfPresentableObjectsIter aPrsIter(theIObj->Children()); aPrsIter.More(); aPrsIter.Next())
	{
		Handle(AIS_InteractiveObject) hChildInteractiveObject = Handle(AIS_InteractiveObject)::DownCast(aPrsIter.Value());
		if (!hChildInteractiveObject.IsNull())
			RecurseShowHide(rInteractiveContext, hChildInteractiveObject, bShow, bRedraw);
	}
}


void CViewer3dView::Addcubeandchildcube()
{
	BRepPrimAPI_MakeBox mkSBoxParent(150.0, 150.0, 150.0);
	TopoDS_Shape BoxParent = mkSBoxParent.Shape();
	hAISCubeParent = new AIS_Shape(BoxParent);

	BRepPrimAPI_MakeBox mkSBoxChild(100.0, 100.0, 100.0);
	TopoDS_Shape BoxChild = mkSBoxChild.Shape();
	hAISCubeChild = new AIS_Shape(BoxChild);

	hAISCubeParent->AddChild(hAISCubeChild);

	CString		aNameParent("Parent");
	gp_Trsf		theTrsfParent;
	CString		aNameChild("Child");
	gp_Trsf		theTrsfChild;

	theTrsfChild.SetTranslation(gp_Vec(0, 0, 400));
	hAISCubeChild->SetLocalTransformation(theTrsfChild);
	hAISCubeChild->shape()

	AISObjectDisplay(myAISContext, hAISCubeParent, AIS_DS_Displayed);
	OnViewZoomall();
}


void CViewer3dView::Removechildcube()
{
	RecurseRemove(myAISContext, hAISCubeChild);
	OnViewZoomall();
}


void CViewer3dView::Togglevisibility()
{
	RecurseShowHide(myAISContext, hAISCubeParent, true, false);
	OnViewZoomall();
}
Daniel Duesentrieb's picture

Try to remove the child as well in RecurseRemove: 

if (!hChildInteractiveObject.IsNull())
{
  RecurseRemove(rInteractiveContext, hChildInteractiveObject);
  theIObj->RemoveChild(hChildInteractiveObject);
}