From 05e516dbba1f2b859214f85ee22b42a9eb8f0857 Mon Sep 17 00:00:00 2001 From: ethan92429 Date: Mon, 20 Aug 2018 10:00:26 -0400 Subject: [PATCH] added copy workspace to client, made documentation video responsive and made assembly insert work --- docs/_static/css/youtube.css | 16 ++++++++++++++++ docs/_templates/layout.html | 2 ++ docs/conf.py | 5 ++++- docs/index.rst | 10 +++++----- onshapepy/__init__.py | 10 ++++------ onshapepy/assembly.py | 11 +++++++---- onshapepy/core/client.py | 22 +++++++++++++++++++++- setup.py | 2 +- tests/conftest.py | 4 ++++ tests/test_assembly.py | 6 +++--- tests/test_core.py | 4 ++++ 11 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 docs/_static/css/youtube.css create mode 100644 docs/_templates/layout.html diff --git a/docs/_static/css/youtube.css b/docs/_static/css/youtube.css new file mode 100644 index 0000000..91f69ca --- /dev/null +++ b/docs/_static/css/youtube.css @@ -0,0 +1,16 @@ +/* Youtube Reflexive */ +.vid-container { + position: relative; + padding-bottom: 50%; + padding-top: 35px; height: 0; overflow: hidden; +} + +.vid-container iframe, +.vid-container object, +.vid-container embed { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; +} diff --git a/docs/_templates/layout.html b/docs/_templates/layout.html new file mode 100644 index 0000000..d81ef9f --- /dev/null +++ b/docs/_templates/layout.html @@ -0,0 +1,2 @@ +{% extends "!layout.html" %} +{% set css_files = css_files + [ "_static/css/youtube.css" ] %} \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 589ce16..bedf1c7 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -174,4 +174,7 @@ # -- Options for todo extension ---------------------------------------------- # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True \ No newline at end of file +todo_include_todos = True + +def setup(app): + app.add_stylesheet("css/youtube.css") diff --git a/docs/index.rst b/docs/index.rst index 69e72f7..390d319 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,4 +1,4 @@ -Welcome to onshapepy's documentation! +Welcome to the OnShapePy Documentation! ============================================= OnShapePy gives you the ability to control OnShape configurable parts with Python, effectively unlocking the vast @@ -7,11 +7,11 @@ Python scientific libraries to OnShape parts. Quick video demo ------------------ -See how to connect to a PartStudio and adjust parameters from python below! - .. raw:: html - +
+ +
Straight to the code --------------------- @@ -26,7 +26,7 @@ First specify your credentials in a file named: "~/.onshapepy". Get your access **Note: For more complex configurations, see :ref:`OnShapePy Configuration `.** -Then follow along with the code below: +Then follow along with the Python code below: .. code-block:: Python diff --git a/onshapepy/__init__.py b/onshapepy/__init__.py index 130e305..a5daddf 100644 --- a/onshapepy/__init__.py +++ b/onshapepy/__init__.py @@ -1,6 +1,4 @@ -''' -onshapepy -========= - -Demonstrates usage of API keys for the Onshape REST API -''' +from onshapepy.part import Part +from onshapepy.core.client import Client +import pint +u=pint.UnitRegistry() \ No newline at end of file diff --git a/onshapepy/assembly.py b/onshapepy/assembly.py index 8d87778..7cd62d0 100644 --- a/onshapepy/assembly.py +++ b/onshapepy/assembly.py @@ -3,6 +3,7 @@ """ from onshapepy.uri import Uri +from onshapepy.core.context import Context import json @@ -10,7 +11,7 @@ class Assembly: def __init__(self, url): - """ Connect to an assembly that points to the assembly specified with did, wid and eid. + """ Connect to an assembly that points to the assembly specified with the url. Args: - url (str): The url of the onshape item @@ -18,15 +19,17 @@ def __init__(self, url): self.uri = Uri(url) - def insert(self, part, client): + def insert(self, part): """ Insert a part into this assembly. Args: - part (onshapepy.part.Part) A Part instance that will be inserted. - - client (onshapepy.core.client.Client) A Client instance that does the inserting. Returns: - requests.Response: Onshape response data """ - pass \ No newline at end of file + params = {k: str(v) for k,v in part.params.items()} + c = Context().client + res=c.create_assembly_instance(self.uri.as_dict(), part.uri.as_dict(), params) + return res \ No newline at end of file diff --git a/onshapepy/core/client.py b/onshapepy/core/client.py index 2581266..ed1bd57 100644 --- a/onshapepy/core/client.py +++ b/onshapepy/core/client.py @@ -154,6 +154,25 @@ def list_documents(self): return self._api.request('get', '/api/documents') + def copy_workspace(self, uri, new_name): + ''' + Copy the current workspace. + + Args: + - uri (dict): the uri of the workspace being copied. Needs to have a did and wid key. + - new_name (str): the new name of the copied workspace. + + Returns: + - requests.Response: Onshape response data + ''' + + payload = { + 'isPublic': True, + 'newName': new_name + } + + return self._api.request('post', '/api/documents/' + uri['did'] + '/workspaces/' + uri['wvm'] + '/copy', body=payload) + def create_assembly(self, did, wid, name='My Assembly'): ''' Creates a new assembly element in the specified document / workspace. @@ -284,7 +303,8 @@ def create_assembly_instance(self, assembly_uri, part_uri, configuration): "isWholePartStudio": True, "configuration": self.encode_configuration(part_uri["did"], part_uri["eid"], configuration) } - return self._api.request('post', '/api/assemblies/d/' + assembly_uri["did"] + '/w/' + assembly_uri["wvm"] + '/e/' + assembly_uri["eid"] + '/instances', body=payload) + return self._api.request('post', '/api/assemblies/d/' + assembly_uri["did"] + '/' + assembly_uri["wvm_type"] + + '/' + assembly_uri["wvm"] + '/e/' + assembly_uri["eid"] + '/instances', body=payload) def encode_configuration(self, did, eid, parameters): ''' diff --git a/setup.py b/setup.py index e279e2b..c35e5d6 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name= 'onshapepy', - version= '0.0.13', + version= '0.0.14', description= 'Drive part configurations from Python.', long_description= long_description, author= 'Ethan Keller', diff --git a/tests/conftest.py b/tests/conftest.py index 58a7b50..b3c95f8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,10 @@ def cube(): return Part("https://cad.onshape.com/documents/2d47b6abec9d1de1d2538372/w/39e483948767f72c97d2792f/e/0639ea3c439aa0947744d29a") +@pytest.fixture +def versioned_cube(): + return Part("https://cad.onshape.com/documents/2d47b6abec9d1de1d2538372/v/460c8d693cb4140726eada1a/e/0639ea3c439aa0947744d29a") + @pytest.fixture def assembly(): return Assembly("https://cad.onshape.com/documents/8ec353ba00f37f447b5a61f5/w/04c36c786829759832bd3d1a/e/6f1f9c485ff1e639f4db63c0") diff --git a/tests/test_assembly.py b/tests/test_assembly.py index 588b593..35596c2 100644 --- a/tests/test_assembly.py +++ b/tests/test_assembly.py @@ -1,6 +1,6 @@ -from onshapepy.core.client import Client -def test_insert(cube, assembly): + +def test_insert(versioned_cube, assembly): # Insert into the assembly - assembly.insert(cube, Client()) + assembly.insert(versioned_cube) diff --git a/tests/test_core.py b/tests/test_core.py index f230694..979bc22 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -11,6 +11,10 @@ def test_new_doc(client): new_doc = client.new_document(public=True).json() assert new_doc['name'] == 'Test Document' +def test_copy_workspace(client): + res = client.copy_workspace(uri.as_dict(), "Test 2") + assert res.status_code == 200 + def test_encode_configuration(client): s = client.encode_configuration(uri.did, uri.eid, {"Length": "5 meters", "height": "2 meters"}) assert s == 'Length=5+meters;height=2+meters'