skyline TEP学习心得(1):工程树ProjectTree操作两例

环境TEP6.1,C#

1.查找或创建组,查找不到则创建,返回ItemID

第二个参数是查找的组的路径,格式为:A\B\C

        /// <summary>
        /// 查找或创建组(查找不到则创建)
        /// </summary>
        /// <param name="tSGWorld"></param>
        /// <param name="tGroupPath">查找路径,格式:A\B\C</param>
        /// <returns></returns>
        public static int FindAndCreateGroup(ISGWorld61 tSGWorld, string tGroupPath)
        {
            if (tGroupPath.EndsWith("\\") == false)
            {
                tGroupPath += "\\";
            }

            int tItemID = tSGWorld.ProjectTree.FindItem(tGroupPath);
            if (tItemID == 0)
            {
                string tParentPath = tGroupPath.Substring(0, tGroupPath.LastIndexOf("\\"));
                string tGroupName = tParentPath.Substring(tParentPath.LastIndexOf("\\") + 1);
                int tItemIDParent = 0;

                if (tParentPath.IndexOf("\\") < 0)
                {
                    return tSGWorld.ProjectTree.CreateGroup(tParentPath, 0);
                }
                else
                {
                    tParentPath = tGroupPath.Substring(0, tParentPath.LastIndexOf("\\"));

                    tItemIDParent = FindAndCreateGroup(tSGWorld, tParentPath);
                }

                tItemID = tSGWorld.ProjectTree.CreateGroup(tGroupName, tItemIDParent);
            }

            return tItemID;
        }


2.在ProjectTree中找指定名称的图层的Layer

返回ILayer61对象

当从根节点找起,第二个参数为0,第三个参数为要找的图层名

        /// <summary>
        /// 在ProjectTree中找指定名称的图层的Layer
        /// </summary>
        /// <param name="tSGWorld"></param>
        /// <param name="tParentItemID"></param>
        /// <param name="tLayerName"></param>
        /// <returns></returns>
        public static ILayer61 FindLayerInProjectTree(ISGWorld61 tSGWorld, int tParentItemID, string tLayerName)
        {
            if (string.IsNullOrEmpty(tLayerName)) return null;

            int tItemID = tSGWorld.ProjectTree.GetNextItem(tParentItemID, ItemCode.CHILD);
            if (tItemID == 0) return null;
            while (tItemID != 0)
            {
                if (tSGWorld.ProjectTree.IsGroup(tItemID) == true)
                {
                    if (tSGWorld.ProjectTree.IsLayer(tItemID) == true)
                    {
                        if (tSGWorld.ProjectTree.GetItemName(tItemID) == tLayerName)
                        {
                            return tSGWorld.ProjectTree.GetLayer(tItemID);
                        }
                    }

                    ILayer61 tLayer = FindLayerInProjectTree(tSGWorld, tItemID, tLayerName);
                    if (tLayer != null)
                    {
                        return tLayer;
                    }

                }
                else
                {
                    if (tSGWorld.ProjectTree.IsLayer(tItemID) == true)
                    {
                        if (tSGWorld.ProjectTree.GetItemName(tItemID) == tLayerName)
                        {
                            return tSGWorld.ProjectTree.GetLayer(tItemID);
                        }
                    }
                }
                tItemID = tSGWorld.ProjectTree.GetNextItem(tItemID, ItemCode.NEXT);

            }

            return null;
        }
原文地址:https://www.cnblogs.com/cannel/p/2608653.html