Cascade appanding -TopoDS_Shape- to -vtkAppendPolyData- or -vtkCleanPolyData- or simply convert -TopoDS_Shape- to -vtkPolyData- NOT WORKING

//Dear OCC VTK stuff

//In short, The problem is, I cannot append TopoDS_Shape to vtkAppendPolyData nor to vtkCleanPolyData.

//I cant even convert TopoDS_Shape to vtkPolyData.

//If you run this simple example, this is the same as shown in Lesson 17 but in VTK.

//https://www.youtube.com/watch?v=QHyhWH6xgbE&list=PL_WFkJrQIY2iVVchOPhl77xl432jeNYfQ&index=1&t=804s&ab_channel=Quaoar%27sWorkshop

//you will see the same blade from my previous examples(and some extra shapes from the VTK library)

//The problem is that I can show only the points and the shapes, but I cannot append to them, my IGES Import.

//Or even to a simple TopoDS_Shape box from BRepPrimAPI_MakeBox

//I have Written two Questions in the correct places. if you could spare the time.

//thank you very much for any help !!

#include <BRepPrimAPI_MakeBox.hxx>
#include <IVtkTools_ShapeDataSource.hxx>
#include<vtkAutoInit.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkInteractorStyleTrackballCamera.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkPolyDataMapper.h>
//VTK_MODULE_INIT(vtkRenderingOpenGl2);
#include <IGESControl_Reader.hxx> 
#include <STEPCAFControl_Reader.hxx>
VTK_MODULE_INIT(vtkInteractionStyle);
//Lesson 17
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <vtkCubeSource.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkAppendPolyData.h >
#include <vtkCleanPolyData.h >
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
int main()
{
	//TopoDS_ShapeBox
	BRepPrimAPI_MakeBox mkBox(1, 2, 3);//mkBox1.Build();	
	const TopoDS_Shape& TopoDS_ShapeBox = mkBox.Shape();

	//VTKsphere
	vtkNew<vtkSphereSource> VTKsphere;
	VTKsphere->SetCenter(5, 0, 0);
	VTKsphere->Update();

	//VTKcone
	vtkNew<vtkConeSource> VTKcone;
	VTKcone->SetCenter(-5, 0, 0);
	VTKcone->Update();

	//vtkPolyData creation
	vtkNew<vtkPolyData> PolyDataVTKCone;
	vtkNew<vtkPolyData> PolyDataVTKSphere;

	//vtkPolyData initialization
	PolyDataVTKCone->ShallowCopy(VTKcone->GetOutput());
	PolyDataVTKSphere->ShallowCopy(VTKsphere->GetOutput());
	
	//Append the meshes
	vtkNew<vtkAppendPolyData> appendFilter;
	appendFilter->AddInputData(PolyDataVTKCone);
	appendFilter->AddInputData(PolyDataVTKSphere);

	/////First question /////////////////////////////////////////////////////
	//Is there a way to convert TopoDS_Shape to vtkPolyData ?
	//the following ways,didnt work.doing somting wrong.
	//vtkNew<vtkPolyData> PolyDataTopoDS_ShapeBox;
	//1)appendFilter->AddInputData(TopoDS_ShapeBox);
	//2)appendFilter->AddInputData(Init(TopoDS_ShapeBox));
	//3)appendFilter->AddInputData(new IVtkOCC_Shape(TopoDS_ShapeBox));

	// Remove any duplicate points.
	vtkNew<vtkCleanPolyData> cleanFilter;
	cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
	cleanFilter->Update();

	vtkNew<IVtkTools_ShapeDataSource> TOPOBOXource;
	TOPOBOXource->SetShape(new IVtkOCC_Shape(TopoDS_ShapeBox));///NO SHOW ON SCREEN !!

	// Create a mapper and actor
	vtkNew<vtkPolyDataMapper> mapper;

	/////Second Question/////////////////////////////////////////////////////
	//if appending my TopoDS_Shapes cant be done (box created by BRepPrimAPI_MakeBox)
	//Why does AddInputConnection does not append my BRepPrimAPI_MakeBox and my cleanFilter?(with the TOPOBOXource and VTK SHAPES) 
	//Should and can i,Append IGESource and TOPOBOXource as they are both IVtkTools_ShapeDataSource type
	//And if so how?
	//Thank You Vey Much!! 
	mapper->SetInputConnection(cleanFilter->GetOutputPort());
	mapper->AddInputConnection(TOPOBOXource->GetOutputPort());
	vtkNew<vtkActor> actor;
	actor->SetMapper(mapper);
	//////////////////////////////////////
	vtkNew<vtkRenderWindow> renwin;
	vtkNew<vtkRenderer> ren;
	renwin->AddRenderer(ren);
	vtkNew<vtkInteractorStyleTrackballCamera> istyle;
	vtkNew<vtkRenderWindowInteractor> iren;
	iren->SetRenderWindow(renwin);
	iren->SetInteractorStyle(istyle);
	ren->AddActor(actor);
	renwin->Render();
	iren->Start();
	return 0;
}

No, you need to write the conversion code yourself. See here for an example:
https://github.com/f3d-app/f3d/blob/master/src/library/vtkF3DOCCTReader.cxx#L68

Dear @mwestphal
1)please check out my new post about the Latest OpenCascade YouTube. :pray:
https://discourse.vtk.org/t/lesson-17-1-point-membership-classification-with-opencascade-vtk/8009

If you will look inside, you will notice, I represent a Cone with old AIS OCC BRepPrimAPI_MakeCone.
Wich will be represented as TopoDS_Shape.

2)Is there no simple conversion so that i can insert a TopoDS_Shape in the next lines. :roll_eyes:

gp_Ax2 sphere_origin(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
BRepPrimAPI_MakeCone mkCone(sphere_origin, 1.0,0.01,1);
const TopoDS_Shape& TopoDS_ShapemkConeTMP = mkCone.Shape();
TopoDS_Shape TopoDS_ShapeCONE = static_cast<TopoDS_Shape>(TopoDS_ShapemkConeTMP);

//VTKplane
vtkNew VTKplane;
VTKplane->SetOrigin(0, 0, 0);
VTKplane->SetNormal(0, 0, 1);

// Create cutter
vtkNew cutter;
cutter->SetCutFunction(VTKplane);
cutter->SetInputConnection(VTKsphere->GetOutputPort()); // PUT TopoDS_ShapeCONE instead!!
cutter->Update();

Of Course, my Goul is to be able to put TopoDS_ShapeIGES, In this code.
But , the mesh is also represented in TopoDS_Shape. So the question remains the same.

Thank you as always. for the time and effort :pray:

This seems to be more of an OpenCascade question than a VTK one.

OpenCascade offers this API for meshing TopoDS_Shape and creating vtkPolyData. I use this API without issues.

2 Likes