Guides and Articles

Use this documentation when building your Trimble Connect apps

Code example: Get name of the active Trimble Connect project

Updated: 22 May 2019

This code example provides step by step instructions for creating the first simple application with Trimble Connect for Desktop API.

You will be using Microsoft Visual Studio as the development environment. Instructions are connected to an example provided in the class level API Reference documentation to support familiarization of the whole API.

 

  1. Open Microsoft Visual Studio

  2. Create a new Visual C# Console Application project

  3. Add References to the following class libraries from the Trimble Connect for Desktop installation folder:

    • Trimble.Connect.Desktop.API.dll

    • Trimble.Connect.Desktop.API.Common.dll

    Trimble Connect API code example - references

  4. Check the Properties of the added references. Make sure that they have the option Copy Local set to false.

    Trimble Connect API code example - copy local

  5. Open the class level documentation from the Namespaces section

  6. Open the documentation for TrimbleConnectDesktopClient class

  7. Copy the code example from the TrimbleConnectDesktopClient class documentation. The code should look like this:

    namespace TrimbleConnectDesktopAPISampleApplication
    {
        using System;
    
        using Trimble.Connect.Desktop.API;
    
        class Program
        {
            static void Main(string[] args)
            {
                var example = new Example();
    
                Console.WriteLine("Connect to Trimble Connect for Desktop by pressing any key...");
                Console.ReadKey();
                Console.WriteLine();
    
                if (example.Connect())
                {
                    Console.WriteLine("Connection succeeded! Press any key to start the operation...");
                    Console.ReadKey();
                    Console.WriteLine();
                    example.Example1();
                    Console.WriteLine("Operation completed! Press any key to continue...");
                    Console.ReadKey();
                    Console.WriteLine();
                    example.Disconnect();
                }
                else
                {
                    Console.WriteLine("Connection to Trimble Connect for Desktop failed.");
                }
            }
        }
    
        public class Example
        {
            private readonly TrimbleConnectDesktopClient client = new TrimbleConnectDesktopClient();
    
            private bool connected = false;
    
            public bool Connect()
            {
                this.connected = this.client.Connect();
    
                return this.connected;
            }
    
            public void Disconnect()
            {
                this.client.Disconnect();
                this.connected = false;
            }
    
            public void Example1()
            {
                if (this.connected)
                {
                    var activeProject = this.client.ProjectManager.GetActiveProject();
    
                    if (activeProject != null)
                    {
                        Console.WriteLine("The name of the active project is: {0}", activeProject.Name);
                    }
                }
            }
        }
    }

     

  8. Replace the existing content in the Microsoft Visual Studio's code editor with the copied content

  9. Build the application

  10. Run Trimble Connect for Desktop and open a project

  11. Start debugging the application by pressing Start button with green triangle on the main toolbar

  12. Verify that the output written to console application is correct

  13. Close the application