# View 3D/CAD data

**URL:** <https://community.plotly.com/t/view-3d-cad-data/16920>\
**Category:** Dash Python\
**Created:** [December 14, 2018, 7:19am UTC](https://community.plotly.com/t/view-3d-cad-data/16920 "2018-12-14T07:19:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxt](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/maxt/32/4972_2.png) [@maxt](https://community.plotly.com/u/maxt)\
**Post date:** [December 14, 2018, 7:19am UTC](https://community.plotly.com/t/view-3d-cad-data/16920/1 "2018-12-14T07:19:29Z")

</div>

I wonder if there are any modules/plugins/examples for Dash to view CAD files like OBJ, DXF or SKP? I am also interested in visualizing data on the Cad/3D files.  
/Max

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [December 14, 2018, 2:19pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/2 "2018-12-14T14:19:16Z")

</div>

There isn’t a native CAD viewer in Dash yet, but here are some other options:

- In the community, @empet made a really amazing `.ply` to `mesh3d` script, here’s an example: [https://plot.ly/~empet/14749](https://plot.ly/~empet/14749)
- In the React community, looks like autodesk has opensourced some work to render models in the browser: [https://github.com/Autodesk-Forge/viewer-react-express-headless](https://github.com/Autodesk-Forge/viewer-react-express-headless). If there is a standalone JS library available, then it could be wrapped as a custom Dash component: [https://github.com/plotly/dash-component-boilerplate](https://github.com/plotly/dash-component-boilerplate)

Just curious, what’s your particular use case for using Dash to do this?

---

<div class="post-metadata">

**Author:** ![maxt](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/maxt/32/4972_2.png) [@maxt](https://community.plotly.com/u/maxt)\
**Post date:** [December 15, 2018, 8:32pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/3 "2018-12-15T20:32:42Z")

</div>

Thanks a lot. I am involved in a project where we want to visualize 3D Rhino grasshopper simulations (daylight, energy performance and thermal comfort) combined with CAD geometries to optimize buildings and whole areas. The plan is develop a platform for architects and engineers to exchange information and I think Dash would be very useful.

---

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [December 18, 2018, 6:04pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/4 "2018-12-18T18:04:29Z")

</div>

@maxt A `mesh3d` can be derived from an `obj` file, too: [https://plot.ly/~empet/15040](https://plot.ly/~empet/15040)

Data from a `DXF` file can be extracted via the Python package `dxfgrabber`  
[https://dxfgrabber.readthedocs.io/en/latest/](https://dxfgrabber.readthedocs.io/en/latest/)  
and then use them to define plotly traces  
representing the corresponding DXF drawings.

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [December 18, 2018, 8:16pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/5 "2018-12-18T20:16:22Z")

</div>

Amazing @empet!!

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/258d7db47558d5d0b881b856d4f9b6cb715cf629.jpeg)

The interactivity is really neat, you can even zoom inside the car:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/9/9b943cb4569383ea00ac8da88a413760a1b921b6.jpeg)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/b/b8f419bb409bdb2122507096b161132aabfe0d9d.jpeg)

---

<div class="post-metadata">

**Author:** ![archmoj](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/archmoj/32/8388_2.png) [@archmoj](https://community.plotly.com/u/archmoj)\
**Post date:** [October 25, 2019, 11:28pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/7 "2019-10-25T23:28:11Z")

</div>

Hi Max!  
Did you get the answer to your question here?  
Small world : )  
I thought your page on listing building energy optimization tools at

> **[64 Best Building optimization images | Thermal comfort, Tools, Building](https://www.pinterest.ca/bengtdahlgren/building-optimization/)**
>
> Jun 16, 2015- Tools and methods to optimize building according to energy, daylight and thermal comfort. See more ideas about Thermal comfort, Tools and Building.

  
may be of ineterst to @alexcjohnson @jack

---

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [October 27, 2019, 7:19pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/8 "2019-10-27T19:19:32Z")

</div>

Updates of the notebooks illustrating how to extract data for a Mesh3d instance from files in `ply`, `off`, Wavefront `obj` or `STL` format:

- [https://plot.ly/~empet/14749](https://plot.ly/~empet/14749) for `ply`, `off` or `obj` without vertex colors
- [https://plot.ly/~empet/15040](https://plot.ly/~empet/15040) `obj` with color code for each vertex
- [https://plot.ly/~empet/15276](https://plot.ly/~empet/15276)`STL`

---

<div class="post-metadata">

**Author:** ![Alexboiboi](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/alexboiboi/32/6420_2.png) [@Alexboiboi](https://community.plotly.com/u/Alexboiboi)\
**Post date:** [April 23, 2020, 10:00am UTC](https://community.plotly.com/t/view-3d-cad-data/16920/9 "2020-04-23T10:00:07Z")

</div>

Hi @empet, thanks for the useful answer.

Regarding STL files, some exporters include color data in the attributes ([see Wiki](https://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL)). Here some code example, based on yours that takes advantage of this feature. It is not optimized but just to show what is possible.

BR, Alex-

```auto
from stl import mesh
import numpy as np
import plotly.graph_objects as go

def get_stl_color(x):
    #two "attribute byte count" bytes at the end of every triangle to store a 15-bit RGB color
    #bits 0 to 4 are the intensity level for blue (0 to 31)
    #bits 5 to 9 are the intensity level for green (0 to 31)
    #bits 10 to 14 are the intensity level for red (0 to 31)
    sb = f'{x:015b}'[::-1]
    r = str(int(255/31*int(sb[:5],base=2)))
    g = str(int(255/31*int(sb[5:10],base=2)))
    b = str(int(255/31*int(sb[10:15],base=2)))
    color = f'rgb({r},{g},{b})'
    return color

def stl2mesh3d(stl_file):
    stl_mesh = mesh.Mesh.from_file(stl_file)
    # stl_mesh is read by nympy-stl from a stl file; it is an array of faces/triangles (i.e. three 3d points) 
    # this function extracts the unique vertices and the lists I, J, K to define a Plotly mesh3d
    p, q, r = stl_mesh.vectors.shape #(p, 3, 3)
    # the array stl_mesh.vectors.reshape(p*q, r) can contain multiple copies of the same vertex;
    # extract unique vertices from all mesh triangles
    vertices, ixr = np.unique(stl_mesh.vectors.reshape(p*q, r), return_inverse=True, axis=0)
    I = np.take(ixr, [3*k for k in range(p)])
    J = np.take(ixr, [3*k+1 for k in range(p)])
    K = np.take(ixr, [3*k+2 for k in range(p)])
    facecolor = np.vectorize(get_stl_color)(stl_mesh.attr.flatten())
    x,y,z = vertices.T
    trace = go.Mesh3d(x=x, y=y, z=z, i=I, j=J, k=K, facecolor=facecolor)
    # optional parameters to make it look nicer
    trace.update(flatshading=True, lighting_facenormalsepsilon=0, lighting_ambient=0.7)
    return trace

```

Here an example of how it looks:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/f/f5d4b3123718151540119008e171a0caf74ab520.png)

---

<div class="post-metadata">

**Author:** ![EulerFaria](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@EulerFaria](https://community.plotly.com/u/EulerFaria)\
**Post date:** [May 18, 2020, 4:55pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/10 "2020-05-18T16:55:34Z")

</div>

Hey all, thanks for putting this material available.

I’m facing a challenge to get data from a DXF file and plot it using traces as @empet suggested. I’m using [ezdxf](https://pypi.org/project/ezdxf/) to get the entities (CIRCLE, LINES, ARC, LWPOLYLINE, etc…) from the file as well as the points related to each one. Here is an example for the lines:

```auto

import ezdxf
import numpy as np
import plotly.graph_objects as go
import plotly.offline as pyo

doc = ezdxf.readfile("8770PZ-M-00003.dxf")

msp = doc.modelspace()

lines = msp.query('LINE')

len(lines)

data = []

for line in lines:
    x,y,z = np.linspace(line.dxf.start,line.dxf.end).T   

    data.append(
        go.Scatter3d(
        x=x,
        y=y,
        z=z,
        line=dict(
            color='darkblue',
            width=1
        )    
        ))

fig = go.Figure(data=data)

fig.update_layout()

pyo.plot(fig,filename='test.html')

```

The problem is rendering the image, in this case there are 10418 lines, which seem to be too many lines for plotting it. Am I missing something here? Should I just go over every entity and append to the data list as a specific trace to that entity?

---

<div class="post-metadata">

**Author:** ![jharm](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jharm](https://community.plotly.com/u/jharm)\
**Post date:** [July 15, 2021, 11:01am UTC](https://community.plotly.com/t/view-3d-cad-data/16920/11 "2021-07-15T11:01:47Z")

</div>

Hi,

What a great thing with STL and colors!

I know it is old topic, but maybe others find their way to here.

I use Catia for CAD.  
To export to STL with colors I use Keyshot [Export formats - KeyShot Manual](https://manual.keyshot.com/manual/models-tab/export/export-formats/)

Then using yours and Empet script.

```auto
import numpy as np
from stl import mesh
import plotly.graph_objects as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot

def get_stl_color(x):
    #two "attribute byte count" bytes at the end of every triangle to store a 15-bit RGB color
    #bits 0 to 4 are the intensity level for blue (0 to 31)
    #bits 5 to 9 are the intensity level for green (0 to 31)
    #bits 10 to 14 are the intensity level for red (0 to 31)
    sb = f'{x:015b}'[::-1]
    r = str(int(255/31*int(sb[:5],base=2)))
    g = str(int(255/31*int(sb[5:10],base=2)))
    b = str(int(255/31*int(sb[10:15],base=2)))
    color = f'rgb({r},{g},{b})'
    return color

def stl2mesh3d(stl_file):
    stl_mesh = mesh.Mesh.from_file(stl_file)
    # stl_mesh is read by nympy-stl from a stl file; it is an array of faces/triangles (i.e. three 3d points) 
    # this function extracts the unique vertices and the lists I, J, K to define a Plotly mesh3d
    p, q, r = stl_mesh.vectors.shape #(p, 3, 3)
    # the array stl_mesh.vectors.reshape(p*q, r) can contain multiple copies of the same vertex;
    # extract unique vertices from all mesh triangles
    vertices, ixr = np.unique(stl_mesh.vectors.reshape(p*q, r), return_inverse=True, axis=0)
    I = np.take(ixr, [3*k for k in range(p)])
    J = np.take(ixr, [3*k+1 for k in range(p)])
    K = np.take(ixr, [3*k+2 for k in range(p)])
    facecolor = np.vectorize(get_stl_color)(stl_mesh.attr.flatten())
    x,y,z = vertices.T
    trace = go.Mesh3d(x=x, y=y, z=z, i=I, j=J, k=K, facecolor=facecolor)
    # optional parameters to make it look nicer
    trace.update(flatshading=True)
    return trace

filename = 'CEOPS.stl'

trace = stl2mesh3d(filename)

layout = go.Layout(paper_bgcolor='rgb(220,220,220)',
            title_text='Pump house', title_x=0.5,
                    font_color='black',
            width=800,
            height=800,
            scene_camera=dict(eye=dict(x=1.25, y=-1.25, z=1)),
            scene_xaxis_visible=False,
            scene_yaxis_visible=False,
            scene_zaxis_visible=False)

fig = go.Figure(go.Mesh3d(trace), layout=layout)

fig.data[0].update(lighting=dict(ambient= 0.18,
                                  diffuse= 1,
                                  fresnel= .1,
                                  specular= 1,
                                  roughness= .1,
                                  facenormalsepsilon=0))
fig.data[0].update(lightposition=dict(x=3000,
                                      y=3000,
                                      z=10000));

plot(fig)

```

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [July 15, 2021, 6:20pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/12 "2021-07-15T18:20:06Z")

</div>

Awesome @jharm ! Do you have a screenshot of what this looks like?

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [July 15, 2021, 6:22pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/13 "2021-07-15T18:22:30Z")

</div>

> [@chriddyp](#):
>
> There isn’t a native CAD viewer in Dash yet

Some news on this front - We have [`dash-vtk`](https://dash.plotly.com/vtk) which is an interface to the venerable VTK library, a very powerful and high performance mesh visualization library.

@skiefer wrote a great blog post explaining how they integrated `dash-vtk` with Ansys: [Dash-vtk for visualizing unstructured mesh grids and integrating with Ansys](https://community.plotly.com/t/dash-vtk-for-visualizing-unstructured-mesh-grids-and-integrating-with-ansys/53393)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/c/c0a166aed6435c12645523822448bb182f6fffdf.png)

---

<div class="post-metadata">

**Author:** ![jharm](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jharm](https://community.plotly.com/u/jharm)\
**Post date:** [July 16, 2021, 10:02am UTC](https://community.plotly.com/t/view-3d-cad-data/16920/14 "2021-07-16T10:02:13Z")

</div>

Of course I can share a picture.

![STL_colors](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/5/50e9e8f3c389a1ffc5d501912bfbaae6ff1dc2ec.png)

---

<div class="post-metadata">

**Author:** ![jharm](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jharm](https://community.plotly.com/u/jharm)\
**Post date:** [July 16, 2021, 12:08pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/15 "2021-07-16T12:08:04Z")

</div>

There are still some issues with translating colors from keyshot. I have changed the get\_stl\_color so it reads a little bit different. It is better but still not correct. This must be after the vacation! Have a great summer 🙂

```auto
def get_stl_color(x):
    #two "attribute byte count" bytes at the end of every triangle to store a 15-bit RGB color
    #bits 1 to 5 are the intensity level for red (0 to 31)
    #bits 6 to 11 are the intensity level for green (0 to 31)
    #bits 12 to 16 are the intensity level for blue (0 to 31)
    
    sb = f'{x:015b}'[1:]
    r = str(int(255/31*int(sb[:5],base=2)))
    g = str(int(255/31*int(sb[5:10],base=2)))
    b = str(int(255/31*int(sb[10:15],base=2)))
    color = f'rgb({r},{g},{b})'
    return color

```

---

<div class="post-metadata">

**Author:** ![Mahoney](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mahoney](https://community.plotly.com/u/Mahoney)\
**Post date:** [October 6, 2021, 2:04pm UTC](https://community.plotly.com/t/view-3d-cad-data/16920/16 "2021-10-06T14:04:14Z")

</div>

Hey Maxt, I am also looking into doing something very similar. Curious to hear if you managed to get this done?
