Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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_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!
0
Comments
/** * 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)www.virtualmold.com
chadstoltzfus@premiercb.com
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
I knew literally none of this! Thanks for sharing.