Menu

How to Load/Create a new simulation from my python IDE and error when calling flowsheet in python IDE

Simon Sob
2021-01-08
2021-01-10
  • Simon Sob

    Simon Sob - 2021-01-08

    Is there anyway I can load or create a new simulation from my python IDE.

    My second question:

    using my python IDE i am trying to control DWSIM so I use this code:

    import clr
    import sys
    import subprocess

    clr.AddReference('D:\DWSIM6\DWSIM.Interfaces.dll')
    clr.AddReference('D:\DWSIM6\DWSIM.Automation.dll')
    clr.AddReference('D:\DWSIM6\DWSIM.Thermodynamics.dll')
    clr.AddReference('D:\DWSIM6\DWSIM')

    from DWSIM.Thermodynamics import *
    from DWSIM import Interfaces
    from DWSIM import Automation
    from DWSIM import *

    cooler = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.Cooler, 100, 100, 'COOLER-001')

    But I get this error:
    NameError: name 'Flowsheet' is not defined

    I think both of my questions are really asking how to fully connect my python IDE to DWSIM so I can code simulations outside DWSIM and see the calculated results in my IDE.

    Thank you for any solutions to my problem.

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-01-09

    you need to create the flowsheet from the automation interface.

    interf = DWSIM.Automation.Automation2()
    Flowsheet = interf.CreateFlowsheet()
    
     
  • Simon Sob

    Simon Sob - 2021-01-09

    thank you very much. When doing this I got an error that says:
    'InvalidOperationException: The calling thread must be STA, because many UI components require this.

    at System.Windows.Input.InputManager..ctor()
    at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
    at System.Windows.Input.KeyboardNavigation..ctor()
    at System.Windows.FrameworkElement.FrameworkServices..ctor()
    at System.Windows.FrameworkElement.EnsureFrameworkServices()
    at System.Windows.FrameworkElement..ctor()
    at Eto.Wpf.Forms.WpfPanel`3..ctor()
    at Eto.Wpf.Forms.FormHandler..ctor()
    at Eto.Wpf.Platform.<>c.<addto>b__10_90()
    at Eto.Widget..ctor()
    at Eto.Forms.Control..ctor()
    at Eto.Forms.Window..ctor()
    at DWSIM.UI.Forms.Flowsheet..ctor() in C:\Users\Daniel\source\repos\DanWBR\dwsim6\DWSIM.UI.Desktop.Forms\Forms\Flowsheet\Flowsheet.cs:line 13
    at DWSIM.Automation.Automation2.CreateFlowsheet() in C:\Users\Daniel\source\repos\DanWBR\dwsim6\DWSIM.Automation\Interface.cs:line 190</addto>

    STA stands for Single Thread Apartment.

    So I know each Object listed should be on a single thread and I need to initialize COM library for each thread. But the .Net documentation on Microsoft has all examples in C# do you know how to go about this since im completely lost. Once again thank you.

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-01-09

    Check this: https://gist.github.com/DanWBR/c355fd5420d20d960f5d084a7142cde8

    You may have to add

    import pythoncom
    pythoncom.CoInitialize()
    

    before everything else.

     
  • Simon Sob

    Simon Sob - 2021-01-09

    Ah okay thank you it worked! Just for anyone watching the thread I'm using this in jupyter notebook so you can use DWSIM in an ipynb. Just One more thing is it possible to code from outside DWSIM and view a screenshot of the flowsheet. So you can see the a picture of the components and streams in your python IDE without opening the simulator itself. Thanks again

     
  • Simon Sob

    Simon Sob - 2021-01-09

    Also where is the documentation for python and DWSIM sorry I was struggling to find it on the website. Ive only been able to locate the C# thanks in advance

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-01-09

    You can try this (VB code), but I don't know if it will work on Python:

    Using bmp As New SKBitmap(width, height)
                Using canvas As New SKCanvas(bmp)
                    canvas.Scale(Zoom)
                    PFDSurface.UpdateCanvas(canvas)
                    Dim d = SKImage.FromBitmap(bmp).Encode(SKEncodedImageFormat.Png, 100)
                    Using str As New IO.MemoryStream
                        d.SaveTo(str)
                        image = Image.FromStream(str)
                    End Using
                End Using
    End Using
    

    The "SK" objects are contained in the SkiaSharp.dll assembly. Image comes from System.Drawing.dll

    PFDSurface is the PFD drawing area, you can get a reference to it through

    PFDSurface = Flowsheet.GetSurface()
    
     

    Last edit: Daniel Medeiros 2021-01-09
  • Simon Sob

    Simon Sob - 2021-01-10

    The code above didnt work in my python IDE but I think you can connect python and VB.NET so I will try that and when I find a solution I will post it on here, but thank you for your help

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-01-10

    This works (tested):

    clr.AddReference(dwsimpath + "SkiaSharp.dll")
    clr.AddReference("System.Drawing")
    
    from SkiaSharp import SKBitmap, SKImage, SKCanvas, SKEncodedImageFormat
    from System.IO import MemoryStream
    from System.Drawing import Image
    from System.Drawing.Imaging import ImageFormat
    
    PFDSurface = sim.GetSurface()
    
    bmp = SKBitmap(1024, 768)
    canvas = SKCanvas(bmp)
    canvas.Scale(1.0)
    PFDSurface.UpdateCanvas(canvas)
    d = SKImage.FromBitmap(bmp).Encode(SKEncodedImageFormat.Png, 100)
    str = MemoryStream()
    d.SaveTo(str)
    image = Image.FromStream(str)
    imgPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "pfd.png")
    image.Save(imgPath, ImageFormat.Png)
    str.Dispose()
    canvas.Dispose()
    bmp.Dispose()
    
    from PIL import Image
    
    im = Image.open(imgPath)
    im.show()
    
     
  • Simon Sob

    Simon Sob - 2021-01-10

    Yes it worked! Thank you for being extremely helpful! Hopefully this will help others aswell! I think I need to have better knowledge of .NET to help me with my customization, but thanks again!

     
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.