Skip to content

Commit f86415a

Browse files
committed
Fixed merge issues, added workspace support to link versions
2 parents a19073b + 0c9d3d0 commit f86415a

File tree

108 files changed

+2950
-1352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2950
-1352
lines changed

.gcloudignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# Re-add app.yaml
55
!app.yaml
6+
# Re-add dist
7+
!backend/dist/
68

79
.vscode/
810
backend_tools/

.vscode/extensions.json

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"streetsidesoftware.code-spell-checker",
44
"ms-python.python",
55
"ms-python.black-formatter",
6-
"GoogleCloudTools.cloudcode",
76
"esbenp.prettier-vscode",
87
"dbaeumer.vscode-eslint"
98
]

README.md

+111-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ A monorepo hosting python scripts for updating and maintaining my Robot FeatureS
55
This repo is set up to work directly with my FeatureScript backend document which can be found here:
66
[Alex's FeatureScript Backend](https://cad.onshape.com/documents/00dd11dabe44da2db458f898/w/6c20cd994b174cc99668701f)
77

8+
Note: This repo is equipped to run with VSCode on Linux (specifically, WSL Ubuntu).
9+
10+
# Repo Setup
11+
12+
First, create a new file in the root of this project named `.env` and add the following contents:
13+
14+
```
15+
# Server config
16+
API_LOGGING=true # Enable or disable logging
17+
API_BASE_PATH=https://cad.onshape.com # Use a different base path
18+
API_VERSION=10 # Use a different version of the API
19+
20+
# API Keys
21+
API_ACCESS_KEY=<Your API Access Key>
22+
API_SECRET_KEY=<Your API Secret Key>
23+
24+
# OAuth
25+
OAUTH_CLIENT_ID=<Your OAuth client id>
26+
OAUTH_CLIENT_SECRET=<Your OAuth client secret>
27+
SESSION_SECRET=literallyAnythingWillDo
28+
29+
NODE_ENV=development
30+
FIRESTORE_EMULATOR_HOST=127.0.0.1:8080
31+
```
32+
833
# Onshape API
934

1035
A generic library for connecting with and using the Onshape API.
@@ -16,23 +41,12 @@ They are explained in more detail below.
1641

1742
Besides the environment variables required for each API variant, you can also set the variables:
1843

19-
```
20-
API_LOGGING=true # Enable logging
21-
API_BASE_PATH=https://cad.onshape.com # Use a different base path
22-
API_VERSION=6 # Use a different version of the API
23-
```
24-
2544
## Key API
2645

2746
This allows you to use the Onshape API with API keys.
2847

2948
1. Get an API key from the [Onshape developer portal](https://dev-portal.onshape.com/keys).
30-
1. Add your access key and secret key to `.env`:
31-
32-
```
33-
API_ACCESS_KEY=<Your access key>
34-
API_SECRET_KEY=<Your secret key>
35-
```
49+
1. Add your access key and secret key to `.env`.
3650

3751
You can then call `make_key_api()` to get an `Api` instance you can pass to endpoints or invoke directly.
3852

@@ -62,42 +76,119 @@ Install `pipx`:
6276
```
6377
sudo apt install pipx
6478
pipx ensurepath
79+
```
80+
81+
The use `pipx` to install poetry and install the project:
82+
83+
```
6584
pipx install poetry
85+
poetry install
86+
```
87+
88+
## Scripts
89+
90+
Several scripts for pulling code from Onshape and pushing new versions of Robot FeatureScripts are included in `scripts`. Scripts can be invoked as follows:
91+
92+
```
93+
./scripts/onshape.sh
6694
```
6795

96+
The following scripts are available:
97+
98+
- deploy - Deploys the Robot manager app to google cloud.
99+
- onshape - Can be used to push and pull code from Onshape via the API.
100+
- robot - Can be used to release new versions of Robot FeatureScripts.
101+
68102
# Robot Manager
69103

70104
The Robot manager app lives in the `frontend` and `backend` folders. The app uses Python and flask as the backend and API and Vite and React for the frontend. The app is deployed using Google Cloud App Engine, with Google Cloud Firestore as the database.
71105

72-
## Robot Manager Setup
106+
## Onshape App Setup
107+
108+
To test Onshape app changes, you will need to create an OAuth application in the [Onshape Developer Portal](https://cad.onshape.com/appstore/dev-portal/oauthApps). Fill out the following information:
109+
110+
- Name: (Arbitrary) Robot Manager Test
111+
- Primary format: (Arbitrary) com.robot-manager-test
112+
- Redirect URLs: `https://localhost:3000/redirect`
113+
- OAuth URL: `https://localhost:3000/sign-in`
114+
- Uncheck "Application can request purchases on your behalf".
115+
116+
Click save, then copy your OAuth app's OAuth client identifier and OAuth client secret and add them to your `.env` file.
117+
118+
Next, add the necessary Extensions to your OAuth application so you can see it in documents you open:
119+
120+
1. Open your OAuth application in the [Onshape Developer Portal](https://cad.onshape.com/appstore/dev-portal/oauthApps).
121+
2. Go to the Extensions tab.
122+
3. Create two extensions with the following properties:
123+
- Name: Robot manager test
124+
- Location: Element right panel
125+
- Context: Inside assembly/Inside part studio
126+
- Action URL:
127+
`https://localhost:3000/app?elementType=ASSEMBLY&documentId={$documentId}&instanceType={$workspaceOrVersion}&instanceId={$workspaceOrVersionId}&elementId={$elementId}`
128+
`https://localhost:3000/app?elementType=PART_STUDIO&documentId={$documentId}&instanceType={$workspaceOrVersion}&instanceId={$workspaceOrVersionId}&elementId={$elementId}`
73129

74-
You'll need to create a store entry in Onshape and generate secure certificates for the python dev server so Onshape will connect with it in your dev environment.
130+
### Flask Credentials Setup
75131

76-
Use the `Launch servers` task to launch the dev servers necessary to view the app. You should set up Onshape to connect to:
132+
Onshape requires all apps, even temporary test apps, to use https. This creates a headache for local development.
133+
In order to solve this issue, you'll need to generate a certificate and add it to a folder named `credentials` in the root of this project:
77134

78135
```
79-
https://localhost:3000/app
136+
/credentials/cert.pem
137+
/credentials/key.pem
138+
```
139+
140+
This can be done automatically by running the script `make_credentials.sh`:
141+
142+
```
143+
./scripts/make_credentials.sh
144+
```
145+
146+
You'll then need to add a security exception to your browser to avoid getting blocked by a security exception.
147+
On Firefox, the procedure is:
148+
149+
1. Start the development servers using the `Launch servers` VSCode task.
150+
2. Open Firefox and go to `Settings > Certificates > View Certificates... > Servers > Add Exception...`
151+
3. Enter `https://localhost:3000` as the Location and click `Get Certificate`.
152+
4. Check `Permanently store this exception` and then click `Confirm Security Exception`.
153+
154+
## Frontend Setup
155+
156+
Install Node.js, then use npm to install the dependencies in `frontend`:
157+
158+
```
159+
cd frontend
160+
npm install
80161
```
81162

82163
## Google Cloud Dev Setup
83164

84165
To emulate the google cloud database locally, you'll need to install the [Google Cloud CLI](https://cloud.google.com/sdk/docs/install#deb).
85166

167+
You should also install the firestore emulator and a Java JRE:
168+
169+
```
170+
sudo apt install google-cloud-cli-firestore-emulator default-jre
171+
```
172+
86173
Then start up the google cloud emulator:
87174

88175
```
89176
gcloud emulators firestore start
90177
```
91178

92-
To enable python to connect to the emulator, add the variable `FIRESTORE_EMULATOR_HOST` to `.env` with the value `127.0.0.1:8080`.
93-
94179
<!-- Then restart the distro. This prevents google cloud from using the google cloud version located outside of WSL. -->
95180

96181
Note: this project uses Google Cloud Firestore as it's database. This is not to be confused with Google Firebase's Firestore, as Firebase is a separate project from Google Cloud.
97182

98-
## Deploying in Google Cloud
183+
## Development Servers
184+
185+
Run the `Launch servers` VSCode task to launch the dev servers necessary to view and test the app.
186+
If everything is setup properly, you should see all three servers start successfully.
187+
You should also be able to launch Robot Manager from the right panel of any Onshape Part Studio or Assembly and see the Robot manager UI appear.
188+
189+
# Deploying in Google Cloud
99190

100-
The app can be deployed using the google cloud CLI - `gcloud app deploy`.
191+
The app can be deployed by running the script `./scripts/deploy.sh`.
101192

102193
Some notes:
103194

backend/api.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
import flask
33

44
import onshape_api
5-
from onshape_api import endpoints
65

7-
from backend.common import backend_exceptions, connect
6+
from backend.common import backend_exceptions, connect, database
87
from backend.endpoints import (
98
assembly_mirror,
9+
copy_design,
1010
generate_assembly,
1111
linked_documents,
1212
references,
1313
)
14+
from onshape_api.endpoints import documents, versions
15+
from onshape_api.endpoints.documents import ElementType
1416

1517

1618
router = flask.Blueprint("api", __name__, url_prefix="/api", static_folder="dist")
@@ -35,6 +37,7 @@ def user_exception(e: backend_exceptions.UserException):
3537
router.register_blueprint(assembly_mirror.router)
3638
router.register_blueprint(linked_documents.router)
3739
router.register_blueprint(references.router)
40+
router.register_blueprint(copy_design.router)
3841

3942

4043
# @app.post("/auto-assembly")
@@ -63,20 +66,21 @@ def default_name(element_type: str, **kwargs):
6366
Route Args:
6467
element_type: The type of element to fetch. One of part-studio, assembly, or version.
6568
"""
66-
api = connect.get_api()
67-
document_path = connect.get_instance_path("wv")
69+
db = database.Database()
70+
api = connect.get_api(db)
71+
document_path = connect.get_route_instance_path("wv")
6872
if element_type == "version":
69-
version_list = endpoints.get_versions(api, document_path)
73+
version_list = versions.get_versions(api, document_path)
7074
# len(versions) is correct due to Start version
7175
return {"name": "V{}".format(len(version_list))}
7276
elif element_type == "assembly":
73-
assemblies = endpoints.get_document_elements(
74-
api, document_path, element_type=endpoints.ElementType.ASSEMBLY
77+
assemblies = documents.get_document_elements(
78+
api, document_path, element_type=ElementType.ASSEMBLY
7579
)
7680
return {"name": "Assembly {}".format(len(assemblies) + 1)}
7781
elif element_type == "part-studio":
78-
part_studios = endpoints.get_document_elements(
79-
api, document_path, element_type=endpoints.ElementType.PART_STUDIO
82+
part_studios = documents.get_document_elements(
83+
api, document_path, element_type=ElementType.PART_STUDIO
8084
)
8185
return {"name": "Part Studio {}".format(len(part_studios) + 1)}
8286

backend/common/assembly_data.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
from typing import Any, Iterable
33
import onshape_api
4-
from onshape_api import endpoints
4+
from onshape_api.endpoints.assemblies import get_assembly, get_assembly_features
55

66

77
class Assembly:
@@ -86,7 +86,7 @@ def resolve_path(self, instance_or_part: dict) -> onshape_api.ElementPath:
8686

8787

8888
def assembly(api: onshape_api.Api, assembly_path: onshape_api.ElementPath) -> Assembly:
89-
assembly_data = endpoints.get_assembly(
89+
assembly_data = get_assembly(
9090
api, assembly_path, include_mate_features=True, include_mate_connectors=True
9191
)
9292
return Assembly(assembly_data, assembly_path)
@@ -140,5 +140,5 @@ def get_parameter(feature: dict, parameter_id: str) -> Any:
140140
def assembly_features(
141141
api: onshape_api.Api, assembly_path: onshape_api.ElementPath
142142
) -> AssemblyFeatures:
143-
assembly_data = endpoints.get_assembly_features(api, assembly_path)
143+
assembly_data = get_assembly_features(api, assembly_path)
144144
return AssemblyFeatures(assembly_data)

0 commit comments

Comments
 (0)