/// <summary> /// 将子节点移动到新的索引位置。 /// </summary> /// <param name="newIndex">新的索引位置</param> /// <param name="child">要移动位置的子节点</param> /// <returns>成功与否?</returns> public virtual bool MoveChild(int newIndex, ViNamedObject child) { if (this.Owner != null) { if (child.GetParent() != this) { return(false); } } int oldIndex = this.IndexOfChild(child); if (oldIndex == newIndex) { return(true); } if (oldIndex > newIndex) { ++newIndex; } this.Children.RemoveAt(oldIndex); this.InsertChild(newIndex, child); return(true); }
/// <summary> /// 找到指定名称的子节点,递归或不递归方式。 /// </summary> /// <param name="name">子节点名称</param> /// <param name="recursive">是否递归查找子节点的子节点?</param> /// <returns>指定名称的子节点</returns> public ViNamedObject ChildByName(string name, bool recursive) { int index = this.IndexOfChild(name); if (index < 0) { if (recursive) { foreach (ViNamedObject child in this.Children) { SortedTreeObject treeobj = child as SortedTreeObject; if (treeobj == null) { continue; } ViNamedObject findobj = treeobj.ChildByName(name, recursive); if (findobj != null) { return(findobj); } } } return(null); } return(this.Children[index]); }
/// <summary> /// 在指定的索引位置,插入子节点。 /// </summary> /// <param name="index">索引位置,从 0 开始</param> /// <param name="child">要插入的子节点</param> public virtual void InsertChild(int index, ViNamedObject child) { if (this.Owner != null) { child.SetParent(this); } this.Children.Insert(index, child); }
/// <summary> /// 添加子节点。子节点会按照字母序(不区分大小写)添加到子节点集合中。 /// /// @note 是按照“从小到大”排序的方式加入到子对象集合中的。 /// </summary> /// <param name="child">需要添加的子节点</param> public virtual void AddChild(ViNamedObject child) { if (this.Owner != null) { child.SetParent(this.Owner); } this.Children.SortedAdd(child); }
/// <summary> /// 添加子节点到子节点集合最后。 /// </summary> /// <param name="child">需要添加的子节点</param> public override void AddChild(ViNamedObject child) { this.Children.Add(child); // 注意:SetParent 不能放到 Add(child)前面,因为此时功能块的 PropertyChanged有可能为null,所以功能块在设置Parent的时候, //有可能没法通知UndoEngine来进行记录属性的修改。 if (this.Owner != null) { child.SetParent(this); } }
/// <summary> /// 删除子节点。 /// </summary> /// <param name="child">被删除的子节点</param> /// <returns>如果子节点不在本对象的子节点列表中,则失败</returns> public virtual bool DeleteChild(ViNamedObject child) { if (this.Owner != null) { if (child.GetParent() != this.Owner) { return(false); } child.SetParent(null); } return(this.Children.Remove(child)); }
protected ViNamedObject ChildByPath(string[] path, int start) { ViNamedObject child = this.ChildByName(path[start], false); if (child == null) { return(null); } if (start + 1 >= path.Count()) { return(child); } SortedTreeObject treeobj = child as SortedTreeObject; if (treeobj == null) { return(null); } return(treeobj.ChildByPath(path, start + 1)); }
/// <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); }
/// <summary> /// 返回指定对象在子节点集合中的索引,< 0 表示没有找到。 /// </summary> /// <param name="child">指定的子节点</param> /// <returns>子节点的集合索引,< 0 表示没有找到</returns> public virtual int IndexOfChild(ViNamedObject child) { return(this.Children.IndexOf(child)); }