pyOCCT and VTK

hamedo

Looking around for some CAD
I hope my question is not irrelevant.
I found out that "PythonOCC" doesn't support VTK integration, however, the other package "pyOCCT" does.
The VTK integration in C++ is given HERE
Code:
TopoDS_Shape aShape;
IVtkOCC_Shape::Handle aShapeImpl = new IVtkOCC_Shape(aShape);

vtkSmartPointer<IVtkTools_ShapeDataSource> aDS = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();
aDS->SetShape(aShapeImpl);
vtkSmartPointer<vtkPolyDataMapper> Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();

Mapper->SetInputConnection(aDS->GetOutputPort());
vtkSmartPointer<vtkActor> Actor = vtkSmartPointer<vtkActor>::New();
Actor->SetMapper(Mapper);

The problem is with aDS->GetOutputPort() becase this class has no GetOutputPort() function in pyOCCT:
in C++ sources GetOutputPort() is inherited from vtkAlgorithm

but in OCP IVtkTools_ShapeDataSource has no parent classes.
https://github.com/CadQuery/OCP-stu...1855288f/OCP-stubs/IVtkTools/__init__.pyi#L83

Python:
from OCP.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCP.IVtkOCC import IVtkOCC_Shape
from OCP.IVtkTools import IVtkTools_ShapeDataSource

from vtkmodules.vtkRenderingCore import vtkPolyDataMapper

my_box = BRepPrimAPI_MakeBox(10.0, 20.0, 30.0).Shape()
vo_shape = IVtkOCC_Shape(my_box)
sds = IVtkTools_ShapeDataSource.New_s()
sds.SetShape(vo_shape)
vpdm = vtkPolyDataMapper()
vpdm.SetInputConnection(sds.GetOutput())
 
Last edited:

natalia

CAD community veteran
Hello, @hamedo
and welcome here!

Manual that you're mentioned above relates to C++ code and as we see, pyOCCT has no described interface.
It seems that we need to follow another example of using VTK + OCCT + python.
After some looking around, the next example was found. I did not use it, but may be it would be useful in your research.
It's copied from cadquery/occ_impl/shapes.py:

Python:
        """
        Convert shape to vtkPolyData
        """

        vtk_shape = IVtkOCC_Shape(self.wrapped)
        shape_data = IVtkVTK_ShapeData()
        shape_mesher = IVtkOCC_ShapeMesher()

        drawer = vtk_shape.Attributes()
        drawer.SetUIsoAspect(Prs3d_IsoAspect(Quantity_Color(), Aspect_TOL_SOLID, 1, 0))
        drawer.SetVIsoAspect(Prs3d_IsoAspect(Quantity_Color(), Aspect_TOL_SOLID, 1, 0))

        if tolerance:
            drawer.SetDeviationCoefficient(tolerance)

        if angularTolerance:
            drawer.SetDeviationAngle(angularTolerance)

        shape_mesher.Build(vtk_shape, shape_data)

        rv = shape_data.getVtkPolyData()

        # convert to triangles and split edges
        t_filter = vtkTriangleFilter()
        t_filter.SetInputData(rv)
        t_filter.Update()

        rv = t_filter.GetOutput()

Best regards, Natalia
 
Last edited:

hamedo

Looking around for some CAD
I have one more question to ask.
Is there any way to hide the extra lines and points?
1677587695361.png
Thanks.
 

hamedo

Looking around for some CAD
@natalia
Thank you for your suggestion. I tried it, but unfortunately, it didn't work in my case. Instead, I created a new mesh using PyVista that only included the points and faces of the RV. While it may not be the most efficient solution, it did solve my problem. I'm hopeful that I can find a better solution in the future.
Code:
mesh = pv.PolyData(pv_box.points, pv_box.faces)

1677666097475.png
 
Top