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.

Trouble selecting shapes,edges using query/filters

fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
Have a feature script that imports an excel file with some vertices and creates a sketch with a rectangle and a few circles inside that rectangle that I want to extrude.

Tried long and (too) hard to get it to work.

At the moment my script is not doing what I want. 
The extruding results in a filled rectangle and not with the holes (from the circles) which is really what I want.

Understand this is because I do:
var entities = qEverything(EntityType.FACE);
var entities2 = qSketchFilter(entities, SketchObject.YES);

But I'm failing to understand how I could select only the rectangle and exclude the faces of the circles.
It would have been nice to use the names of the id's but I cannot get it to work.
Tried placing all my circle id's in an array and go from there but I't not working.

This is what I have come up with so far. Playing with the entitiesX variables and debug to figure out how to select (actually excluding them in the extrude)  my inner_circles 

Any ideas?




  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 2,616
    That's because an inner loop is still a face. Have you tried this?

    qSketchRegion (featureId is Id, filterInnerLoops is boolean) returns Query

    A query for all fully enclosed, 2D regions created by a sketch.

    ParameterTypeAdditional Info

    The feature id of the Sketch being queried.



    Specifies whether to exclude sketch regions fully contained in other sketch regions. A region whose border has a vertex or edge on the outside boundary is not considered "contained." Default is false.

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 940 ✭✭✭✭✭
    did you tried qSketchRegion(id, "filterInnerLoops": true) ?

  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Thanks for quick turnaround :-)

    Yes, this worked.
    qSketchRegion(sketchID, true);

    It solved the issue partially.
    My biggest issue is trying to figure out how to select/query/filter the shapes I do need.
    My sketch container an inner and an outer circle for each vertices and I would like to add the outer circle faces to my extrude as well. So really sketchRegion and all the faces named circle_inner* but wild carding does seem to be an existing option?

    I.e the part I colored yellow (and all of them).


  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Can I build an array of inner circle id's and pass it to the opExtrude some how?

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 940 ✭✭✭✭✭
    edited June 2017
    do you really need all this circles in one sketch? maybe better create several sketches each containing only geometry you want to extrude
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Well tried that just now. But I run into the same issue at this stage. 
    Started with sketch1 and put my inner circles there. Extrudes successfully..
    Create sketch2 and places my outer circles there. Same result. Need to select all the id's "outer_circle"* at this stage?
    OR could I go for selecting all shapes of the type = circle somehow?
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    And.. If I do the circles in multiple sketches it does not give me the option to create the "collars" i'm looking for.
    The yellow part I highlighted in the previous screenshot

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 940 ✭✭✭✭✭
    edited June 2017
    i don't really understand what result you want to obtain from those concentric circles. you need a plate with a number of holes, and a collar in each hole, created in place by FS? if it is so you need to create rectangular profile and a number of outer holes in sketch1, extrude it, then create a number of inner and outer circles in sketch2 and extrude it again, all extrudes with filterInnerLoops=true
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    This is what I'm trying to accomplish with the featurescript 

  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    So Im trying to learn how to best select the faces I want. Really should be possible with a single sketch and both my inner and outer circles. Making a first extrude using the rectangle and the inner cicles blindly in one direction and then a second extrude opposite direction and only using the faces created inbetween the inner and outer circle.
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 455
    Two sketches seems like by far the simplest solution.

    Sketch 1 should include the surrounding rectangle and the outer circles
    Sketch 2 should include the outer circles and the inner circles

    Both sketches can then be queried with qSketchRegion(...true...) to get the desired content of the two extrudes (Sketch 1 to the shorter depth, Sketch 2 to the longer depth).

    A single sketch would be much harder to work with, since querying the "right" regions isn't trivial. With a sketch, the functions you call draw lines, but the outputs you want to query are regions. If you don't segregate by sketch, you would have to do a lot of tricky work using queries like qContainsPoint() to get the correct regions. There's a similar tradeoff when making this without FeatureScript: With one sketch, you would have to manually pick a bunch of sketch regions to extrude by selecting them, but with two sketches, you can just click the full sketches to extrude.

    Let us know if you have further questions!

  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Updated as you suggested Kevin looks much better. Thanks Kevin and other for the quick support!
    Now I just have to make a boolean merge of all the parts.

    Not sure if it's an old feature request or a duplicate but....

    Feature reqs:
    - Generate corresponding feature script code during edit in the UI to simplify further. Probable a quite large request.
    - Allow one to store id's in an array during the creation of shapes to pass them into opExtrude as an example.

  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Merging them .... regenerates ok but still as separate solids.. A total of 9 parts.
    First tried to get all bodies and use only tools and not targets. 
    Was hoping since I select the target as the one with the rectangle (large solid) they do intersect so they should be possible to merge into a single solid..

            var target = qCreatedBy(id + "extrude1", EntityType.BODY);
            var tools = qCreatedBy(id + "extrude2", EntityType.BODY);
            debug(context, tools); // debug: Query resolves to 8 bodies
            opBoolean(context, id + "boolean1", {
                    "tools" : tools,
                    "targets" : target,
                    "operationType" : BooleanOperationType.UNION

    But.... NOT

    If I use the UI it becomes obvious that union only uses the tools and not targets. So I'm guessing the targets is not taken into account and there is no intersecting solids. So how can I merge all solids in the context?
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 940 ✭✭✭✭✭
    if boolean operation type is union then you need only tools - use "tools" : qUnion(tools,target)

  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    edited June 2017
    Would be nice if + was overloaded for the map type.

    var target = qCreatedBy(id + "extrude1", EntityType.BODY); 
    var tools = qCreatedBy(id + "extrude2", EntityType.BODY); 
    var merge = target + tools;

    But this generates :
     Can not add Query (map) and Query (map)

  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Or if I could do
    var tools = qEverything(EntityType.SOLID);


    var tools = qEverything(EntityType.BODY);

    returns 90 bodies which are not all solids (or maybe none are??)

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,419

    You are running into an issue because our UNION merges all the tools together, as documented in the following:

    You could do "tools" : qUnion([tools, targets]) and not set "targets at all" and your opBoolean would work as expected.

    The better way to do this might be to use 'extrude' instead of 'opExtrude' and supply the following in the definition map:
    "operationType" : NewBodyOperationType.ADD,
    "defaultScope" : false,
    booleanScope : qCreatedBy(id + "extrude1", EntityType.BODY)

    This is the same as selecting "Add" in the UI and selecting the rectangular body as the merge scope.

    Let us know if you have any other questions. Thanks for using FeatureScript!

    Jake Rosenfeld - Modeling Team
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    edited June 2017
    if boolean operation type is union then you need only tools - use "tools" : qUnion(tools,target)

    Thanks Konstantin,

    After adjusting the syntax it worked.... Much appreciated!

    var target = qCreatedBy(id + "extrude1", EntityType.BODY);
    var tools = qCreatedBy(id + "extrude2", EntityType.BODY);
    var merge = qUnion([tools,target]);
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,419
    A couple additional resources for you:
    Description of queries in general: https://cad.onshape.com/FsDoc/library.html#module-query.fs
    qUnion specifically (what you are looking for with query + query): https://cad.onshape.com/FsDoc/library.html#qUnion-array 
    Jake Rosenfeld - Modeling Team
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Thanks @Jake_Rosenfeld! Will try to read-up
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    But I guess feature script is scripting and not coding in traditional sense but overloading + would make easier to understand.
    Or a push operation if it was a list.

    But never mind it works. But I guess I'll be doing alot more scripting in the future since it's truly awesome!
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 952
    edited June 2017
    I don't like overloading + for something that is not really addition, but you can.
    Something like
    operator+(q1 is Query, q2 is Query) returns Query
        return qUnion([q1, q2]);
    Be sure to remember to export if using across modules.
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • fredrik_tärnellfredrik_tärnell Member Posts: 26 ✭✭
    Super @ilya_baran!
Sign In or Register to comment.