Request

Response

    Implementing Exchange Visibility Control

    Control the visibility of loaded exchanges in your host application without unloading them. This feature works like layer visibility controls in design software—users can temporarily hide exchanges to reduce visual clutter while keeping the data loaded and ready for immediate display.

    When to Implement This Feature

    Implement exchange visibility control when your users need to:

    • Manage visual complexity: Hide exchanges temporarily to focus on specific design elements
    • Compare design alternatives: Toggle between different design options without reloading
    • Improve performance: Reduce rendering overhead by hiding non-essential exchanges
    • Support workflows: Enable phased reviews where different exchanges are shown at different stages

    How It Works

    The SDK tracks visibility state for each exchange through the IsExchangeVisible property on ExchangeItem and DataExchange models. When users toggle visibility in the Connector UI, the SDK calls your implementation of SetExchangeVisibilityAsync to update the host application’s display.

    Think of it like Photoshop layers: the exchange data remains loaded, but your implementation controls whether the host application renders it.

    Implementation Steps

    1. Override SetExchangeVisibilityAsync

    Override the SetExchangeVisibilityAsync method in your BaseExchangeModel derived class:

    public override async Task SetExchangeVisibilityAsync(
        List<string> exchangeIds, 
        bool isVisible)
    {
        foreach (var exchangeId in exchangeIds)
        {
            // Get exchange data from your tracking system
            var exchangeData = GetLoadedExchange(exchangeId);
            if (exchangeData == null) continue;
    
            // Update visibility in host application
            if (isVisible)
            {
                ShowExchangeInHost(exchangeData);
            }
            else
            {
                HideExchangeInHost(exchangeData);
            }
        }
    
        await Task.CompletedTask;
    }
    
    Show More

    2. Track Loaded Exchanges

    Maintain a mapping between exchange IDs and their corresponding host application objects. This enables quick lookups when visibility changes:

    private readonly Dictionary<string, HostApplicationObject> loadedExchanges = new();
    
    protected void TrackLoadedExchange(string exchangeId, HostApplicationObject hostObject)
    {
        loadedExchanges[exchangeId] = hostObject;
    }
    
    protected HostApplicationObject GetLoadedExchange(string exchangeId)
    {
        return loadedExchanges.TryGetValue(exchangeId, out var hostObject) 
            ? hostObject 
            : null;
    }
    
    Show More

    3. Implement Host-Specific Visibility Logic

    The visibility control mechanism depends on your host application’s API. Below are examples for common platforms.

    Revit Implementation

    private void ShowExchangeInHost(DirectShape shape)
    {
        using (Transaction trans = new Transaction(document, "Show Exchange"))
        {
            trans.Start();
            shape.Unhide(document.ActiveView);
            trans.Commit();
        }
    }
    
    private void HideExchangeInHost(DirectShape shape)
    {
        using (Transaction trans = new Transaction(document, "Hide Exchange"))
        {
            trans.Start();
            shape.Hide(document.ActiveView);
            trans.Commit();
        }
    }
    
    Show More

    Rhino Implementation

    private void ShowExchangeInHost(Guid objectId)
    {
        var rhinoObject = RhinoDoc.ActiveDoc.Objects.FindId(objectId);
        if (rhinoObject != null)
        {
            RhinoDoc.ActiveDoc.Objects.Show(objectId, true);
        }
    }
    
    private void HideExchangeInHost(Guid objectId)
    {
        var rhinoObject = RhinoDoc.ActiveDoc.Objects.FindId(objectId);
        if (rhinoObject != null)
        {
            RhinoDoc.ActiveDoc.Objects.Hide(objectId, true);
        }
    }
    
    Show More

    AutoCAD Implementation

    private void ShowExchangeInHost(ObjectId objectId)
    {
        using (Transaction trans = database.TransactionManager.StartTransaction())
        {
            Entity entity = trans.GetObject(objectId, OpenMode.ForWrite) as Entity;
            if (entity != null)
            {
                entity.Visible = true;
            }
            trans.Commit();
        }
    }
    
    private void HideExchangeInHost(ObjectId objectId)
    {
        using (Transaction trans = database.TransactionManager.StartTransaction())
        {
            Entity entity = trans.GetObject(objectId, OpenMode.ForWrite) as Entity;
            if (entity != null)
            {
                entity.Visible = false;
            }
            trans.Commit();
        }
    }
    
    Show More

    Complete Example

    Here’s a complete implementation showing all components working together:

    public class MyCustomExchangeModel : BaseReadWriteExchangeModel
    {
        private readonly Dictionary<string, List<DirectShape>> exchangeShapes = new();
        private readonly Document revitDocument;
    
        public MyCustomExchangeModel(Client client, Document document) 
            : base(client)
        {
            this.revitDocument = document;
        }
    
        public override async Task SetExchangeVisibilityAsync(
            List<string> exchangeIds, 
            bool isVisible)
        {
            foreach (var exchangeId in exchangeIds)
            {
                if (!exchangeShapes.TryGetValue(exchangeId, out var shapes))
                    continue;
    
                using (Transaction trans = new Transaction(
                    revitDocument, 
                    isVisible ? "Show Exchange" : "Hide Exchange"))
                {
                    trans.Start();
                    
                    foreach (var shape in shapes)
                    {
                        if (isVisible)
                        {
                            shape.Unhide(revitDocument.ActiveView);
                        }
                        else
                        {
                            shape.Hide(revitDocument.ActiveView);
                        }
                    }
                    
                    trans.Commit();
                }
            }
    
            await Task.CompletedTask;
        }
    
        protected override async Task OnGetLatestExchangeDataAsync(
            ExchangeItem exchangeItem, 
            CancellationToken cancellationToken)
        {
            // Load exchange and create geometry
            var shapes = CreateGeometryInRevit(exchangeItem);
            
            // Track the loaded exchange
            exchangeShapes[exchangeItem.ExchangeId] = shapes;
            
            await base.OnGetLatestExchangeDataAsync(exchangeItem, cancellationToken);
        }
    
        public override async Task<IEnumerable<string>> UnloadExchangesAsync(
            List<ExchangeItem> exchanges)
        {
            var unloadedIds = new List<string>();
            
            foreach (var exchange in exchanges)
            {
                if (exchangeShapes.TryGetValue(exchange.ExchangeId, out var shapes))
                {
                    // Delete geometry from Revit
                    using (Transaction trans = new Transaction(
                        revitDocument, 
                        "Unload Exchange"))
                    {
                        trans.Start();
                        foreach (var shape in shapes)
                        {
                            revitDocument.Delete(shape.Id);
                        }
                        trans.Commit();
                    }
                    
                    exchangeShapes.Remove(exchange.ExchangeId);
                    unloadedIds.Add(exchange.ExchangeId);
                }
            }
            
            return unloadedIds;
        }
    
        // Required abstract method implementations
        public override async Task<List<DataExchange>> GetExchangesAsync(
            ExchangeSearchFilter filter)
        {
            // Implementation specific to your hosting provider
            return new List<DataExchange>();
        }
    
        public override async Task<bool> SelectElementsAsync(List<string> exchangeIds)
        {
            // Implementation to highlight elements in host application
            return true;
        }
    }
    
    Show More

    Best Practices

    Performance Considerations

    • Batch operations: Process multiple exchange IDs in a single transaction when possible
    • Cache lookups: Store references to host application objects for quick access
    • Async operations: Use await appropriately to avoid blocking the UI thread

    Error Handling

    Handle cases where exchanges may no longer exist in the host application:

    public override async Task SetExchangeVisibilityAsync(
        List<string> exchangeIds, 
        bool isVisible)
    {
        var failedIds = new List<string>();
        
        foreach (var exchangeId in exchangeIds)
        {
            try
            {
                if (!exchangeShapes.TryGetValue(exchangeId, out var shapes))
                {
                    failedIds.Add(exchangeId);
                    continue;
                }
    
                // Update visibility...
            }
            catch (Exception ex)
            {
                Logger.LogError($"Failed to set visibility for {exchangeId}: {ex.Message}");
                failedIds.Add(exchangeId);
            }
        }
        
        if (failedIds.Any())
        {
            // Notify user or log failures
            await NotifyVisibilityFailures(failedIds);
        }
    }
    
    Show More

    View-Specific Visibility

    Some host applications (like Revit) support view-specific visibility. Consider whether visibility changes should affect:

    • Current view only: Users can hide exchanges in one view while keeping them visible in others
    • All views: Visibility changes apply globally across the document

    Choose the approach that best fits your users’ workflows.

    See Also