调试Tekla官方例程HeadlessTeklaStructuresExample

Tekla官方例程HeadlessTeklaStructuresExample实例不启动Tekla UI界面而运行TEKLA的例程,其代码为:

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="Copyright © 2022 Trimble Solutions Corporation. Trimble Solutions Corporation is a Trimble Company">
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace HeadlessExample
{
    using System;
    using System.IO;
    using System.Reflection;

    using Tekla.Structures.Model;
    using Tekla.Structures.Service;

    class Program
    {
        /// <summary>
        /// Main entry point
        /// </summary>
        /// <param name="args">additional version field: Ex: HeadlessExample.2021.exe 2021.0 for running example against 2021 version of ts</param>
        static void Main(string[] args)
        {
            // default ts to use. change to 2021 as example. 
            var tsVersion = "2022.0";
            if (args.Length != 0)
            {
                tsVersion = args[0];
            }

            var binDir = $@"C:\Program Files\Tekla Structures\{tsVersion}\bin";
            if (tsVersion.Equals("2021.0"))
            {
                // backwards compitbility
                binDir = $@"C:\Program Files\Tekla Structures\{tsVersion}\nt\bin";
            }

            ConfigureHeadlessIniFiles(binDir);

            AppDomain.CurrentDomain.AssemblyResolve += (s, a) => CurrentDomainAssemblyResolve(a, binDir);
            // TeklaStructuresService needs to know the location of the TS binaries.
            // additionally we need to set a resolver to load dlls from binary folder
            using (var service = new TeklaStructuresService(
                new DirectoryInfo(binDir),
                "ENGLISH",
                new FileInfo($@"C:\ProgramData\Trimble\Tekla Structures\{tsVersion}\Environments\default\env_Default_environment.ini"),
                new FileInfo($@"C:\ProgramData\Trimble\Tekla Structures\{tsVersion}\Environments\default\role_Steel_Detailer.ini")))
            {
                // change for model path
                var modelPath = @"C:\TeklaStructuresModels\New model";
                // see Initialize for role, license and identity
                service.Initialize(new DirectoryInfo(modelPath));

                //!IMPORTANT NOTICE!
                //!IMPORTANT NOTICE!
                //!IMPORTANT NOTICE!
                //You cannot run the above code when in debugger. 
                //You can add a ReadKey here, so you can attach debugger to be able to debug your own code.
                //Console.ReadKey();

                // at this point we can call TeklaOpen api to do TS operations
                Console.WriteLine("ProjectInfo.Name        : " + new Model().GetProjectInfo().Name);
                Console.WriteLine("ProjectInfo.Description : " + new Model().GetProjectInfo().Description);
            } // service must be disposed so that a clean exit is done. Once disposed, it cannot be used again during the process lifetime.
        }

        /// <summary>
        /// Helpers function to set need patching for licensing or any other things required
        /// </summary>
        /// <param name="binDir"></param>
        private static void ConfigureHeadlessIniFiles(string binDir)
        {
            var licenseServer = "27001@yourserver";
            var runPath = @"C:\TeklaStructuresModels\RunPath";
            var overrideIniFile = Path.Combine(runPath, "TeklaStructures.ini");
            Directory.CreateDirectory(runPath);
            // Always make a copy of a main the ini file so we dont break TS installation.
            File.Copy(Path.Combine(binDir, "TeklaStructures.ini"), overrideIniFile, true);
            File.AppendAllText(overrideIniFile, $"\r\nset XS_LICENSE_SERVER_HOST={licenseServer}\r\n");
            File.AppendAllText(overrideIniFile, $"set XS_DEFAULT_LICENSE=Full\r\n");
            
            var envVar = Environment.GetEnvironmentVariable("TS_OVERRIDE_INI_FILE");
            if (envVar == null || !envVar.Equals(overrideIniFile))
            {
                throw new ArgumentException($"Please set TS_OVERRIDE_INI_FILE, before exec the program: set TS_OVERRIDE_INI_FILE={overrideIniFile}");
            }
        }

        /// <summary>
        /// assembly resolver - for loading dlls from bin without copying dlls into exectution path
        /// </summary>
        /// <param name="args">event args</param>
        /// <param name="binDir">ts binary folder</param>
        /// <returns>assembly to load</returns>
        private static System.Reflection.Assembly CurrentDomainAssemblyResolve(ResolveEventArgs args, string binDir)
        {
            var requestedAssembly = new AssemblyName(args.Name);

            if (File.Exists(Path.Combine(binDir, requestedAssembly.Name + ".dll")))
            {
                return System.Reflection.Assembly.LoadFile(Path.Combine(binDir, requestedAssembly.Name + ".dll"));
            }

            return null;            
        }
    }
}

为了使其运行,必须做一下更改:

  1.        Modify the project net framework version to V4.7.2
  2.        Modify licenseServer string
  3.        C:\TeklaStructuresModels\RunPath\TeklaStructures.ini (Copy from the tekla's bin directory)
  4.        create default\env_Default_environment.ini (Copy from ..\common\env_global_default.ini)
  5.         create default\role_Steel_Detailer.ini (empty file)
  6.         set modelPath(to your project model's path)
  7.         Set the Tekla's Version
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_16215957

如果有帮助一杯咖啡奶茶均可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值