Feature script for filling in missing faces to fix faulty topology.

Hello,

I want to create a feature script that let's me select bodies with missing faces (faulty topology), i.e. surfaces, and automatically have these missing faces filled. So far I came up with the following feature script:

  • https://cad.onshape.com/documents/7636bb7733c1c2c502d0810b/w/c9b207aa5d8f3be062fdb8ac/e/65be33586f5da95b0cc2559f

I'm able to create the missing faces just fine, but unfortunately I'm struggling at combining the creates faces with the existing surfaces to get body. Any help would be greatly appreciated.

Answers

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,968 PRO
    edited February 2025

    Try something like this instead. Note this is not optimized, i simply took your code and made it work. There are better and are more shorthand ways to do all of this, but this should get you going:

    import(path : "onshape/std/common.fs", version : "2581.0");
    
    annotation { "Feature Type Name" : "My Feature", "Feature Type Description" : "" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Surfaces", "Filter" : EntityType.BODY && BodyType.SHEET }
            definition.surfaces is Query;
        }
        {
            const evSurfaces = evaluateQuery(context, definition.surfaces);
            const surfaceCount = size(evSurfaces);
    
            for (var i = 0; i < surfaceCount; i += 1)
            {
                const thisSurface = evSurfaces[i];
                const boundaryEdges = qEdgeTopologyFilter(qOwnedByBody(thisSurface, EntityType.EDGE), EdgeTopology.LAMINAR);
                const evEdges = evaluateQuery(context, boundaryEdges);
                const edgeCount = size(evEdges);
                var processedEdges = new box([]);
    
                for (var k = 0; k < edgeCount; k += 1)
                {
                    const thisEdge = evEdges[k];
    
                    // perform operations with the entity
                    if (isIn(thisEdge, processedEdges[]))
                    {
                        return;
                    }
    
                    var loop = qLoopEdges(thisEdge);
    
                    try
                    {
                        var idFillSurface = id + "fillSurface1" + i + k;
                        opFillSurface(context, idFillSurface, {
                                    "edgesG0" : loop
                                });
    
                        var idBoolean = id + "boolean1" + i + k;
                        opBoolean(context, idBoolean, {
                                    "tools" : qUnion(thisSurface, qCreatedBy(idFillSurface, EntityType.BODY)),
                                    "operationType" : BooleanOperationType.UNION,
                                    "makeSolid" : true,
                                });
                    }
    
                    processedEdges[] = concatenateArrays([processedEdges[], evaluateQuery(context, loop)]);
                }
            }
        });
    

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • MichaelPascoe
    MichaelPascoe Member Posts: 2,968 PRO
    edited February 2025

    Don't forget to complete Onshape's FeatureScript Fundamentals Course.

    Also check out the CADSharp FeatureScript Course. 


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