0

我有一个 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 创建的烧瓶应用程序开始在 localhost 中运行,因此我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 模板中使用这个渲染器?你能给点建议吗?

4

2 回答 2

1

HTML 包含一些您可能并不都需要的变量。可能是<head>您将自己创建的部分。JavaScript 部分对于插入您的模板很有用。一种相对简单的方法是添加上下文处理器,请参阅 Django 文档的这一部分。基本上,您定义一个函数,为您正在渲染的模板提供额外的变量。

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 }}
于 2020-09-02T16:53:12.047 回答
0

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

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())
于 2020-09-03T11:39:35.433 回答