-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_rpc.py
More file actions
195 lines (156 loc) · 5.98 KB
/
demo_rpc.py
File metadata and controls
195 lines (156 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
"""
Demonstration script for the FreeCAD RPC server/client.
This script demonstrates how to use the RPC client to communicate with
a FreeCAD instance running the RPC server.
Prerequisites:
1. Start FreeCAD
2. In FreeCAD's Python console, run:
>>> import sys
>>> sys.path.insert(0, '/path/to/morphe')
>>> from morphe.adapters.freecad import start_server
>>> start_server()
3. Then run this script from the command line:
$ python demo_rpc.py
Usage:
python demo_rpc.py # Show status and list sketches
python demo_rpc.py --import-demo # Import the demo mounting bracket
python demo_rpc.py --export NAME # Export sketch NAME to JSON
python demo_rpc.py --roundtrip NAME # Export and re-import sketch NAME
"""
import argparse
import json
import sys
from pathlib import Path
from morphe.adapters.freecad import FreeCADClient, check_server
def print_status(client: FreeCADClient) -> None:
"""Print server and FreeCAD status."""
status = client.get_status()
print("\n=== FreeCAD RPC Server Status ===")
print(f"Server version: {status['server_version']}")
print(f"FreeCAD version: {status.get('freecad_version', 'N/A')}")
print(f"Active document: {status.get('active_document', 'None')}")
print(f"Sketch count: {status.get('sketch_count', 0)}")
def list_sketches(client: FreeCADClient) -> None:
"""List all sketches in FreeCAD."""
sketches = client.list_sketches()
print("\n=== Sketches ===")
if not sketches:
print("No sketches in active document")
return
for sketch in sketches:
print(f" {sketch['name']} ({sketch['label']})")
print(f" Geometry: {sketch['geometry_count']}")
print(f" Constraints: {sketch['constraint_count']}")
def import_demo(client: FreeCADClient) -> None:
"""Import the demo mounting bracket sketch."""
# Import the demo module to create the sketch
from demo import create_mounting_bracket
print("\n=== Importing Demo Sketch ===")
doc = create_mounting_bracket()
print(f"Created: {doc.name}")
print(f" Primitives: {len(doc.primitives)}")
print(f" Constraints: {len(doc.constraints)}")
# Import into FreeCAD
sketch_name = client.import_sketch(doc)
print(f"\nImported as: {sketch_name}")
# Get solver status
status, dof = client.get_solver_status(sketch_name)
print(f"Solver status: {status}, DOF: {dof}")
# Open in Sketcher
print("\nOpening in Sketcher workbench...")
client.open_sketch(sketch_name)
def export_sketch(client: FreeCADClient, sketch_name: str) -> None:
"""Export a sketch to JSON."""
print(f"\n=== Exporting '{sketch_name}' ===")
doc = client.export_sketch(sketch_name)
print(f"Exported: {doc.name}")
print(f" Primitives: {len(doc.primitives)}")
print(f" Constraints: {len(doc.constraints)}")
# Save to file
from core import sketch_to_json
output_file = Path(f"{sketch_name}.json")
json_str = sketch_to_json(doc)
with open(output_file, "w") as f:
json.dump(json.loads(json_str), f, indent=2)
print(f"\nSaved to: {output_file}")
def roundtrip_sketch(client: FreeCADClient, sketch_name: str) -> None:
"""Export and re-import a sketch to test roundtrip."""
print(f"\n=== Roundtrip Test: '{sketch_name}' ===")
# Export
doc = client.export_sketch(sketch_name)
print(f"Exported: {len(doc.primitives)} primitives, {len(doc.constraints)} constraints")
# Re-import with new name
new_name = f"{sketch_name}_roundtrip"
created_name = client.import_sketch(doc, name=new_name)
print(f"Re-imported as: {created_name}")
# Compare solver status
orig_status, orig_dof = client.get_solver_status(sketch_name)
new_status, new_dof = client.get_solver_status(created_name)
print(f"\nOriginal: {orig_status}, DOF: {orig_dof}")
print(f"Roundtrip: {new_status}, DOF: {new_dof}")
if orig_status == new_status and orig_dof == new_dof:
print("\nRoundtrip successful - solver status matches!")
else:
print("\nWarning: Solver status differs after roundtrip")
def main() -> None:
parser = argparse.ArgumentParser(
description="Demo for FreeCAD RPC server/client",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
To start the server in FreeCAD:
>>> from morphe.adapters.freecad import start_server
>>> start_server()
""",
)
parser.add_argument(
"--host",
default="localhost",
help="Server host (default: localhost)",
)
parser.add_argument(
"--port",
type=int,
default=9876,
help="Server port (default: 9876)",
)
parser.add_argument(
"--import-demo",
action="store_true",
help="Import the demo mounting bracket sketch",
)
parser.add_argument(
"--export",
metavar="NAME",
help="Export sketch NAME to JSON file",
)
parser.add_argument(
"--roundtrip",
metavar="NAME",
help="Export and re-import sketch NAME",
)
args = parser.parse_args()
# Check if server is running
print(f"Connecting to FreeCAD RPC server at {args.host}:{args.port}...")
if not check_server(args.host, args.port, timeout=2.0):
print("\nError: Cannot connect to FreeCAD RPC server.")
print("\nMake sure FreeCAD is running and the server is started:")
print(" >>> from morphe.adapters.freecad import start_server")
print(" >>> start_server()")
sys.exit(1)
client = FreeCADClient(args.host, args.port)
client.connect()
print("Connected!")
# Show status and sketches by default
print_status(client)
list_sketches(client)
# Perform requested action
if args.import_demo:
import_demo(client)
elif args.export:
export_sketch(client, args.export)
elif args.roundtrip:
roundtrip_sketch(client, args.roundtrip)
print("\nDone!")
if __name__ == "__main__":
main()