How to use Tekla Macro Scripts with Tekla API

Tekla Macro Scripts - using in c# application or script
Quick view scale switch app example

If you carefully study the Tekla API, then pretty soon you will notice that the functionality which is listed at Reference Documentations does not exhaust the whole possibilities of Tekla Structures. If you have access to the Tekla Extranet, in the appropriate forums, you can see that it is indeed the case. However a way to use all the functions exists. It consists in working with Tekla Macro Scripts.

Tekla Open API Limits

There is no documentation for macro script writing (at least I couldn’t find it). But actually creating, recording and analysis of scripts at Tekla is a task which is not easy at first glance, but in fact it’s trivial. Unsolved question is how to embed scripting functionality to your  C# application?

Below you could find listing of the code that is binds on button of application interface. This function create, write as script and invoke that script. As result script changes the scale of the selected view on current drawing and switch it on value that is set in the button text. Script it self call the standard View Dialog, throws all of it’s check boxes, sets a new scale, marks scale check box, apply new settings and close dialog.

Tekla Macro Scripts example:

//Change Scale of View on Tekla Drawing
        private void btViewScale_Click(object sender, EventArgs e)
        {
            string Name = GetMacroFileName();//Temporary file name.
            string MacrosPath = string.Empty;//would store Path to script
            Tekla.Structures.TeklaStructuresSettings.GetAdvancedOption("XS_MACRO_DIRECTORY", ref MacrosPath);//Find out where is actual macro directory.
            if (MacrosPath.IndexOf(';') > 0) { MacrosPath = MacrosPath.Remove(MacrosPath.IndexOf(';')); }
            
            //Create a script as string
            string script2 = "namespace Tekla.Technology.Akit.UserScript" +
                            "{" +
                            " public class Script" +
                                "{" +
                                    " public static void Run(Tekla.Technology.Akit.IScript akit)" +
                                    " {" +
            @"akit.Callback(""acmd_display_attr_dialog"", ""view_dial"", ""main_frame"");" +
            @"akit.PushButton(""view_on_off"", ""view_dial"");" +
            @"akit.ValueChange(""view_dial"", ""gr_view_scale_en"", ""1"");" +
            @"akit.ValueChange(""view_dial"", ""gr_view_scale"", """ + (sender as Button).Text + @""");" +
            @"akit.PushButton(""view_modify"", ""view_dial"");" +
            @"akit.PushButton(""view_ok"", ""view_dial"");" +
                                    "}}}";

       //Save to file
            File.WriteAllText(Path.Combine(MacrosPath, Name), script2);
       //Run it at Tekla Structures application
            Tekla.Structures.Model.Operations.Operation.RunMacro("..\\" + Name);
        }
        
        //Additional Function - File name generator
                private string GetMacroFileName()
        {
            lock (Random)
            {
                if (_TempFileIndex < 0)
                {
                    _TempFileIndex = Random.Next(0, MaxTempFiles);
                }
                else
                {
                    _TempFileIndex = (_TempFileIndex + 1) % MaxTempFiles;
                }

                return string.Format(FileNameFormat, _TempFileIndex);
            }
        }

 

2 thoughts on “How to use Tekla Macro Scripts with Tekla API

Comments are closed.