Skip to content

Commit

Permalink
opics - loading library
Browse files Browse the repository at this point in the history
requires the PDK to already have "import opics_<libname>" done, e.g., import opics_ebeam
  • Loading branch information
lukasc-ubc committed Jan 8, 2025
1 parent 8be2845 commit f5b2e43
Showing 1 changed file with 12 additions and 69 deletions.
81 changes: 12 additions & 69 deletions klayout_dot_config/python/SiEPIC/opics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,75 +300,18 @@ def NetlistProcessor(spice_filepath, Network, libraries, c_, circuitData, verbos
# Using the libraries installed within the OPICS folder
libs_comps[each_lib] = all_libraries[each_lib].component_factory
else:
if each_lib == "ebeam":
try:
# Try importing from siepic_ebeam_pdk first
print(f"Attempting to import from siepic_ebeam_pdk...")
from siepic_ebeam_pdk.EBeam.pymacros.opics_ebeam import component_factory
print(f"Successfully imported component_factory")
libs_comps[each_lib] = component_factory
except ImportError as e:
print(f"First import attempt failed with error: {e}")
print(f"Attempting second import method...")
# Fallback to regular opics_ebeam import
opics_lib_name = "opics_"+each_lib
try:
import sys
import os
debug_info = [
f"Python path: {sys.path}",
f"Current working directory: {os.getcwd()}",
f"Attempting to import {opics_lib_name}",
f"First import error: {str(e)}"
]

# Try to find the package in the KLayout salt directory
klayout_salt_dir = os.path.join(os.path.expanduser('~'), 'KLayout', 'salt')
if os.path.exists(klayout_salt_dir):
sys.path.append(klayout_salt_dir)

# Look for the specific file path
potential_paths = [
os.path.join(klayout_salt_dir, 'siepic_ebeam_pdk', 'EBeam', 'pymacros', 'opics_ebeam'),
]

for path in potential_paths:
if os.path.exists(path):
print(f"Found opics_ebeam at: {path}")
sys.path.append(os.path.dirname(path))
try:
from opics_ebeam import component_factory
libs_comps[each_lib] = component_factory
break
except ImportError as e:
print(f"Found path but import failed: {e}")
continue

if each_lib in libs_comps:
continue

# Try original method as last resort
import importlib
opics_lib = importlib.import_module(opics_lib_name)
from importlib import reload
opics_lib = reload(opics_lib)
globals()[opics_lib_name] = opics_lib
libs_comps[each_lib] = opics_lib.component_factory

except Exception as e:
raise Exception('Unable to import the library: %s. Please ensure siepic_ebeam_pdk is installed via KLayout package manager. Error: %s\n\nDebug Information:\n' % (opics_lib_name, str(e)))
else:
# Original code for other libraries
opics_lib_name = "opics_"+each_lib
try:
import importlib
opics_lib = importlib.import_module(opics_lib_name)
from importlib import reload
opics_lib = reload(opics_lib)
globals()[opics_lib_name] = opics_lib
libs_comps[each_lib] = opics_lib.component_factory
except:
raise Exception('Unable to import the library: %s' % opics_lib_name)
# Original code for other libraries
opics_lib_name = "opics_"+each_lib
try:
# import the library named opics_xxx where xxx is the library name
import importlib
opics_lib = importlib.import_module(opics_lib_name)
from importlib import reload
opics_lib = reload(opics_lib)
globals()[opics_lib_name] = opics_lib
libs_comps[each_lib] = opics_lib.component_factory
except:
raise Exception('Unable to import the library: %s' % opics_lib_name)

# add circuit components
for i in range(len(circuitData["compModels"])):
Expand Down

0 comments on commit f5b2e43

Please sign in to comment.