/// <summary> /// 遍历所有的功能块,如果 loopFunc 返回 false,则停止遍历。 /// </summary> /// <param name="loopFunc">功能块处理函数,如果返回 false,则停止遍历</param> /// <param name="pouSourceFilter">对嵌套的 Pou Source 对象进行过滤,如果返回 false,则该 Pou Source 对象就不遍历了。</param> /// <param name="child">子对象</param> /// <param name="loopedTypes">用于记录已经被遍历过的功能块信息</param> /// <returns>遍历是否没有被 loopFunc 函数终止?</returns> protected virtual bool LoopPOUs(Func <ViFirmBlockType, bool> loopFunc, Func <ViPouSource, bool> pouSourceFilter, ViNamedObject child, Dictionary <string, ViFirmBlockType> loopedTypes) { if (child is ViFirmBlockType) { ViFirmBlockType blockType = child as ViFirmBlockType; // 记录遍历过的功能块信息 string key = blockType.Name.ToUpper(); if (loopedTypes.ContainsKey(key)) { return(true); } loopedTypes[key] = blockType; // 遍历功能块 if (!loopFunc(blockType)) { return(false); } } else if (child is ViPouSource) { ViPouSource pouSource = child as ViPouSource; if (pouSourceFilter == null || pouSourceFilter(pouSource)) { if (!pouSource.LoopPOUs(loopFunc, pouSourceFilter, loopedTypes)) { return(false); } } } return(true); }
private static void AppendPouSource(ArrayList alPouSources, ViHardwareType hardwareType, string sourceFile, Func <string, ViPouSource> factory) { sourceFile = GetPouSrcFileName(hardwareType, sourceFile); if (string.IsNullOrEmpty(sourceFile)) { return; } // 自动创建 PouSource 字典对象 if (_pouSources == null) { _pouSources = new Dictionary <string, ViPouSource>(); } // 可能会在 UI/Build 线程中调用,因此互斥访问 lock (_pouSources) { // 先在字典中查找,如果找不到,则自动创建 string sourceKey = (hardwareType.HardwareName + ':' + sourceFile).ToUpper(); if (_pouSources.ContainsKey(sourceKey)) { alPouSources.Add(_pouSources[sourceKey]); } else { ViPouSource pouSource = factory(sourceFile); if (pouSource != null) { _pouSources[sourceKey] = pouSource; alPouSources.Add(pouSource); } } } }
/// <summary> /// 查找指定名称的功能块。 /// </summary> /// <param name="name">功能块名称(不区分大小写)</param> /// <returns>指定名称的功能块。</returns> public virtual ViFirmBlockType GetPOU(string name) { // 先保证数据被更新了 object _Children = this.Children; ViFirmBlockType blockType; // 先在子节点中查找 name = name.ToUpper(); if (this.Children != null && this.Children.Count > 0) { blockType = this.Children.Where(o => string.Compare(o.Name, name, true) == 0).FirstOrDefault() as ViFirmBlockType; if (blockType != null) { return(blockType); } } if (this.RecursiveSearch) { // 在子节点中递归查找 foreach (ViNamedObject child in this.Children) { ViPouSource pouSource = child as ViPouSource; if (pouSource == null) { continue; } blockType = pouSource.GetPOU(name); if (blockType != null) { return(blockType); } } } // 没有找到 return(null); }