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: 5
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: 4,228
    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.
    Director, Technical Services, EMEAI
  • Alex_KempenAlex_Kempen Member Posts: 213 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.



  • alan_baljeualan_baljeu Member, User Group Leader Posts: 102 ✭✭
    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: 213 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.
  • GregBrownGregBrown Member, Onshape Employees Posts: 27
    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 ^^^.  
  • Evan_ReeseEvan_Reese Member Posts: 1,357 PRO
    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 / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Alex_KempenAlex_Kempen Member Posts: 213 EDU
    The other alternative to creating sketch entities is to use something like my sketchData module, which can be used to store sketch entities (lines, arcs, and circles) in a data format which can be instantiated later.
    In particular, you can use the Save sketch data feature to print sketches you select into the console:

    You can then copy and paste the saved sketches into a Feature Studio and instantiate them at run time using either createSketchDataArray or skSketchDataArray.

    Example use feature:
    https://cad.onshape.com/documents/4c21d0c3c89c0a81aadfdac6/w/a7ccf556a74ce09cd04151e0/e/5460141960ce7eee86c3d6ea

    This is method is less flexible compared to configuration driven sketches, which make it pretty easy to create sketches which vary with arbitrary input parameters, but it is still very useful for saving static complex profiles which would otherwise be a pain to code into FeatureScript. Plus it's extremely performant, being just as fast as calling the sk functions yourself, while still being very straightforward to update in the event of future changes (as all you need to do is update your sketches, then copy and paste the constants again).

    As a fun side note, this functionality could also be extended pretty easily to allow saving and using arbitrary sketches inside variables. There's even already a function to save sketch entities at run time named extractSketchDataArray. You could also extend the general technique to other types of operations, like extrude and revolve, in order to allow saving more complex types of models (although you would need to also create a system for generating and using historical queries at runtime, which is non-trivial).

    You can find the sketchData use functions here:
    https://cad.onshape.com/documents/4c21d0c3c89c0a81aadfdac6/w/a7ccf556a74ce09cd04151e0/e/aba2e5feac042b470b86d6d1

    You can find the sketchCapture convenience feature here:
    https://cad.onshape.com/documents/4c21d0c3c89c0a81aadfdac6/w/a7ccf556a74ce09cd04151e0/e/523503ee53c9c58beac33b19
  • Evan_ReeseEvan_Reese Member Posts: 1,357 PRO
    @Alex_Kempen
    I knew literally none of this! Thanks for sharing.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • aleksei_pomelovaleksei_pomelov Member Posts: 5
    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.