(Tekla Structures二次开发)ModelObjectEnumerator.AutoFetch

这篇博客探讨了 Tekla Structures 中一个新添加的属性如何影响模型对象枚举的速度。通过测试代码展示了不同设置下获取零件的速度差异,包括 ModelObjectEnumerator.AutoFetch 的真伪对性能的影响。实验结果显示,AutoFetch 设置为 true 可显著提升枚举速度。

新的版本中增加了这个属性,据国外同行测试会影响速度。我查看帮助文件,发现2017版本已经有这个属性。
国外同行Github代码地址:
https://github.com/razorcx/speed-test/blob/master/SpeedTest/Program.cs

using System.Collections;
using System.Collections.Generic;
using Tekla.Structures;
using Tekla.Structures.Filtering;
using Tekla.Structures.Filtering.Categories;
using Tekla.Structures.Model;

namespace SpeedTest
{
	public class Program
	{
		static void Main(string[] args)
		{
			new SpeedTest().Run();
		}
	}

	public class SpeedTest
	{
		public void Run()
		{
			var model = new Model();

			//IMPORTANT!!! ModelObjectEnumerator.AutoFetch = true;
			var parts1 = model.GetAllParts(true);  //fastest!!!

			//USING THE FILTER EXPRESSIONS - ModelObjectEnumerator.AutoFetch = true
			var parts2 = model.GetParts(true);  //very fast with more flexibility

			//ModelObjectEnumerator.AutoFetch - ModelObjectEnumerator.AutoFetch = false
			var parts3 = model.GetAllParts(false);  //very slow

			//USING THE FILTER EXPRESSIONS - ModelObjectEnumerator.AutoFetch = false
			var parts4 = model.GetParts(false);  //very slower
		}
	}

	public static class ExtensionMethods
	{
		public static List<Part> GetAllParts(this Model model, bool autoFetch)
		{
			//IMPORTANT!!!
			ModelObjectEnumerator.AutoFetch = autoFetch;

			//parts
			var types = new[] { typeof(Beam), typeof(BentPlate),
				typeof(ContourPlate), typeof(PolyBeam) };

			var parts = model
				.GetModelObjectSelector()
				.GetAllObjectsWithType(types)
				.ToAList<Part>();

			return parts;
		}

		public static List<T> ToAList<T>(this IEnumerator enumerator)
		{
			var list = new List<T>();
			while (enumerator.MoveNext())
			{
				var current = (T)enumerator.Current;
				if (current != null)
					list.Add(current);
			}
			return list;
		}

		public static List<Part> GetParts(this Model model, bool autoFetch)
		{
			ObjectFilterExpressions.Type objectType = new ObjectFilterExpressions.Type();
			NumericConstantFilterExpression type =
				new NumericConstantFilterExpression(TeklaStructuresDatabaseTypeEnum.PART);

			var expression2 = new BinaryFilterExpression(objectType, NumericOperatorType.IS_EQUAL, type);

			BinaryFilterExpressionCollection filterCollection =
				new BinaryFilterExpressionCollection
				{
					new BinaryFilterExpressionItem(expression2, BinaryFilterOperatorType.BOOLEAN_AND),
				};

			//IMPORTANT!!!
			ModelObjectEnumerator.AutoFetch = autoFetch;

			return model
				.GetModelObjectSelector()
				.GetObjectsByFilter(filterCollection)
				.ToAList<Part>();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值