3838import logging
3939import numbers
4040import os
41+ import pathlib
4142import queue
4243import textwrap
4344import threading
@@ -450,18 +451,30 @@ def model(
450451 # set variables
451452 self ._model_name = name # Model class name
452453 self ._libraries = libraries # may be needed if model is derived from other model
453- if file is not None :
454- file_name = self ._session .omcpath (file ).resolve ()
455- else :
456- file_name = None
457- self ._file_name = file_name # Model file/package name
458454 self ._variable_filter = variable_filter
459455
460- if self ._file_name is not None and not self ._file_name .is_file (): # if file does not exist
461- raise IOError (f"{ self ._file_name } does not exist!" )
462-
463456 if self ._libraries :
464457 self ._loadLibrary (libraries = self ._libraries )
458+
459+ self ._file_name = None
460+ if file is not None :
461+ file_path = pathlib .Path (file )
462+ # special handling for OMCProcessLocal - consider a relative path
463+ if isinstance (self ._session .omc_process , OMCProcessLocal ) and not file_path .is_absolute ():
464+ file_path = pathlib .Path .cwd () / file_path
465+ if not file_path .is_file ():
466+ raise IOError (f"Model file { file_path } does not exist!" )
467+
468+ self ._file_name = self .getWorkDirectory () / file_path .name
469+ if (isinstance (self ._session .omc_process , OMCProcessLocal )
470+ and file_path .as_posix () == self ._file_name .as_posix ()):
471+ pass
472+ elif self ._file_name .is_file ():
473+ raise IOError (f"Simulation model file { self ._file_name } exist - not overwriting!" )
474+ else :
475+ content = file_path .read_text (encoding = 'utf-8' )
476+ self ._file_name .write_text (content )
477+
465478 if self ._file_name is not None :
466479 self ._loadFile (fileName = self ._file_name )
467480
@@ -1635,7 +1648,7 @@ def convertMo2Fmu(
16351648 fmuType : str = "me_cs" ,
16361649 fileNamePrefix : Optional [str ] = None ,
16371650 includeResources : bool = True ,
1638- ) -> str :
1651+ ) -> OMCPath :
16391652 """Translate the model into a Functional Mockup Unit.
16401653
16411654 Args:
@@ -1662,15 +1675,19 @@ def convertMo2Fmu(
16621675 properties = (f'version="{ version } ", fmuType="{ fmuType } ", '
16631676 f'fileNamePrefix="{ fileNamePrefix } ", includeResources={ includeResourcesStr } ' )
16641677 fmu = self ._requestApi (apiName = 'buildModelFMU' , entity = self ._model_name , properties = properties )
1678+ fmu_path = self ._session .omcpath (fmu )
16651679
16661680 # report proper error message
1667- if not os . path . exists ( fmu ):
1668- raise ModelicaSystemError (f"Missing FMU file: { fmu } " )
1681+ if not fmu_path . is_file ( ):
1682+ raise ModelicaSystemError (f"Missing FMU file: { fmu_path . as_posix () } " )
16691683
1670- return fmu
1684+ return fmu_path
16711685
16721686 # to convert FMU to Modelica model
1673- def convertFmu2Mo (self , fmuName ): # 20
1687+ def convertFmu2Mo (
1688+ self ,
1689+ fmu : os .PathLike ,
1690+ ) -> OMCPath :
16741691 """
16751692 In order to load FMU, at first it needs to be translated into Modelica model. This method is used to generate
16761693 Modelica model from the given FMU. It generates "fmuName_me_FMU.mo".
@@ -1679,13 +1696,24 @@ def convertFmu2Mo(self, fmuName): # 20
16791696 >>> convertFmu2Mo("c:/BouncingBall.Fmu")
16801697 """
16811698
1682- fileName = self ._requestApi (apiName = 'importFMU' , entity = fmuName )
1699+ fmu_path = self ._session .omcpath (fmu )
1700+
1701+ if not fmu_path .is_file ():
1702+ raise ModelicaSystemError (f"Missing FMU file: { fmu_path .as_posix ()} " )
1703+
1704+ filename = self ._requestApi (apiName = 'importFMU' , entity = fmu_path .as_posix ())
1705+ filepath = self .getWorkDirectory () / filename
16831706
16841707 # report proper error message
1685- if not os .path .exists (fileName ):
1686- raise ModelicaSystemError (f"Missing file { fileName } " )
1708+ if not filepath .is_file ():
1709+ raise ModelicaSystemError (f"Missing file { filepath .as_posix ()} " )
1710+
1711+ self .model (
1712+ name = f"{ fmu_path .stem } _me_FMU" ,
1713+ file = filepath ,
1714+ )
16871715
1688- return fileName
1716+ return filepath
16891717
16901718 def optimize (self ) -> dict [str , Any ]:
16911719 """Perform model-based optimization.
0 commit comments