Skip to content

Commit dc9dfb5

Browse files
authored
feat(operator_doc): include operator usage examples in the operator documentation (#2777)
1 parent bb30822 commit dc9dfb5

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

src/ansys/dpf/core/documentation/generate_operators_doc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from ansys.dpf.core.changelog import Changelog
3434
from ansys.dpf.core.core import load_library
3535
from ansys.dpf.core.dpf_operator import available_operator_names
36+
from ansys.dpf.core.mapping_types import reflection_type_to_cpp_type
3637

3738

3839
class Jinja2ImportError(ModuleNotFoundError): # pragma: nocover
@@ -233,22 +234,27 @@ def fetch_doc_info(server: dpf.AnyServerType, operator_name: str) -> dict:
233234
configurations_info = []
234235
for input_pin in spec.inputs:
235236
input_pin_info = spec.inputs[input_pin]
237+
input_type_names = input_pin_info._type_names
236238
input_info.append(
237239
{
238240
"pin_number": input_pin,
239241
"name": input_pin_info.name,
240-
"types": [str(t) for t in input_pin_info._type_names],
242+
"types": [str(t) for t in input_type_names],
243+
"cpp_types": [reflection_type_to_cpp_type(t) for t in input_type_names],
241244
"document": input_pin_info.document,
242245
"optional": input_pin_info.optional,
246+
"ellipsis": input_pin_info.ellipsis,
243247
}
244248
)
245249
for output_pin in spec.outputs:
246250
output = spec.outputs[output_pin]
251+
output_type_names = output._type_names
247252
output_info.append(
248253
{
249254
"pin_number": output_pin,
250255
"name": output.name,
251-
"types": [str(t) for t in output._type_names],
256+
"types": [str(t) for t in output_type_names],
257+
"cpp_types": [reflection_type_to_cpp_type(t) for t in output_type_names],
252258
"document": output.document,
253259
"optional": output.optional,
254260
}

src/ansys/dpf/core/documentation/operator_doc_template.j2

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,60 @@ This operator supports the following keys ([file formats](../../index.md#overvie
5959

6060
**License**: {{ scripting_info.license }}
6161

62+
## Examples
63+
64+
<details>
65+
<summary>C++</summary>
66+
67+
```cpp
68+
#include "dpf_api.h"
69+
70+
ansys::dpf::Operator op("{{ scripting_info.internal_name }}"); // operator instantiation
71+
{%- for input in inputs %}
72+
op.connect({{ input.pin_number }}, my_{{ input.name }});
73+
{%- endfor %}
74+
{%- for output in outputs %}
75+
{{ output.cpp_types[0] }} my_{{ output.name }} = op.getOutput<{{ output.cpp_types[0] }}>({{ output.pin_number }});
76+
{%- endfor %}
77+
```
78+
</details>
79+
80+
<details>
81+
<summary>CPython</summary>
82+
83+
```python
84+
import ansys.dpf.core as dpf
85+
86+
op = dpf.operators.{{ scripting_info.category }}.{{ scripting_info.scripting_name }}() # operator instantiation
87+
88+
{%- for input in inputs %}
89+
{% if input.ellipsis %}op.inputs.{{ input.name }}1.connect(my_{{ input.name }}1)
90+
op.inputs.{{ input.name }}2.connect(my_{{ input.name }}2){% else%}op.inputs.{{ input.name }}.connect(my_{{ input.name }}){% endif %}
91+
{%- endfor %}
92+
{%- for output in outputs %}
93+
{% if output.types|length > 1 %}my_{{ output.name }}_as_{{ output.types[0] }} = op.outputs.{{ output.name }}_as_{{ output.types[0] }}(){% else %}my_{{ output.name }} = op.outputs.{{ output.name }}(){% endif %}
94+
{%- endfor %}
95+
```
96+
</details>
97+
98+
<details>
99+
<summary>IPython</summary>
100+
101+
```python
102+
import mech_dpf
103+
import Ans.DataProcessing as dpf
104+
105+
op = dpf.operators.{{ scripting_info.category }}.{{ scripting_info.scripting_name }}() # operator instantiation
106+
107+
{%- for input in inputs %}
108+
op.inputs.{{ input.name }}.Connect(my_{{ input.name }})
109+
{%- endfor %}
110+
{%- for output in outputs %}
111+
my_{{ output.name }} = op.outputs.{{ output.name }}.GetData()
112+
{%- endfor %}
113+
```
114+
</details>
115+
<br>
62116

63117
## Changelog
64118

src/ansys/dpf/core/mapping_types.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,39 @@ def __missing__(self, key):
7676
map_types_to_python["vector<double>"] = "list[float]"
7777
map_types_to_python["vector<string>"] = "list[str]"
7878
map_types_to_python["b"] = "bool"
79+
80+
81+
def reflection_type_to_cpp_type(reflection_type: str) -> str:
82+
"""Convert a reflection type to its corresponding C++ type.
83+
84+
The reflection type is obtained from the server specification of the operator.
85+
86+
Used to generate the C++ code examples in the operator documentation.
87+
88+
Parameters
89+
----------
90+
reflection_type : str
91+
The reflection type to convert.
92+
93+
Returns
94+
-------
95+
str
96+
The corresponding C++ type.
97+
"""
98+
if reflection_type == "abstract_meshed_region":
99+
reflection_type = "meshed_region"
100+
if reflection_type == "streams_container":
101+
reflection_type = "streams"
102+
if reflection_type == "int32":
103+
return "int"
104+
elif reflection_type in ["double", "bool"]:
105+
return reflection_type
106+
elif reflection_type == "string":
107+
return "std::string"
108+
elif reflection_type[:7] == "vector<":
109+
inner_type = reflection_type[7:-1]
110+
cpp_inner_type = reflection_type_to_cpp_type(inner_type)
111+
return f"std::vector<{cpp_inner_type}>"
112+
else:
113+
reflection_type = f"ansys::dpf::{_snake_to_camel_case(reflection_type)}"
114+
return reflection_type

0 commit comments

Comments
 (0)