Question

run quick translator by c#

  • 10 September 2019
  • 15 replies
  • 14 views

Badge +1

Dear everybody,

 

I'm not quiet a FME-guy and neither very good in C#, however I need to combine those 2 now. :-). Have done some research on the internet, but can't really find an answer so maybe you guys know what to do. The problem is:

 

1) Some guy in my company made an FME-routine which I can run by going to the routine, right click, and then choose "run with quick translator", which opens up the necesarry window.

2) In autocad I've got some C# code which loads a palette, from which several dvb-files can be started.

 

What I want is to add the FME-routine to that palette. In other words, I need some c#-code for getting to the routine, right click on it, and chose 'run with quick translator'. Anyone who's more familiar with all this got any code for this? Or at least can give me some direction?

 

Some help would really be appreciated.

 

Thanks!


15 replies

Userlevel 2
Badge +17

Hi @sveurink,

If you installed the SDK as part of your FME installation (always a good idea), you have access to FMEObjects, which allows using FME functionality in your own code.

In your case, you can call FMEOWorkspaceRunner.PromptRun() within your code to get the equivalent of the QuickTranslator. The method will pop up the same dialog to get the workspace parameters (source file, destination file, etc.), then run the workspace with those settings. It returns True if the workspace is successful, False otherwise.

For more information, please see the FMEObjects tutorial in your <FME Install>\\fmeobjects\\doc\\fmeobjects_tutorial folder, and the .NET API documentation in <FME Install>\\fmeobjects\\dotnet\\apidoc.

Badge

As part of my initial certification, I wrote an FME Launcher application in C#. This allows you to select a workspace, from multiple directories. It then displays the user parameters for you to modify as necessary before clicking go - which runs the workspace from the command line.

Badge +1

Sorry for my late respons, been kind of busy last days.

 

Anyways, thanks for both of your answers, I'll study them. If there's any questions I'll ask them.

 

Edit:

@daveatsafe I'm working at a company where you don't do installations yourself but by some program, but I guess the sdk is installed because I do have an directory called fmeobjects.

However, I've added a reference to the fmeobjectsdotnet4.dll, but still I can't find the fmeworkspacerunner.promptrun(). when I type this into the 'click button' void it isn't recognized.

Probably I've made some very dumb mistake, like I said I barely can program any C#. You've got any idea why it isn't recognized? I cant add a using .dll either, it's also not recognized.

Next to that:

 

- I guess, when I've got the .promptrun working, adding a path to the right workspace? might automatically open the right workspace? (without the user needing to find it on the harddrive?). Because there's more workbenches available and this number might even expand, I think it would be preferred to automatically activate the right one by clicking the right button in autocad.

 

Userlevel 2
Badge +17

Sorry for my late respons, been kind of busy last days.

 

Anyways, thanks for both of your answers, I'll study them. If there's any questions I'll ask them.

 

Edit:

@daveatsafe I'm working at a company where you don't do installations yourself but by some program, but I guess the sdk is installed because I do have an directory called fmeobjects.

However, I've added a reference to the fmeobjectsdotnet4.dll, but still I can't find the fmeworkspacerunner.promptrun(). when I type this into the 'click button' void it isn't recognized.

Probably I've made some very dumb mistake, like I said I barely can program any C#. You've got any idea why it isn't recognized? I cant add a using .dll either, it's also not recognized.

Next to that:

 

- I guess, when I've got the .promptrun working, adding a path to the right workspace? might automatically open the right workspace? (without the user needing to find it on the harddrive?). Because there's more workbenches available and this number might even expand, I think it would be preferred to automatically activate the right one by clicking the right button in autocad.

 

Hi @sveurink,

Please ensure that you have declared FMEObjects in your C# source file:

using Safe.FMEObjects;

The different FMEObjects APIs name their objects and methods a bit differently. For C#, you should call:

bool didItRun = FMEOWorkspaceRunner.PromptRun();

didItRun will be given the value of true if the workspace ran successfully, false otherwise.

Badge +1

Hi @sveurink,

If you installed the SDK as part of your FME installation (always a good idea), you have access to FMEObjects, which allows using FME functionality in your own code.

In your case, you can call FMEOWorkspaceRunner.PromptRun() within your code to get the equivalent of the QuickTranslator. The method will pop up the same dialog to get the workspace parameters (source file, destination file, etc.), then run the workspace with those settings. It returns True if the workspace is successful, False otherwise.

For more information, please see the FMEObjects tutorial in your <FME Install>\\fmeobjects\\doc\\fmeobjects_tutorial folder, and the .NET API documentation in <FME Install>\\fmeobjects\\dotnet\\apidoc.

Could you please explain the following sentence a little?

"you can call FMEOWorkspaceRunner.PromptRun() within your code"

how do i do this? I'm trying to but I can't get it done. so frustrating.

Userlevel 2
Badge +17

Could you please explain the following sentence a little?

"you can call FMEOWorkspaceRunner.PromptRun() within your code"

