mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.96k stars 575 forks source link

Only last added Text entity is exported to dxf File #671

Closed matheusfillipe closed 4 years ago

matheusfillipe commented 4 years ago

I'm trying to add and export multiple texts entities to a path2D. It works on the matplotlib view but when I export to a dxf I only see the last text I added. This issue does not happen with line entities. Following is the code I used to reproduce this problem:

 import trimesh
 import numpy as np
 mesh = trimesh.load_mesh('/home/matheus/tmp/cube.stl')
 z_extents = mesh.bounds[:,2]
 z_levels  = np.arange(*z_extents, step=1)
 sections = mesh.section_multiplane(plane_origin=mesh.bounds[0],
    .                                  plane_normal=[0,0,1],
                                       heights=z_levels)
 combined = np.sum([s for s in sections if not s is None])
 text = trimesh.path.entities.Text(
            origin=len(combined.vertices),
             text='center')

combined.vertices=vertices = np.vstack((combined.vertices,
                               combined.bounds.mean(axis=0)))
combined.entities = np.append(combined.entities, text)

text = trimesh.path.entities.Text(
            origin=0,
            text='side')
combined.entities = np.append(combined.entities, text)

combined.show() #shows both side and center text
combined.export("test.dxf") #only shows side text

The cube.stl files and resulting test.dxf are in the appended file.zip.

All works fine here (combined.show())

image

but not here (combined.export("test.dxf"))

image

files.zip

mikedh commented 4 years ago

I think this is related to not explicitly setting the height, although I have some outstanding trimesh text changes that might be related as well:

import trimesh
import numpy as np

if __name__ == '__main__':
    mesh = trimesh.load('cube.stl')
    z_extents = mesh.bounds[:, 2]
    z_levels = np.arange(*z_extents, step=1)
    sections = mesh.section_multiplane(
        plane_origin=mesh.bounds[0],
        plane_normal=[0, 0, 1],
        heights=z_levels)
    combined = trimesh.path.util.concatenate(
        [s for s in sections if not s is None])

    height = combined.extents[1] / 10

    text = trimesh.path.entities.Text(
        origin=len(combined.vertices),
        text='center',
        height=height)

    combined.vertices = np.vstack((combined.vertices,
                                   combined.bounds.mean(axis=0)))
    combined.entities = np.append(combined.entities, text)

    text = trimesh.path.entities.Text(
        origin=0,
        text='side',
        height=height)

    combined.entities = np.append(combined.entities, text)

    combined.show()  # shows both side and center text
    combined.export("test.dxf")  # only shows side text

Results in: height

matheusfillipe commented 4 years ago

Just runned your code setting the height and still the same result. The "side" text is bigger but no "center" text is showing up.

mikedh

have some outstanding trimesh text changes that might be related as well:

Would you share those? Do you have your branch here or something? Please link me to it!

matheusfillipe commented 4 years ago

This is the last thing i need for my project. I would also like to know if there's a better way to add multiple text's entities in given coordinates. Does it always have to be in a vertex that belongs to the path2D?

Anyway... I'm waiting to know your "outstanding trimesh text changes".... :P

mikedh commented 4 years ago

All my changes should be merged and released now (#672), give my example code another try.

matheusfillipe commented 4 years ago

Unfortunately didn't work...

I downloaded the version 3.5.5, ran the script, still the text displays correctly with the show method but not on the exported dxf, which is weird since it works fine for you.

My environment is python 3.6.9, ubuntu 18.04 LTS. So what can this be?

files.zip

matheusfillipe commented 4 years ago

Just tested a fresh install of trimesh 3.5.5 and all it's dependencies in another debian based linux machine and the result is the same: No text "center" on the dxf, only on the matplotlib window, running that exact code you showed.

mikedh commented 4 years ago

Not sure what's going on, did you try opening the output DXF in a text editor and searching for each of the two strings? Or try reloading the exported file with trimesh and see how many text entities it has, to see if it's a DXF viewer issue, exporter, or other. My bet would be on some setting in the exported Text entity.

matheusfillipe commented 4 years ago

This is my resulting file:

test.zip

You are right!!! That didn't even occur to me. If you look on the file the string "center" is there, but I wasn't able to see it using Draftsight. Might be a bug in Draftsight then.... Ill search for another viewer. Maybe it is super small? I am not sure how to read those numbers bellow it.

matheusfillipe commented 4 years ago

Just works flawlessly on librecad..... Drafsight's Fault :P ... probably was working since always!

Thanks a lot for your patience and the amazing work on this library!! And sorry for bothering with this..