Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

How can I efficiently find every body that has a coplanar face to an input?

EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
I have a query of planar face(s) as inputs, and would like to query every body that has any face that is co-planar with any of the inputs (selectedFaces). I got it working like this, but I'm getting really bad re-build times. I'm sure there's got to be a much faster way that my 5pm brain isn't seeing. Anybody have any tips or examples for me?
var selections = qNothing();            
var allPlanarFaces = evaluateQuery(context, qSketchFilter(qConstructionFilter(qGeometry(qEverything(EntityType.FACE), GeometryType.PLANE), ConstructionObject.NO), SketchObject.NO));
            var selectedFaces = evaluateQuery(context, definition.faces);
            var selectedBodies = qNothing();
            for (var face in selectedFaces)
            {
                var plane = evPlane(context, {
                        "face" : face
                    });
                for (var referenceFace in allPlanarFaces)
                {
                    var referencePlane = evPlane(context, {
                            "face" : referenceFace
                        });
                    var isCoplanar = coplanarPlanes(plane, referencePlane);
                    if (isCoplanar)
                    {
                        selections = qUnion([selections, qOwnerBody(referenceFace)]);
                    }
                }
            }

Evan Reese

Best Answers

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    edited January 2021 Answer ✓
    Hey Evan!

    I suspect the fastest you can do it is something like:
    // -> notation is newly released.  
    // qGeometry(qEverything(EntityType.FACE), GeometryType.PLANE) can now be written more simply as as qEverything(EntityType.FACE)->qGeometry(GeometryType.PLANE)
    // The arrow simply means "insert the thing before the arrow as the first argument to the thing after the arrow"
    const allPlanarFaces = qEverything(EntityType.FACE)->qGeometry(GeometryType.PLANE)->qConstructionFilter(ConstructionObject.NO)->qSketchFilter(SketchObject.NO);
    const resultArr = [];
    for (var face in evaluateQuery(context, definition.faces))
    {
        const plane = evPlane(... face ...);
        // filter down to parallel planes, and then filter further to coincident, resulting in coplanar
        const coplanar = qParallelPlanes(allPlanarFaces, plane.normal)->qCoincidesWithPlane(plane));
        resultArr = append(result, coplanar); // Appending to an array is better than qUnion with a master query<br>}
    const result = qUnion(resultArr);
    
    debug(context, result);

    ** formatting is getting messed up. -&gt; should be "->"
    Jake Rosenfeld - Modeling Team
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,212
    Answer ✓
    Haven't tested it, but qCoincidesWithPlane would be key here I think.  Something like:
    qEverything(EntityType.FACE)->qCoincidesWithPlane(myPlane)->qOwnerBody()
    looking at our internal implementations, it's possible that qEverything(EntityType.FACE)->qParallelPlanes(myPlane)->qCoincidesWithPlane(myPlane)->qOwnerBody() would actually be faster -- worth testing.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc

Answers

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    edited January 2021 Answer ✓
    Hey Evan!

    I suspect the fastest you can do it is something like:
    // -> notation is newly released.  
    // qGeometry(qEverything(EntityType.FACE), GeometryType.PLANE) can now be written more simply as as qEverything(EntityType.FACE)->qGeometry(GeometryType.PLANE)
    // The arrow simply means "insert the thing before the arrow as the first argument to the thing after the arrow"
    const allPlanarFaces = qEverything(EntityType.FACE)->qGeometry(GeometryType.PLANE)->qConstructionFilter(ConstructionObject.NO)->qSketchFilter(SketchObject.NO);
    const resultArr = [];
    for (var face in evaluateQuery(context, definition.faces))
    {
        const plane = evPlane(... face ...);
        // filter down to parallel planes, and then filter further to coincident, resulting in coplanar
        const coplanar = qParallelPlanes(allPlanarFaces, plane.normal)->qCoincidesWithPlane(plane));
        resultArr = append(result, coplanar); // Appending to an array is better than qUnion with a master query<br>}
    const result = qUnion(resultArr);
    
    debug(context, result);

    ** formatting is getting messed up. -&gt; should be "->"
    Jake Rosenfeld - Modeling Team
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,212
    Answer ✓
    Haven't tested it, but qCoincidesWithPlane would be key here I think.  Something like:
    qEverything(EntityType.FACE)->qCoincidesWithPlane(myPlane)->qOwnerBody()
    looking at our internal implementations, it's possible that qEverything(EntityType.FACE)->qParallelPlanes(myPlane)->qCoincidesWithPlane(myPlane)->qOwnerBody() would actually be faster -- worth testing.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    Thanks so much for the willingness to educate, guys. It's so helpful to see how the pros think about breaking down a problem in steps. I also need to get with it with the -> syntax. it's so much better!

    I might be taking this script in a whole different direction, but If I come back to this, I'll implement these changes. I'll mark it "answered" even though I've not tested it, because I'm pretty sure you're going to be right.
    Evan Reese
Sign In or Register to comment.