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.

FeatureScript behaviour

hervé_piponhervé_pipon Member Posts: 60 ✭✭
I'm trying to understand how FeatureScripts work.
I wrote a simple script ::
export const main = defineFeature(function(context is Context, id is Id, definition is map)
precondition {
    annotation { "Name" : "Select Spline", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 100 }
    definition.curves is Query ;
}

{
    println("Feature Start");
    // Get the selected spline
    const selectedCurves = definition.curves;

    // Evaluate the query to get the entities
    const entities = evaluateQuery(context, selectedCurves);

    if (size(entities) > 0) {
        // Get the first entity from the query (assuming it's a spline)
        const selectedSplineEntity = entities[0];
        print("selectedSplineEntity=");
        println(selectedSplineEntity);
        //walk through curves
        for (var curve in evaluateQuery(context, selectedCurves))
        {
            // Output the results
            print("curve=");
            println(curve);
        }
    }
    println("Feature End");
});

My script print the id of the selected rows ::
Feature Start
selectedSplineEntity={ queryType : TRANSIENT , transientId : KFBB }
curve={ queryType : TRANSIENT , transientId : KFBB }
curve={ queryType : TRANSIENT , transientId : JFt }
Feature End
and each time I click on a segment a new line is inserted in the same print
Feature Start
selectedSplineEntity={ queryType : TRANSIENT , transientId : KFBB }
curve={ queryType : TRANSIENT , transientId : KFBB }
curve={ queryType : TRANSIENT , transientId : JFt }
curve={ queryType : TRANSIENT , transientId : JF9 }
Feature End
How is it possible ?

As we went through Feature Start and Feature End, I expected that we would print everything again, but with one more line.

If we stayed in the loop, Feature End should not have been displayed. 
If we exited it, next time selectedSplineEntity should have been displayed.

Tagged:

Best Answer

  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    Answer ✓
    If you create multiple instances of the same custom feature in a given part studio, then you will also see multiples of the output created by each instance of said custom feature.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



Answers

  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    When passed a query from a query UI parameter, evaluateQuery returns an array of queries which has the following properties:
    1. One query per selection - each query in the array corresponds with a selection in the UI parameter.
    2.  Matching order - the order of queries in the array matches the order of selections in the UI parameter.
    If no elements are selected in the UI parameter, evaluteQuery returns an empty array. This can also be tested for directly using the function isQueryEmpty on the original query (rather than using evaluateQuery and checking if the resulting array is of size 0).

    In your case, your code has the effect of printing the first element in the UI parameter, then printing them all again. When you print the first element, you label it a "selectedSplineEntity", but when you print it again (along with the rest of the elements in the query parameter), you (perhaps incorrectly) label them as "curves". This results in the query for the first selection in the query parameter being printed twice - once as "selectedSplineEntity", and once again as "curve".

    As a side note, it is recommended to use the function debug to test queries (as well as geometrical data like Planes and Vectors); this will highlight the query in the graphics window (and print a summary of the contents to the console), which should make it easier to understand what the queries you have correspond to.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • hervé_piponhervé_pipon Member Posts: 60 ✭✭
    Alex_Kempen Thanks for your comment , I will use isQueryEmpty . 
  • hervé_piponhervé_pipon Member Posts: 60 ✭✭
    I know the first selection is printed twice - these are just tests for now.

    My misunderstanding is that Feature Start and Feature End lines only appear once, I thought it will adds in FeaturesScript Notice, like this

    Feature Start
    selectedSplineEntity={ queryType : TRANSIENT , transientId : JFt }
    curve={ queryType : TRANSIENT , transientId : JFt }
    (-5.3169301892694085e-18 meter, 0.02593898214399814 meter, 0 meter)
    Feature End
      Result: Regeneration complete
      
    Feature Start
    selectedSplineEntity={ queryType : TRANSIENT , transientId : JFt }
    curve={ queryType : TRANSIENT , transientId : JFt }
    (-5.3169301892694085e-18 meter, 0.02593898214399814 meter, 0 meter)
    curve={ queryType : TRANSIENT , transientId : KFBB }
    (-0.01884775049984455 meter, 0.025938982143998146 meter, 0 meter)
    Feature End
      Result: Regeneration complete
    So I now understand that the screen is cleared,  before running the code : this will explain the illusion that the new line is inserted....
  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    Answer ✓
    If you create multiple instances of the same custom feature in a given part studio, then you will also see multiples of the output created by each instance of said custom feature.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • hervé_piponhervé_pipon Member Posts: 60 ✭✭
    I thought FeaturesScript Notice behaved like a running log, but actually this is smarter and it displays the information preserving the order of creation.

    Now I understand as it is not a log, there is no way to clear it, and the position remains the same: updates are made dynamically in the right place

    Thank you 
    Alex_Kempen , it is more clear in my mind.
Sign In or Register to comment.