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 to choose a vector and draw a simple circle in FS?

gary_parkingary_parkin Member Posts: 4
edited February 2020 in FeatureScript
I am building a feature to create a tabbed slot. It's a hole cut through with a rectangle inside it. It mates with an extrusion with a tab on either side. You fit these parts together and give them 1/4 turn and they lock together.
btw, I'm not new to programming. I've been a developer for years now but this live query stuff has me baffled.
I've been playing with this for 3 days now.


Here is what I'm building:


See how the tab fits?

I'll be doing a lot of these so I thought I'd automate it.  :) 

Here is a pic of the test:



The problem I am getting is that the Precondition of skCircle failed, so I put the entire thing in a giant if statement.
Maybe this is not the way to do it but on the first time in, and you haven't selected the center point, then it errors out.

My code is as follows: (is there a format option for this forum?)

<div>annotation { "Feature Type Name" : "Tab Socket" }

</div><div>export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
<span style="background-color: transparent; color: inherit; font-size: inherit; font-family: Flama, sans-serif;">&nbsp; &nbsp; precondition
</span><span style="background-color: transparent; color: inherit; font-size: inherit; font-family: Flama, sans-serif;">&nbsp; &nbsp; {</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "Choose face", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; definition.partToCut is Query;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "Choose center", "Filter" : EntityType.VERTEX, "MaxNumberOfPicks" : 1 }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; definition.centerXY is Query;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "Hole Radius" }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; isLength(definition.holeRadius, BLEND_BOUNDS);</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // annotation { "Name" : "Tab Height" }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // isLength(definition.tabHeight, LENGTH_BOUNDS);</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // annotation { "Name" : "Tab Width" }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // isLength(definition.tabWidth, LENGTH_BOUNDS);</div><div><br>&nbsp; &nbsp; }<br></div><div>&nbsp; &nbsp; {</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // Step 1 - Get the face</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // Step 2 - get the center point</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // step 3 - set up the sketch</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // step 4 draw the circle</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // Circle errors out because of precondition fails #$%@</div><div><br><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; //debug(context, evaluateQuery(context, definition.centerXY));</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // precondition fails so wait until all is filled in to run</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (evaluateQuery(context, definition.centerXY) != [])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println("Got here");</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var centerPoint = evaluateQuery(context, definition.centerXY)[0];</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debug(context, centerPoint);</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //debug(context, "definition.centerXY == undefined");</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var origin = evVertexPoint(context, {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "vertex" : centerPoint</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debug(context, origin);</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var testSketch = newSketch(context, id + "sketch1", {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //create a circle</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; skCircle(testSketch, "circle1", {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "center" : centerPoint,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "radius" : definition.holeRadius</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</div>
=========================================================

Thanks for any and all help




Best Answer

Answers

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,212
    I think the problem is that you're passing centerPoint to skCircle, which is a query, while it expects a 2D vector in the coordinate system of the sketch plane.  Pass worldToPlane(XY_PLANE, origin) instead, which will take the 3D world-space vector origin and convert it to the coordinate system of the XY plane (Top).  As you teach your feature about other planes, you should use newSketchOnPlane instead of newSketch and pass that plane also to worldToPlane

    A couple of more suggestions:
    1. You can pass 
    definition.centerXY directly into evVertexPoint -- no need to evaluate the query beforehand
    2. Yes, if you want a meaningful error msg if you don't have any selections, you do have to evaluate and check for equality to []
    3. The best way to ask code questions on the forum is to make a version of your public document with an issue and post a link.  That way we can see the exact code in its entirety, the errors, and can make a copy to experiment with solutions if necessary.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • owen_sparksowen_sparks Member, Developers Posts: 2,660 PRO
    gary_parkin said:

    My code is as follows: (is there a format option for this forum?)


    Indeed there is:-



    Hope that helps,
    Owen S.

    Business Systems and Configuration Controller
    HWM-Water Ltd
  • gary_parkingary_parkin Member Posts: 4
    edited February 2020
    Thanks all for the comments and help.
    I'm working through this and I got the circle to display.

    ilya_baran, thanks. I didn't know I could pass that w/o changing it.

    I was missing skSolve too.

    I'm looking at the hex nut demo here
    http://www.cadjunkie.com/featurescript-101/nut

    Sketch

    There are three steps to creating any sketch in FeatureScript:

    Oh, and owen_sparks , using that code formatting seems to append it all in a line. I was going to split it up with carage returns but it's just not worth it. I think a better way was to share a project.  At least I think so.
    I must have done something wrong.

    Sorry. I'm really new to feature script.


  • gary_parkingary_parkin Member Posts: 4
    edited February 2020
    Hey all, I figured out how to share the project so you can see it.

    I can make it work if I use a sketch, but not if I use a part. I've included the TabSocket and 2 test pieces that I wanted it to work on.  It seems to work fine if I use a sketch but not using a part. I think it's because it can't find a plane, using the choose part then choose center point.   I think this is because the center point on the actual part was created in in the sketch and then after it was extruded, now it's 1mm (extrusion height) below the actual face.
    There must be a create plane using a face I'm sure but then I still have the how to select a vertex vector.

    Has anyone done thins kind of thing that you might point me?
    I've been a C# developer for over 30 years and this is bugging me. :smile:

    Link
    https://cad.onshape.com/documents/b0f08bded8713f461a7a2eab/w/34787ac915a63f3394407bc8/e/5ed5dd6f543e742796d5b613


    Edit:
    This works if I select the sketch part, but not the actual part.
    The way it should work is select the face then the center point but i'm missing something. It needs a sketch.
    I tried using 2 construction lines that form a XY on the face to select but I can't select them, because they are not a EntityType.VERTEX.

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    Hi @gary_parkin

    Your feature works fine for the part, your original sketch was just hidden:


    Sketches are auto-hidden after they are used by a solid feature (like extrude), you can tell they are hidden because they are greyed out in the feature list.  You can toggle their hide/show state with the eye toggle:




    If I might make a suggestion though:

    I think you may gain some advantage by using implicit mate connectors here, in addition to sketch vertices.  Then, if your user does not want to sketch in advance they will not have to make a sketch as a pre-processing step and can just infer a mate connector while they have your feature dialog open.  You could change your selection filter for `center` to:
    (EntityType.VERTEX && SketchObject.YES) || BodyType.MateConnector
    and then evaluate the plane and point like so:
    var point;
    var plane = try silent(evOwnerSketchPlane(...));
    if (plane != undefined)
    {
        // evOwnerSketchPlane succeeded
        point = evVertexPoint();
    }
    else
    {
        // evOwnerSketchPlane threw an exception because query was not from a sketch
        const MCCSys = evMateConnector(...);
        point = MCCSys.origin;
        plane = Plane(MCCSys);
    }
    More detain on this: https://forum.onshape.com/discussion/11254/implicit-mate-connectors-in-custom-features
    Jake Rosenfeld - Modeling Team
Sign In or Register to comment.