Menu

Custom UO in python : Save as a type?

2024-02-19
2024-02-20
  • Esben Jacobsen

    Esben Jacobsen - 2024-02-19

    I have made my own custom filter UO in a Python script block. It works as I want it for now, but I need about 20 of the same filter-unit in my flowsheet and I want to be able to edit the python code in one place and not have to edit all 20 units every time i need to make a small change to the code.
    How can this be done? Can I save the custom block as a type somehow?

     
  • Daniel Medeiros

    Daniel Medeiros - 2024-02-19

    You can save the main part of the code in a new script on the script manager, then call it using

    code = Flowsheet.GetScriptText('MyScript')
    
    exec(code)
    
     
  • Esben Jacobsen

    Esben Jacobsen - 2024-02-20

    Hello Daniel,
    That sound like an acceptable solution.
    However when I move the code to the script editor and use the exec(code) in the Python Script I get an error. I think I am missing some small simple thing because I get the same error at the first line of code no matter what I put in the script.
    This error:

    System.Exception: File "<string>", line 7
    feed=ims1
    ^
    SyntaxError: unexpected token '\r' ---> Microsoft.Scripting.SyntaxErrorException: unexpected token '\r'</string>

     
  • Daniel Medeiros

    Daniel Medeiros - 2024-02-20

    Sorry, try this:

    code = Flowsheet.GetScriptText('MyScript').replace('\r', '')
    
    exec(code)
    
     
  • Esben Jacobsen

    Esben Jacobsen - 2024-02-20

    Daniel, that worked !
    Thank you very much for the fast support 👍

    I have been using DWSIM only a few days and I already have some very nice simulations going. And I have the source code compiled in Visual Studio so I can dig a bit into the code. Very useful.

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.