python3dcadcadquery

how to iteratively loft wires in cadquery?


I can loft a 2D shape from one plane to another like this:

import cadquery as cq
from jupyter_cadquery.cadquery import show
result = cq.Workplane("XY").rect(1,2).workplane()  \
     .transformed(offset=cq.Vector(0, -0.5, 1.0),rotate=cq.Vector(10, 0, 0)) \
     .rect(1,2).loft(combine=True)
show(result)

enter image description here

I would like to repeat the operation several times like this:

import cadquery as cq
from jupyter_cadquery.cadquery import show

wp = cq.Workplane("XY").rect(1,2).workplane()
result = None
for i in range(0,5):
    wp2 = wp.transformed(offset=cq.Vector(0, -0.5, 1.0),rotate=cq.Vector(10, 0, 0))
    if result == None:
        result = wp2.rect(1,2).loft(combine=True)
    else:
        nextpart = wp2.rect(1,2).loft(combine=True)
        result = result.union(nextpart)
    wp = wp2
show(result)

I have the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-54-37e88c00eca8> in <module>
      9         result = wp2.rect(1,2).loft(combine=True)
     10     else:
---> 11         nextpart = wp2.rect(1,2).loft(combine=True)
     12         result = result.union(nextpart)
     13     wp = wp2

~/anaconda3/envs/cq-jl/lib/python3.7/site-packages/cadquery/cq.py in loft(self, filled, ruled, combine)
   2897         self.ctx.pendingWires = []
   2898 
-> 2899         r = Solid.makeLoft(wiresToLoft, ruled)
   2900 
   2901         if combine:

~/anaconda3/envs/cq-jl/lib/python3.7/site-packages/cadquery/occ_impl/shapes.py in makeLoft(cls, listOfWire, ruled)
   1594         # the True flag requests building a solid instead of a shell.
   1595         if len(listOfWire) < 2:
-> 1596             raise ValueError("More than one wire is required")
   1597         loft_builder = BRepOffsetAPI_ThruSections(True, ruled)
   1598 

ValueError: More than one wire is required

At least, If I suppress the for loop and keep the same variable decomposition, it works:

import cadquery as cq
from jupyter_cadquery.cadquery import show

wp = cq.Workplane("XY").rect(1,2).workplane()
wp2 = wp.transformed(offset=cq.Vector(0, -0.5, 1.0),rotate=cq.Vector(10, 0, 0))
result = wp2.rect(1,2).loft(combine=True)
wp = wp2
show(result)

I think I am close to the result, but I cannot find where I mistake myself.


Solution

  • The point is to push back the last wire in the CQ stack, what a simple python assigment could not do:

    wp = wp2

    So I changed it to:

    wp = wp.transformed(offset=cq.Vector(0, -0.5, 1.0),rotate=cq.Vector(10, 0, 0)).rect(1,2).workplane()

    to make work the CQ kernel.

    Here is the working code:

    import cadquery as cq
    wp = cq.Workplane("XY").rect(1,2).workplane()
    result = None
    for i in range(0,5):
        wp2 = wp.transformed(offset=cq.Vector(0, -0.5, 1.0),rotate=cq.Vector(10, 0, 0)).rect(1,2).workplane()
        if result == None:
            result = wp2.loft(combine=True)
        else:
            nextpart = wp2.loft(combine=True)
            result = result.union(nextpart)
        wp = wp.transformed(offset=cq.Vector(0, -0.5, 1.0),rotate=cq.Vector(10, 0, 0)).rect(1,2).workplane()
    show_object(result, options=dict(alpha=0.5,color='red'))
    

    and the result:

    enter image description here