how do i do this? I'm trying to but I can't get it done. so frustrating.

Please show me your code for the section where you want to run the workspace, and which variable you are storing the workspace location in.

Badge +1

Please show me your code for the section where you want to run the workspace, and which variable you are storing the workspace location in.

the place where I want to run the workspace is: 

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

its by the click of a button. 

 

I've not stored the location in a variable yet. I expected something similar to: 

private void Projectie_Click(object sender, EventArgs e)
        {
            dynamic acadapplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
            acadapplication.loaddvb(@"P:\.....\Dwarsprofiel_03_R2017 v4.1.dvb");
            acadapplication.runmacro("projectieNLCS");
        }

(which works to run a macro) would be good enough. 

 

 

 

 

Userlevel 2
Badge +17

the place where I want to run the workspace is: 

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

its by the click of a button. 

 

I've not stored the location in a variable yet. I expected something similar to: 

private void Projectie_Click(object sender, EventArgs e)
        {
            dynamic acadapplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
            acadapplication.loaddvb(@"P:\.....\Dwarsprofiel_03_R2017 v4.1.dvb");
            acadapplication.runmacro("projectieNLCS");
        }

(which works to run a macro) would be good enough. 

 

 

 

 

Hi @sveurink,

Please try:

        private void button1_Click(object sender, EventArgs e)
        {
            bool didItRun = FMEOWorkspaceRunner.PromptRun(@"c:\temp\myworkspace.fmw"); 
        }

Also add the following to the top of your file:

using Safe.FMEObjects;
Badge +1

Hi @sveurink,

Please try:

        private void button1_Click(object sender, EventArgs e)
        {
            bool didItRun = FMEOWorkspaceRunner.PromptRun(@"c:\temp\myworkspace.fmw"); 
        }

Also add the following to the top of your file:

using Safe.FMEObjects;

Thanks 

With the using Safe.FMEObjects it should work yes. 

However I can't get it to work yet, because of 'protection problems': 

When I do: "FMEOWorkspaceRunner.Promptrun" it is protected and tells me to use the "IFMEOWorkspaceRunner.Promptrun". However, when using that one, it gives me: "an object reference is required for the non static field, method, property '....'". 

I'm trying to find out how to solve this, haven't got an answer yet, but guess this can't be to hard. However If you got any ideas on that they would be helpfull. 

Thanks anyways.  

 

 

 

 

Userlevel 2
Badge +17

Thanks 

With the using Safe.FMEObjects it should work yes. 

However I can't get it to work yet, because of 'protection problems': 

When I do: "FMEOWorkspaceRunner.Promptrun" it is protected and tells me to use the "IFMEOWorkspaceRunner.Promptrun". However, when using that one, it gives me: "an object reference is required for the non static field, method, property '....'". 

I'm trying to find out how to solve this, haven't got an answer yet, but guess this can't be to hard. However If you got any ideas on that they would be helpfull. 

Thanks anyways.  

 

 

 

 

Hi @sveurink,

Sorry, that was a mistake on my part - forgot the preceding 'I'. Please try:

private void button1_Click(object sender, EventArgs e)
        {
            bool didItRun = IFMEOWorkspaceRunner.PromptRun(@"c:\temp\myworkspace.fmw"); 
        }

You will also need to add a reference in your project to 'C:\Program Files\FME\FMEObjectsDotNet4.dll.

Badge +1

Hi @sveurink,

Sorry, that was a mistake on my part - forgot the preceding 'I'. Please try:

private void button1_Click(object sender, EventArgs e)
        {
            bool didItRun = IFMEOWorkspaceRunner.PromptRun(@"c:\temp\myworkspace.fmw"); 
        }

You will also need to add a reference in your project to 'C:\Program Files\FME\FMEObjectsDotNet4.dll.

About the reference to FMEObjectsDotNet4.dll I had this one in my references allready. 

Still, when I put in the code the way you described it gives me the error: 

CS0120 - An object reference is required for the non-static field, method, or property 'IFMEOWorkspaceRunner.PromptRun(string)'

guess the word 'reference' means something else here. any idea how to solve this?

Userlevel 2
Badge +17

Hi @sveurink,

I'm sorry, it's been a while since I used FMEObjects in C#. I forgot the most important part: creating the FMEObjects session.

There is a good example in Program Files\FME\fmeobjects\samples\CSharp\Viewer. It's fairly large, but it does illustrate how FMEObjects should be used.

In the MainForm method, FME Objects is initialized by creating an FME session, and a log object is created for logging any warnings or errors

         // Create the session object. Every FME Objects application requires
         // a session
         fmeSession_ = FMEObjects.CreateSession();

         // Always initialize the session. If no special initialization is
         // needed, simply set it to null or pass in an empty StringCollection
         // as a parameter, otherwise pass in a StringCollection containing
         // initialization directives.
         StringCollection sessionDirectives = new StringCollection();
         sessionDirectives.Add("FME_USE_RICH_GEOMETRY");
         sessionDirectives.Add("yes");
         fmeSession_.Init(sessionDirectives)
         
         // Get the log file
         DirectoryInfo dirOneLevelUp = Directory.GetParent(Directory.GetCurrentDirectory());
         DirectoryInfo dirTwoLevelUp = Directory.GetParent(dirOneLevelUp.FullName);
         string logFileDir = dirTwoLevelUp.FullName;

         fmeLogFile_ = fmeSession_.LogFile();
         fmeLogFile_.SetFileName(logFileDir + @"\CSharpViewer.log", false);


 

The Button click methods brings up a dialog to locate the workspace, and sends it to another method to actually run the workspace. Any errors are posted to the log file:

      //-----------------------------------------------------------------------
      // PromptRunWorkspace_Click
      private void PromptRunWorkspace_Click(object sender, EventArgs e)
      {
         // Bring up a file select dialog.
         OpenFileDialog fileDialog = new OpenFileDialog();
         fileDialog.Filter = "FME Workspaces (*.fmw)|*.fmw";
         fileDialog.Title = "Select Workspace";

         String workspace = "";
         if (fileDialog.ShowDialog() == DialogResult.OK)
         {
            workspace = fileDialog.FileName;
         }

         fileDialog.Dispose();
         fileDialog = null;

         if (workspace != null)
         {
            if (workspace == "")
            {
               // User pressed cancel
               fmeLogFile_.LogMessageString("User Pressed Cancel.",
                                            FMEOMessageLevel.Inform);
               return;
            }
            fmeLogFile_.LogMessageString("Workspace selected: " + workspace,
                                         FMEOMessageLevel.Inform);
            try
            {
               PromptRunFMEWorkspace(workspace);
            }
            catch (FMEOException ex)
            {
               // Log errors to log file
               fmeLogFile_.LogMessageString(ex.FmeErrorMessage, FMEOMessageLevel.Error);
               fmeLogFile_.LogMessageString(ex.FmeStackTrace, FMEOMessageLevel.Error);
               fmeLogFile_.LogMessageString(ex.FmeErrorNumber.ToString(), FMEOMessageLevel.Error);

               // Clean up any outstanding objects
               this.Dispose();
               // Now exit the application
               Application.Exit();
            }
         }
      }

 

The Button click method calls the following to actually run the workspace:

      //=====================================================================
      public bool PromptRunFMEWorkspace(String workspace)
      {
         IFMEOWorkspaceRunner runner = fmeSession_.CreateWorkspaceRunner();
         // Run the workspace
         bool success = runner.PromptRun(workspace);
         runner.Dispose();

         if (success)
         {
            MessageBox.Show("The workspace was run successfully, check log file for details.",
                            "FME Run Workspace", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            fmeLogFile_.LogMessageString("The workspace was run successfully",
                                         FMEOMessageLevel.Inform);
         }
         else
         {
            MessageBox.Show("The workspace run failed, check log file for details.",
                            "FME Run Workspace", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            fmeLogFile_.LogMessageString("The workspace run failed",
                                         FMEOMessageLevel.Error);
         }

         return success;
      }
Badge +1

About the reference to FMEObjectsDotNet4.dll I had this one in my references allready.

Still, when I put in the code the way you described it gives me the error:

CS0120 - An object reference is required for the non-static field, method, or property 'IFMEOWorkspaceRunner.PromptRun(string)'

guess the word 'reference' means something else here. any idea how to solve this?

Anyone? By the way I'd been away for a month but I still don't got it solved any help would be really appreciated.

Badge +1

Hey everybody, I'm back, same problem. 

 

Last time i got it working with following code: 

public void button1_Click(object sender, EventArgs e)
        {
            string relativepathFME = Path.Combine(assemblypath, @"blablabla.fmw");
            string fullpathFME = Path.GetFullPath(relativepathFME);
 
            Process.Start("fmequicktranslator.exe");
            Process.Start(fullpathFME);
        }

However, we have updated FME version now to 2020.2, and when I run this now, it starts up FME and the quicktranslator separately. 

 

And when going back to the solution offered here, being: 

private void button1_Click(object sender, EventArgs e)
        {
            bool didItRun = IFMEOWorkspaceRunner.PromptRun(@"c:\temp\myworkspace.fmw"); 
        }

I still get same error: "an object reference is required for the non static field, method, property '...' "

 

So any ideas on this issue allready? The reference "FMEObjectsDotNet4.dll" is added correctly, also "using Safe.FMEObjects;" is in the code.

 

BTW the code above in the post above this one (#13) might be helpfull but it is way to complicated for me, doesn't make me ring any bells. But thanks anyhow

Badge +1

For your information, I also tried it by: 

Process mp = new Process();
 
 mp.StartInfo.FileName = "fmequicktranslator.exe";
mp.StartInfo.Arguments = fullpathFME;
 mp.Start();

But then it asks me about a reader and a writer... totally not the 'form' I expected. 

And by the way... it DOES work with any other program I tested it with. 

Reply