diff --git a/_unittest/conftest.py b/_unittest/conftest.py index 0a86658b890..f00921d7f1e 100644 --- a/_unittest/conftest.py +++ b/_unittest/conftest.py @@ -49,7 +49,7 @@ test_project_name = "test_primitives" sys.path.append(local_path) -from .launch_desktop_tests import run_desktop_tests +from _unittest.launch_desktop_tests import run_desktop_tests # set scratch path and create it if necessary scratch_path = tempfile.gettempdir() diff --git a/_unittest/example_models/test.lib b/_unittest/example_models/test.lib new file mode 100644 index 00000000000..c1be06decfb --- /dev/null +++ b/_unittest/example_models/test.lib @@ -0,0 +1,19 @@ +*---------------------------------------------------------------------- +* PSPICE Model generated by PyAEDT +* Property : C = 0.22 [uF] +*---------------------------------------------------------------------- +* Applicable Conditions: +* Frequency Range = 100Hz - 6GHz +* Temperature = -55 degC - 125 degC +* DC Bias Voltage = 0V - 100V +* Small Signal Operation +*---------------------------------------------------------------------- +* Encrypted Netlist +*---------------------------------------------------------------------- +.subckt GRM1234 Port1 Port2 PARAMS: temperature=25 + C1 Port1 Port2 1e-12 +.ends + +.subckt GRM2345 Port1 Port2 + C1 Port1 Port2 1e-12 +.ends \ No newline at end of file diff --git a/_unittest/test_21_Circuit.py b/_unittest/test_21_Circuit.py index 5f0c150af54..fe55a9e0d44 100644 --- a/_unittest/test_21_Circuit.py +++ b/_unittest/test_21_Circuit.py @@ -320,3 +320,9 @@ def test_28_load_and_save_diff_pair_file(self): with open(diff_file2, "r") as fh: lines = fh.read().splitlines() assert len(lines) == 3 + + def test_29_create_circuit_from_spice(self): + model = os.path.join(local_path, "example_models", "test.lib") + assert self.aedtapp.modeler.schematic.create_component_from_spicemodel(model) + assert self.aedtapp.modeler.schematic.create_component_from_spicemodel(model, "GRM2345") + assert not self.aedtapp.modeler.schematic.create_component_from_spicemodel(model, "GRM2346") diff --git a/pyaedt/circuit.py b/pyaedt/circuit.py index 39bdfce16cf..7b70ebde92a 100644 --- a/pyaedt/circuit.py +++ b/pyaedt/circuit.py @@ -264,7 +264,11 @@ def create_schematic_from_netlist(self, file_to_import): already_exist = True if not already_exist: self.modeler.schematic.create_new_component_from_symbol( - parameter, pins, fields[0][0], parameter_list, parameter_value + parameter, + pins, + refbase=fields[0][0], + parameter_list=parameter_list, + parameter_value=parameter_value, ) mycomp = self.modeler.schematic.create_component( fields[0], @@ -295,7 +299,11 @@ def create_schematic_from_netlist(self, file_to_import): already_exist = True if not already_exist: self.modeler.schematic.create_new_component_from_symbol( - parameter, pins, fields[0][0], parameter_list, parameter_value + parameter, + pins, + refbase=fields[0][0], + parameter_list=parameter_list, + parameter_value=parameter_value, ) mycomp = self.modeler.schematic.create_component( fields[0], diff --git a/pyaedt/modeler/PrimitivesNexxim.py b/pyaedt/modeler/PrimitivesNexxim.py index bb98e1c9e50..7c6a98f9302 100644 --- a/pyaedt/modeler/PrimitivesNexxim.py +++ b/pyaedt/modeler/PrimitivesNexxim.py @@ -1,4 +1,5 @@ import os +import re import warnings from pyaedt.generic.general_methods import generate_unique_name @@ -928,7 +929,7 @@ def create_new_component_from_symbol( pin_lists, time_stamp=1591858313, description="", - refbase="U", + refbase="x", parameter_list=[], parameter_value=[], gref="", @@ -1032,13 +1033,12 @@ def create_new_component_from_symbol( arg2 = ["NAME:Parameters"] for el, val in zip(parameter_list, parameter_value): - if isinstance(val, str): + if "MOD" in el: arg2.append("TextValueProp:=") arg2.append([el, "D", "", val]) - else: - arg2.append("ValueProp:=") - arg2.append([el, "D", "", str(val), False, ""]) + arg2.append("ValuePropNU:=") + arg2.append([el, "D", "", str(val), 0, ""]) arg2.append("ButtonProp:=") arg2.append(["CosimDefinition", "D", "", "Edit", "Edit", 40501, "ButtonPropClientData:=", []]) @@ -1051,7 +1051,7 @@ def create_new_component_from_symbol( while id < len(pin_lists): spicesintax += "%" + str(id) + " " id += 1 - spicesintax += symbol_name + " " + # spicesintax += symbol_name + " " for el, val in zip(parameter_list, parameter_value): if "MOD" in el: spicesintax += "@{} ".format(el) @@ -1750,3 +1750,54 @@ def assign_sin_excitation2ports(self, ports, settings): DeprecationWarning, ) return self._app.assign_voltage_sinusoidal_excitation_to_ports(ports, settings) + + @pyaedt_function_handler() + def _parse_spice_model(self, model_path): + models = [] + with open(model_path, "r") as f: + for line in f: + if ".subckt" in line.lower(): + pinNames = [i.strip() for i in re.split(" |\t", line) if i] + models.append(pinNames[1]) + return models + + @pyaedt_function_handler() + def create_component_from_spicemodel(self, model_path, model_name=None, location=None): + """Create and place a new component based on a spice .lib file. + + Parameters + ---------- + model_path : str + Path to .lib file. + model_name : str, optional + Model name to import. If `None` the first subckt in the lib file will be placed. + location : list, optional + Position in the schematic of the new component. + + Returns + ------- + :class:`pyaedt.modeler.Object3d.CircuitComponent` + Circuit Component Object. + """ + models = self._parse_spice_model(model_path) + if not model_name and models: + model_name = models[0] + elif model_name not in models: + return False + arg = ["NAME:Options", "Mode:=", 2, "Overwrite:=", False, "SupportsSimModels:=", False, "LoadOnly:=", False] + arg2 = ["NAME:Models"] + for el in models: + arg2.append(el + ":=") + if el == model_name: + arg2.append([True, "", "", False]) + else: + arg2.append([False, "", "", False]) + arg.append(arg2) + self.o_component_manager.ImportModelsFromFile(model_path, arg) + + return self.create_component( + None, + component_library=None, + component_name=model_name, + location=location, + ) diff --git a/requirements_test.txt b/requirements_test.txt index 216b5dd3a60..43840513098 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,6 +1,6 @@ pyvista matplotlib numpy -pytest +pytest==7.0.0 pytest-cov codecov