Intersection between Circle and Ellipse

nir_naron
nir_naron Member Posts: 26 EDU
Input is an Ellipse, my code generates a circle that intercects the ellipse exactly two times (its the smalles enclosing circle). How can I get the value of said two intersection points?

Sorry for messy code

FeatureScript 2221;
import(path : "onshape/std/common.fs", version : "2221.0");

annotation { "Feature Type Name" : "Elliptical Helix" }
export const EllipticalHelix = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        //qGeometry(definition.Ellipse is Query, GeometryType.OTHER_CURVE);
        annotation { "Name" : "Ellipse", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
        definition.Ellipse is Query;
        //annotation { "Name" : "Number of Turns"}
        //isInteger(definition.NumTurns, POSITIVE_REAL_BOUNDS);
        annotation { "Name" : "Circle", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
        definition.Circle is Query;
        
    }
    {
        // Define the function's action
        var sketchId = id + "sketch";
        var Ellipse = definition.Ellipse;
        var Circle = definition.Circle;
        
        var Center = evApproximateCentroid(context, {"entities" : Ellipse});
        var BigRad = evCurveDefinition(context, {"edge" : Ellipse})["majorRadius"];
        
        //var TwoDCenter = sort(Center, function(a,b){return(b-a);});
        //println(resize(Center,2));
        
        var sketchPlane = evOwnerSketchPlane(context, { "entity" : Ellipse });
        var cSys = planeToCSys(sketchPlane);
        var sketchPlane2 = plane(Center*meter, cSys.zAxis);
        var sketch1 = newSketchOnPlane(context, sketchId, { "sketchPlane" : sketchPlane2 });
        skCircle(sketch1, "circle1", {
                "center" : vector(0, 0)*meter,
                "radius" : BigRad});       
        skPoint(sketch1, "point1", {
                "position" : vector(0*meter, BigRad)});
        //definition.point1 is Query;      
        //var point1 = definition.point1;
        //println(point1);
        
        opHelix(context, id + "helix1", {
                "direction" : sketchPlane2["normal"],
                "axisStart" : Center,
                "startPoint" : point1,
                "interval" : [0, 10],
                "clockwise" : true,
                "helicalPitch" : 0.1 * inch,
                "spiralPitch" : 0 * inch
        });
        
        skSolve(sketch1);

        println("haha");
    });

//qGeometry(qBodyType(qEntityFilter(seed1_REPLACE_ME, EntityType.EDGE), BodyType.WIRE), GeometryType.CIRCLE)

