From 4b75b56df6f414910bebeadeb4819cd854b6c2bc Mon Sep 17 00:00:00 2001 From: Etienne LESOT Date: Fri, 17 Nov 2023 15:21:35 +0100 Subject: [PATCH] refactor network area diagram parameters Signed-off-by: Etienne LESOT --- cpp/src/bindings.cpp | 8 +++- cpp/src/pypowsybl-api.h | 4 ++ cpp/src/pypowsybl.cpp | 36 ++++++++++++++-- cpp/src/pypowsybl.h | 15 ++++++- .../python/commons/PyPowsyblApiHeader.java | 9 ++++ .../network/NetworkAreaDiagramUtil.java | 32 +++++++-------- .../python/network/NetworkCFunctions.java | 41 +++++++++++++++++-- .../python/network/SingleLineDiagramUtil.java | 2 +- .../network/NetworkAreaDiagramUtilTest.java | 3 +- pypowsybl/_pypowsybl.pyi | 8 +++- pypowsybl/network/__init__.py | 1 + pypowsybl/network/impl/nad_parameters.py | 24 +++++++++++ pypowsybl/network/impl/network.py | 23 +++++++---- tests/test_network.py | 4 +- 14 files changed, 170 insertions(+), 40 deletions(-) create mode 100644 pypowsybl/network/impl/nad_parameters.py diff --git a/cpp/src/bindings.cpp b/cpp/src/bindings.cpp index dff480e18..9fe2fb6bd 100644 --- a/cpp/src/bindings.cpp +++ b/cpp/src/bindings.cpp @@ -472,6 +472,10 @@ PYBIND11_MODULE(_pypowsybl, m) { .def_readwrite("topological_coloring", &pypowsybl::SldParameters::topological_coloring) .def_readwrite("component_library", &pypowsybl::SldParameters::component_library); + py::class_(m, "NadParameters") + .def(py::init(&pypowsybl::createNadParameters)) + .def_readwrite("edge_name_displayed", &pypowsybl::NadParameters::edge_name_displayed); + m.def("write_single_line_diagram_svg", &pypowsybl::writeSingleLineDiagramSvg, "Write single line diagram SVG", py::arg("network"), py::arg("container_id"), py::arg("svg_file"), py::arg("metadata_file"), py::arg("sld_parameters")); @@ -484,10 +488,10 @@ PYBIND11_MODULE(_pypowsybl, m) { 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", - py::arg("network"), py::arg("svg_file"), py::arg("voltage_level_ids"), py::arg("depth"), py::arg("high_nominal_voltage_bound"), py::arg("low_nominal_voltage_bound"), py::arg("edge_name_displayed")); + py::arg("network"), py::arg("svg_file"), py::arg("voltage_level_ids"), py::arg("depth"), py::arg("high_nominal_voltage_bound"), py::arg("low_nominal_voltage_bound"), py::arg("nad_parameters")); m.def("get_network_area_diagram_svg", &pypowsybl::getNetworkAreaDiagramSvg, "Get network area diagram SVG as a string", - py::arg("network"), py::arg("voltage_level_ids"), py::arg("depth"), py::arg("high_nominal_voltage_bound"), py::arg("low_nominal_voltage_bound"), py::arg("edge_name_displayed")); + py::arg("network"), py::arg("voltage_level_ids"), py::arg("depth"), py::arg("high_nominal_voltage_bound"), py::arg("low_nominal_voltage_bound"), py::arg("nad_parameters")); m.def("get_network_area_diagram_displayed_voltage_levels", &pypowsybl::getNetworkAreaDiagramDisplayedVoltageLevels, "Get network area diagram displayed voltage level", py::arg("network"), py::arg("voltage_level_ids"), py::arg("depth")); diff --git a/cpp/src/pypowsybl-api.h b/cpp/src/pypowsybl-api.h index 2ab707577..a3ea4cd1d 100644 --- a/cpp/src/pypowsybl-api.h +++ b/cpp/src/pypowsybl-api.h @@ -308,6 +308,10 @@ typedef struct sld_parameters_struct { char* component_library; } sld_parameters; +typedef struct nad_parameters_struct { + unsigned char edge_name_displayed; +} nad_parameters; + typedef enum { ALPHA_BETA_LOAD = 0, ONE_TRANSFORMER_LOAD, diff --git a/cpp/src/pypowsybl.cpp b/cpp/src/pypowsybl.cpp index 1c72510e5..21006606d 100644 --- a/cpp/src/pypowsybl.cpp +++ b/cpp/src/pypowsybl.cpp @@ -804,14 +804,16 @@ std::vector getSingleLineDiagramSvgAndMetadata(const JavaHandle& ne return svgAndMetadata.get(); } -void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& svgFile, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, bool edgeNameDisplayed) { +void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& svgFile, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, const NadParameters& parameters) { + auto c_parameters = parameters.to_c_struct(); ToCharPtrPtr voltageLevelIdPtr(voltageLevelIds); - callJava(::writeNetworkAreaDiagramSvg, network, (char*) svgFile.data(), voltageLevelIdPtr.get(), voltageLevelIds.size(), depth, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed); + callJava(::writeNetworkAreaDiagramSvg, network, (char*) svgFile.data(), voltageLevelIdPtr.get(), voltageLevelIds.size(), depth, highNominalVoltageBound, lowNominalVoltageBound, c_parameters.get()); } -std::string getNetworkAreaDiagramSvg(const JavaHandle& network, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, bool edgeNameDisplayed) { +std::string getNetworkAreaDiagramSvg(const JavaHandle& network, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, const NadParameters& parameters) { + auto c_parameters = parameters.to_c_struct(); ToCharPtrPtr voltageLevelIdPtr(voltageLevelIds); - return toString(callJava(::getNetworkAreaDiagramSvg, network, voltageLevelIdPtr.get(), voltageLevelIds.size(), depth, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed)); + return toString(callJava(::getNetworkAreaDiagramSvg, network, voltageLevelIdPtr.get(), voltageLevelIds.size(), depth, highNominalVoltageBound, lowNominalVoltageBound, c_parameters.get())); } std::vector getNetworkAreaDiagramDisplayedVoltageLevels(const JavaHandle& network, const std::vector& voltageLevelIds, int depth) { @@ -1247,6 +1249,10 @@ SldParameters::SldParameters(sld_parameters* src) { component_library = toString(src->component_library); } +NadParameters::NadParameters(nad_parameters* src) { + edge_name_displayed = (bool) src->edge_name_displayed; +} + void SldParameters::sld_to_c_struct(sld_parameters& res) const { res.use_name = (unsigned char) use_name; res.center_name = (unsigned char) center_name; @@ -1256,6 +1262,10 @@ void SldParameters::sld_to_c_struct(sld_parameters& res) const { res.component_library = copyStringToCharPtr(component_library); } +void NadParameters::nad_to_c_struct(nad_parameters& res) const { + res.edge_name_displayed = (unsigned char) edge_name_displayed; +} + std::shared_ptr SldParameters::to_c_struct() const { sld_parameters* res = new sld_parameters(); sld_to_c_struct(*res); @@ -1265,6 +1275,15 @@ std::shared_ptr SldParameters::to_c_struct() const { }); } +std::shared_ptr NadParameters::to_c_struct() const { + nad_parameters* res = new nad_parameters(); + nad_to_c_struct(*res); + //Memory has been allocated here on C side, we need to clean it up on C side (not java side) + return std::shared_ptr(res, [](nad_parameters* ptr){ + delete ptr; + }); +} + SldParameters* createSldParameters() { sld_parameters* parameters_ptr = callJava(::createSldParameters); auto parameters = std::shared_ptr(parameters_ptr, [](sld_parameters* ptr){ @@ -1274,6 +1293,15 @@ SldParameters* createSldParameters() { return new SldParameters(parameters.get()); } +NadParameters* createNadParameters() { + nad_parameters* parameters_ptr = callJava(::createNadParameters); + auto parameters = std::shared_ptr(parameters_ptr, [](nad_parameters* ptr){ + //Memory has been allocated on java side, we need to clean it up on java side + callJava(::freeNadParameters, ptr); + }); + return new NadParameters(parameters.get()); +} + void removeElementsModification(pypowsybl::JavaHandle network, const std::vector& connectableIds, dataframe* dataframe, remove_modification_type removeModificationType, bool throwException, JavaHandle* reporter) { ToCharPtrPtr connectableIdsPtr(connectableIds); pypowsybl::callJava(::removeElementsModification, network, connectableIdsPtr.get(), connectableIds.size(), dataframe, removeModificationType, throwException, (reporter == nullptr) ? nullptr : *reporter); diff --git a/cpp/src/pypowsybl.h b/cpp/src/pypowsybl.h index 0c423b1f6..8813a8a89 100644 --- a/cpp/src/pypowsybl.h +++ b/cpp/src/pypowsybl.h @@ -277,6 +277,15 @@ class SldParameters { std::string component_library; }; +class NadParameters { +public: + NadParameters(nad_parameters* src); + std::shared_ptr to_c_struct() const; + void nad_to_c_struct(nad_parameters& params) const; + + bool edge_name_displayed; +}; + char* copyStringToCharPtr(const std::string& str); char** copyVectorStringToCharPtrPtr(const std::vector& strings); int* copyVectorInt(const std::vector& ints); @@ -382,9 +391,9 @@ std::vector getSingleLineDiagramSvgAndMetadata(const JavaHandle& ne std::vector getSingleLineDiagramComponentLibraryNames(); -void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& svgFile, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, bool edgeNameDisplayed); +void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& svgFile, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, const NadParameters& parameters); -std::string getNetworkAreaDiagramSvg(const JavaHandle& network, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, bool edgeNameDisplayed); +std::string getNetworkAreaDiagramSvg(const JavaHandle& network, const std::vector& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, const NadParameters& parameters); std::vector getNetworkAreaDiagramDisplayedVoltageLevels(const JavaHandle& network, const std::vector& voltageLevelIds, int depth); @@ -536,6 +545,8 @@ void removeElementsModification(pypowsybl::JavaHandle network, const std::vector SldParameters* createSldParameters(); +NadParameters* createNadParameters(); + //=======dynamic modeling for dynawaltz package========== //handle creation diff --git a/java/src/main/java/com/powsybl/python/commons/PyPowsyblApiHeader.java b/java/src/main/java/com/powsybl/python/commons/PyPowsyblApiHeader.java index 026a2a777..c6ab27c5e 100644 --- a/java/src/main/java/com/powsybl/python/commons/PyPowsyblApiHeader.java +++ b/java/src/main/java/com/powsybl/python/commons/PyPowsyblApiHeader.java @@ -999,6 +999,15 @@ public interface SldParametersPointer extends PointerBase { void setComponentLibrary(CCharPointer componentLibrary); } + @CStruct("nad_parameters") + public interface NadParametersPointer extends PointerBase { + @CField("edge_name_displayed") + void setEdgeNameDisplayed(boolean edgeNameDisplayed); + + @CField("edge_name_displayed") + boolean isEdgeNameDisplayed(); + } + @CEnum("DynamicMappingType") public enum DynamicMappingType { ALPHA_BETA_LOAD, diff --git a/java/src/main/java/com/powsybl/python/network/NetworkAreaDiagramUtil.java b/java/src/main/java/com/powsybl/python/network/NetworkAreaDiagramUtil.java index 78483f4f5..2fcbc93c0 100644 --- a/java/src/main/java/com/powsybl/python/network/NetworkAreaDiagramUtil.java +++ b/java/src/main/java/com/powsybl/python/network/NetworkAreaDiagramUtil.java @@ -36,30 +36,21 @@ private NetworkAreaDiagramUtil() { } static void writeSvg(Network network, List voltageLevelIds, int depth, Writer writer, - double highNominalVoltageBound, double lowNominalVoltageBound, boolean edgeNameDisplayed) { - SvgParameters svgParameters = new SvgParameters() - .setSvgWidthAndHeightAdded(true) - .setFixedWidth(800) - .setFixedHeight(600) - .setEdgeNameDisplayed(edgeNameDisplayed); - + double highNominalVoltageBound, double lowNominalVoltageBound, NadParameters nadParameters) { Predicate filter = !voltageLevelIds.isEmpty() ? getNominalVoltageFilter(network, voltageLevelIds, highNominalVoltageBound, lowNominalVoltageBound, depth) : VoltageLevelFilter.NO_FILTER; - NadParameters nadParameters = new NadParameters() - .setSvgParameters(svgParameters); - NetworkAreaDiagram.draw(network, writer, nadParameters, filter); } - static String getSvg(Network network, List voltageLevelIds, int depth, boolean edgeNameDisplayed) { - return getSvg(network, voltageLevelIds, depth, -1, -1, edgeNameDisplayed); + static String getSvg(Network network, List voltageLevelIds, NadParameters nadParameters) { + return getSvg(network, voltageLevelIds, 0, -1, -1, nadParameters); } static String getSvg(Network network, List voltageLevelIds, int depth, - double highNominalVoltageBound, double lowNominalVoltageBound, boolean edgeNameDisplayed) { + double highNominalVoltageBound, double lowNominalVoltageBound, NadParameters nadParameters) { try (StringWriter writer = new StringWriter()) { - writeSvg(network, voltageLevelIds, depth, writer, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed); + writeSvg(network, voltageLevelIds, depth, writer, highNominalVoltageBound, lowNominalVoltageBound, nadParameters); return writer.toString(); } catch (IOException e) { throw new UncheckedIOException(e); @@ -67,14 +58,23 @@ static String getSvg(Network network, List voltageLevelIds, int depth, } static void writeSvg(Network network, List voltageLevelIds, int depth, String svgFile, - Double highNominalVoltageBound, Double lowNominalVoltageBound, boolean edgeNameDisplayed) { + Double highNominalVoltageBound, Double lowNominalVoltageBound, NadParameters nadParameters) { try (Writer writer = Files.newBufferedWriter(Paths.get(svgFile), StandardCharsets.UTF_8)) { - writeSvg(network, voltageLevelIds, depth, writer, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed); + writeSvg(network, voltageLevelIds, depth, writer, highNominalVoltageBound, lowNominalVoltageBound, nadParameters); } catch (IOException e) { throw new UncheckedIOException(e); } } + static NadParameters createNadParameters() { + SvgParameters svgParameters = new SvgParameters() + .setSvgWidthAndHeightAdded(true) + .setFixedWidth(800) + .setFixedHeight(600); + return new NadParameters() + .setSvgParameters(svgParameters); + } + static VoltageLevelFilter getNominalVoltageFilter(Network network, List voltageLevelIds, double highNominalVoltageBound, double lowNominalVoltageBound, int depth) { diff --git a/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java b/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java index 052bd53c9..628359d22 100644 --- a/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java +++ b/java/src/main/java/com/powsybl/python/network/NetworkCFunctions.java @@ -29,6 +29,7 @@ import com.powsybl.dataframe.update.UpdatingDataframe; import com.powsybl.iidm.network.*; import com.powsybl.iidm.reducer.*; +import com.powsybl.nad.NadParameters; import com.powsybl.python.commons.CTypeUtil; import com.powsybl.python.commons.Directives; import com.powsybl.python.commons.PyPowsyblApiHeader; @@ -879,6 +880,21 @@ public static SldParametersPointer createSldParameters(IsolateThread thread, PyP return doCatch(exceptionHandlerPtr, () -> convertToSldParametersPointer(SingleLineDiagramUtil.createSldParameters())); } + public static NadParametersPointer convertToNadParametersPointer(NadParameters parameters) { + NadParametersPointer paramsPtr = UnmanagedMemory.calloc(SizeOf.get(NadParametersPointer.class)); + copyToCNadParameters(parameters, paramsPtr); + return paramsPtr; + } + + public static void copyToCNadParameters(NadParameters parameters, NadParametersPointer cParameters) { + cParameters.setEdgeNameDisplayed(parameters.getSvgParameters().isEdgeNameDisplayed()); + } + + @CEntryPoint(name = "createNadParameters") + public static NadParametersPointer createNadParameters(IsolateThread thread, PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) { + return doCatch(exceptionHandlerPtr, () -> convertToNadParametersPointer(NetworkAreaDiagramUtil.createNadParameters())); + } + public static void freeSldParametersPointer(SldParametersPointer sldParametersPtr) { UnmanagedMemory.free(sldParametersPtr); } @@ -891,6 +907,14 @@ public static void freeSldParameters(IsolateThread thread, SldParametersPointer }); } + @CEntryPoint(name = "freeNadParameters") + public static void freeNadParameters(IsolateThread thread, NadParametersPointer nadParametersPointer, + PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) { + doCatch(exceptionHandlerPtr, () -> { + UnmanagedMemory.free(nadParametersPointer); + }); + } + public static SldParameters convertSldParameters(SldParametersPointer sldParametersPtr) { String componentLibraryName = CTypeUtil.toString(sldParametersPtr.getComponentLibrary()); SldParameters sldParameters = SingleLineDiagramUtil.createSldParameters() @@ -904,6 +928,13 @@ public static SldParameters convertSldParameters(SldParametersPointer sldParamet return sldParameters; } + public static NadParameters convertNadParameters(NadParametersPointer nadParametersPointer) { + NadParameters nadParameters = NetworkAreaDiagramUtil.createNadParameters(); + nadParameters.getSvgParameters() + .setEdgeNameDisplayed(nadParametersPointer.isEdgeNameDisplayed()); + return nadParameters; + } + @CEntryPoint(name = "writeSingleLineDiagramSvg") public static void writeSingleLineDiagramSvg(IsolateThread thread, ObjectHandle networkHandle, CCharPointer containerId, CCharPointer svgFile, CCharPointer metadataFile, SldParametersPointer sldParametersPtr, @@ -949,24 +980,26 @@ public static PyPowsyblApiHeader.ArrayPointer getSingleLine @CEntryPoint(name = "writeNetworkAreaDiagramSvg") public static void writeNetworkAreaDiagramSvg(IsolateThread thread, ObjectHandle networkHandle, CCharPointer svgFile, CCharPointerPointer voltageLevelIdsPointer, int voltageLevelIdCount, int depth, - double highNominalVoltageBound, double lowNominalVoltageBound, boolean edgeNameDisplayed, + double highNominalVoltageBound, double lowNominalVoltageBound, NadParametersPointer nadParametersPointer, ExceptionHandlerPointer exceptionHandlerPtr) { doCatch(exceptionHandlerPtr, () -> { Network network = ObjectHandles.getGlobal().get(networkHandle); String svgFileStr = CTypeUtil.toString(svgFile); List voltageLevelIds = toStringList(voltageLevelIdsPointer, voltageLevelIdCount); - NetworkAreaDiagramUtil.writeSvg(network, voltageLevelIds, depth, svgFileStr, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed); + NadParameters nadParameters = convertNadParameters(nadParametersPointer); + NetworkAreaDiagramUtil.writeSvg(network, voltageLevelIds, depth, svgFileStr, highNominalVoltageBound, lowNominalVoltageBound, nadParameters); }); } @CEntryPoint(name = "getNetworkAreaDiagramSvg") public static CCharPointer getNetworkAreaDiagramSvg(IsolateThread thread, ObjectHandle networkHandle, CCharPointerPointer voltageLevelIdsPointer, int voltageLevelIdCount, int depth, double highNominalVoltageBound, - double lowNominalVoltageBound, boolean edgeNameDisplayed, ExceptionHandlerPointer exceptionHandlerPtr) { + double lowNominalVoltageBound, NadParametersPointer nadParametersPointer, ExceptionHandlerPointer exceptionHandlerPtr) { return doCatch(exceptionHandlerPtr, () -> { Network network = ObjectHandles.getGlobal().get(networkHandle); List voltageLevelIds = toStringList(voltageLevelIdsPointer, voltageLevelIdCount); - String svg = NetworkAreaDiagramUtil.getSvg(network, voltageLevelIds, depth, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed); + NadParameters nadParameters = convertNadParameters(nadParametersPointer); + String svg = NetworkAreaDiagramUtil.getSvg(network, voltageLevelIds, depth, highNominalVoltageBound, lowNominalVoltageBound, nadParameters); return CTypeUtil.toCharPtr(svg); }); } diff --git a/java/src/main/java/com/powsybl/python/network/SingleLineDiagramUtil.java b/java/src/main/java/com/powsybl/python/network/SingleLineDiagramUtil.java index f5b4cd965..e736e2619 100644 --- a/java/src/main/java/com/powsybl/python/network/SingleLineDiagramUtil.java +++ b/java/src/main/java/com/powsybl/python/network/SingleLineDiagramUtil.java @@ -30,7 +30,7 @@ private SingleLineDiagramUtil() { static void writeSvg(Network network, String containerId, String svgFile, String metadataFile, SldParameters sldParameters) { try (Writer writer = Files.newBufferedWriter(Paths.get(svgFile)); - Writer metadataWriter = metadataFile.isEmpty() ? new StringWriter() : Files.newBufferedWriter(Paths.get(metadataFile))) { + Writer metadataWriter = metadataFile == null || metadataFile.isEmpty() ? new StringWriter() : Files.newBufferedWriter(Paths.get(metadataFile))) { writeSvg(network, containerId, writer, metadataWriter, sldParameters); } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/java/src/test/java/com/powsybl/python/network/NetworkAreaDiagramUtilTest.java b/java/src/test/java/com/powsybl/python/network/NetworkAreaDiagramUtilTest.java index ef73084a8..4464f9232 100644 --- a/java/src/test/java/com/powsybl/python/network/NetworkAreaDiagramUtilTest.java +++ b/java/src/test/java/com/powsybl/python/network/NetworkAreaDiagramUtilTest.java @@ -18,6 +18,7 @@ import java.nio.charset.StandardCharsets; import java.util.*; +import static com.powsybl.python.network.NetworkAreaDiagramUtil.createNadParameters; import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -28,7 +29,7 @@ class NetworkAreaDiagramUtilTest { @Test void test() throws IOException { Network network = IeeeCdfNetworkFactory.create14(); - String svg = NetworkAreaDiagramUtil.getSvg(network, Collections.emptyList(), 0, false); + String svg = NetworkAreaDiagramUtil.getSvg(network, Collections.emptyList(), createNadParameters()); assertEquals(TestUtil.normalizeLineSeparator(new String(ByteStreams.toByteArray(Objects.requireNonNull(NetworkAreaDiagramUtil.class.getResourceAsStream("/nad.svg"))), StandardCharsets.UTF_8)), TestUtil.normalizeLineSeparator(svg)); } diff --git a/pypowsybl/_pypowsybl.pyi b/pypowsybl/_pypowsybl.pyi index 9a0088b1c..071d4d7b5 100644 --- a/pypowsybl/_pypowsybl.pyi +++ b/pypowsybl/_pypowsybl.pyi @@ -234,6 +234,10 @@ class SldParameters: component_library: str def __init__(self) -> None: ... +class NadParameters: + edge_name_displayed: bool + def __init__(self) -> None: ... + class LimitType: __members__: ClassVar[Dict[str, LimitType]] = ... # read-only CURRENT: ClassVar[LimitType] = ... @@ -612,7 +616,7 @@ def create_loadflow_provider_parameters_series_array(provider: str) -> SeriesArr def get_security_analysis_provider_parameters_names(provider: str) -> List[str]: ... def get_sensitivity_analysis_provider_parameters_names(provider: str) -> List[str]: ... def get_limit_violations(result: JavaHandle) -> SeriesArray: ... -def get_network_area_diagram_svg(network: JavaHandle, voltage_level_ids: Union[str, List[str]], depth: int, high_nominal_voltage_bound: float, low_nominal_voltage_bound: float, edge_name_displayed: bool) -> str: ... +def get_network_area_diagram_svg(network: JavaHandle, voltage_level_ids: Union[str, List[str]], depth: int, high_nominal_voltage_bound: float, low_nominal_voltage_bound: float, nad_parameters: NadParameters) -> str: ... def get_network_area_diagram_displayed_voltage_levels(network: JavaHandle, voltage_level_ids: Union[str, List[str]], depth: int) -> List[str]: ... def get_network_elements_ids(network: JavaHandle, element_type: ElementType, nominal_voltages: List[float], countries: List[str], main_connected_component: bool, main_synchronous_component: bool, not_connected_to_same_bus_at_both_sides: bool) -> List[str]: ... def get_network_export_formats() -> List[str]: ... @@ -668,7 +672,7 @@ def update_connectable_status(arg0: JavaHandle, arg1: str, arg2: bool) -> bool: def update_network_elements_with_series(network: JavaHandle, array: Dataframe, element_type: ElementType) -> None: ... def update_switch_position(arg0: JavaHandle, arg1: str, arg2: bool) -> bool: ... def validate(network: JavaHandle) -> ValidationLevel: ... -def write_network_area_diagram_svg(network: JavaHandle, svg_file: str, voltage_level_ids: Union[str, List[str]], depth: int, high_nominal_voltage_bound: float, low_nominal_voltage_bound: float, edge_name_displayed: bool) -> None: ... +def write_network_area_diagram_svg(network: JavaHandle, svg_file: str, voltage_level_ids: Union[str, List[str]], depth: int, high_nominal_voltage_bound: float, low_nominal_voltage_bound: float, nad_parameters: NadParameters) -> None: ... def write_single_line_diagram_svg(network: JavaHandle, container_id: str, svg_file: str, metadata_file: str, parameters: SldParameters) -> None: ... def add_network_element_properties(network: JavaHandle, dataframe: Dataframe) -> None: ... def remove_network_element_properties(network: JavaHandle, ids: List[str], properties: List[str]) -> None: ... diff --git a/pypowsybl/network/__init__.py b/pypowsybl/network/__init__.py index 5dbda0a59..65c88996a 100644 --- a/pypowsybl/network/__init__.py +++ b/pypowsybl/network/__init__.py @@ -14,6 +14,7 @@ from .impl.bus_breaker_topology import BusBreakerTopology from .impl.node_breaker_topology import NodeBreakerTopology from .impl.sld_parameters import SldParameters +from .impl.nad_parameters import NadParameters from .impl.layout_parameters import LayoutParameters from .impl.network_creation_util import ( create_empty, diff --git a/pypowsybl/network/impl/nad_parameters.py b/pypowsybl/network/impl/nad_parameters.py new file mode 100644 index 000000000..e1c197fb5 --- /dev/null +++ b/pypowsybl/network/impl/nad_parameters.py @@ -0,0 +1,24 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# SPDX-License-Identifier: MPL-2.0 +# +import pypowsybl._pypowsybl as _pp + + +class NadParameters: + """ + This class represents nad parameters for a network area diagram svg generation.""" + + def __init__(self, edge_name_displayed: bool = True): + self._edge_name_displayed = edge_name_displayed + + @property + def edge_name_displayed(self) -> bool: + """edge_name_displayed""" + return self._edge_name_displayed + + def _to_c_parameters(self) -> _pp.NadParameters: + c_parameters = _pp.NadParameters() + c_parameters.edge_name_displayed = self._edge_name_displayed + return c_parameters \ No newline at end of file diff --git a/pypowsybl/network/impl/network.py b/pypowsybl/network/impl/network.py index c254b86e3..aabd729a5 100644 --- a/pypowsybl/network/impl/network.py +++ b/pypowsybl/network/impl/network.py @@ -41,6 +41,7 @@ from .bus_breaker_topology import BusBreakerTopology from .node_breaker_topology import NodeBreakerTopology from .sld_parameters import SldParameters +from .nad_parameters import NadParameters from .svg import Svg from .util import create_data_frame_from_series_array, ParamsDict @@ -180,7 +181,8 @@ def save_to_string(self, format: str = 'XIIDM', parameters: ParamsDict = None, r return _pp.save_network_to_string(self._handle, format, parameters, None if reporter is None else reporter._reporter_model) # pylint: disable=protected-access - def save_to_binary_buffer(self, format: str = 'XIIDM', parameters: ParamsDict = None, reporter: Reporter = None) -> io.BytesIO: + def save_to_binary_buffer(self, format: str = 'XIIDM', parameters: ParamsDict = None, + reporter: Reporter = None) -> io.BytesIO: """ Save a network to a binary buffer using a specified format. In the current implementation, whatever the specified format is (so a format creating a single file or a format @@ -199,7 +201,6 @@ def save_to_binary_buffer(self, format: str = 'XIIDM', parameters: ParamsDict = return io.BytesIO(_pp.save_network_to_binary_buffer(self._handle, format, parameters, None if reporter is None else reporter._reporter_model)) # pylint: disable=protected-access - def reduce(self, v_min: float = 0, v_max: float = sys.float_info.max, ids: List[str] = None, vl_depths: tuple = (), with_dangling_lines: bool = False) -> None: if ids is None: @@ -250,7 +251,7 @@ def get_single_line_diagram(self, container_id: str, parameters: SldParameters = 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, - edge_name_displayed: bool = False) -> None: + nad_parameters: NadParameters = None) -> None: """ Create a network area diagram in SVG format and write it to a file. @@ -258,24 +259,31 @@ def write_network_area_diagram_svg(self, svg_file: PathOrStr, voltage_level_ids: svg_file: a svg file path voltage_level_id: the voltage level ID, center of the diagram (None for the full diagram) depth: the diagram depth around the voltage level + high_nominal_voltage_bound: high bound to filter voltage level according to nominal voltage + low_nominal_voltage_bound: low bound to filter voltage level according to nominal voltage + nad_parameters: parameters for network area diagram """ svg_file = path_to_str(svg_file) if voltage_level_ids is None: voltage_level_ids = [] if isinstance(voltage_level_ids, str): voltage_level_ids = [voltage_level_ids] + nad_p = nad_parameters._to_c_parameters() if nad_parameters is not None else _pp.NadParameters() _pp.write_network_area_diagram_svg(self._handle, svg_file, voltage_level_ids, depth, high_nominal_voltage_bound, - low_nominal_voltage_bound, edge_name_displayed) + low_nominal_voltage_bound, nad_p) def get_network_area_diagram(self, voltage_level_ids: Union[str, List[str]] = None, depth: int = 0, high_nominal_voltage_bound: float = -1, low_nominal_voltage_bound: float = -1, - edge_name_displayed: bool = False) -> Svg: + nad_parameters: NadParameters = None) -> Svg: """ Create a network area diagram. Args: - voltage_level_id: the voltage level ID, center of the diagram (None for the full diagram) + voltage_level_ids: the voltage level IDs, centers of the diagram (None for the full diagram) depth: the diagram depth around the voltage level + high_nominal_voltage_bound: high bound to filter voltage level according to nominal voltage + low_nominal_voltage_bound: low bound to filter voltage level according to nominal voltage + nad_parameters: parameters for network area diagram Returns: the network area diagram @@ -284,9 +292,10 @@ def get_network_area_diagram(self, voltage_level_ids: Union[str, List[str]] = No voltage_level_ids = [] if isinstance(voltage_level_ids, str): voltage_level_ids = [voltage_level_ids] + nad_p = nad_parameters._to_c_parameters() if nad_parameters is not None else _pp.NadParameters() return Svg(_pp.get_network_area_diagram_svg(self._handle, voltage_level_ids, depth, high_nominal_voltage_bound, low_nominal_voltage_bound, - edge_name_displayed)) + nad_p)) def get_network_area_diagram_displayed_voltage_levels(self, voltage_level_ids: Union[str, List[str]], depth: int = 0) -> List[str]: diff --git a/tests/test_network.py b/tests/test_network.py index 9445ec84b..26db69dce 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -26,7 +26,7 @@ import pypowsybl.report as rp import util from pypowsybl import PyPowsyblError -from pypowsybl.network import ValidationLevel, SldParameters, LayoutParameters +from pypowsybl.network import ValidationLevel, SldParameters, NadParameters, LayoutParameters TEST_DIR = pathlib.Path(__file__).parent DATA_DIR = TEST_DIR.parent / 'data' @@ -850,6 +850,7 @@ def test_sld_nad(): assert re.search('.*