
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/06_advanced/cad_friendly_plot.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_06_advanced_cad_friendly_plot.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_06_advanced_cad_friendly_plot.py:


CAD-friendly plotting
=====================

A generic mesh viewer draws the *triangulation*: with ``show_edges=True``
you see every facet edge, an artifact of the tessellation tolerance that
means nothing to a CAD user. What a CAD user wants is the model's
*topological* edges (the B-rep feature curves) over smoothly shaded
faces, exactly like a CAD application's viewport.

``pyvista-cad`` reproduces that pipeline:

* :func:`pyvista_cad.topods_to_multiblock` tessellates each topological
  face independently and attaches *analytic* surface normals (a coarse
  cylinder still shades perfectly round), plus a sibling edges block of
  the real B-rep curves recovered from the same mesh pass.
* The ``plotter.cad`` component and the ``.cad.plot()`` accessor render
  faces smooth with their triangle edges hidden and the topological
  edges overlaid in the theme edge colour.

.. GENERATED FROM PYTHON SOURCE LINES 23-29

.. code-block:: Python

    import numpy as np
    import pyvista as pv

    import pyvista_cad
    from pyvista_cad import examples








.. GENERATED FROM PYTHON SOURCE LINES 30-33

Read a STEP bracket. It comes back as a :class:`pyvista.MultiBlock`
with the originating ``TopoDS`` cached on each block, so the CAD-view
helpers can recover exact topology.

.. GENERATED FROM PYTHON SOURCE LINES 33-36

.. code-block:: Python


    mb = pyvista_cad.read_step(examples.bracket_step_path())








.. GENERATED FROM PYTHON SOURCE LINES 37-39

The generic view: triangle mesh with facet edges. The filleted bend
looks polygonal and the edges are tessellation noise, not features.

.. GENERATED FROM PYTHON SOURCE LINES 39-43

.. code-block:: Python


    combined = mb.combine().extract_surface()
    combined.plot(show_edges=True, color='lightblue')








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/06_advanced/images/sphx_glr_cad_friendly_plot_001.png
        :alt: cad friendly plot
        :srcset: /examples/06_advanced/images/sphx_glr_cad_friendly_plot_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-cad/pyvista-cad/doc/examples/06_advanced/images/sphx_glr_cad_friendly_plot_001.vtksz



.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    /home/runner/work/pyvista-cad/pyvista-cad/examples/06_advanced/cad_friendly_plot.py:40: PyVistaFutureWarning: The default value of `algorithm` for the filter
    `UnstructuredGrid.extract_surface` will change in the future. It currently defaults to
    `'dataset_surface'`, but will change to `None`. Explicitly set the `algorithm` keyword to
    silence this warning.
      combined = mb.combine().extract_surface()




.. GENERATED FROM PYTHON SOURCE LINES 44-46

The CAD-friendly view: smooth analytic shading, only the topological
edges drawn. Same data, but it reads like a CAD viewport.

.. GENERATED FROM PYTHON SOURCE LINES 46-49

.. code-block:: Python


    mb.cad.plot()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/06_advanced/images/sphx_glr_cad_friendly_plot_002.png
        :alt: cad friendly plot
        :srcset: /examples/06_advanced/images/sphx_glr_cad_friendly_plot_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-cad/pyvista-cad/doc/examples/06_advanced/images/sphx_glr_cad_friendly_plot_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 50-55

The data behind the render. ``topods_to_multiblock`` keeps per-face
identity and classifies every edge curve.

``.cad.cad_view()`` resolves the read result (recovering each block's
cached B-rep) into a ``{'faces', 'edges'}`` MultiBlock.

.. GENERATED FROM PYTHON SOURCE LINES 55-65

.. code-block:: Python

    cad = mb.cad.cad_view(linear_deflection=0.2)
    edges = cad['edges']
    legend = edges.field_data['cad.edge_kind_legend'][0]
    kinds, counts = np.unique(edges.cell_data['cad.edge_kind'], return_counts=True)
    n_faces = sum(1 for _ in cad['faces'].cad.walk())
    print(f'topological faces : {n_faces}')
    print(f'topological edges : {edges.n_cells}')
    print(f'edge-kind legend  : {legend}')
    print(f'edge-kind counts  : {dict(zip(kinds.tolist(), counts.tolist()))}')





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    topological faces : 1
    topological edges : 19
    edge-kind legend  : 0=line;1=circle;2=ellipse;3=hyperbola;4=parabola;5=bezier;6=bspline;7=offset;8=other
    edge-kind counts  : {0: 17, 1: 2}




.. GENERATED FROM PYTHON SOURCE LINES 66-67

A silhouette-only outline, handy for drawings and thumbnails.

.. GENERATED FROM PYTHON SOURCE LINES 67-71

.. code-block:: Python


    pl = pv.Plotter()
    pl.cad.add(cad, color='white', silhouette=True)
    pl.show()







.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/06_advanced/images/sphx_glr_cad_friendly_plot_003.png
        :alt: cad friendly plot
        :srcset: /examples/06_advanced/images/sphx_glr_cad_friendly_plot_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-cad/pyvista-cad/doc/examples/06_advanced/images/sphx_glr_cad_friendly_plot_003.vtksz







.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.699 seconds)


.. _sphx_glr_download_examples_06_advanced_cad_friendly_plot.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: cad_friendly_plot.ipynb <cad_friendly_plot.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: cad_friendly_plot.py <cad_friendly_plot.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: cad_friendly_plot.zip <cad_friendly_plot.zip>`