Answers

  • _anton
    _anton Member, Onshape Employees Posts: 555 image
    Looks like sketchEntityQuery(sketchId, EntityType.VERTEX, "point1")
  • nir_naron
    nir_naron Member Posts: 26 EDU
    _anton said:
    Looks like sketchEntityQuery(sketchId, EntityType.VERTEX, "point1")
    Cool. What should I put under "sketchEntityId"?
  • nir_naron
    nir_naron Member Posts: 26 EDU
    _anton said:
    Looks like sketchEntityQuery(sketchId, EntityType.VERTEX, "point1")
    Allright,
    var point1 = sketchEntityQuery(id + "sketch1", EntityType.VERTEX, "point1");
    
    seems to work. But it doesn't really contain the point's values, so I can't really use it for the Helixe's Start Point.
  • _anton
    _anton Member, Onshape Employees Posts: 555 image
    evVertexPoint(context, { vertex: point1 })
  • nir_naron
    nir_naron Member Posts: 26 EDU
    _anton said:
    evVertexPoint(context, { vertex: point1 })
    It isn't working.
    Error:
    </code>@evVertexPoint: CANNOT_RESOLVE_ENTITIES</pre><span><br>Code:<br><pre class="CodeBlock"><code>        skPoint(sketch1, "point1", {
                    "position" : vector(0*meter, BigRad)});
            var point1 = sketchEntityQuery(id + "sketch1", EntityType.VERTEX, "point1");
            
            println(evVertexPoint(context, {"vertex" : point1}));
    Thanks a lot for your help
  • nir_naron
    nir_naron Member Posts: 26 EDU
    _anton said:
    evVertexPoint(context, { vertex: point1 })
    Code:
            skPoint(sketch1, "point1", {
                    "position" : vector(0*meter, BigRad)});
            var point1 = sketchEntityQuery(id + "sketch1", EntityType.VERTEX, "point1");
            
            println(evVertexPoint(context, {"vertex" : point1}));

  • _anton
    _anton Member, Onshape Employees Posts: 555 image
    Check that the query resolves to something - easiest way is with debug(context, myQuery);
  • nir_naron
    nir_naron Member Posts: 26 EDU
    _anton said:
    Check that the query resolves to something - easiest way is with debug(context, myQuery);
    debug: Query resolves to nothing
    What am I doing wrong?

  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    That freaks me out having skSolve after opHelix - always put it straight after all the sketch creation operations - if you put the debug before skSolve then that's your problem.
    Senior Director, Technical Services, EMEA
  • nir_naron
    nir_naron Member Posts: 26 EDU
    NeilCooke said:
    That freaks me out having skSolve after opHelix - always put it straight after all the sketch creation operations - if you put the debug before skSolve then that's your problem.

            skPoint(sketch1, "point1", {
                    "position" : vector(0*meter, BigRad)});
            skSolve(sketch1);
            
            var point1 = sketchEntityQuery(id + "sketch1", EntityType.VERTEX, "point1");
            evVertexPoint(context, { vertex: point1 });
            debug(context, point1);CODE:
    ERROR:@evVertexPoint: CANNOT_RESOLVE_ENTITIES
    All I want is to use point1 as a Start Point for a Helix. How can I do that?
    <br><br><br>
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    edited January 2024
    It was your id in sketchEntityQuery - just copy/paste this code:

    FeatureScript 2221;
    import(path : "onshape/std/common.fs", version : "2221.0");
    
    annotation { "Feature Type Name" : "Elliptical Helix" }
    export const EllipticalHelix = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Ellipse", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
            definition.ellipse is Query;
    
            annotation { "Name" : "Circle", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
            definition.circle is Query;
        }
        {
            var sketchId = id + "sketch";
    
            var ellipse = evCurveDefinition(context, {
                    "edge" : definition.ellipse
                });
    
            var center = ellipse.coordSystem.origin;
            var bigRad = ellipse.majorRadius;
            var sketchPlane = plane(ellipse.coordSystem);
    
            var sketch1 = newSketchOnPlane(context, sketchId, { "sketchPlane" : sketchPlane });
    
            skCircle(sketch1, "circle1", {
                        "center" : vector(0, 0) * meter,
                        "radius" : bigRad
                    });
    
            skPoint(sketch1, "point1", {
                        "position" : vector(0 * meter, bigRad)
                    });
    
            skSolve(sketch1);
    
            var point1 = evVertexPoint(context, {
                    "vertex" : sketchEntityQuery(sketchId, EntityType.VERTEX, "point1")
                });
    
            debug(context, point1, DebugColor.RED);
    
            opHelix(context, id + "helix1", {
                        "direction" : sketchPlane.normal,
                        "axisStart" : center,
                        "startPoint" : point1,
                        "interval" : [0, 10],
                        "clockwise" : true,
                        "helicalPitch" : 0.1 * inch,
                        "spiralPitch" : 0 * inch
                    });
        });


    Senior Director, Technical Services, EMEA
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    I've edited the above ^^ to make it simpler. Question: do you even need the sketch?
    Senior Director, Technical Services, EMEA
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    Here's a super-simplified version of the same thing:

    FeatureScript 2221;
    import(path : "onshape/std/common.fs", version : "2221.0");
    
    annotation { "Feature Type Name" : "Elliptical Helix" }
    export const EllipticalHelix = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Ellipse", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
            definition.ellipse is Query;
        }
        {
            var ellipse = evCurveDefinition(context, {
                    "edge" : definition.ellipse
                });
    
            opHelix(context, id + "helix1", {
                        "direction" : ellipse.coordSystem.zAxis,
                        "axisStart" : ellipse.coordSystem.origin,
                        "startPoint" : toWorld(ellipse.coordSystem, vector(0 * meter, ellipse.majorRadius, 0 * meter)),
                        "interval" : [0, 10],
                        "clockwise" : true,
                        "helicalPitch" : 0.1 * inch,
                        "spiralPitch" : 0 * inch
                    });
        });

    Senior Director, Technical Services, EMEA
  • nir_naron
    nir_naron Member Posts: 26 EDU
    NeilCooke said:
    I've edited the above ^^ to make it simpler. Question: do you even need the sketch?
    This works, thanks a lot.

    I don't really need the sketch. What I'm trying to do is write a script that receives an ellipse and a hight as inputs and outputs an elliptical helix matching them. I plan on doing this using the Projected Curve tool.

    Link to document. Check out the Part Studio named "Goal".
    https://cad.onshape.com/documents/6e86fb25b6b8f98a9f75d61f/w/4ed281d19b48cdb345ec86ed/e/12a0d3a6f8e2817aba5b2e3d?renderMode=0&uiState=6597e1949a15484af8f96795

    I managed to write a script that gets an ellipse as an input and creats a circle that fully encloses said ellipse. I was hoping to be able to just feed that into OnShape's Helix feature instead of working with opHelix. Is there a way to do that? If not, how would you reccomend getting this done with opHelix?

    I am very new to FeatureScript. Many thanks
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    You should use "op" functions always - plus there are always easier ways of doing things, but that comes with experience. Here is what you need:

    FeatureScript 2221;
    import(path : "onshape/std/common.fs", version : "2221.0");
    
    annotation { "Feature Type Name" : "Elliptical helix" }
    export const EllipticalHelix = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Ellipse", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
            definition.ellipse is Query;
        }
        {
            var ellipse = evCurveDefinition(context, {
                    "edge" : definition.ellipse
                });
    
            opHelix(context, id + "helix", {
                        "direction" : ellipse.coordSystem.zAxis,
                        "axisStart" : ellipse.coordSystem.origin,
                        "startPoint" : toWorld(ellipse.coordSystem, vector(0, 1, 0) * meter),
                        "interval" : [0, 10],
                        "clockwise" : true,
                        "helicalPitch" : 0.1 * inch,
                        "spiralPitch" : 0 * inch
                    });
    
            opTransform(context, id + "transform", {
                        "bodies" : qCreatedBy(id + "helix", EntityType.BODY),
                        "transform" : scaleNonuniformly(ellipse.majorRadius.value, ellipse.minorRadius.value, 1, ellipse.coordSystem)
                    });
        });

    Senior Director, Technical Services, EMEA
  • nir_naron
    nir_naron Member Posts: 26 EDU
    NeilCooke said:
    You should use "op" functions always - plus there are always easier ways of doing things, but that comes with experience. Here is what you need:

    FeatureScript 2221;
    import(path : "onshape/std/common.fs", version : "2221.0");
    
    annotation { "Feature Type Name" : "Elliptical helix" }
    export const EllipticalHelix = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Ellipse", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
            definition.ellipse is Query;
        }
        {
            var ellipse = evCurveDefinition(context, {
                    "edge" : definition.ellipse
                });
    
            opHelix(context, id + "helix", {
                        "direction" : ellipse.coordSystem.zAxis,
                        "axisStart" : ellipse.coordSystem.origin,
                        "startPoint" : toWorld(ellipse.coordSystem, vector(0, 1, 0) * meter),
                        "interval" : [0, 10],
                        "clockwise" : true,
                        "helicalPitch" : 0.1 * inch,
                        "spiralPitch" : 0 * inch
                    });
    
            opTransform(context, id + "transform", {
                        "bodies" : qCreatedBy(id + "helix", EntityType.BODY),
                        "transform" : scaleNonuniformly(ellipse.majorRadius.value, ellipse.minorRadius.value, 1, ellipse.coordSystem)
                    });
        });

    Awesome. Thanks a lot. I think I managed to wrap my head around it.
     I would be curious to see how you would go about doing this with the Projected Curve approach (See the part studio named “Goal”). 
    Plus, how can I add a menu to switch feature-input types? Same way the Helix feature can receive either a circle or an axis or a face, and the Extrude feature can either be a New or an Add etc…