bernhard-42 / jupyter-cadquery

An extension to render cadquery objects in JupyterLab via pythreejs
Apache License 2.0
312 stars 44 forks source link

bug in stl export #91

Open roipoussiere opened 2 years ago

roipoussiere commented 2 years ago

I'm trying to handle stl export in cq-server.

So far I tried this:

from jupyter_cadquery.ocp_utils import write_stl_file
# ...
assembly = to_assembly(*self.models, names=self.names)
write_stl_file(assembly.compound(), 'output.stl')

It fails with this error:

  File "/home/somed/.cache/pypoetry/virtualenvs/cadquery-server-Io9SCdfb-py3.10/lib/python3.10/site-packages/jupyter_cadquery/ocp_utils.py", line 248, in write_stl_file
    mesh = BRepMesh_IncrementalMesh(compound, tolerance, True, angular_tolerance)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. OCP.BRepMesh.BRepMesh_IncrementalMesh()
    2. OCP.BRepMesh.BRepMesh_IncrementalMesh(theShape: OCP.TopoDS.TopoDS_Shape, theLinDeflection: float, isRelative: bool = False, theAngDeflection: float = 0.5, isInParallel: bool = False)
    3. OCP.BRepMesh.BRepMesh_IncrementalMesh(theShape: OCP.TopoDS.TopoDS_Shape, theParameters: IMeshTools_Parameters, theRange: OCP.Message.Message_ProgressRange = <OCP.Message.Message_ProgressRange object at 0x7fbe17cac8b0>)

Invoked with: <OCP.TopoDS.TopoDS_Compound object at 0x7fbe0662a7b0>, None, True, None

In the write_stl_file function, there is a call to BRepMesh_IncrementalMesh:

mesh = BRepMesh_IncrementalMesh(compound, tolerance, True, angular_tolerance)

Where tolerance is defaulted to False, but it's supposed to be a required float.

Maybe you have to set the default tolerance to a float in the function signature?

roipoussiere commented 2 years ago

Anyway I used CadQuery builtin export function, so you have time to investigate. :)

bernhard-42 commented 2 years ago

I'd recommend to use the CadQuery functions.

Nevertheless, write_stl_file works as

from jupyter_cadquery.ocp_utils import write_stl_file
from jupyter_cadquery import Part

box = cq.Workplane('XY').box(1, 2, 3).edges().fillet(0.1)

write_stl_file(
    box.objects[0].wrapped,
    "test.stl",
    tolerance=0.1,
    angular_tolerance=0.3,
)
bernhard-42 commented 2 years ago

Since each object depending on size needs different params (a tradeoff between smoothness and time needed for tessellation, write_stl_file doesn't have default values for tolerance and angular_tolerance.

roipoussiere commented 2 years ago

I understand, but why in this line you set tolerance to Noneinstead something like 0.1?

bernhard-42 commented 2 years ago

Fair point. I should change the defaults. I am anyhow currently preparing a new release (with isometric view and orientation buttons working like in FreeCAD or Onshape). Will change it

bernhard-42 commented 2 years ago

@roipoussiere just published 1.7.0 of three-cad-viewer The reason why I increased the minor version is that it changes the default orientation behavior:

Will now start to publish the dependent package and jupyter cadquery I think that addresses the comment in discord about wrong orientation.

bernhard-42 commented 1 year ago

@roipoussiere V3.3.0 is out. It fixes this issue by using 0.01 and 0.2 as defaults in exportSTL (module exports) exportSTL wraps write_stl_file to create a compound before. This is the routine that should be used, write_stl_file is an internal function and shouldn't be used.