Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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é_pipon
Member Posts: 60 ✭✭
I'm trying to understand how FeatureScripts work.
I wrote a simple script ::
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 Endand 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:
0
Best Answer
-
Alex_Kempen Member Posts: 248 EDUIf 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 DallasAlex.Kempen@utdallas.eduCheck out my FeatureScripts here:0
Answers
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.
Thank you Alex_Kempen , it is more clear in my mind.