Skip to content

Commit

Permalink
Move over the rest of files from examples-modeling-api
Browse files Browse the repository at this point in the history
  • Loading branch information
lf94 committed Jan 12, 2024
1 parent 124b8d8 commit 929d376
Show file tree
Hide file tree
Showing 7 changed files with 3,082 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ jobs:
env:
KITTYCAD_TOKEN: ${{secrets.KITTYCAD_API_TOKEN}}

build-test-rs-versions:
runs-on: ubuntu-latest
strategy:
matrix:
test-path:
- ./tutorials/websocket_tutorial/draw_cube_rust

steps:
- uses: actions/checkout@v4
with:
lfs: true
- name: Set up node ${{ matrix.python-version }}
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Run sources
shell: bash
run: |
cd ${{ matrix.test-path }}
cargo run
env:
KITTYCAD_TOKEN: ${{secrets.KITTYCAD_API_TOKEN}}

commit-script-outputs:
# Using the python script to commit the output of the scripts
# The go scripts should have the same output
Expand Down
171 changes: 171 additions & 0 deletions tutorials/import_file/import_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import os
import uuid

from kittycad.client import ClientFromEnv

from kittycad.api.modeling import modeling_commands_ws
from kittycad.models import (
Axis,
AxisDirectionPair,
Direction,
ImageFormat,
ImportFile,
InputFormat,
ModelingCmd,
ModelingCmdId,
System,
UnitLength,
WebSocketRequest,
)
from kittycad.models.input_format import obj
from kittycad.models.modeling_cmd import (
default_camera_focus_on,
import_files,
take_snapshot,
)
from kittycad.models.web_socket_request import modeling_cmd_req

def test_ws_import():
# Create our client.
client = ClientFromEnv()

# Connect to the websocket.
with modeling_commands_ws.WebSocket(
client=client,
fps=30,
unlocked_framerate=False,
video_res_height=360,
video_res_width=480,
webrtc=False,
) as websocket:
# read the content of the file
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
file_name = "dodecahedron.obj"
file = open(os.path.join(dir_path, file_name), "rb")
content = file.read()
file.close()
cmd_id = uuid.uuid4()
ImportFile(data=content, path=file_name)
# form the request
req = WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
import_files(
files=[ImportFile(data=content, path=file_name)],
format=InputFormat(
obj(
units=UnitLength.M,
coords=System(
forward=AxisDirectionPair(
axis=Axis.Y, direction=Direction.NEGATIVE
),
up=AxisDirectionPair(
axis=Axis.Z, direction=Direction.POSITIVE
),
),
)
),
)
),
cmd_id=ModelingCmdId(cmd_id),
)
)
# Import files request must be sent as binary, because the file contents might be binary.
websocket.send_binary(req)

# Get the success message.
object_id = ""
for message in websocket:
message_dict = message.model_dump()
if message_dict["success"] is not True:
raise Exception(message_dict)
elif message_dict["resp"]["type"] != "modeling":
continue
elif (
message_dict["resp"]["data"]["modeling_response"]["type"]
!= "import_files"
):
# We have a modeling command response.
# Make sure its the import files response.
raise Exception(message_dict)
else:
# Okay we have the import files response.
# Break since now we know it was a success.
object_id = str(
message_dict["resp"]["data"]["modeling_response"]["data"][
"object_id"
]
)
break

# Now we want to focus on the object.
cmd_id = uuid.uuid4()
# form the request
req = WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(default_camera_focus_on(uuid=object_id)),
cmd_id=ModelingCmdId(cmd_id),
)
)
websocket.send(req)

# Get the success message.
for message in websocket:
message_dict = message.model_dump()
if message_dict["success"] is not True:
raise Exception(message_dict)
elif message_dict["resp"]["type"] != "modeling":
continue
elif message_dict["request_id"] == str(cmd_id):
# We got a success response for our cmd.
break
else:
raise Exception(message_dict)

# Now we want to snapshot as a png.
cmd_id = uuid.uuid4()
# form the request
req = WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(take_snapshot(format=ImageFormat.PNG)),
cmd_id=ModelingCmdId(cmd_id),
)
)
websocket.send(req)

# Get the success message.
png_contents = b""
for message in websocket:
message_dict = message.model_dump()
if message_dict["success"] is not True:
raise Exception(message_dict)
elif message_dict["resp"]["type"] != "modeling":
continue
elif (
message_dict["resp"]["data"]["modeling_response"]["type"]
!= "take_snapshot"
):
# Make sure its the correct response.
raise Exception(message_dict)
else:
print(message_dict)
# Okay we have the snapshot response.
# Break since now we know it was a success.
png_contents = message_dict["resp"]["data"]["modeling_response"][
"data"
]["contents"].get_decoded()
break

# Save the contents to a file.
png_path = os.path.join(dir_path, "snapshot.png")
with open(png_path, "wb") as f:
f.write(png_contents)

# Ensure the file is not empty.
assert len(png_contents) > 0

# Ensure the file exists.
assert os.path.exists(png_path)

test_ws_import()
1 change: 1 addition & 0 deletions tutorials/websocket_tutorial/draw_cube_rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading

0 comments on commit 929d376

Please sign in to comment.