Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get_matrix_multi_substation_single_line_diagram #781

16 changes: 16 additions & 0 deletions cpp/powsybl-cpp/powsybl-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,22 @@ std::vector<std::string> getSingleLineDiagramSvgAndMetadata(const JavaHandle& ne
return svgAndMetadata.get();
}

std::vector<std::string> getMatrixMultiSubstationSvgAndMetadata(const JavaHandle& network, const std::vector<std::vector<std::string>>& matrixIds, const SldParameters& parameters){
auto c_parameters = parameters.to_c_struct();
int nbRows = matrixIds.size();
std::vector<std::string> substationIds;
for (int row = 0; row < nbRows; ++row) {
const std::vector<std::string>& colIds = matrixIds[row];
for (int col = 0; col < matrixIds[row].size(); ++col) {
substationIds.push_back(colIds[col]);
}
}
ToCharPtrPtr substationIdPtr(substationIds);
auto svgAndMetadataArrayPtr = PowsyblCaller::get()->callJava<array*>(::getMatrixMultiSubstationSvgAndMetadata, network, substationIdPtr.get(), substationIds.size(), nbRows, c_parameters.get());
ToStringVector svgAndMetadata(svgAndMetadataArrayPtr);
return svgAndMetadata.get();
}

void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& svgFile, const std::vector<std::string>& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, const NadParameters& parameters) {
auto c_parameters = parameters.to_c_struct();
ToCharPtrPtr voltageLevelIdPtr(voltageLevelIds);
Expand Down
2 changes: 2 additions & 0 deletions cpp/powsybl-cpp/powsybl-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ std::string getSingleLineDiagramSvg(const JavaHandle& network, const std::string

std::vector<std::string> getSingleLineDiagramSvgAndMetadata(const JavaHandle& network, const std::string& containerId, const SldParameters& parameters);

std::vector<std::string> getMatrixMultiSubstationSvgAndMetadata(const JavaHandle& network, const std::vector<std::vector<std::string>>& matrixIds, const SldParameters& parameters);

std::vector<std::string> getSingleLineDiagramComponentLibraryNames();

void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& svgFile, const std::vector<std::string>& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, const NadParameters& parameters);
Expand Down
3 changes: 3 additions & 0 deletions cpp/pypowsybl-cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ PYBIND11_MODULE(_pypowsybl, m) {
m.def("get_single_line_diagram_svg_and_metadata", &pypowsybl::getSingleLineDiagramSvgAndMetadata, "Get single line diagram SVG and its metadata as a list of strings",
py::arg("network"), py::arg("container_id"), py::arg("sld_parameters"));

m.def("get_matrix_multi_substation_single_line_diagram_svg_and_metadata", &pypowsybl::getMatrixMultiSubstationSvgAndMetadata, "Get matrix multi-substation single line diagram SVG and its metadata as a list of strings",
py::arg("network"), py::arg("matrix_ids"), py::arg("sld_parameters"));

m.def("get_single_line_diagram_component_library_names", &pypowsybl::getSingleLineDiagramComponentLibraryNames, "Get supported component library providers for single line diagram");

m.def("write_network_area_diagram_svg", &pypowsybl::writeNetworkAreaDiagramSvg, "Write network area diagram SVG",
Expand Down
1 change: 1 addition & 0 deletions docs/reference/network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Miscellaneous network functions
Network.merge
Network.get_single_line_diagram
Network.write_single_line_diagram_svg
Network.get_matrix_multi_substation_single_line_diagram
Network.write_matrix_multi_substation_single_line_diagram_svg
Network.get_network_area_diagram
Network.write_network_area_diagram_svg
Expand Down
7 changes: 7 additions & 0 deletions docs/user_guide/network_visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ It is also possible to display a multi-substation single line diagram (currently
.. image:: ../_static/images/ieee14_s1_s2_s3_s4.svg
:class: forced-white-background

Or in a Jupyter Notebook:

.. code-block:: python

>>> network.get_matrix_multi_substation_single_line_diagram([['S1', 'S2'],['S3','S4']])


Network area diagram
--------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,19 @@ public static ArrayPointer<CCharPointerPointer> getSingleLineDiagramSvgAndMetada
});
}

@CEntryPoint(name = "getMatrixMultiSubstationSvgAndMetadata")
public static ArrayPointer<CCharPointerPointer> getMatrixMultiSubstationSvgAndMetadata(IsolateThread thread, ObjectHandle networkHandle, CCharPointerPointer substationIdsPointer,
int substationIdCount, int substationIdRowCount,
SldParametersPointer sldParametersPtr, ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr, () -> {
Network network = ObjectHandles.getGlobal().get(networkHandle);
String[][] matrixIds = CTypeUtil.toString2DArray(substationIdsPointer, substationIdCount, substationIdRowCount);
SldParameters sldParameters = convertSldParameters(sldParametersPtr);
List<String> svgAndMeta = SingleLineDiagramUtil.getMatrixMultiSubstationSvgAndMetadata(network, matrixIds, sldParameters);
return createCharPtrArray(svgAndMeta);
});
}

