Catch Event when Clicking over LoadintoProject

Hans_Dwe
Enthusiast
Enthusiast

Catch Event when Clicking over LoadintoProject

Hans_Dwe
Enthusiast
Enthusiast

Hello everyone, 

 

I would like to ask if it's possible to catch an event when the user clicks over, for example, the LoadintoProject button, for example, I get a popup msg that shows "button clicked"?  I tried to use RevitCommandId but it was unable to show the popup msg. I appreciate any suggestions thanks !! 

 

0 Likes
Reply
214 Views
2 Replies
Replies (2)

jeremy_tammik
Autodesk
Autodesk

No official support from the Revit API itself, afaik, but I believe you can achieve this by using the .NET Automation library:

  

  

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

Hans_Dwe
Enthusiast
Enthusiast

Hello @jeremy_tammik, I meant to be able to see which family has been loaded into the file and save it in an external temp directory, it worked to catch that event(FamilyLoadedIntoDocument) but the problem was I couldn't either save the family while it's being loaded into the Revit file or either to save it after its already loaded into the Revit file, is there a possible way to do it? this is my code so far for more clarification. 

 

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using System.IO;
using Autodesk.Revit.DB.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace LoadInProjectWatcher
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Class1 : IExternalApplication
    {
        private readonly string saveDirectory = @"";
        private string doctitle { get; set;}
       
        public Result OnStartup(UIControlledApplication application)
        {
            application.ControlledApplication.FamilyLoadedIntoDocument += OnFamilyLoading;
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            application.ControlledApplication.FamilyLoadedIntoDocument -= OnFamilyLoading;
            return Result.Succeeded;
        }

        private void OnFamilyLoading(object sender, FamilyLoadedIntoDocumentEventArgs e)
        {
            doctitle = e.FamilyName;

            string logFilePath = Path.Combine(saveDirectory, "FamilyLoadLog.txt");
            if (!Directory.Exists(saveDirectory))
                Directory.CreateDirectory(saveDirectory);

            using (StreamWriter sw = File.AppendText(logFilePath))
                sw.WriteLine(doctitle);

            TaskDialog.Show("Info", $"{doctitle} is loading into the project.");

            SaveFamilies(e.Document);
        }


        public void SaveFamilies(Document doc)
        {
            if (!Directory.Exists(saveDirectory))
                Directory.CreateDirectory(saveDirectory);

            FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Family));

            StringBuilder errorMessages = new StringBuilder();

            foreach (Family family in collector)
            {
                if (family.Name == doctitle)
                {
                    Document famDoc = null;
                    try
                    {
                        famDoc = doc.EditFamily(family);
                        if (famDoc != null)
                        {
                            string fileName = Path.Combine(saveDirectory, family.Name + ".rfa");
                            SaveFamilyDocument(famDoc, fileName);
                        }
                    }
                    catch (Exception e)
                    {
                        errorMessages.AppendLine($"Failed to save family {family.Name}: {e.Message}");
                    }
                    finally
                    {
                        if (famDoc != null)
                        {
                            famDoc.Close(false);
                        }
                    }

                    
                }
            }
        }

        private void SaveFamilyDocument(Document famDoc, string fileName)
        {
            using (Transaction trans = new Transaction(famDoc, "Save Family"))
            {
                trans.Start();
                SaveAsOptions saveAsOptions = new SaveAsOptions();
                famDoc.SaveAs(fileName, saveAsOptions);
                trans.Commit();
            }
        }
    }
}

 

 

0 Likes