Issue with Saving FMU File in FMIExport.jl: "Cannot find a package where this file is stored"

Hello,

I am trying to save the FMU file generated using the FMIExport package, as per the examples provided in the Manipulation folder. The following is the Julia code that I used from the example:

using FMI
using FMIExport
using FMICore
using FMIZoo
#import FMIBuild:fmi2Save 
originalGetReal = nothing # function pointer to the original fmi2GetReal c-function


# function change to be implemented

# custom function for fmi2GetReal!(fmi2Component, Union{Array{fmi2ValueReference}, Ptr{fmi2ValueReference}}, Csize_t, value::Union{Array{fmi2Real}, Ptr{fmi2Real}}::fmi2Status
# for information on how the FMI2-functions are structured, have a look inside FMICore.jl/src/FMI2_c.jl or the FMI2.0.3-specification on fmi-standard.org
function myGetReal!(c::fmi2Component, vr::Union{Array{fmi2ValueReference}, Ptr{fmi2ValueReference}}, nvr::Csize_t, value::Union{Array{fmi2Real}, Ptr{fmi2Real}})
    global originalGetReal
    
    # first, we do what the original function does
    status = fmi2GetReal!(originalGetReal, c, vr, nvr, value)

    # if we have a pointer to an array, we must interprete it as array to access elements
    if isa(value, Ptr{fmi2Real})
        value = unsafe_wrap(Array{fmi2Real}, value, nvr, own=false)
    end
    if isa(vr, Ptr{fmi2Real})
        vr = unsafe_wrap(Array{fmi2Real}, vr, nvr, own=false)
    end

    # now, we multiply the position sensor output by two (just for fun!)
    for i in 1:nvr 
        if vr[i] == 335544320 # value reference for "positionSensor.s"
            value[i] *= 2.0 
        end
    end 

    # ... and we return the original status
    return status
end


#overwriting original fun with new fun, after loading fmu


# this function is called, as soon as the DLL is loaded and Julia is initialized 
# must return a FMU2-instance to work with
FMIBUILD_CONSTRUCTOR = function(resPath)
    global originalGetReal

    # loads an existing FMU inside the FMU
    fmu = loadFMU(joinpath(resPath, "SpringDamperPendulum1D.fmu"))

    # save, where the original `fmi2GetReal` function was stored, so we can access it in our new function
    originalGetReal = fmu.cGetReal

    # now we overwrite the original function
    fmi2SetFctGetReal(fmu, myGetReal!)

    return fmu
end
import FMIZoo

#path to save fmu


tmpDir = mktempdir(; prefix="fmibuildjl_test_", cleanup=false) 
@info "Saving example files at: $(tmpDir)"
fmu_save_path = joinpath(tmpDir, "Manipulation.fmu")  

sourceFMU = FMIZoo.get_model_filename("SpringDamperPendulum1D", "Dymola", "2022x")
fmu = FMIBUILD_CONSTRUCTOR(dirname(sourceFMU)) # -> returns desired fmu after implementing changed function

but the issue arises when i run the below code snippet

using FMIBuild
FMIBuild.saveFMU(fmu, fmu_save_path)

this throws the following error,

ERROR: AssertionError: ["fmiBuild(...): Cannot find a package where this file is stored in. For FMU-Export, this source file needs to be inside of a package."]

I would appreciate any help or guidance on how to resolve this issue.

Or else, it would be really helpful if someone can provide me a julia script of simplest case where i can import a fmu, manipulate a single parameter value and then save the manipulated fmu.

Thank you in advance!

This assertion should only get thrown, if you try to compile an FMU that is not stored inside of a package. FMIExport.jl compiles packages (via PackageCompiler.jl), so files including an export statement must be inside of a package (for now).

Can you try executing the script while keeping the file inside of the original directory (as part of FMIExport.jl)?

Feel free to tag me in related questions in the future, so I can find related issues faster :slight_smile:

To save the modified FMU, ensure your code is part of a Julia package or explicitly specify the package path. A simplified example involves loading the FMU, modifying a parameter, and saving the modified FMU using FMIBuild.saveFMU. Remember to replace the placeholder path with the actual path to your FMU file. By following these steps, you should be able to successfully save the manipulated FMU.