mikedh / trimesh

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

Not able to open exported Dxf's on autoCad when they have text's #681

Closed matheusfillipe closed 4 years ago

matheusfillipe commented 4 years ago

I am using trimesh to open and add text labels to a draft dxf like this:

        draw=trimesh.load(filename)
        textHeight=5
        l=20
        for e in estacas:
            p=np.array([float(e[-2]), float(e[-1])])
            text = trimesh.path.entities.Text(origin=len(combined.vertices), text=str(e[0]) + "  " + str(e[1]),
                                              height=textHeight)
            draw.vertices = np.vstack((combined.vertices, p))
            draw.entities = np.append(combined.entities, text)
            line = trimesh.path.entities.Line(np.array([len(combined.vertices), len(combined.vertices) + 1]))
            draw.vertices = np.vstack(
                (draw.vertices, np.array([p[0], p[1]+l])))
            draw.vertices = np.vstack(
                (draw.vertices, np.array([p[0], p[1]-l])))
            draw.entities = np.append(draw.entities, line)

        draw.export(filename)

Where "estacas" is a list of points and labels of interest. I am also using this library for other things on my project but getting texts to work is being hard. Only LibreCad opened the dxf sucessfully. Draftsight only shows the last added text and Autodesk's AutoCad doesn't open it at all. Autocad asks for pressing enter and fails importing it if you press. The error reported presing F2 is:

The following error was encountered while reading
in TEXT starting at line 1822:
Invalid DXF data.
Invalid or incomplete DXF input -- drawing discarded.

The resulting file is: corrupt.zip

If i just open this file on librecad and save it it gets fixed and works fine on autocad: fixed.zip

Am I missing something or this is really an issue?

mikedh commented 4 years ago

Hey, I think this is a result of unicode characters in the text, the lines referenced:

Out[26]: ['8', 'TRAÇADOHORIZONTAL_TRAÇADO4', '62', '255']

As a hotfix I'll force text into ASCII. Supporting every encoding flag is a bit more complicated.

matheusfillipe commented 4 years ago

Hi @mikedh! before anything, thanks a lot for the efffort!

I applied your patch by replacing the file \trimesh\path\exchange\dxf but unfortunantly it didn't help on nothing.

I also created a thread on AutoCad forums asking why it doesn't open the file (since librecad opens it just fine) and I think you might understand this more than I would be able to:

https://forums.autodesk.com/t5/autocad-forum/why-does-this-dxf-file-fail-importing/m-p/9230549

The problem is that the text style name (the line after group code 7) is blank:

7

71 0

This causes an error when AutoCAD tries to find that empty name string in the TextStyle table.

There are two other text entities further on that have the same problem. If you remove the line with the 7 and the empty line after it in the problem text entities, that will fix those problems (the text will use the default style).

the other problem is that all of your text entities have the same handle "4D". Every object in the dxf file must have its own unique handle value.

I hope that can help you!

mikedh commented 4 years ago

Thanks that's very helpful! I updated the fix to pass a unique handle to text entities, and make sure we're stripping unicode before exporting.

matheusfillipe commented 4 years ago

What about having the standart style name or avoiding the those blank lines that autocad hates? Is it possible to set a style name already?

matheusfillipe commented 4 years ago

The way it is now autocad still complains with the same error on the TEXT.

mikedh commented 4 years ago

How is it after #686? There should be no unicode or empty lines in the generated files, and every entity has a unique ID... there should at least be a new exciting error message 😄

matheusfillipe commented 4 years ago

Well the error is new haha.

Customization file loaded successfully. Customization Group: ACAD
Customization file loaded successfully. Customization Group: CUSTOM
Customization file loaded successfully. Customization Group: AUTODESKSEEK
Customization file loaded successfully. Customization Group: MODELDOC
Customization file loaded successfully. Customization Group: EXPRESS
Customization file loaded successfully. Customization Group: APPMANAGER
Customization file loaded successfully. Customization Group: FEATUREDAPPS
Error 50 in drawing header on line 208.
Invalid or incomplete DXF input -- drawing discarded.

for the file: test.zip

Well I think it is getting close...

If you look on the thread on the autocad forum for the librecad saved file :

The LibreCad file changed the handles of pretty much all of the objects, and it changed the empty >text style name to "STANDARD" in the problem text entities.

And this is the file that works: https://forums.autodesk.com/autodesk/attachments/autodesk/706/1004864/1/test.zip

mikedh commented 4 years ago

Do you have access to AutoCAD? Could you save the working file from AutoCAD in ASCII DXF R14? Then I can just use their verbatim header... IIRC I pulled the current header from a Draftsight export.

matheusfillipe commented 4 years ago

Here are some dxf formats I coud export from autocad:

DXF.zip

matheusfillipe commented 4 years ago

Was that of any help?

matheusfillipe commented 4 years ago

Did you forget this issue @mikedh ? :sweat_smile: You were almost there....

mikedh commented 4 years ago

Unfortunately I don't have cycles to work on this, and it's kind of tough for me to actually test the results without an AutoCAD license. I may get around to it eventually but super happy to take PR's on it in the meantime though!