在 Django 中使用 pythonOCC 的渲染函数

Using pythonOCC's render function in Django

我有一个 Django 应用程序,我在其中使用 pythonOCC 包。我必须在我的模板中显示 3D .stl、.stp、.igs 文件。我尝试使用包中 x3dom_renderer.py 文件中的 render() 函数。

这是我的看法:

from OCC.Extend.DataExchange import read_step_file
from OCC.Display.WebGl import x3dom_renderer
from OCC.Core.BRep import BRep_Builder
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.BRepTools import breptools_Read

def index(request):
    shape = read_step_file('test.stp')
    my_renderer = x3dom_renderer.X3DomRenderer()
    my_renderer.DisplayShape(shape)
    my_renderer.render()
    return render(request, 'index.html')

当我调用 render() 函数时,以下输出出现在我的 vscode 控制台上,并且由于 pythonocc 而不是 django 创建的 flask 应用程序在 运行 中启动本地主机,我的 index.html 从未呈现。

我调用渲染函数时的输出:

 **  Model Complete Check List  **
Check:1 -- Entity (n0:id) 5:#14   Type:CURVE_STYLE
Parameter n0.2 (curve_font) not an Entity
Check:2 -- Entity (n0:id) 6:#15   Type:CURVE_STYLE
Parameter n0.2 (curve_font) not an Entity
Check:3 -- Entity (n0:id) 7:#16   Type:CURVE_STYLE
Parameter n0.2 (curve_font) not an Entity
Check:4 -- Entity (n0:id) 8:#17   Type:CURVE_STYLE
Parameter n0.2 (curve_font) not an Entity
Check:5 -- Entity (n0:id) 9:#18   Type:CURVE_STYLE
Parameter n0.2 (curve_font) not an Entity
Check:6 -- Entity (n0:id) 10:#19   Type:CURVE_STYLE
Parameter n0.2 (curve_font) not an Entity
## x3dom webgl renderer - render axes/planes : True - axes/plane zoom factor : 1
| meshing shapes... 100%
## Serving C:\Users\imgea\AppData\Local\Temp\tmppopa5opx
## using Flask
## Open your webbrowser at the URL: http://localhost:8080

如您在此 x3dom_renderer.py https://github.com/tpaviot/pythonocc-core/blob/master/src/Display/WebGl/x3dom_renderer.py 中所见,html 文件是在此 python 文件中创建的,并根据我发送的图像进行调整。我如何在我的 Django 模板中使用这个渲染器?你能给点建议吗?

HTML 包含一些您可能不需要的变量。您可能会自己创建 <head> 部分。 JavaScript 部分对于插入模板很有用。一个相对简单的方法是添加一个上下文处理器,see this section of the Django documentation。基本上你定义了一个函数,为你正在渲染的模板提供额外的变量。

my_app/context_processors.py中。请注意,因为我们信任 HTML,所以我们向其添加 mark_safe,以防止模板转义 HTML:

from Display.WebGl.three_js_renderer import BODY_PART1, BODY_PART2
from django.utils.safestring import mark_safe

def threejs_context(request):
  return {
    'threejs_body_part1':  mark_safe(BODY_PART1),
    'threejs_body_part2': mark_safe(BODY_PART2),
  }

在您项目的settings.py中:

TEMPLATES = [
  {
    ...
    'context_processors': [
      ...
      'my_app.context_processors.threejs_context',
    ],
    ...
  }
]

现在在您的模板中,您可以使用定义的变量在您的上下文中插入 HTML:

{{ threejs_body_part1 }}
{{ threejs_body_part1 }}

render 函数启动了它自己的服务器,所以我认为不应该调用这个。扩展渲染器 class 以向其添加我们错过的功能可能很有用。在这种情况下,可以选择呈现为字符串,因此我们可以使用输出。

from OCC.Extend.DataExchange import read_step_file
from OCC.Display.WebGl import x3dom_renderer
from OCC.Core.BRep import BRep_Builder
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.BRepTools import breptools_Read
from django.http.response import HttpResponse


class CustomX3DomRenderer(x3dom_renderer.X3DomRenderer):
    def render_to_string(self):
        # N.B. Writing the html file to disk isn't really needed; you 
        # could also build the string directly without writing it
        # to disk
        self.generate_html_file(self._axes_plane, self._axes_plane_zoom_factor)
        return open(self._html_filename, 'r').read()


def index(request):
    shape = read_step_file('test.stp')
    my_renderer = CustomX3DomRenderer()
    my_renderer.DisplayShape(shape)
    return HttpResponse(my_renderer.render_to_string())