Polyhedrons and Moebius Solid in CQ?

442 views
Skip to first unread message

Dario Pellegrini

unread,
Apr 10, 2020, 12:53:25 PM4/10/20
to CadQuery
Hi,

I have a strong OpenSCAD background, but I am just starting with CQ.
I have gone through the examples and the API ref, still there are few things that I cannot figure out how to achieve.

There are intrinsically 3D shapes which are not easily generated from a plane (1) or that require advanced operations such as revolved and twisted extrusion (2):
  1. (irregular) polyhedrons
  2. Moebius Solids (https://www.thingiverse.com/thing:3654597)











a6f1f5c7dd1170a64e834eef07133de2_display_large.jpg























In OpenSCAD I would hunt these beasts by specifying all the vertices and the edges with the polyhedron function, but to my understanding CQ allows this only in 2D.
I would love to see minimal examples for these two cases in CQ.

Thank you!

Jeremy Wright

unread,
Apr 10, 2020, 1:51:07 PM4/10/20
to CadQuery
Hi Dario,

Does this functionality help?


The referenced PR has since been merged and is in master.

Adam Urbanczyk

unread,
Apr 11, 2020, 9:49:44 AM4/11/20
to CadQuery
Hi Dario,

regarding polyhedra there is en example for a tetrahedron in the tests:


For Moebius Solids you'd need a sweep + auxiliary spine. It is not currently implemented in CQ as such. There is a straight extrude with a twist. You could try playing with PythonOCC to get it working (BRepOffsetAPI_MakePipeShell is the relevant class). It seems like FreeCAD has it (see https://forum.freecadweb.org/viewtopic.php?f=22&t=21193 ) so reading their code might be a good starting point.

HTH,
Adam

Dario Pellegrini

unread,
May 20, 2020, 2:43:28 PM5/20/20
to CadQuery
Thank you both for your replies and sorry for being so late, I have been absorbed by other projects.
I have found the tetrahedron example particularly enlightening.
However, consider this code which makes the union between a sphere and the tetrahedron built as in the test:

import math
import cadquery as cq


from cadquery import Workplane as WP
from cadquery import exporters

from cadquery import Edge
from cadquery import Wire
from cadquery import Face
from cadquery import Shell
from cadquery import Solid
from cadquery import Vector

def Tetra(s=1):
    c0
= s*math.sqrt(2) / 4
    vertices
= [[c0, -c0, c0], [c0, c0, -c0], [-c0, c0, c0], [-c0, -c0, -c0]]
    faces_ixs
= [[0, 1, 2, 0], [1, 0, 3, 1], [2, 3, 0, 2], [3, 2, 1, 3]]
   
    faces
= []
   
for ixs in faces_ixs:
        lines
= []
       
for v1, v2 in zip(ixs, ixs[1:]):
            lines
.append(
               
Edge.makeLine(Vector(*vertices[v1]), Vector(*vertices[v2]))
           
)
        wire
= Wire.combine(lines)
        faces
.append(Face.makeFromWires(wire))
   
    shell
= Shell.makeShell(faces)
    solid
= Solid.makeSolid(shell)
   
return solid

def save( self, fname, tol = 1):
   
with open(fname, 'w') as f:
        exporters
.exportShape(self, "STL", f, tol)

WP
.save = save


s
= WP("XY").sphere(radius=1).union(Tetra(s=3))
s
.save("./cq-output.stl", tol=0.01)


This is the resulting mesh inspected with gmsh:

Screenshot from 2020-05-20 20-31-51.png

The meshing of the sphere is ugly. It does not even connect properly with the tetrahedron. 
Even on the upper side, which seems ok at a first look, there are weird vertices popping up.

Am I doing something wrong?

Jeremy Wright

unread,
May 21, 2020, 7:20:50 AM5/21/20
to CadQuery
I bumped up the tolerance, and with the master branch of cadquery the STL export looks like this.


With the OCP branch of cadquery (next release version) using OCCT 7.4, everything looks like it's been tessellated at the same tolerance, but there are gaps in the mesh present on one face.

Dario Pellegrini

unread,
May 21, 2020, 10:08:16 AM5/21/20
to CadQuery
Thanks a lot for taking the time to test it!

So your conclusion is that it is a kernel issue?
Do we have a way to report upstream?

It is a bit discouraging finding issues at such a basic level.

Adam Urbanczyk

unread,
May 24, 2020, 8:05:18 AM5/24/20
to CadQuery
If I run your code on the OCP branch and use angular tolerance=0.01 for STL export I get a nice looking mesh. It seems to have some self-intersections (marked in yellow). I don't know enough about STL repair  to be able to say if it is killing or not for e.g. printing. Long story short: you might want to try to modify the angular tolerance of the STL export.


s
.val().exportStl("./cq-output.stl", 1e-2,1e-1)





Justin SDK

unread,
Oct 31, 2021, 11:34:41 PM10/31/21
to CadQuery
I'm working on a CadQuery plugin.
https://github.com/JustinSDK/cqMore

It provides a `polyhedron` method on `Workplane` and here's an example.

There're some pre-defined polyhedra in `cqmore.polyhedron`.

As for  Moebius Solid, here's an example.

1.JPG

pellegri...@gmail.com 在 2020年4月11日 星期六上午12:53:25 [UTC+8] 的信中寫道:

Jeremy Wright

unread,
Nov 1, 2021, 2:03:06 PM11/1/21
to CadQuery
Very cool. I have added this in the third-party listing in the cadquery-plugins repo. https://github.com/CadQuery/cadquery-plugins/blob/main/third_party.md

Adam Urbanczyk

unread,
Nov 2, 2021, 1:56:51 AM11/2/21
to CadQuery
Cool, thanks for sharing. Out of curiosity: is there a technical reason why you construct a blocky  moebius version instead of a smooth one (e.g. with splines)?

Justin SDK

unread,
Nov 2, 2021, 4:39:12 AM11/2/21
to CadQuery
It's just a demonstration of `cqmore.polyhedron.sweep` (the polyhedron version of `sweep` ) .

Here're smooth versions.

1.JPG

Someone might have the problem: why `cqmore.polyhedron`? At times, it's a style for modeling; at other time I just want to control faces. 

Adam Urbanczyk 在 2021年11月2日 星期二下午1:56:51 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages