Menu

C# (DSSsharp) help needed

Beginners
2021-05-31
2022-07-20
  • Neil Higgins

    Neil Higgins - 2021-05-31

    I am not at all familiar with C#, but I need to do a small project with it. I can adapt pretty quickly given a few hints. I have had a look at DSSsharp. It builds perfectly from source, no errors, in Visual Studio. Sadly there doesn't seem to be even a basic example in the github repository, showing how to use it.

    Could a more expert person than me please provide the C# equivalent to the following Python code? I would be extremely grateful. Sorry about the large print - I think that is SourceForge interpreting # codes as Markdown (or some such).


    def doOpenDSSCommand(an_engine, a_command):
    print("Command: ", a_command)
    an_engine.Text.Command = a_command

    Import the win32com from the pywin32 package

    import win32com.client
    import os

    data_folder = "C:/Users/ether/Documents/DT Partners/CD001 - AVR Spec/Phase2-OpenDSSFamiliarisation/OpenDSSControl/"
    print("Data folder = ", data_folder)

    Define the com engine

    engine=win32com.client.Dispatch("OpenDSSEngine.DSS")

    Start the engine

    if engine.Start("0"):
    print("Engine started ok")
    else:
    print("Engine failed to start")
    os.exit()

    Command can be given via text as shown below

    doOpenDSSCommand(engine, 'clear') # or equivalently engine.ClearAll

    Prepare the circuit

    circuit=engine.ActiveCircuit

    Then we can compile a DSS file

    file_to_open = data_folder + "prototype_example.dss"
    print("File = ", file_to_open)
    doOpenDSSCommand(engine, 'compile "'+ file_to_open + '"')

    This command opens a text file which contains the SYMMETRICAL COMPONENT VOLTAGES BY BUS (for 3-phase buses)

    doOpenDSSCommand(engine, 'show voltage')

    This command opens a text file which contains the PHASE-PHASE VOLTAGES BY BUS & NODE

    doOpenDSSCommand(engine, 'show voltage LL nodes')

    This command opens a text file which contains the NODE-GROUND VOLTAGES BY BUS & NODE

    doOpenDSSCommand(engine, 'show voltage LN nodes')

    It is possible to have names of all the buses.

    names=engine.ActiveCircuit.AllBusNames
    n_bus=len(names)
    print("Number of buses = ", n_bus)
    print("Bus names = ", names)

     
    • Davis Montenegro

      Hello,

      This is a very simple script, I would do it with a console app as follows:

      using System;
      using System.Runtime.InteropServices;
      using OpenDSSengine;
      
      namespace ConsoleApp1
      {
          using System.IO;
      //    using NUnit.Framework;
          class Program
          {
              static void Main(string[] args)
              {
                  string currentDir = AppDomain.CurrentDomain.BaseDirectory;
                  var OpenDSS = new DSS();
                  if (OpenDSS.Error.Number != 0)
                  {
                      throw new ApplicationException($"Error in initializing OpenDSS: {OpenDSS.Error.Description}. ");
                  }
                  else
                  {
                      string data_folder = @"C:\Program Files\OpenDSS\IEEETestCases\123Bus\";
                      string file_to_open = data_folder + "IEEE123Master.dss";
                      Console.WriteLine("File= " + file_to_open);
                      var DSSText = OpenDSS.Text;
                      var DSSCircuit = OpenDSS.ActiveCircuit;
                      var DSSSolution = DSSCircuit.Solution;
                      DSSText.Command = "Compile " + '"' + file_to_open +'"';
                      DSSSolution.Solve();
                      // Line 2 Line voltages
                      DSSText.Command = "show voltage LL nodes";
                      // Line 2 ground voltages
                      DSSText.Command = "show voltage LN nodes";
                      // The all bus names
                      string[] myBusNames = DSSCircuit.AllBusNames;
                      Console.WriteLine("Number of buses = " + myBusNames.Length);
                      foreach (string BusName in myBusNames)
                      {
                          Console.WriteLine(BusName);
                      }
                  }
              }
          }
      }
      

      That should do the job. We have more examples here:

      https://sourceforge.net/p/electricdss/code/HEAD/tree/trunk/Version8/Distrib/Examples/C_Sharp/

      Best regards

      Davis

       
  • Paulo Meira

    Paulo Meira - 2021-05-31

    About DSS Sharp, it's currently on a development hiatus. The plan is to rewrite it to make it multi-platform as the other DSS Extensions, and we're finishing features in the engine code before that. Even though it has not been updated in a while, the current version is compatible with the latest DSS C-API release (just tested to be sure).

    Currently, DSS Sharp targets advanced users -- if you're not familiar with C#, it's better to just use the official COM implementation. DSS Sharp is mostly a drop-in replacement anyway -- if in the future you need its features, you'd be able to replace the COM version with minor changes (basically remove the COM reference, add the dss_sharp.dll reference, replace using OpenDSSengine; with using dss_sharp;, and remember to include the DLLs related to DSS C-API in the solution). The lack of examples is thus partially on purpose, there's little benefit in duplicating EPRI's docs and examples for everything. I'll make a note to add a basic example and how to replace the COM version with DSS Sharp.

    Some more context and useful info in the original ticket at https://github.com/dss-extensions/dss_capi/issues/3

    Other tickets to track future progress are at https://github.com/dss-extensions/dss_sharp/issues/

     
  • Neil Higgins

    Neil Higgins - 2021-06-01

    Thank you Davis, thank you Paulo for your quick responses.

    A little bit of backstory, which might lead to some further education on my part: I could start OpenDSSengine.dll without any problems, but I could not pass a command string successfully. At the time, it appeared that some kind of "hieroglyphics" (compiler directives, e.g [DllImport("dss_capi", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]) were needed to get compatible data representations on either side of the C#-COM interface. The DSSsharp code contains such directives, so I thought that using DSSsharp would ease the pain.

    Looking at Davis's code, it appears that is NOT the case: I assume the System.Runtime.InteropServices are beneficial in this regard(?) Having said that, I'm still not much the wiser :-(

    Anyway, thank you again for your help. I should be able to proceed independently from this point.

    Kind regards,
    Neil

     
  • Neil Higgins

    Neil Higgins - 2021-06-01

    Sigh. Not quite. I copied OpenDSSengine.dll to the repo, "Added" it to the project, then tried to build, and I got the following error messages. Where I have I gone wrong?

    Severity Code Description Project File Line Suppression State

    Error CS0246 The type or namespace name 'DSS' could not be found (are you missing a using directive or an assembly reference?) ConsoleApp1 C:\Users\ether\source\repos\ConsoleApp1\Program.cs 14 Active

    Error CS0246 The type or namespace name 'OpenDSSengine' could not be found (are you missing a using directive or an assembly reference?) ConsoleApp1 C:\Users\ether\source\repos\ConsoleApp1\Program.cs 3 Active

     
  • Neil Higgins

    Neil Higgins - 2021-06-01

    Ah. It's ok. I solved that problem. I had to add a reference.

     
  • Paulo Meira

    Paulo Meira - 2022-07-19

    A year later, we're testing a NuGet package for DSS Sharp, and it's much easier to install directly from Visual Studio: https://www.nuget.org/packages/dss_sharp/

    It's now multi-platform too, like the rest of our projects. More at https://github.com/dss-extensions/dss_sharp/releases/tag/0.12.1

     
    • Neil Higgins

      Neil Higgins - 2022-07-20

      For python, what are the pros and cons of opendssdirect.py versus dss-python?

      Neil Higgins (iPhone)
      higgins-demack@bigpond.com

      On 20 Jul 2022, at 5:57 am, Paulo Meira meira@users.sourceforge.net wrote:

      
      A year later, we're testing a NuGet package for DSS Sharp, and it's much easier to install directly from Visual Studio: https://www.nuget.org/packages/dss_sharp/

      It's now multi-platform too, like the rest of our projects. More at https://github.com/dss-extensions/dss_sharp/releases/tag/0.12.1

      C# (DSSsharp) help needed

      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/electricdss/discussion/beginners/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       
      • Paulo Meira

        Paulo Meira - 2022-07-20

        The TLDR is that the two share most features. Right now, I'd recommend to use what you prefer. I believe both are faster than the alternatives, but I need to check again since it's been a while.

        Both, though, are restricted to the conventions of the official OpenDSS implementation, such as only one active element of certain type, not everything is exposed, etc. And there is the backwards compatibility requirement: we have a lot of users and cannot change too much without breaking their code.
        Keep an eye out in the next month or so, as a new modernized alternative may be born (something is already gestating in the latest DSS Python under DSS.Obj)

        On to the differences...

        DSS Python was created to be used in code that used the official COM implementation of OpenDSS with few changes, providing source-code level compatibility. Besides better performance compared to the COM implementation, there are new modules/classes, functions and some pythonic magic, such as iterators, e.g.

        numLoads = len(DSS.ActiveCircuit.Loads)
        for load in DSS.ActiveCircuit.Loads:
            print(load.Name)
        

        This is not available in OpenDSSDirect.py yet but there is a pending change that will land soon. We actually have a function named patch_dss_com to apply that to the COM implementation too.

        DSS Python uses the same style of the COM implementation, using properties (read/write Loads.Name) , while OpenDSSDirect.py uses functions for most of the API (read Loads.Name() and write Loads.Name("aloadname")).

        Due to the needs for COM-API compatibility, in DSS Python we have DSS.ActiveCircuit.Loads. In ODD. py the simple form is used, opendssdirect.Loads.

        New features from DSS C-API usually land on DSS Python first, but it doesn't take long for them to be adapted for ODD.py. For example, the latest release allows creating multiple/independent OpenDSS instances:

        from dss import dss
        dss.Text.Command = 'new circuit.dss1'
        dss2 = dss.NewContext()
        dss2.Text.Command = 'new circuit.dss2'
        print(dss.ActiveCircuit.Name)
        print(dss2.ActiveCircuit.Name)
        

        Prints

        dss1
        dss2
        

        Other recent additions like the Parallel functions and our ZIP file support are already available in both packages.

        Naturally, features at engine level are shared between the two packages. The experimental plotting backend from DSS Python can be used with ODD.py:

        import opendssdirect as odd
        import dss.plot
        dss.plot.enable()
        
        odd.Text.Command('redirect somecircuit/Master.dss')
        odd.Text.Command('solve')
        odd.Text.Command('plot profile')
        odd.Text.Command('plot circuit')
        

        There are some other differences, like DSS Python returning NumPy arrays and ODD.py returning lists, but that's also something that will be changed soon.

         

        Last edit: Paulo Meira 2022-07-23
        • Neil Higgins

          Neil Higgins - 2022-07-20

          Many thanks for your commentary.

          From: discussion@electricdss.p.re.sourceforge.net
          discussion@electricdss.p.re.sourceforge.net On Behalf Of Paulo Meira
          Sent: Wednesday, 20 July 2022 1:01 PM
          To: [electricdss:discussion]
          beginners@discussion.electricdss.p.re.sourceforge.net
          Subject: [electricdss:discussion] Re: C# (DSSsharp) help needed

          The TLDR is that the two share most features. Right now, I'd recommend to
          use what you prefer. I believe both are faster than the alternatives, but I
          need to check again since it's been a while.

          Both, though, are restricted to the conventions of the official OpenDSS
          implementation, such as only one active element of certain type, not
          everything is exposed, etc. And there is the backwards compatibility
          requirement: we have a lot of users and cannot change too much without
          breaking their code.
          Keep an eye out in the next month or so, as a new modernized alternative may
          be born (something is already gestating in the latest DSS Python under
          DSS.Obj)

          On to the differences...

          DSS Python was created to be used in code that used the official COM
          implementation of OpenDSS with few changes, providing source-code level
          compatibility. Besides better performance compared to the COM
          implementation, there are new modules/classes, functions and some pythonic
          magic, such as iterators, e.g.

          numLoads = len(DSS.ActiveCircuit.Loads)
          for load in DSS.ActiveCircuit.Loads:
          print(load.Name)

          This is not available in OpenDSSDirect.py yet but there is a pending change
          that will land soon. We actually have a function named patch_dss_com to
          apply that to the COM implementation too.

          DSS Python uses the same style of the COM implementation, using properties
          (read/write Loads.Name) , while OpenDSSDirect.py uses functions for most of
          the API (read Loads.Name() and write Loads.Name("aloadname")).

          Due to the needs for COM-API compatibility, in DSS Python we have
          DSS.ActiveCircuit.Loads. In ODD. py the simple form is used,
          opendssdirect.Loads.

          New features from DSS C-API usually land on DSS Python first, but it doesn't
          take long for them to be adapted for ODD.py. For example, the latest release
          allows creating multiple/independent OpenDSS instances:

          from dss import dss
          dss.Text.Command = 'new circuit.dss1'
          dss2 = dss.NewContext()
          dss2.Text.Command = 'new circuit.dss2'
          print(dss.ActiveCircuit.Name)
          print(dss2.ActiveCircuit.Name)

          Prints

          dss1
          dss2

          Other recent additions like the Parallel functions and our ZIP file support
          are already available in both packages.

          Naturally, features at engine level are shared between the two packages. The
          experimental plotting backend from DSS Python can be used with ODD.py:

          import opendssdirect as odd
          import dss.plot
          dss.plot.enable()

          odd.Text.Command('redirect somecircuit/Master.dss')
          odd.Text.Command('solve')
          odd.Text.Command('plot voltages ln')
          odd.Text.Command('plot circuit')

          There are some other differences, like DSS Python returning NumPy arrays and
          ODD.py returning lists, but that's also something that will be changed soon.


          C# (DSSsharp) help needed
          https://sourceforge.net/p/electricdss/discussion/beginners/thread/8031cde60 e/?limit=25#4aca/5791/8bbb


          Sent from sourceforge.net because you indicated interest in
          https://sourceforge.net/p/electricdss/discussion/beginners/

          To unsubscribe from further messages, please visit
          https://sourceforge.net/auth/subscriptions/

           

Log in to post a comment.

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.