zalo / CascadeStudio

A Full Live-Scripted CAD Kernel in the Browser
https://zalo.github.io/CascadeStudio/
MIT License
1.03k stars 129 forks source link

Union operation failure #115

Closed timbod7 closed 3 years ago

timbod7 commented 3 years ago

The script shown below gives the following error in the cascadestudio console:

Line 78: Uncaught ReferenceError: Line 68: Union() encountered ___cxa_is_pointer_type is not defined

and this traceback in the chrome browser console:

Uncaught ReferenceError: Line 68: Union() encountered  ___cxa_is_pointer_type is not defined
    at ___cxa_find_matching_catch_2 (opencascade.wasm.js:11)
    at opencascade.wasm.wasm:0x8a0cbe
    at opencascade.wasm.wasm:0xbe966e
    at invoke_viiiiidddiiiiii (opencascade.wasm.js:11)
    at opencascade.wasm.wasm:0x8b318d
    at opencascade.wasm.wasm:0xbec1c2
    at invoke_viii (opencascade.wasm.js:11)
    at opencascade.wasm.wasm:0x8ba63f
    at opencascade.wasm.wasm:0xa3f12b
    at ShapeUpgrade_UnifySameDomain.Build.ShapeUpgrade_UnifySameDomain.Build (opencascade.wasm.js:11)

The two components being combined with Union() both render correctly without error.

What's the way to debug this?


Test script

let num_leds = 30;
let led_spacing = 17;
let triangle_spacing = led_spacing * Math.cos(30 * Math.PI / 180);
let wall_thickness = 1.5;
let lightbox_depth = 35;
let inner_diameter = num_leds * triangle_spacing / Math.PI;
let outer_diameter = inner_diameter + lightbox_depth;
let layer_height = outer_diameter * Math.PI / num_leds * Math.tan(60 * Math.PI / 180);
let diffuser_overhang_thickness = 1.2;
let diffuser_wall_thickness = 1.0;
let diffuser_radius_gap = 0.1;
let cap_vert_overhang = 5;
let cap_horz_overhang = 3;
let top_cap_diam = outer_diameter + 2 * (diffuser_radius_gap + diffuser_wall_thickness + cap_horz_overhang);
let base_outer_radius = top_cap_diam / 2;
let base_outer_height = 20;
let inset1_radius = base_outer_radius - cap_horz_overhang;
let inset1_height = cap_vert_overhang;
let inset2_radius = inner_diameter / 2;

// A box in cylindrical coordinates 
function ccbox(x1, x2, a1, a2, z1, z2) {
    const rect = new Sketch([x1, z1])
        .LineTo([x1, z2])
        .LineTo([x2, z2])
        .LineTo([x2, z1])
        .End(true)
        .Face();
    return Rotate([0, 0, 1], a1, Revolve(Rotate([1, 0, 0], 90, rect), a2 - a1, [0, 0, 1], false, false))
}

function leg() {
    let height = 40;
    let hextra = 5;
    let angle = 20;
    let aextra = 20;
    let fillet1 = 10;

    const box1 = ccbox(inset2_radius, inset1_radius, -(angle + aextra) / 2, (angle + aextra) / 2, -height, hextra)
    const box2 = FilletEdges(
        ccbox(inset2_radius, inset1_radius, -(angle + aextra) / 2, -angle / 2, -height, 0),
        fillet1, [6]
    )
    return  Difference(box1, [box2])
}

function base() {
    let shell = Cylinder(base_outer_radius, base_outer_height);
    let inset1 = Cylinder(inset1_radius, inset1_height);
    let inset2 = Cylinder(inset2_radius, base_outer_height);
    return Difference(shell, [
        Translate([0, 0, base_outer_height - inset1_height], inset1),
        inset2
    ])
}

// Works:
//
// base()

// Works:
//
// leg()

// Reports error, but renders as expected:
//
Union([base(),leg()])

// Reports same error and fails to render correctly as intersection
//
// Intersection([
//     ccbox(30, 100, -30, 30, -50, 50),
//     Union([base(),leg()]),
// ])
timbod7 commented 3 years ago

The base component

Screenshot from 2021-09-17 08-50-05

The leg component:

Screenshot from 2021-09-17 08-50-56

zalo commented 3 years ago

It looks like the kernel doesn't like the inset radius 2 being co-circular, if I add a 0.01 offset to line 40, it makes it work again.

The OpenCascade kernel has quirks like these from time to time.

timbod7 commented 3 years ago

Thanks so much.

I'd love to know how you worked out that fix for the problem - I fiddled with this for some time trying to work out what was wrong.