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.
Execute FeatureScript using API and Python
fluvio_lobo_fenoglietto
Member Posts: 36 PRO
Hello,
I am trying to use the Onshape API and Python to evaluate a FeatureScript that I created to process an imported .CSV file.
This is what I have built so far;
Now, when I run the program, I actually just get:
Also, I am currently using "eid" as the element Id of the imported .CSV file (blob). Will this have to be substituted with the "eid" of the FeatureScript?
Can someone provide something close to an example?
Thank you, thank you, thank you!
I am trying to use the Onshape API and Python to evaluate a FeatureScript that I created to process an imported .CSV file.
This is what I have built so far;
# Import Libraries and/or Modules from apikey.client import Client # Set-Up Stacks stacks = { 'cad': 'https://cad.onshape.com' } # Create Instance for Onshape Stack c = Client(stack=stacks['cad'], logging=True) # Generate New Onshape Document new_doc = c.new_document(name="input",public=True).json() did = new_doc['id'] wid = new_doc['defaultWorkspace']['id'] # Get Document Information details = c.get_document(did) print 'Document name: ' + details.json()['name'] # Import Dataset input_file_path = "data/demo_rand_array.csv" input_file = c.upload_blob(did, wid, filepath=input_file_path) eid = details.json()['id']<br>In short:
- Created a new document
- Uploaded an external file (blob)
details = c.evaluate_featurescript(did, wid, eid, script_name='import_data')Where the function evaluate_featurescript() was build on the apikey.client file;
# Evaluate FeatureScript # # The following function evaluates or executes a specific FeatureScript within the context of a part studio def evaluate_featurescript(self, did, wvm, eid, script_name='0'): req_headers = { 'Content-Type': 'application/json' } payload = { 'script': script_name } return self._api.request('get', '/api/partstudios/d/' + did + '/[wvm]/' + wvm + '/e/' + eid + '/featurescript', headers=req_headers, body=payload)I built this following the structure of the other functions, but I did notice some differences.
Now, when I run the program, I actually just get:
request failed, details: {
"moreInfoURL" : "",
"code" : 0,
"message" : "Not found.",
"status" : 404
}
So my guess is that I need to import the FeatureScript to my document, but all the API functions seem to be oriented towards creating one. Do I have to create it every time?"moreInfoURL" : "",
"code" : 0,
"message" : "Not found.",
"status" : 404
}
Also, I am currently using "eid" as the element Id of the imported .CSV file (blob). Will this have to be substituted with the "eid" of the FeatureScript?
Can someone provide something close to an example?
Thank you, thank you, thank you!
Tagged:
0
Comments
First, to address your question about eid, this is intended to be an elementId, which is expected to be the elementId of a Part Studio element in the document that you want to access.
Next, the URL you are constructing is using '[wvm]' as a literal. It is intended to mean "choose one of w or v or m". The wvm argument must then agree with that (i.e. must be a workspaceId or versionId or microversionId). This is why you are getting a 404 response.
Additionally, the URL that it appears you are trying to call is a POST API only, so once you get the URL to be correct, the GET will result in a 405 response.
Also, the POST body has a special form to it, and your input isn't shown here, but I suspect it's not in the right form. You can see details on what it should look like in the developer portal help topic titled "Feature list API".
Perhaps the most important point to make is that this API is used to execute a literal FeatureScript function that executes in a transient context (i.e. no side effects), so it likely won't do what you are hoping for, unless you are just looking for it to return some results to you. It will not modify the document.
If you have a Feature that you want to exercise, you need to create a FeatureStudio from it, either by manually uploading it or by calling the Create Feature Studio API. If the FeatureScript is static, you can create a feature studio and create a version of the document containing it and then reference it from other documents. To do so, you would create a feature in a part studio (perhaps using the Part Studio Add Feature API). Be sure to see the section of the Feature list API help section titled Custom Features to understand how to set up the inter-document references.
First of all, thank you for the detailed response. It will take me some time to go over everything but I did not want to wait to ask about the issue you brought up.
I have built a FeatureScript that reads data from a .CSV file to generate sketches and loft these as faces. The code does not require any interaction other than gathering the data from the .CSV that I have successfully uploaded using my remote python script. Here is the code, if it helps;
Essentially, the only thing I want is to execute this script. I don't need to manipulate the program nor take additional input from the user. Is this considered modifying the document?
I am a little lost on your last paragraph, but perhaps this bit of additional information would help clarify my application. I am not sure what is the difference between a static/dynamic FeatureScript. My script does not change nor I plan to edit it. The only thing that will ever change dynamically (on every execution) would be the input .CSV file that gets imported.
Let me know if I can provide more information,
Mark,
Once again, thank you for the prompt reply. I think I now understand, but I have two interpretations of your answer.
Just a few more questions;
- When you say add a feature do you mean adding a FeatureScript script/code to the part studio? - I am a little confuse with the wording here. Or, do you mean adding something like an extrusion?
- When you say the effects do not persist, do you mean that the solid is not created within the part studio? - My goal is to export this solid, so I could technically live with a temporary part as long as it can be exported at the end of the function call.
- Assuming a feature (= extrusion, cut) has to be created, which is not an issue, could this creation also be made through the FeatureScript or the API? - My ultimate goal is to automate the entire process. My main requirement is: NO MANUAL STEPS.
Thank you!What mark is trying to say here is that you cannot use the Evaluate FeatureScript API to create lasting changes to a document. As soon as your REST request completes, the changes you've made are gone. When Mark says "add a feature" he means adding another feature to the feature list of a part studio. This can be anything, an extrude, loft, fillet, or (in your case) an instance of the feature you've created. So what he is saying here is that you should upload your FeatureScript feature as a separate file, do the following:
1) Make an api call to add an instance of your feature to the part studio
2) Make an api call to export your solid
3) (optional - if you want to clean up after yourself) Make an api call to remove the instance of your feature from the part studio.
In terms of the ui, this is the equivalent of clicking on the button for your custom feature, then exporting a solid, then right clicking on your feature in the feature list and deleting it.
Mark also mentions versioning the FeatureScript file you upload. This is important in the context of how custom features work in Onshape. You can upload your feature once in a separate document, then, as long as you version that document, you can create instances of your feature in other documents.
Although your feature looks like it relies on changing the import path for the CSV file, so maybe its better for you to do this document-by-document, calling the Create Feature Studio API and injecting your FeatureScript (with the import changed), and executing using steps 1, 2, and 3 from above.
Jake,
Thanks for the explanation. I just want summarized and confirm. I want to make sure I am looking at the right API calls.
For your 1-2-3 step, would the functions call be:
- Add Feature
- Export Part Studio to STL - I will use STEP later, I am aware I need to use translation for this
- Delete Feature (optional)
In general, I would not even use Execute FeatureScript.If I follow the path on the last paragraph:
- Create Feature Studio - Literal parse the raw text/script
- Add Feature - Adding the FeatureScript that I just created?
- Export Part Studio to STL
- Delete Feature (optional)
If this is correct, my only question is. How does the Add Feature function know the custom feature? Do I have to publish the feature? Is that what you mean with versioning? I have not gone that far with my custom feature (import_data) #N00BThank you for your patience and sorry for the inconvenience,
Take a look at the Feature list API section of the dev-portal help. It provides simple examples of how to add features. Be sure to pay special attention to the section titled "Custom Features". It explains how the namespace field in the json body of your Add Feature call identifies where your custom feature code lives. It an be in another tab in the same document or in some other document.