mikedh / trimesh

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

Add text on path2D #654

Closed matheusfillipe closed 4 years ago

matheusfillipe commented 4 years ago

I've seen the examples of how to create path2D's and i was able to export it as a dxf. Is it possible to add text data too in a certain coordinate and with certain rotation to this path2D and export as a dxf too? I would like any example if that's possible. Thanks!

mikedh commented 4 years ago

Yeah, you can add a Text entity and export to DXF:

import trimesh
import numpy as np

if __name__ == '__main__':
    trimesh.util.attach_to_log()

    # get a scene with a number of meshes
    s = trimesh.load('models/cycloidal.3DXML')
    # get a mesh with a single large facet
    m = s.geometry['fixed_top']

    # get a Path3D of the outline of the facet
    p3 = m.outline(m.facets[m.facets_area.argmax()])

    # get a Path2D of the outline on the XY plane
    p2 = p3.to_planar()[0]

    # create a single Text entity at vertex 0
    text = trimesh.path.entities.Text(
        origin=len(p2.vertices),
        text='center')

    # add a vertex at the center of the AABB
    p2.vertices = np.vstack((p2.vertices,
                             p2.bounds.mean(axis=0)))
    # add the entity
    p2.entities = np.append(p2.entities, text)

    # export as a DXF file
    p2.export('text.dxf')

Which results in a DXF that looks like this: image

matheusfillipe commented 4 years ago

Thanks a lot! This library seems pretty impressive!

matheusfillipe commented 4 years ago

Okay, I think i had an issue. I want to add multiple texts to a topographical level curves thing im trying to create. The code is as follows:

            for e in self.tableCorte:
                azi=np.deg2rad(float(e[7])+90)
                vec=np.array([np.cos(azi), np.sin(azi)])
                l=50
                text = trimesh.path.entities.Text(origin=len(self.combined.vertices), text=str(e[0])+"  "+str(e[1]), height=240)
                self.combined.vertices=np.vstack((self.combined.vertices, np.array([float(e[4]), float(e[3])])))
                self.combined.entities=np.append(self.combined.entities, text)
                line=trimesh.path.entities.Line(np.array([len(self.combined.vertices), len(self.combined.vertices)+1]))
                self.combined.vertices=np.vstack((self.combined.vertices, np.array([float(e[4])+l*np.sin(azi), float(e[3])+l*np.cos(azi)])))
                self.combined.vertices=np.vstack((self.combined.vertices, np.array([float(e[4])-l*np.sin(azi), float(e[3])-l*np.cos(azi)])))
                self.combined.entities = np.append(self.combined.entities, line)

    def sliceCorte(self, file, tipo, step, depth, plane_normal, offset=0):
        from ... import trimesh
        import numpy as np
        mesh = trimesh.load_mesh(file)
        if tipo=="T":
            n=0
        elif tipo=="V":
            n=1
        else:
            n=2
        z_extents = mesh.bounds[:, n]
        depth=min(z_extents[1]-z_extents[0],depth)
        z_levels = np.arange(*z_extents, step=step)[int(offset/step):int(depth/step)]
        sections = mesh.section_multiplane(plane_origin=[0,0,mesh.bounds[0][2]],
                                           plane_normal=plane_normal,
                                           heights=z_levels)
        sections=[s for s in sections if not s is None]
        msgLog("--> Corte: " +str(len(sections))+" sessões válidas encontradas!")
        return sections

It does show all the texts and lines if I do self.combined.show() on the matplolib window, but with self.combined.export(file), all the lines get displayed but only the last added text gets displayed. Am I doing something wrong?

matheusfillipe commented 4 years ago

Should I create a new issue?

mikedh commented 4 years ago

Sure! Though that example is quite hard to follow, if you could reduce it to a minimal reproducible example it would be helpful.

matheusfillipe commented 4 years ago

But maybe I am just doing something wrong. Could you give me an example of how to add multiple text entities and check if it works on the extracted dxf? In my case it works on the viewer but not on the dxf.