Home > Uncategorized > Scripting Carmenta Engine 5

Scripting Carmenta Engine 5

The possibility to access the Carmenta Engine via scripts is valuable in many different scenarios. Just to mention a few:

  • Testing configuration files in production environments (where Carmenta Explorer is not available)
  • Getting meta data information from datasets
  • Coordinate conversion
    The vastly improved api:s in version 5.x opens up a couple of new, or at least different, ways to access Carmenta Engine. One more thing that makes it interesting to explore the scripting facilities is that the old Carmenta specific script language (pilsner) has been replaced with a modern python interface. This means that we cannot solve some problems the way we used to do in version 4.x but, if we are fortunate, in a cooler and more efficient way. It may be worth mentioning that the python interface is primarily intended for usage within the script operator according to the documentation. I guess that the underline here is that we should not develop applications using the python interface but for these kind of tasks we should be good to go.
    I decided to try out the following script languages:
  • Python
  • Powershell
  • F# scripts (FSX)
    The first scenario that I wanted to try out was to write a script that loaded a configuration and displayed it. This should be useful when testing configurations in a production environment or anywhere that we only have a runtime installation and not the full SDK.
    Python:

from Carmenta.Engine.Core import *

 

Runtime.initialize()

config = Configuration(“C:\\Program Files\\Carmenta\\Carmenta Engine 5 SDK\\samples\\MapConfigurations\\street_map.px”)

view = config.getPublicObject(“View0”)

view.update()

view.drawable.save(“c:\\temp\\img.png”)

Runtime.shutdown()

 

Carmenta Engine includes a python distribution which makes it easy to get started. Simply locate python.exe (default is C:\Program Files\Carmenta\Carmenta Engine 5 SDK\bin) and run the script. If you intend to experiment a lot with the Carmenta Engine python interface it might be worth downloading eclipse and pydev. Features like code completion and syntax highlighting are good even when scripting.

 

Compared to the next scripts this one is not as fancy since there are no map control that fits any python gui framework.

 

    Powershell:

[System.Reflection.Assembly]::LoadFrom(“C:\\Program Files\\Carmenta\\Carmenta Engine 5 SDK\\bin\\CECore.Dotnet.dll”)
Add-Type -Assembly System.Windows.Forms

[Carmenta.Engine.Runtime]::Initialize()
$config = new-object Carmenta.Engine.Configuration -argumentlist “C:\\Program Files\\Carmenta\\Carmenta Engine 5 SDK\\samples\\MapConfigurations\\street_map.px”
$view = $config.GetPublicObject(“View0”)
$spacex = new-object Carmenta.Engine.Forms.MapControl
$spacex.View = $view
$spacex.Tool = new-object Carmenta.Engine.StandardTool
$spacex.Dock = [System.Windows.Forms.DockStyle]::Fill
$form = new-object System.Windows.Forms.Form
$form.Add_Closing({ Write-Host $view.area })
$form.Controls.Add($spacex)
[void]$form.ShowDialog()
[Carmenta.Engine.Runtime]::Shutdown()

Since Powershell is included in the windows operating system it is also very easy to get started. Simply open up a Powershell command prompt or (even better) the Powershell ISE and access Carmenta Engine using the .NET api. Not much to add here; simple and straight forward.

    F#:

#r “C:\\Program Files\\Carmenta\\Carmenta Engine 5 SDK\\bin\\CECore.Dotnet.dll”

 

open Carmenta.Engine

open System.Windows.Forms

 

Runtime.Initialize()

let config = new Configuration(“C:\\Program Files\\Carmenta\\Carmenta Engine 5 SDK\\samples\\MapConfigurations\\street_map.px”)

let view = config.GetPublicObject(“View0”) :?> Carmenta.Engine.View

let spacex = new Carmenta.Engine.Forms.MapControl(View = view, Tool = new StandardTool(), Dock = DockStyle.Fill)

let form = new Form()

form.Closing.Add(fun _ -> printf “%A” view.Area)

form.Controls.Add(spacex)

do Application.Run(form)

Runtime.Shutdown()

 

Since Carmenta Engine provides a .NET api we can also use F# scripts (.FSX). F# requires a runtime package to be installed which will disqualify for usage in production environments (unless you are already installing it for some other reason of course). For developer purposes things are better since F# ships with visual studio 2010. The script will not run unless you make modifications in your fsi.exe.config since the carmenta assemblies are not built using .net runtime 4.

PS> & ‘C:\Program Files\Microsoft F#\v4.0\fsi.exe’ ce-sample-script.fsx

error FS0193: Mixed mode assembly is built against version ‘v2.0.50727’ of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

This is my fixed fsi.exe.config with an activation policy added:

<?xml version=”1.0″ encoding=”utf-8″?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy=”true”>
    <supportedRuntime version=”v4.0″/>
  </startup>
  <runtime>
     <legacyUnhandledExceptionPolicy enabled=”true” />
  </runtime>
</configuration>

I hope that Carmenta Engine will be built against .net runtime version 4 in a near future to avoid this kind of trouble even though there might be good reasons for the current choice.

When summing up I would say that Powershell worked best for the following reasons:

  • Powershell is installed everywhere and even includes a simple but useful development environment with debugging support.
  • Powershell supports windows forms (and wpf) so that we can use the map controls.
  • The Carmenta Engine assemblies can be loaded without trouble (which might just be luck but anyway)

It is exciting to see the new Carmenta API in action and being able to script a basic map application in just a few lines like this (ignoring choice of language) is indeed powerful. Hopefully, I will have time to test a couple of other scripting scenarios soon.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: