python3dautodesk-viewerdxfezdxf

Dxf file generated using ezdxf doesn't render text in 3d model when viewed in Autodesk Viewer


I have written code that created dxf file with 2 cylinders stacked on top of each other, most of the part is working fine except when I add text into the 3d model it doesn't show in the Autodesk Viewer, any idea why or any possible fix? Below is my code

import ezdxf
from ezdxf.render import forms
from ezdxf.gfxattribs import GfxAttribs
from ezdxf import colors
from ezdxf.math import UCS, Vec3
from ezdxf.enums import TextEntityAlignment

# create the DXF document and model space
doc = ezdxf.new()
doc.styles.new('TXT', dxfattribs={'font': 'romans.shx'})
msp = doc.modelspace()

# create the first cylinder with a height of 10
cylinder_1 = forms.cylinder(radius=1.0, count=8).scale(1, 1, 5)
green = GfxAttribs(color=colors.GREEN)

# create the second cylinder with a height of 5
cylinder_2 = forms.cylinder(radius=1.0, count=8).scale(1, 1, 2)
blue = GfxAttribs(color=colors.BLUE)

# render the first cylinder as a MESH entity
cylinder_1.render_polyface(msp, dxfattribs=green)

# add text to first cylinder
ucs_1 = UCS(origin=(0, 0, 10), ux=(1, 0, 0), uz=(0, 1, 0))
text_1 = msp.add_text("Cylinder 1", dxfattribs={
    'rotation': ucs_1.to_ocs(Vec3(45, 0, 0)).angle_deg,
    'extrusion': ucs_1.uz,
    'color': 1,
    'style': 'TXT',
}).set_placement(ucs_1.to_ocs((0, 0, 0)), align=TextEntityAlignment.CENTER)

# translate the second cylinder to stack at bottom of first cylinder
cylinder_2.translate(0, 0, -2)

# render the second cylinder as a MESH entity
cylinder_2.render_polyface(msp, dxfattribs=blue)


doc.saveas("test.dxf")

I am only getting this view below in Autodesk Viewer, can't see any text enter image description here

Hope to get some help on this. thank you all


Solution

  • It's easier to let ezdxf do the transformation than setting the OCS and rotation "by hand":

    # add text to first cylinder
    ucs_1 = UCS(origin=(0, 0, 10), ux=(1, 0, 0), uz=(0, 1, 0))
    text_1 = msp.add_text("Cylinder 1", dxfattribs={
        'color': 1,
        'style': 'TXT',
    }).set_placement((0, 0, 0), align=TextEntityAlignment.CENTER)
    # the text is placed in the xy-plane of the UCS
    # transform the text from UCS to WCS (OCS is managed automatically)
    text_1.transform(ucs_1.matrix)  
    
    

    and you get this:

    enter image description here

    but I'm pretty sure that's what you wanted:

    enter image description here

    Therefore select the UCS as follows: text x-axis in WCS-x and text y-axis in WCS-z:

    ucs_1 = UCS(origin=(0, 0, 10), ux=(1, 0, 0), uy=(0, 0, 1))