Skip to content

Commit

Permalink
Able to use parts of source files and test them (#163)
Browse files Browse the repository at this point in the history
* Able to use parts of source files and test them

* Remove the test file

* Remove trailing new lines

* Correct the double line

* Updating script outputs

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
lf94 and github-actions[bot] authored Jan 10, 2024
1 parent 36a43d1 commit 7163cae
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
- ./tutorials/getting_started
- ./tutorials/modeling_websocket
- ./tutorials/beginner_tutorial
- ./tutorials/websocket_tutorial
- ./samples/convert_file
- ./samples/file_density
- ./samples/file_mass
Expand All @@ -95,6 +96,13 @@ jobs:
- name: Run scripts
shell: bash
run: |
p=${{ matrix.test-path }}
for f in $(find "$p" -name "*.*.*" | sed 's/.*[0-9]*_\(.*\)\.part/\1/' | sort -u | uniq);
do
cd "$p";
cat $(ls *$f.part | sort -g) > "$f";
cd -;
done
FILE=$(find ${{ matrix.test-path }} -name "*.py")
python $FILE
env:
Expand Down
2 changes: 1 addition & 1 deletion tutorials/conversion_obj_step/output.step
Git LFS file not shown
26 changes: 26 additions & 0 deletions tutorials/websocket_tutorial/10_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Search through the websocket messages and find the one that has the snapshot.
png_contents = b""
for message in websocket:
message_dict = message.model_dump()
print(message_dict)
if (
message_dict["resp"]["data"]["modeling_response"]["type"] == "take_snapshot"
):
png_contents = message_dict["resp"]["data"]["modeling_response"][
"data"
]["contents"].get_decoded()
break

# Save the contents to a file.
dir_path = os.path.dirname(os.path.realpath(__file__))
png_path = os.path.join(dir_path, "snapshot.png")
print(png_path)
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)
4 changes: 4 additions & 0 deletions tutorials/websocket_tutorial/1_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import json
import os
import uuid

1 change: 1 addition & 0 deletions tutorials/websocket_tutorial/2_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from kittycad.client import ClientFromEnv
18 changes: 18 additions & 0 deletions tutorials/websocket_tutorial/3_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from kittycad.api.modeling import modeling_commands_ws
from kittycad.models import (
ImageFormat,
ModelingCmd,
ModelingCmdId,
WebSocketRequest,
)
from kittycad.models.path_segment import PathSegment, line
from kittycad.models.modeling_cmd import (
default_camera_look_at,
start_path,
take_snapshot,
extend_path,
move_path_pen,
extrude,
close_path,
)
from kittycad.models.web_socket_request import modeling_cmd_req
16 changes: 16 additions & 0 deletions tutorials/websocket_tutorial/4_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def make_cube():
# Create the client.
client = ClientFromEnv()

# Create a unique id for the sketch path.
sketch_path_id = uuid.uuid4()

# 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:
4 changes: 4 additions & 0 deletions tutorials/websocket_tutorial/5_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Start the Path
websocket.send(WebSocketRequest(
modeling_cmd_req(cmd=ModelingCmd(start_path()), cmd_id=ModelingCmdId(sketch_path_id)),
))
85 changes: 85 additions & 0 deletions tutorials/websocket_tutorial/6_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# continued from previous code snippet.

# Move the pen to the bottom left corner.
websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
move_path_pen(
path=str(sketch_path_id),
to={
"x": -5,
"y": -5,
"z": 0,
}
)
),
cmd_id=ModelingCmdId(uuid.uuid4())))) # Create a new UUID for this command.

# The next three websocket.send blocks are drawing the line to the right, up, and then to the left.
websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
extend_path(
path=str(sketch_path_id),
segment=line(
end={
"x": 10,
"y": 0,
"z": 0,
},
relative=True, # This means that the line is relative to the current position of the pen.
)
)),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))

websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
extend_path(
path=str(sketch_path_id),
segment=line(
end={
"x": 0,
"y": 10,
"z": 0,
},
relative=True,
)
)),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))

websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
extend_path(
path=str(sketch_path_id),
segment=line(
end={
"x": -10,
"y": 0,
"z": 0,
},
relative=True,
)
)),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))

# Now, close the path.

# Close the sketch
websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
close_path(
path_id=ModelingCmdId(sketch_path_id) # Notice that we need to provide the sketch path UUID we created in the beginning.
)
),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))
15 changes: 15 additions & 0 deletions tutorials/websocket_tutorial/7_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# continued from previous code snippet.

# Extrude the sketch.
websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(
extrude(
cap=True,
distance=10,
target=ModelingCmdId(sketch_path_id),
)
),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))
5 changes: 5 additions & 0 deletions tutorials/websocket_tutorial/8_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Get the messages.
while True:
message = websocket.recv()
print(json.dumps(message.model_dump_json(), indent=4, sort_keys=True))
break
21 changes: 21 additions & 0 deletions tutorials/websocket_tutorial/9_sample.py.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# continued from previous code snippet.

# Orient the camera.
websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(default_camera_look_at(
center = {"x": 0, "y": 0, "z": 0},
up = {"x": 0, "y": 0, "z": 1},
vantage = {"x": 20, "y": 20, "z": 20},
)),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))

# Take a snapshot.
websocket.send(WebSocketRequest(
modeling_cmd_req(
cmd=ModelingCmd(take_snapshot(format=ImageFormat.PNG)),
cmd_id=ModelingCmdId(uuid.uuid4())
)
))

0 comments on commit 7163cae

Please sign in to comment.