sgenoud / replicad

The library to build browser based 3D models with code.
https://replicad.xyz
MIT License
323 stars 38 forks source link

Error more gracefully in set operations #145

Open andrewkrippner opened 4 months ago

andrewkrippner commented 4 months ago

In general, geometrical errors are very difficult to troubleshoot. Is there a way to throw more informative messages?

In addition, are there better ways to handle the cases where errors are thrown? For instance, I am trying to filter a set of edges together in:

body = body.fillet(0.5, (e) => e.inDirection('Z'))

This is erroring out I assume because some of my edges are already tangent on both sides, so there is no fillet that can be added. I would much prefer if that single edge error was caught and the rest of the operation was allowed to continue.

andrewkrippner commented 2 months ago

I was able to address this with a try-catch in a loop. It doesn't seem the most efficient but this does the job I needed.

export const safeFillet = (
    body: Solid,
    edgeFinder: EdgeFinder,
    radius: number,
) => {
    let newBody = body
    edgeFinder.find(body).forEach((edge) => {
        try {
            newBody = newBody.fillet((r) => {
                if (r.isEqual(edge)) {
                    return radius
                }
                return 0
            })
        } catch {}
    })
    return newBody
}