ArcGIS Pro SDK (八)地理数据库 6 版本控制

ArcGIS Pro SDK (八)地理数据库 6 版本控制

文章目录

  • [ArcGIS Pro SDK (八)地理数据库 6 版本控制](#ArcGIS Pro SDK (八)地理数据库 6 版本控制)
    • [1 连接到版本](#1 连接到版本)
    • [2 在单独的编辑会话中协调版本并将其与其父级进行核对和提交](#2 在单独的编辑会话中协调版本并将其与其父级进行核对和提交)
    • [3 在同一编辑会话中协调版本并将其与其父级提交](#3 在同一编辑会话中协调版本并将其与其父级提交)
    • [4 使用版本](#4 使用版本)
    • [5 使用默认版本](#5 使用默认版本)
    • [6 创建版本](#6 创建版本)
    • [7 创建历史版本](#7 创建历史版本)
    • [8 在版本之间切换](#8 在版本之间切换)
    • [9 部分过账](#9 部分过账)

环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0

1 连接到版本

csharp 复制代码
public Geodatabase ConnectToVersion(Geodatabase geodatabase, string versionName)
{
    Geodatabase connectedVersion = null;

    if (geodatabase.IsVersioningSupported())
    {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
            using (Version version = versionManager.GetVersion(versionName))
        {
            connectedVersion = version.Connect();
        }
    }
    return connectedVersion;
}

2 在单独的编辑会话中协调版本并将其与其父级进行核对和提交

csharp 复制代码
public void ReconcileAndPost(Geodatabase geodatabase)
{
    // 获取当前版本及其父版本的引用
    if (geodatabase.IsVersioningSupported())
    {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        using (Version currentVersion = versionManager.GetCurrentVersion())
        using (Version parentVersion = currentVersion.GetParent())
        {
            // 创建 ReconcileOptions 对象
            var reconcileOptions = new ReconcileOptions(parentVersion)
            {
                ConflictResolutionMethod = ConflictResolutionMethod.Continue, // 发现冲突时继续
                ConflictDetectionType = ConflictDetectionType.ByRow, // 默认值
                ConflictResolutionType = ConflictResolutionType.FavorTargetVersion // 或者 FavorEditVersion
            };

            // 执行协调
            ReconcileResult reconcileResult = currentVersion.Reconcile(reconcileOptions);
            if (!reconcileResult.HasConflicts)
            {
                // 无冲突,执行过账
                var postOptions = new PostOptions(parentVersion)
                {
                    ServiceSynchronizationType = ServiceSynchronizationType.Synchronous // 默认值
                };
                currentVersion.Post(postOptions);
            }
        }
    }
}

3 在同一编辑会话中协调版本并将其与其父级提交

csharp 复制代码
public void ReconcileAndPost2(Geodatabase geodatabase)
{
    // 获取当前版本及其父版本的引用
    if (geodatabase.IsVersioningSupported())
    {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        using (Version currentVersion = versionManager.GetCurrentVersion())
        using (Version parentVersion = currentVersion.GetParent())
        {
            // 创建 ReconcileOptions 对象
            var reconcileOptions = new ReconcileOptions(parentVersion)
            {
                ConflictResolutionMethod = ConflictResolutionMethod.Continue, // 发现冲突时继续
                ConflictDetectionType = ConflictDetectionType.ByRow, // 默认值
                ConflictResolutionType = ConflictResolutionType.FavorTargetVersion // 或者 FavorEditVersion
            };

            // 创建 PostOptions 对象
            var postOptions = new PostOptions(parentVersion)
            {
                ServiceSynchronizationType = ServiceSynchronizationType.Synchronous // 默认值
            };

            // 执行协调和过账
            ReconcileResult reconcileResult = currentVersion.Reconcile(reconcileOptions, postOptions);
            if (reconcileResult.HasConflicts)
            {
                // 处理冲突
                // TODO 解决冲突
            }
        }
    }
}

4 使用版本

csharp 复制代码
public async Task WorkingWithVersions()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
                using (VersionManager versionManager = geodatabase.GetVersionManager())
            {
                IReadOnlyList<Version> versionList = versionManager.GetVersions();

                // 默认版本的父版本为 null
                Version defaultVersion = versionList.First(version => version.GetParent() == null);

                IEnumerable<Version> publicVersions = versionList.Where(
                    version => version.GetAccessType() == VersionAccessType.Public);
                Version qaVersion = defaultVersion.GetChildren().First(
                    version => version.GetName().Contains("QA"));

                Geodatabase qaVersionGeodatabase = qaVersion.Connect();

                FeatureClass currentFeatureClass = geodatabase.OpenDataset<FeatureClass>("featureClassName");
                FeatureClass qaFeatureClass = qaVersionGeodatabase.OpenDataset<FeatureClass>("featureClassName");
            }
        });
}

5 使用默认版本

csharp 复制代码
// 检查当前版本是否为默认版本
// 适用于分支和传统版本管理
public bool IsDefaultVersion(Version version)
{
    Version parentVersion = version.GetParent();
    if (parentVersion == null)
    {
        return true;
    }
    parentVersion.Dispose();
    return false;
}

public bool IsDefaultVersion(Geodatabase geodatabase)
{
    if (!geodatabase.IsVersioningSupported()) return false;
    using (VersionManager versionManager = geodatabase.GetVersionManager())
        using (Version currentVersion = versionManager.GetCurrentVersion())
    {
        return IsDefaultVersion(currentVersion);
    }
}

// 获取默认版本
// 适用于分支和传统版本管理
// 此方法依赖于 IsDefaultVersion() 方法
public Version GetDefaultVersion(Version version)
{
    if (IsDefaultVersion(version))
    {
        return version;
    }
    else
    {
        Version parent = version.GetParent();
        Version ancestor = GetDefaultVersion(parent);
        if (parent != ancestor)
        {
            parent.Dispose(); // 如果版本树超过两层深,释放中间版本对象
        }
        return ancestor;
    }
}

public Version GetDefaultVersion(Geodatabase geodatabase)
{
    if (!geodatabase.IsVersioningSupported()) return null;

    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        Version currentVersion = versionManager.GetCurrentVersion();
        Version defaultVersion = GetDefaultVersion(currentVersion);
        if (currentVersion != defaultVersion)
        {
            currentVersion.Dispose(); // 如果当前版本不是默认版本,释放当前版本对象
        }
        return defaultVersion;
    }
}

6 创建版本

csharp 复制代码
public Version CreateVersion(Geodatabase geodatabase, string versionName, string description, VersionAccessType versionAccessType)
{
    if (!geodatabase.IsVersioningSupported()) return null;

    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        VersionDescription versionDescription = new VersionDescription(versionName, description, versionAccessType);
        return versionManager.CreateVersion(versionDescription);
    }
}

7 创建历史版本

csharp 复制代码
public HistoricalVersion CreateHistoricalVersion(Geodatabase geodatabase, string versionName)
{
    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        HistoricalVersionDescription historicalVersionDescription = new HistoricalVersionDescription(versionName, DateTime.Now);
        HistoricalVersion historicalVersion = versionManager.CreateHistoricalVersion(historicalVersionDescription);

        return historicalVersion;
    }
}

8 在版本之间切换

csharp 复制代码
public void ChangeVersions(Geodatabase geodatabase, string toVersionName)
{
    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        VersionBaseType versionBaseType = versionManager.GetCurrentVersionBaseType();

        if (versionBaseType == VersionBaseType.Version)
        {
            Version fromVersion = versionManager.GetCurrentVersion();
            Version toVersion = versionManager.GetVersion(toVersionName);

            // 在版本之间切换
            MapView.Active.Map.ChangeVersion(fromVersion, toVersion);
        }

        if (versionBaseType == VersionBaseType.HistoricalVersion)
        {
            HistoricalVersion fromHistoricalVersion = versionManager.GetCurrentHistoricalVersion();
            HistoricalVersion toHistoricalVersion = versionManager.GetHistoricalVersion(toVersionName);

            // 在历史版本之间切换
            MapView.Active.Map.ChangeVersion(fromHistoricalVersion, toHistoricalVersion);
        }

        // 从历史版本切换到版本,反之亦然
        // MapView.Active.Map.ChangeVersion(fromHistoricalVersion, toVersion);
        // MapView.Active.Map.ChangeVersion(fromVersion, toHistoricalVersion);
    }
}

9 部分过账

csharp 复制代码
// 部分过账允许开发人员提交版本中的部分更改。
// 一个示例用例是电力公用事业公司使用版本来设计新的住宅细分区的设施。
// 在流程的某个阶段,一个街区的新房屋已经建成,而细分区的其余部分尚未建造。
// 部分过账允许用户提交已完成的工作,同时保留版本中尚未建成的要素,以便以后提交。
// 部分过账需要使用 ArcGIS Enterprise 10.9 及更高版本的分支版本化要素服务。

// 指定已构建的要素集合
QueryFilter constructedFilter = new QueryFilter()
{
    WhereClause = "ConstructedStatus = 'True'"
};

// 此选择表示我们希望提交到支持结构要素类的插入和更新
using (Selection constructedSupportStructures = supportStructureFeatureClass.Select(
    constructedFilter, SelectionType.ObjectID, SelectionOption.Normal))
{
    // 指定要提交的删除要素稍微复杂,因为无法发出查询来获取已删除要素的集合,必须使用 ObjectIDs 的列表。
    using (Selection deletedSupportStructures = supportStructureFeatureClass.Select(
        null, SelectionType.ObjectID, SelectionOption.Empty))
    {
        deletedSupportStructures.Add(deletedSupportStructureObjectIDs);  // deletedSupportStructureObjectIDs 定义为 List<long>

        // 执行带有部分过账的协调
        // 在 2.x 版本中 -
        // ReconcileDescription reconcileDescription = new ReconcileDescription();
        // reconcileDescription.ConflictDetectionType = ConflictDetectionType.ByColumn;
        // reconcileDescription.ConflictResolutionMethod = ConflictResolutionMethod.Continue;
        // reconcileDescription.ConflictResolutionType = ConflictResolutionType.FavorEditVersion;
        // reconcileDescription.PartialPostSelections = new List<Selection>() { constructedSupportStructures, deletedSupportStructures };
        // reconcileDescription.WithPost = true;

        // ReconcileResult reconcileResult = designVersion.Reconcile(reconcileDescription);

        var reconcileOptions = new ReconcileOptions();  // 协调选项,针对默认版本
        reconcileOptions.ConflictDetectionType = ConflictDetectionType.ByColumn;
        reconcileOptions.ConflictResolutionMethod = ConflictResolutionMethod.Continue;
        reconcileOptions.ConflictResolutionType = ConflictResolutionType.FavorEditVersion;

        var postOptions = new PostOptions();  // 提交选项,针对默认版本
        postOptions.PartialPostSelections = new List<Selection>() { 
            constructedSupportStructures, deletedSupportStructures 
        };
        postOptions.ServiceSynchronizationType = ServiceSynchronizationType.Synchronous;

        var reconcileResult = designVersion.Reconcile(reconcileOptions, postOptions);

        // 处理结果
        // TODO 处理结果的逻辑
    }
}
相关推荐
921正在学习编程7 分钟前
模拟实现strstr 找到母串中是否有与子串相同的部分(代码)
c语言·笔记·c#
友恒写实3 小时前
C#高级教程(三)看不懂捶我:泛型
c#
咩咩觉主4 小时前
en造数据结构与算法C# 之 堆排序
数据结构·算法·c#
一丝晨光4 小时前
静态变量、变量作用域、命名空间
java·开发语言·c++·c#·命名空间·静态变量·变量作用域
AutoAutoJack4 小时前
C# 中继承的说明
开发语言·数据结构·算法·架构·c#
小码编匠5 小时前
.NET 中利用 TPL 与 PLINQ 充分发挥多核处理器性能
后端·c#·.net
小威编程6 小时前
C#多线程基本使用和探讨
开发语言·c#
m0_619731196 小时前
C# 自适应屏幕分辨率
开发语言·windows·c#
yngsqq6 小时前
007集—— 自动获取图形的外边界(外轮廓)(CAD—C#二次开发入门)
windows·microsoft·c#
时光追逐者7 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 8 期(2024年10.01-10.06)
microsoft·开源·c#·.net·.netcore