Page 1 of 1

Using COFE from COM

Posted: 25 February 2014, 11:08
by HenkF
Hi,
I'm trying to use COFE from a Lazarus (Free Pascal) setup. I guess this will be quite a struggle, as the COM support of Lazarus has not been fully developed yet.
Nevertheless, using the automation documents you provided I've been able to initialize COFE. I even have been able to generate pascal source from the TLB file!
I'm now trying to load an existing flowsheet into the automation object. This should be done by something like GetObject("FileName.fsd").
Does this mean that I should create the COFE COM object directly from a file? Is there a way to directly load a file into an existing COFE Document (LoadFromstring obviously isn't meant for that, it provides me with a typing error)

Regards,
Henk

Re: Using COFE from COM

Posted: 25 February 2014, 11:32
by jasper
LoadFromString and SaveToString match each other; these strings are not file names, but string-encoded (b64) versions of the fsd file content. You should not call LoadFromString on documents that have any content in it:

http://cocosimulator.org/index_help.php ... FromString

Note that COFE is not exposed as just a COM object, but as OLE object; this implies that is requires an OLE site to interact with. The OLE machinery to start from an existing file therefore is supported; I suppose this is what GetObject does in Lazarus. Typically environments that support OLE also support document persistence.

Re: Using COFE from COM

Posted: 26 February 2014, 09:31
by HenkF
Hi Jasper,

Thanks for your

I've looked at the OLE implementation within Lazarus and Delphi. With Delphi it's just OK to either create an empty server instance, or create one via an existing document.
Lazarus lacks that support. The server instance is created, and I can e.g. call ValidateFlowsheet, which indeed tells me the flowsheet is empty. But... no way to load and use an existing flowsheet...
Maybe I can create a workaround via Excel : Creating and generating an Excel instance works fine within Lazarus. As the Excel instance offers a way to open and use a worksheetfile.

But... would it also be possible to add a LoadFromFile option to the COFE OLE server?

Below a snippet of code capable of creating a COFE server instance. (late binding).

Code: Select all

procedure TMain.OpenFlowsheetClick(Sender: TObject);
const
  COFEServerName = 'COCO_COFE.Document';

var
  Server     : Variant;                 

var
  FileName : Variant;
  Count    : Integer;
  Messages : Variant;
begin
  try
    FileName := 'c:\warmtepomp.rg.h2o.3.1.fsd';
    Server   := GetActiveOleObject(COFEServer);
  except
    try
      // no active server available, create a new one
      Server := CreateOleObject(COFEServerName);
    except
      ShowMessage('Unable to start '+COFEServerName+'.');
    end;
    Server.ValidateFlowsheet(Messages);
    ShowMessage(VarToStr(Messages[0]));
  end;
end; 
regards, Henk

Re: Using COFE from COM

Posted: 26 February 2014, 12:11
by jasper
Not willing to break binary compatibility on the existing interface for that, nor willing to define a new interface for that. Instead, use LoadFromString, and pass "http:///" followed by the file name. Update available via CUP.

Re: Using COFE from COM

Posted: 26 February 2014, 14:17
by HenkF
Wow Jasper, thanks man! :D

Sure you shouldn't break the compatibility. Lazarus hasn't been fully matured regarding COM/OLE implementation (it's cross platform, and OLE is a Windows thing), but this will surely help me out.
Recalling the usage: after creating the object I could then use something like Server.LoadFromeString('http:///c:/warmtepomp.rg.h2o.3.1.fsd'); to load an existing flowsheet?

Regards,
Henk

Re: Using COFE from COM

Posted: 26 February 2014, 14:29
by HenkF
Got it! Figured out by your update notes. Entering 'file:///' instead of 'http:///', and now it's running! :D :D :D

Thnx

Re: Using COFE from COM

Posted: 26 February 2014, 14:34
by jasper
Sorry - typing error. Glad to hear it works.