Este ejemplo muestra cómo ejecutar un archivo de ejecución de MapForce (.mfx) desde código VBScript. En Windows, los archivos de ejemplo están en: C:\Program Files\Altova\MapForceServer2025\etc\Examples.
Antes de ejecutar el siguiente código, compruebe que cumple con estos requisitos:
•MapForce Server está instalado y tiene asignada una licencia válida.
•MapForce Server está disponible como objeto de servidor COM (este proceso suele tener lugar automáticamente durante la instalación de MapForce Server. Consulte el apartado Información sobre la interfaz COM para obtener más información).
Option Explicit REM This script produces extensive output. REM It is best called from a cmd.exe console with "cscript MapForceServerAPI_sample.vbs" 'Create the MapForce Server object Dim objMFS ' Since we load a COM-DLL we need care about the process architecture On Error Resume Next ' ignore any COM errors avoiding uncontrolled script termination Dim WshShell Dim WshProcEnv Set WshShell = CreateObject("WScript.Shell") Set WshProcEnv = WshShell.Environment("Process") Dim process_architecture process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") If process_architecture = "x86" Then Set objMFS = WScript.GetObject( "", "MapForce.Server" ) If Err.Number <> 0 then WScript.Echo("You are running in a 32-bit process but MapForce Server COM-API 32-bit seems not to be installed on your system.") WScript.Quit -1 End If Else Set objMFS = WScript.GetObject( "", "MapForce_x64.Server" ) If Err.Number <> 0 then WScript.Echo("You are running in a 64-bit process but MapForce Server COM-API 64-bit seems not to be installed on your system.") WScript.Echo("If you have installed 32-bit MapForce Server consider calling your script from the 32-bit console 'C:\Windows\SysWOW64\cmd.exe.'") WScript.Quit -1 End If End If On Error Goto 0 ' re-enble default error promotion 'Set a working directory - used as a base for relative paths (you may need to adapt the path to the installation folder) REM objMFS.WorkingDirectory = "C:\Program Files (x86)\Altova\MapForceServer2020\etc\Examples" Dim currDir Dim fso Set fso = CreateObject("Scripting.FileSystemObject") currDir = fso.GetParentFolderName(Wscript.ScriptFullName) 'set working folder to parent of this script objMFS.WorkingDirectory = fso.GetParentFolderName( currDir ) 'Default path to the MapForce Server executable is the installation path (same dir with the MapForceServer.dll) 'In case you moved the binaries on the disk, you need to explicitly set the path to the .exe file 'objMFS.ServerPath = "C:\Program Files (x86)\Altova\MapForceServer2025\bin\MapForceServer.exe" 'Set global resource file and configuration, if your mapping uses global resources 'Call objMFS.SetOption("globalresourcefile", "GlobalResources.xml") '"gr" can be used as short name for "globalresourcefile" 'Call objMFS.SetOption("globalresourceconfig", "Config2") '"gc" can be used as short name for "globalresourceconfig" WScript.Echo( "Running " & objMFS.ProductNameAndVersion & vbCrlf ) ' The Run method will return 'True' if the execution of the mfx file was successful otherwise 'False'. ' In the case of fundamental errors like termination of the server process a COM error will be raised which ' can be handled using the VBScript Err object. On Error Resume Next ' ignore any COM errors avoiding uncontrolled script termination Err.Clear REM ---------------------------------------------------------------------------- REM run an example with input and output paths stored inside the MFX file ' the path to the mfx file can be absolute or relative to the working directory ' depends on existence of file AltovaTools.xml in working direcory ' creates output file AltovaToolFeatures.csv in working directory WScript.Echo( "Processing TokenizeString.mfx..." ) If ( objMFS.Run( "TokenizeString.mfx" ) ) Then 'WScript.Echo( objMFS.LastExecutionMessage ) ' execution log WScript.Echo( "Successfully generated file AltovaToolFeatures.csv." ) Else 'execution failed (e.g. somebody deleted file AltovaTools.xml) WScript.Echo( objMFS.LastExecutionMessage ) End If WScript.Echo("") ' handle COM errors If Err.Number <> 0 Then WScript.Echo("Internal error - " & Err.Description ) WScript.Quit -1 End If REM ----------------------------------------------------------------------------------------- REM this is an example creating a simple output so that we can retrieve the result explicitly ' depends on inpuz XML file ipo.xml WScript.Echo( "Processing SimpleTotal.mfx..." ) If ( objMFS.Run( "SimpleTotal.mfx" ) ) Then 'WScript.Echo( objMFS.LastExecutionMessage ) WScript.Echo( "Mapping result is: " & objMFS.GetOutputParameter("total") ) Else 'execution failed (e.g. somebody deleted file ipo.xml) WScript.Echo( objMFS.LastExecutionMessage ) End If WScript.Echo("") ' handle COM errors If Err.Number <> 0 Then WScript.Echo("Internal error - " & Err.Description ) WScript.Quit -1 End If REM ---------------------------------------------------------------------------- REM This is an example with parameterized input ' the default of 'lower=5' gets changed to the value '10' ' mfx reads file Temperatures.xml and writes its output to Temperatures_out.xml. WScript.Echo( "Processing ClassifyTemperatures.mfx with parameter 'lower' set to '10' ..." ) call objMFS.AddParameter("lower", "10") If ( objMFS.Run( "ClassifyTemperatures.mfx" ) ) Then 'WScript.Echo( objMFS.LastExecutionMessage ) WScript.Echo( "File Temperatures_out.xml has been written successfully." ) Else 'execution failed (e.g. somebody locks file Temperatures_out.xml) WScript.Echo( objMFS.LastExecutionMessage ) End If call objMFS.ClearParameterList() WScript.Echo("") ' handle COM errors If Err.Number <> 0 Then WScript.Echo("Internal error - " & Err.Description ) WScript.Quit -1 End If On Error Goto 0 ' re-enble default error promotion |