mozman / ezdxf

Python interface to DXF
https://ezdxf.mozman.at
MIT License
904 stars 189 forks source link

Mesh colors work only in 2D view #144

Closed dmh126 closed 3 years ago

dmh126 commented 4 years ago

I create dxf files from scratch by creating blocks and adding Mesh objects to blocks. I tried to set color to layer and to mesh directly. I use Forge BIM360 viewer to visualize them, but it displays correct colors only in 2D view. Here are some screenshots:

Screen Shot 2020-04-23 at 14 11 18 Screen Shot 2020-04-23 at 14 14 34

I'm not really sure if this is related to Forge or there is a different approach to set colors using ezdxf. My code sample:

doc = ezdxf.new('R2000') 
block = doc.blocks.new(name='name')
mesh = block.add_mesh(dxfattribs={'color': 3, 'true_color': 3})
with mesh.edit_data() as mesh_data:
    mesh_data.vertices = v
    mesh_data.faces = f
msp.add_blockref('name', (0,0))

I also tried with layers but got the same effect: doc.layers.new(name='green', dxfattribs={'color': 3, 'true_color': 3})

Is there any better method to do that?

mozman commented 4 years ago

The true color value is a bit coded integer, use ... , 'true_color': ezdxf.rgb2int((r, g, b)) to calculate this integer value from a (r, g, b) tuple or use the common graphic entity property rgb to set the true color value: mesh.rgb = (r, g, b). For green (3) the true color value would be (0, 255, 0).

But the visual representation also depends on some other parameters which are not well documented in the DXF reference (Visualstyle, Materials) and on the capabilities of the viewer application. It is difficult to create a particular look, especially if you are not an AutoCAD expert and I am not one. I see the DXF file format as data exchange format and nothing else, there exist better documented file formats to represent entire scenes including materials.

dmh126 commented 4 years ago

I tried to set: mesh.rgb = (0,255,0) but still got the same result. Even 2d view has no color if I don't set dxfattribs.