Guides and Articles

Use this documentation when building your Tekla Structures apps

Code example: Create model plug-in using Windows Forms

Updated: 1 Nov 2017

The following steps create a basic Tekla Structures model plug-in that uses Windows Forms.

Note:
Unfortunately, the whole source code of this example is not available for downloading. You can copy-paste the code snippets to your own project. 

  1. In Microsoft Visual Studio, create a new project. Select Class Library(.Net framework) as template and enter a name for your project.

  2. Add Tekla API assemblies to your project as references.

  3. Rename Class1.cs to MainPlugin.cs (this could be a more specific name).

  4. Edit MainPlugin.cs:

    • Add assemblies needed: 

      using TSDatatype = Tekla.Structures.Datatype;
      using TSModel = Tekla.Structures.Model;
      using TSPlugins = Tekla.Structures.Plugins;
    • Add a method that includes attributes passed from the dialog to the plug-in. Add the lines for each attribute on the dialog. 

      Public class StructuresData
      {
          [TSPlugins.StructuresField("ATTRIBUTE_NAME")]     
          public VARIABLE_TYPE VARIABLE_NAME;
      }

      Replace ATTRIBUTE_NAME with the name entered for the AttributeName property, VARIABLE_TYPE with a type corresponding to AttributeNameType, and VARIABLE_NAME with a suitable variable name.

    • Add the description of your plug-in: 

      [TSPlugins.Plugin("FormPlugin")]
      [TSPlugins.PluginUserInterface("FormPlugin.MainForm")] 
    • Inherit from PluginBase and add code to MainPlugin class: 

      public class MainPlugin : TSPlugins.PluginBase
      {
          // Enable inserting of objects in a model
          private readonly TSModel.Model _model; 
       
          public TSModel.Model Model     
          {         
              get { return _model; }     
          } 
          
          // Enable retrieving of input values
          private readonly StructuresData _data; 
       
          public MainPlugin(StructuresData data)     
          {         
              // Link to model.         
              _model = new TSModel.Model(true); 
       
              // Link to input values.         
              _data = data;     
          } 
      
          // Specify the user input needed for the plugin.
          public override List<InputDefinition> DefineInput()     
      {     
          // Define input objects.     
      }         
      
      // This method is called upon execution of the plug-in and it´s the main method of the plug-in
      public override bool Run(List<InputDefinition> input)     
      {   
          try     
          {             
              // Write your code here.         
          }         
          catch (Exception e)           
          {             
              MessageBox.Show(e.ToString());         
          } 
          return true;     
      }
  5. On Project menu, click Add Windows Form to add a form. Name it MainForm.cs or something specific. Design the dialog and bound the values of controls to attributes.

  6. In Mainform.cs, add event handlers for the OK and Modify buttons: 

    Private void okButton_Click(object sender, EventArgs e)
    {
        this.Apply();
        this.Close();
    }
    
    Private void modifyButton_Click(object sender,EventArgs e)
    {
        this.Modify();
    }
  7. Build the project and copy the .dll file created to \TeklaStructures\[version]\nt\bin\plugins. This adds your plug-in directly to the Applications & components catalog in Tekla Structures.

  8. Run Tekla Structures and look for the plug-in in the Applications & components catalog.

Note:
Plug-ins won't necessarily work in other Tekla Structures versions than the one they were created for. To use a plug-in in a different version, compile it for that version.