微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Django中使用pythonOCC的render函数

如何解决在Django中使用pythonOCC的render函数

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

这是我的观点:

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函数启动其自己的服务器,因此我认为不应调用该服务器。扩展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())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。