void ProcessLinkedDocuments()
        {
            List <string> LinkedDocumentsToRemove = new List <string>();

            // Check for modified linked documents.
            foreach (var LinkedDocEntry in RootCache.LinkedDocumentsCache)
            {
                // Check if the link was removed.
                string LinkedDocPath = LinkedDocEntry.Key;
                if (!ExportedLinkedDocuments.ContainsKey(LinkedDocPath))
                {
                    LinkedDocumentsToRemove.Add(LinkedDocPath);
                    continue;
                }

                // Check if the link was modified.
                FCachedDocumentData LinkedDocCache = LinkedDocEntry.Value;
                if (ModifiedLinkedDocuments.Contains(LinkedDocCache.SourceDocument))
                {
                    LinkedDocCache.Purge(DatasmithScene, true);
                }
                LinkedDocCache.ExportedElements.Clear();
            }

            foreach (var LinkedDoc in LinkedDocumentsToRemove)
            {
                RootCache.LinkedDocumentsCache[LinkedDoc].Purge(DatasmithScene, true);
                RootCache.LinkedDocumentsCache.Remove(LinkedDoc);
            }
        }
        public void OnEndExport()
        {
            if (RootCache.LinkedDocumentsCache.Count > 0)
            {
                ProcessLinkedDocuments();
            }

            RootCache.Purge(DatasmithScene, false);

            ModifiedLinkedDocuments.Clear();
            ExportedLinkedDocuments.Clear();
            RootCache.ClearModified();
            RootCache.ExportedElements.Clear();

            string OutputPath = null;

            IDirectLinkUI DirectLinkUI = IDatasmithExporterUIModule.Get()?.GetDirectLinkExporterUI();

            if (DirectLinkUI != null)
            {
                OutputPath = DirectLinkUI.GetDirectLinkCacheDirectory();
            }
            else
            {
                OutputPath = Path.Combine(Path.GetTempPath(), SceneName);
            }

            Directory.CreateDirectory(OutputPath);

            DatasmithScene.ExportAssets(OutputPath);
            DatasmithScene.BuildScene(SceneName);

            bool bUpdateOk = DatasmithDirectLink.UpdateScene(DatasmithScene);

            SyncCount++;

            // Control metadata export via env var REVIT_DIRECTLINK_WITH_METADATA.
            // We are not interested of its value, just if it was set.
            if (null != Environment.GetEnvironmentVariable("REVIT_DIRECTLINK_WITH_METADATA"))
            {
                Debug.Assert(MetadataTask == null);                 // We cannot have metadata export running at this point (must be stopped in OnBeginExport)
                MetadataCancelToken = new CancellationTokenSource();
                MetadataTask        = Task.Run(() => ExportMetadata());
            }
        }