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.
Measurement Tool Feature
ethan_keller924
Member, csevp Posts: 42 PRO
I want a way for data to go from OnShape to python in a clearly defined interface. Say I have a sketch with many complex constraints, and some of the lengths are driven by configurations. In the simplest case, say I have a right triangle with the two legs driven by config values. Now I want the length of the hypotenuse in order to do some more calculations. How do I get that through the REST API?
My current approach would be to use a feature called something like "take measurement" and then when I'm looking for a measurement of that part in the REST API, I can read the feature list and reliably pull the measurement requested. Essentially, I want to add the measurement functionality OnShape has in the lower right as a FeatureScript feature, where the input is a collection of entities (like it is within the measure tool) and the output is the measurement - this would be dynamic and smart - volume/mass for bodies, length for lines, etc... I'm not looking for an ideal solution - I realize my suggestion is a bit hacky and would clutter the feature list, but it would provide a reasonable way for a part designer to generate "output" for the REST API to consume. Maybe there is a solution to this problem already proposed?
I've been working on a simple OnShape python connector that I put up on pip so my students have a way to manipulate parts from functions defined in Python.
I saw the featurescript measure distance... should I use that? What if I want to measure other things, such as mass? The builtin measurement tool supports this, so how do I access that?
My current approach would be to use a feature called something like "take measurement" and then when I'm looking for a measurement of that part in the REST API, I can read the feature list and reliably pull the measurement requested. Essentially, I want to add the measurement functionality OnShape has in the lower right as a FeatureScript feature, where the input is a collection of entities (like it is within the measure tool) and the output is the measurement - this would be dynamic and smart - volume/mass for bodies, length for lines, etc... I'm not looking for an ideal solution - I realize my suggestion is a bit hacky and would clutter the feature list, but it would provide a reasonable way for a part designer to generate "output" for the REST API to consume. Maybe there is a solution to this problem already proposed?
I've been working on a simple OnShape python connector that I put up on pip so my students have a way to manipulate parts from functions defined in Python.
I saw the featurescript measure distance... should I use that? What if I want to measure other things, such as mass? The builtin measurement tool supports this, so how do I access that?
Tagged:
0
Comments
For the kinds of measurements you want to take, I'd recommend using our "Evaluate FeatureScript" API endpoint. This endpoint evaluates a given FeatureScript function on a given Part Studio.
There's lots of great uses for this functionality. In your case, I can think of a few approaches to do what you want.
This involves knowing how to grab the right edge with a FeatureScript query (they're documented here). The example above measures the largest straight line in the Part Studio.
In all cases, you'll quickly run into the fact that FeatureScript values have units (documented here). If your library just wants plain numbers (which internally assume some unit), you can simply divide the units out when your function returns:
Evaluate FeatureScript is a really powerful interface into everything happening in Part Studios. Let us know if you get stuck, or if you have further questions!
Most custom features use these functions for very specific purposes and need to use the resulting value (not just print it as a string). I don't know of any generic infoDump() function available. But certainly one could be written!
I was checking out the documentation and I can't find a way to see all variables within a context? Looks like I have to specify the name of the variable I am hunting for. Is there any FeatureScript trick around that?
Also, how do I escape quotes in API calls? I want to send some FS, and I'm using strings in the FS. Looks like I can escape through here.
Lastly, I was looking through the help section, and the part under "Evaluate FeatureScript" has a missing example that it says "is below"
If you'd like that feature added, you can certainly submit an improvement request!
Edit for your new questions:
Escaping quotes in JSON should be correct.
Can you link to the section of help with the missing example?
https://cad.onshape.com/documents/898eec6699d15ec450a438d9/w/2daaa579f5e2dc8da576d85b/e/9c7f060b0a844cb075b45a38
If you fork your favorite measure feature and change its "setVariable" code to something like the above, you'll also save the step of needing two different features to make a measurement and consolidate it.
Depending on your usage, this workflow might be more or less difficult than simply having a common set of known values to look up. YMMV.
The section with the example is labelled "Feature Script evaluation", so you can ctrl-F for that instead.
My measurements document is available here.
The goal of this doc is to have a more robust measurement tool that can take volumes, areas, and complex lengths with a simple UI. If you're interested in helping with it, please collaborate via this issue and I can share the doc with you.
Thanks @ethan_keller924 (and of course @ilya_baran) for taking the time to develop this new Measure tool and sharing. I hope others who are code savvy will expand on this as well.
Step one, encode the configuration in question using the "Encode Configuration" endpoint i.e. /elements/d/:did/e/:eid/configurationencodings (Another win: the format of encodeConfiguration is a little less intense than the changeConfiguration command )
Step two, pass the resulting string as a query parameter to the endpoint you want e.g. /partstudios/d/:did/[wvm]/:wvm/e/:eid/featurescript?configuration=<encodedConfiguration>
There is no step three - the API call just executes at the given configuration, and no actual edits to the Part Studio are made.
If you run into issues, I'd verify that the string you're passing is HTML-encoded once and only once (your REST library likely does that by default but perhaps not).
The implementation of the configurationencodings endpoint may seem very obvious to you the first glance, but be aware that for big configurations we assign a hard-coded id to the specific config you pass (rather than the human-readable string), and we might change how we encode things in the future. So don't try to mimic the endpoint without a real API call.
This is cool! I hadn't realized that the configuration could be passed in as a query parameter! One more thing - is it possible to change the config params of instances within an assembly through the API? I posted about it here more than a month ago but I've heard nothing.
That's me
IR for AS/NZS 1100