我有一个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创建的烧瓶应用程序开始在本地主机中运行,所以我的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 renderer.py中看到的,html文件是在这个python文件中创建的,并且是根据我发送的图像形成的。如何在Django模板中使用这个呈现器?你能给我一些建议吗?
发布于 2020-09-03 11:39:35
render函数启动自己的服务器,因此我认为不应该调用这个服务器。扩展Renderer类,添加我们遗漏的函数可能是有用的。在这种情况下,一个选项将呈现为string,因此我们可以使用输出。
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-02 16:53:12
HTML由几个变量组成,您可能并不需要这些变量。可能是您自己创建的<head>部分。在模板中插入JavaScript部件非常有用。一种相对简单的方法是添加上下文处理器请参阅Django文档的本节。。基本上,您定义了一个函数,它为您正在呈现的模板提供额外的变量。
在我的应用程序/上下文_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 }}https://stackoverflow.com/questions/63709690
复制相似问题