How to Retrieve Revit Database for Dockable Panes and Link to View Model Class

How to Retrieve Revit Database for Dockable Panes and Link to View Model Class

lrsmns
Enthusiast Enthusiast
260 Views
1 Reply
Message 1 of 2

How to Retrieve Revit Database for Dockable Panes and Link to View Model Class

lrsmns
Enthusiast
Enthusiast

Hi,

I'm trying to create a dockable pane that displays a list of family symbols from FilteredElementCollector which then through selecting an object calls the method PromptForFamilyInstancePlacement..

 

so far I've created an external event handler that implements the FilteredElementCollector and the event is then created in the dockable pane provider

 

public sealed partial class DockablePanelPage : IDockablePaneProvider
{
    CollectFamilyExternalEventHandler collection = null;
    ExternalEvent ExEvent = null;
    public DockablePanelPage()
    {
        this.InitializeComponent();
        collection = new CollectFamilyExternalEventHandler();
        ExEvent = ExternalEvent.Create(collection);
    }

    
    public void SetupDockablePane(DockablePaneProviderData data)
    {
        data.FrameworkElement = this;
        data.InitialState = new DockablePaneState()
        {
            DockPosition = DockPosition.Tabbed,
            TabBehind = DockablePanes.BuiltInDockablePanes.ProjectBrowser
        };
        data.VisibleByDefault = false;
            

    }

    private void Button_Click(object sender, SelectionChangedEventArgs e)
    {
        ExEvent.Raise();
    }
}

 

public class CollectFamilyExternalEventHandler : IExternalEventHandler
{
    public void Execute(UIApplication uiapp)
    {
        var doc = uiapp.ActiveUIDocument.Document;
        if (doc == null)
        {
            return;
        }

        List<FamilySymbol> familySymb = new FilteredElementCollector(doc)
                .OfClass(typeof(FamilySymbol))
                .OfCategory(BuiltInCategory.OST_Furniture)
                .Cast<FamilySymbol>()
                .ToList();
    }

    
    public string GetName() 
    {
        return "CollectFamilyExternalEventHandler";
    }
}

 

I need some insight on how to work with dockable panes properly.. as I am quite new to event handling and also working with mvvm architecture.. My problem is how do I link my filteredelementcollector from my external event to my viewmodel, so that I can run a command to place the selected family?

 

public static class DockablePanelUtilities
{

    public static void RegisterDockablePanel(UIControlledApplication app)
    {
        
        var vm = new LibraryViewModel();
        var v = new DockablePanelPage
        {
            DataContext = vm
        };

        var panelId = new DockablePaneId(new Guid("-guidnumber-"));

        try
        {
            app.RegisterDockablePane(panelId, "Famlib", v);
        }
        catch (Exception e) 
        { 
        
        }
    }

    public static void ShowDockablePanel(UIApplication app)
    {
        var panelId = new DockablePaneId(new Guid("-guidnumber-"));
        var dp = app.GetDockablePane(panelId);
        if (dp != null && !dp.IsShown()) 
             dp?.Show(); 
        
    }
}

 

0 Likes
261 Views
1 Reply
Reply (1)
Message 2 of 2

jeremy_tammik
Alumni
Alumni

Yes, indeed, mvvm requires being totally clear on what is where, how it is separated and how it can be connected. Encapsulation. Actually, just a special case of the basic principles of OOP:

  

  

Your external handler is modal and runs in the Revit API context. Within it, you have access to the Revit application, the current document and all the rest. You can share that with other non-modal parts of your application. However, calls to the Revit API can only be made from the modal part, within a valid Revit API context.

  

In general, I would suggest trying to retrieve the results that you need directly within the modal context, and only sharing those results to the non-modal parts of the app. Sharing the Revit database with the non-modal components makes little sense, because they cannot make use of that information or the Revit API anyway.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes