Skip to content

Commit b7d0474

Browse files
authored
Merge pull request #77 from gramaziokohler/copilot/unify-proto-definition-files
Refactor proto definitions into geometry, datastructures, and message files
2 parents bd1d301 + b3d1c97 commit b7d0474

72 files changed

Lines changed: 1268 additions & 2409 deletions

Some content is hidden

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

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
* Refactored proto definitions from 31 separate files into three organized files (`geometry.proto`, `datastructures.proto`, `message.proto`) to better reflect COMPAS package structure.
15+
1416
### Removed
1517

1618

‎docs/missing_compas_type.md‎

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,80 @@ This guide explains how to extend compas_pb with support for more COMPAS types.
77

88
## Creating New Serializers
99

10-
### 1. Create a protobuf definition for the type you wish to add (for example `Circle`)
10+
### 1. Add a protobuf definition for the type you wish to add
1111

12-
Create a protobuf definition for the new type you wish to add. This should be placed in `IDL/compas_pb/generated/`.
12+
Add a protobuf message definition for the new type to the appropriate proto file:
1313

14-
```protobuf title="IDL/compas_pb/generated/custom_point.proto"
15-
syntax = "proto3";
14+
- **`geometry.proto`** - For geometry primitives (points, vectors, curves, surfaces, transformations)
15+
- **`datastructures.proto`** - For COMPAS datastructures (mesh, polyhedron)
16+
- **`message.proto`** - For serialization framework types (internal use only)
1617

17-
package compas_pb.data;
18+
For example, to add a custom geometry type, add it to `src/compas_pb/protobuf_defs/compas_pb/generated/geometry.proto`:
1819

19-
import "compas_pb/generated/frame.proto";
20+
```protobuf title="src/compas_pb/protobuf_defs/compas_pb/generated/geometry.proto"
21+
// Add to the appropriate section in geometry.proto
2022
21-
message CircleData {
23+
message CustomShapeData {
2224
string guid = 1;
2325
string name = 2;
2426
float radius = 3;
2527
FrameData frame = 4;
2628
}
2729
```
2830

29-
### 3. Generate the python code for the new type
31+
Or, for a COMPAS datastructure type, add it to `src/compas_pb/protobuf_defs/compas_pb/generated/datastructures.proto`:
3032

31-
Now run the following command to generate the python code:
33+
```protobuf title="src/compas_pb/protobuf_defs/compas_pb/generated/datastructures.proto"
34+
// Import geometry types if needed
35+
import "compas_pb/generated/geometry.proto";
36+
37+
message CustomDataStructureData {
38+
string guid = 1;
39+
string name = 2;
40+
repeated PointData vertices = 3;
41+
}
42+
```
43+
44+
!!! note
45+
Proto definitions are organized to match COMPAS package structure:
46+
- `geometry.proto` → `compas.geometry`
47+
- `datastructures.proto` → `compas.datastructures`
48+
- `message.proto` → internal serialization framework
49+
50+
### 2. Generate the python code for the updated proto file
51+
52+
Now run the following command to regenerate the python code from the updated proto files:
3253

3354
```bash
34-
invoke protobuf.generate-proto-classes -t python
55+
invoke generate-proto-classes -t python
3556
```
3657

37-
A new module called `circle_pb2` should now be available in `compas_pb/generated/`.
58+
The generated modules (`geometry_pb2`, `datastructures_pb2`, `message_pb2`) in `src/compas_pb/generated/` will now include your new type.
3859

39-
### 4. Create a (de)serializer for the new type
60+
### 3. Create a (de)serializer for the new type
4061

4162
To create a (de)serializer for a custom type, use the `@pb_serializer` and `@pb_deserializer` decorators. These decorators will be used to register the serializer and deserializer with the plugin system.
4263

4364
```python title="compas_pb/conversions.py"
44-
from compas_pb.data import pb_serializer
45-
from compas_pb.data import pb_deserializer
46-
from compas_pb.generated import circle_pb2
65+
from compas_pb.registry import pb_serializer
66+
from compas_pb.registry import pb_deserializer
67+
from compas_pb.generated import geometry_pb2 # or datastructures_pb2
4768

4869

49-
@pb_serializer(Circle)
50-
def my_circle_to_pb(obj: Circle) -> circle_pb2.CircleData:
51-
"""Convert Circle to protobuf message."""
52-
result = circle_pb2.CircleData()
53-
result.guid = obj.guid
70+
@pb_serializer(CustomShape)
71+
def custom_shape_to_pb(obj: CustomShape) -> geometry_pb2.CustomShapeData:
72+
"""Convert CustomShape to protobuf message."""
73+
result = geometry_pb2.CustomShapeData()
74+
result.guid = str(obj.guid)
5475
result.name = obj.name
5576
result.radius = obj.radius
56-
result.frame = frame_to_pb(obj.frame)
77+
result.frame.CopyFrom(frame_to_pb(obj.frame))
5778
return result
5879

59-
@pb_deserializer(circle_pb2.CircleData)
60-
def my_circle_from_pb(proto_data: circle_pb2.CircleData) -> Circle:
61-
"""Convert protobuf message to Circle."""
62-
return Circle(
80+
@pb_deserializer(geometry_pb2.CustomShapeData)
81+
def custom_shape_from_pb(proto_data: geometry_pb2.CustomShapeData) -> CustomShape:
82+
"""Convert protobuf message to CustomShape."""
83+
return CustomShape(
6384
guid=proto_data.guid,
6485
name=proto_data.name,
6586
radius=proto_data.radius,

0 commit comments

Comments
 (0)