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 find the intersection points from 2 circles in 2 sketches (w scripting)

hervé_piponhervé_pipon Member Posts: 60 ✭✭
Hello
I want to find the intersection points of 2 circles

I tried : 

        var i = 1;
        var j = 2;
        // sketches Id 
        var test_i = id + ("sketch_" ~ i);
        var test_j = id + ("sketch_" ~ j);
        // circles Id
        var circle_i = "circle_" ~ i;
        var circle_j = "circle_" ~ j;
        
        // circle edges
        var edges_i = sketchEntityQuery(test_i, EntityType.EDGE, circle_i);
        var edges_j = sketchEntityQuery(test_j, EntityType.EDGE, circle_j);

        const curves_i = evaluateQuery(context, edges_i);
        const curves_j = evaluateQuery(context, edges_j);
        println(curves_i);
        println(curves_j);
        // 
        var intersectionPointsQuery = qIntersection(edges_i, edges_j);
        
        for (var intersectionPoints in intersectionPointsQuery )       
        {
            println("intersectionPoints = " ~ intersectionPoints);
        }

It prints 

[ { queryType : TRANSIENT , transientId : JMB } ]
[ { queryType : TRANSIENT , transientId : JRB } ]
intersectionPoints = { "key" : "queryType" , "value" : QueryType : "INTERSECTION" }
intersectionPoints = { "key" : "subqueries" , "value" : [ Query : { "entityType" : EntityType : "EDGE" , "historyType" : "CREATION" , "operationId" : Id : [ "FovTflfcvumxF3Q_0" , "sketch_1" ] , "queryType" : "SKETCH_ENTITY" , "sketchEntityId" : "circle_1" } , Query : { "entityType" : EntityType : "EDGE" , "historyType" : "CREATION" , "operationId" : Id : [ "FovTflfcvumxF3Q_0" , "sketch_2" ] , "queryType" : "SKETCH_ENTITY" , "sketchEntityId" : "circle_2" } ] }

How can I get the actual points from this query ?

Comments

  • _anton_anton Member, Onshape Employees Posts: 413
    edited December 2023
    qIntersection is a set intersection between two queries (that is, qIntersection(A, B ) is a query for entities that match both queries A and B ), not a geometric evaluation of where the entities intersect.

    evDistance is the typical way to do this.
  • hervé_piponhervé_pipon Member Posts: 60 ✭✭
    In fact the more I use onshape scripting, the more I get lost.
    I would never have thought to use evDistance to find the intersection of my two circles, but it works. :smiley:
    I'm really not in the minds of those who designed the featurescript code, too bad..

    @_anton Thanks a lot for your help. I would never have found it alone.
  • emmett_weeksemmett_weeks Onshape Employees Posts: 29
    evDistance will find an intersection between two circles, but it will not find multiple intersections. We do have functionality for that, but it is not currently exposed to featurescript. If you want to find multiple intersections, you can call opExtractWires on one of the circles, then you can use opTrimCurve to trim it with another one and evaluate the end points of the trimmed curve. This will fail if the two circles are tangent.

    If you'd like an api that finds all curve intersections, please make an improvement request on the forum.
  • _anton_anton Member, Onshape Employees Posts: 413
    edited December 2023
    There's definitely a zen to thinking about queries (and we could probably do better at educating users). A query encodes the intent to find some entities, and can be combined and filtered in various ways; an ev* call evaluates a query and then does some computation on the result; an op* call will typically cause a geometric change.

    A query is like a street address - you never know if it's unique, where it is, nor whether the place even exists, until you look it up or go there in person. You can imagine a query like "every address along road X", then taking its set intersection with "every address along road Y" to get the set of houses at the intersection of both roads. But you're still just looking at letters on paper until you find out which houses/restaurants/parking lots/whatever satisfy that query.
  • hervé_piponhervé_pipon Member Posts: 60 ✭✭
    @emmett_weeks Thanks for your comment.
    I rejoiced too quickly : I actually find the point that interests me, but as the circles intersect at 2 points, I don't have the second one.
    Even if it doesn't interest me, there is an element of chance that is not fine....
    I will have a look to opExtractWires and opTrimCurve .

  • hervé_piponhervé_pipon Member Posts: 60 ✭✭
    @emmett_weeks
    Hi , I didn't find the opTrimCurve operation. I saw the trimTool function, is it related ?
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    If it is two circles and you know the circle definitions, then finding the two intersections is simple geometry calcs. Better to do it mathematically than geometrically. 
    Senior Director, Technical Services, EMEAI
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Also, I find the best way to think of a Query is to compare it to an SQL Query. If you are querying information from an SQL database, you have no idea what the results are until the query is run, so a FeatureScript query is equivalent and the results are known when the query is executed with evaluateQuery.
    Senior Director, Technical Services, EMEAI
  • emmett_weeksemmett_weeks Onshape Employees Posts: 29
    @hervé_pipon I had forgotten that the operation is actually called opMoveCurveBoundary. Neil is correct that doing the math yourself will be faster if you are always doing intersections of simple geometry.
Sign In or Register to comment.