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.

Vanishing variable

CAD_SSPCAD_SSP Member Posts: 45 PRO
edited March 2018 in FeatureScript
I have a strange case where a variable vanishes when it is used and I cannot for the life of me see what is causing it.

Here is an example: https://cad.onshape.com/documents/1ac174cfcc38e3897e378a84/w/6d35fed5afbcadef257addd5/e/25f81a16e9d139c6e2d439a0

If you look at line 231 I have a debug on the feature "PCD 5" which says "debug: Query resolves to nothing", if you un-comment the debug on line 220 it correctly shows "debug: Query resolves to 1 bodies".

What is the hole feature doing with my variable?

Best Answer

  • Jake_RosenfeldJake_Rosenfeld Posts: 1,253
    Accepted Answer
    Hi @keith_marsh

    Queries in FS are lazy-evaluated.  When you set up the query on line 209:
    var sidePan = qSubtraction(qContainsPoint(qBodyType(qEverything(EntityType.BODY), BodyType.SOLID),
                    ScSys.origin), tenon);
    the system does not actually do anything to go and find the entities that satisfy the requirements you put forth.  If you go look at qBodyType, for example, you'll see that the query functions don't actually do any heavy lifting; they just make maps which can be evaluated later. 

    In this case, when you call debug after the hole, the query is being evaluated at that point, and returns nothing (I suspect because the point that you've specified to qContainsPoint falls inside the hole).  You can fix this by explicitly evaluating the Query before you make the hole.  You can do this with evaluateQuery.  It would look something like:
    // SPanEntities is an array of `Query`s; one for each entity that SPan resolves to.
    const SPanEntities = evaluateQuery(context, SPan);
    
    make your sketch;
    make your hole;
    
    // debug the first entity in SPanEntities
    debug(context, SPanEntities[0]);
    // debug all the entities in SPanEntities
    debug(context, qUnion(SPanEntities));
    You can sort of think of this as the original Query (SPan) being an order form for the entities you want.  If you try to use that order form after you make the hole, nothing satisfies your order.  If you place your order before your hole, you will get the entities you want.

    There more information about what's going on here:
    https://cad.onshape.com/FsDoc/library.html#module-query.fs
    https://cad.onshape.com/FsDoc/library.html#evaluateQuery-Context-Query
    Jake Rosenfeld - Modeling Team

Answers

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,253
    Accepted Answer
    Hi @keith_marsh

    Queries in FS are lazy-evaluated.  When you set up the query on line 209:
    var sidePan = qSubtraction(qContainsPoint(qBodyType(qEverything(EntityType.BODY), BodyType.SOLID),
                    ScSys.origin), tenon);
    the system does not actually do anything to go and find the entities that satisfy the requirements you put forth.  If you go look at qBodyType, for example, you'll see that the query functions don't actually do any heavy lifting; they just make maps which can be evaluated later. 

    In this case, when you call debug after the hole, the query is being evaluated at that point, and returns nothing (I suspect because the point that you've specified to qContainsPoint falls inside the hole).  You can fix this by explicitly evaluating the Query before you make the hole.  You can do this with evaluateQuery.  It would look something like:
    // SPanEntities is an array of `Query`s; one for each entity that SPan resolves to.
    const SPanEntities = evaluateQuery(context, SPan);
    
    make your sketch;
    make your hole;
    
    // debug the first entity in SPanEntities
    debug(context, SPanEntities[0]);
    // debug all the entities in SPanEntities
    debug(context, qUnion(SPanEntities));
    You can sort of think of this as the original Query (SPan) being an order form for the entities you want.  If you try to use that order form after you make the hole, nothing satisfies your order.  If you place your order before your hole, you will get the entities you want.

    There more information about what's going on here:
    https://cad.onshape.com/FsDoc/library.html#module-query.fs
    https://cad.onshape.com/FsDoc/library.html#evaluateQuery-Context-Query
    Jake Rosenfeld - Modeling Team
  • CAD_SSPCAD_SSP Member Posts: 45 PRO
    @Jake_Rosenfeld thanks for the quick and full answer, I like to know how to resolve the issue but I like even more to understand what caused the issue.
Sign In or Register to comment.