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.

Constraints in FeatureScript

aleksei_pomelovaleksei_pomelov Member Posts: 6
Hi. There are plenty of discussions regarding constraints in feature script. Most of them are ended with a statement "you don't need constraints" or questions "why do you need them". There is lack of documentation and I still want to understand, how it works.

Manually I can draw an "arbitrary" line segment and it will be colored with blue showing me its degrees of freedom. Then I can fix one end to some external vertex, add dimension and say it should be parallel to some edge. In a feature script I can calculate the coordinates, but it's sometimes tricky. For example I need several equilateral triangles built on several external edges. Calculating all the coordinates will be quite cumbersome. When I have the edge, I just say it's parallel to one side, and all sides are equal.

More specific questions:
1. Is there a way to create a "blue" sketch element in the FS, for further control it with constraints? Or coordinates are being given to elements are actually "blue" and can be changed on skSolve() ?
2. Is there any documentation on using constraints in FS?
3. May be one can provide a small example of creating an equilateral triangle? Say with vertex at v (vector), one size parallel to a given edge (another vector or query) and side equal to x (number). This actually defines several triangles, but after applying constraints in UI I still can get one of them (still "colored with blue")

Thanks!

Comments

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    All I will say is that in the 6+ years I've been writing custom features, I've only ever once used skConstraint and that was for a tricky arc tangent to 2 splines and an arc (in the Spur Gear feature). The rest of the time it can be calculated manually.
    Senior Director, Technical Services, EMEAI
  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    Sketching done in the Sketching tab is heavily abstracted when compared to the FeatureScript library. Although the Onshape sketcher does internally use the FeatureScript library functions, there is a huge amount of business logic which sits between the sketcher UI and the actual FeatureScript implementations. For this reason, many sketcher techniques are not practical or applicable to FeatureScript sketches.

    In particular, sketching in FeatureScript is always fixed. Once a sketch entity is created in FeatureScript, it basically can't be moved or interacted with again. For this reason, it's strongly recommended to use trigonometry and other methods to compute the locations of sketch entities exactly, create them once, and then use them from there. Although this can complicate modeling in some cases (like drawing a tangent line between two circles), abstractions like these do enable a degree of performance and fine grained control that a constraint based workflow lacks. For this reason, sketch constraints aren't documented in the standard library.

    If you want to make an equilateral triangle, you can do so using this function I wrote:
    /**<br> * A heuristic-based equilateral triangle function.<br> * @param sketchId : @autocomplete `"triangle"`<br> * @param definition {{<br> *          @field baseWidth {ValueWithUnits} : @autocomplete `2 * millimeter`<br> * }}<br> */<br>export function skTriangle(sketch is Sketch, sketchId is string, definition is map)<br>precondition<br>{<br>    definition.baseWidth is ValueWithUnits;<br>}<br>{<br>    const topPoint = vector(0, 1) * definition.baseWidth;<br>    const leftPoint = vector(-0.866, -0.5) * definition.baseWidth;<br>    const rightPoint = vector(0.866, -0.5) * definition.baseWidth;<br>    skLineSegment(sketch, sketchId ~ "leftLine", {<br>                "start" : leftPoint,<br>                "end" : topPoint<br>            });<br>    skLineSegment(sketch, sketchId ~ "rightLine", {<br>                "start" : rightPoint,<br>                "end" : topPoint<br>            });<br>    skLineSegment(sketch, sketchId ~ "bottomLine", {<br>                "start" : leftPoint,<br>                "end" : rightPoint<br>            });<br>}
    You can also freely make adjustments to make it better suit your purpose, like adding an offset as an argument so you can create it in arbitrary positions relative to a sketch.



    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • alan_baljeualan_baljeu Member, User Group Leader Posts: 111 ✭✭
    This function /** * A heuristic-based equilateral triangle function. * @param sketchId : @autocomplete `"triangle"` * @param definition {{ * @field baseWidth {ValueWithUnits} : @autocomplete `2 * millimeter` * }} */ export function skTriangle(sketch is Sketch, sketchId is string, definition is map) precondition { definition.baseWidth is ValueWithUnits; } { const topPoint = vector(0, 1) * definition.baseWidth; const leftPoint = vector(-0.866, -0.5) * definition.baseWidth; const rightPoint = vector(0.866, -0.5) * definition.baseWidth; skLineSegment(sketch, sketchId ~ "leftLine", { "start" : leftPoint, "end" : topPoint }); skLineSegment(sketch, sketchId ~ "rightLine", { "start" : rightPoint, "end" : topPoint }); skLineSegment(sketch, sketchId ~ "bottomLine", { "start" : leftPoint, "end" : rightPoint }); } (I don’t understand why it must look funny)
    Creating knowledge-driven design automation software, for molds, etc.
    www.virtualmold.com

  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    Yeah, it formatted kinda weird. If you really need a sketch triangle function, you can also find it here:

    ...And now that I've thought about it a little bit more, you could also just use skRegularPolygon with sides set to 3. So maybe go use that instead. But the point is, doing a little 2D trig is usually a lot faster, easier, and more performant than trying to figure out robust constraints.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • GregBrownGregBrown Member, Onshape Employees, csevp Posts: 197
    Personally I just never sketch in FeatureScript, instead I create a Part Studio containing the sketch, use configurations to make it as malleable as I want, import that Part Studio into my custom feature, then use derive/instantiate and align the sketch with a coordinate system. If for some reason the sketch ever changes, I much prefer going back into a Part Studio to edit the sketch as opposed to a bunch of code.  
    This is extremely valuable advice ^^^.  
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    Really good info for sure! Seems like for simple sketches (circles, arcs, polygons etc) the performance load of deriving sketches from another context might not be worth it, but for any kind of complex sketch, this is a great way!
    Evan Reese
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    @Alex_Kempen
    I knew literally none of this! Thanks for sharing.
    Evan Reese
  • aleksei_pomelovaleksei_pomelov Member Posts: 6
    Personally I just never sketch in FeatureScript, instead I create a Part Studio containing the sketch, use configurations to make it as malleable as I want, import that Part Studio into my custom feature, then use derive/instantiate and align the sketch with a coordinate system. If for some reason the sketch ever changes, I much prefer going back into a Part Studio to edit the sketch as opposed to a bunch of code.  
    You've made my day! Thanks a lot. For some reason I forgot about this option and my task is completely solvable via parametrized part to import, configure and just move into a proper place. Even didn't need a custom feature here.
Sign In or Register to comment.