@CEntryPoint(name = "getSingleLineDiagramComponentLibraryNames")
public static PyPowsyblApiHeader.ArrayPointer<CCharPointerPointer> getSingleLineDiagramComponentLibraryNames(IsolateThread thread, PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr, () -> createCharPtrArray(SingleLineDiagramUtil.getComponentLibraryNames()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ static List<String> getSvgAndMetadata(Network network, String containerId, SldPa
}
}

static List<String> getMatrixMultiSubstationSvgAndMetadata(Network network, String[][] matrixIds, SldParameters sldParameters) {
try (StringWriter writer = new StringWriter(); StringWriter writerMeta = new StringWriter()) {
writeMatrixMultiSubstationSvg(network, matrixIds, writer, writerMeta, sldParameters);
writer.flush();
writerMeta.flush();
return List.of(writer.toString(), writerMeta.toString());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

static SldParameters createSldParameters() {
SldParameters sldParameters = new SldParameters();
sldParameters.getSvgParameters().setSvgWidthAndHeightAdded(true);
Expand Down
3 changes: 2 additions & 1 deletion pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,9 @@ def get_network_elements_dataframe_metadata(element_type: ElementType) -> List[S
def get_network_elements_creation_dataframes_metadata(element_type: ElementType) -> List[List[SeriesMetadata]]: ...
def get_single_line_diagram_svg(network: JavaHandle, container_id: str) -> str: ...
def get_single_line_diagram_svg_and_metadata(network: JavaHandle, container_id: str, parameters: SldParameters ) -> List[str]: ...
def get_three_windings_transformer_results(result: JavaHandle) -> SeriesArray: ...
def get_matrix_multi_substation_single_line_diagram_svg_and_metadata(network: JavaHandle, matrix_ids: List[List[str]], parameters: SldParameters) -> List[str]: ...
def get_validation_level(network: JavaHandle) -> ValidationLevel: ...
def get_three_windings_transformer_results(result: JavaHandle) -> SeriesArray: ...
def get_variant_ids(network: JavaHandle) -> List[str]: ...
def get_version_table() -> str: ...
def get_working_variant_id(network: JavaHandle) -> str: ...
Expand Down
17 changes: 17 additions & 0 deletions pypowsybl/network/impl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,23 @@ def get_single_line_diagram(self, container_id: str, parameters: SldParameters =
svg_and_metadata: List[str] = _pp.get_single_line_diagram_svg_and_metadata(self._handle, container_id, p)
return Svg(svg_and_metadata[0], svg_and_metadata[1])

def get_matrix_multi_substation_single_line_diagram(self, matrix_ids: List[List[str]], parameters: SldParameters = None) -> Svg:
"""
Create a single line diagram from multiple substations

Args:
matrix_ids: a two-dimensional list of substation id
parameters:single-line diagram parameters to adjust the rendering of the diagram

Returns:
the single line diagram
"""

p = parameters._to_c_parameters() if parameters is not None else _pp.SldParameters() # pylint: disable=protected-access

svg_and_metadata: List[str] = _pp.get_matrix_multi_substation_single_line_diagram_svg_and_metadata(self._handle, matrix_ids, p)
return Svg(svg_and_metadata[0], svg_and_metadata[1])

def write_network_area_diagram_svg(self, svg_file: PathOrStr, voltage_level_ids: Union[str, List[str]] = None,
depth: int = 0, high_nominal_voltage_bound: float = -1,
low_nominal_voltage_bound: float = -1,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,23 @@ def test_sld_svg():
assert re.search('.*<svg.*', sld2.svg)
assert len(sld2.metadata) > 0

sld_multi_substation = n.get_matrix_multi_substation_single_line_diagram([['S1', 'S2'], ['S3', 'S4']])
assert re.search('.*<svg.*', sld_multi_substation.svg)
assert len(sld_multi_substation.metadata) > 0

sld_multi_substation1 = n.get_matrix_multi_substation_single_line_diagram([['S1', 'S2'], ['S3', 'S4']],
SldParameters(use_name=True, center_name=True,
diagonal_label=True, topological_coloring=False,
tooltip_enabled=True))
assert re.search('.*<svg.*', sld_multi_substation1.svg)
assert len(sld_multi_substation1.metadata) > 0

sld_multi_substation2 = n.get_matrix_multi_substation_single_line_diagram([['S1', 'S2'], ['S3', 'S4']],
SldParameters(use_name=True, center_name=True,
diagonal_label=True, nodes_infos=True, topological_coloring=True,
tooltip_enabled=True))
assert re.search('.*<svg.*', sld_multi_substation2.svg)
assert len(sld_multi_substation2.metadata) > 0

def test_sld_svg_backward_compatibility():
n = pp.network.create_four_substations_node_breaker_network()
Expand Down
Loading