Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I recently had a need to revisit the topic of retrieving a newly created element using the PromptForFamilyInstancePlacement command but to some quirky results. I've read through a lot of the discussions which have led back to the decade old post found here:
https://thebuildingcoder.typepad.com/blog/2010/06/place-family-instance.html
Problem:
- The OnDocumentChanged Event fires right before the PromptForFamilyInstancePlacement is called or in other words fires before I even get to place a family instance.
- After placing an instance or two, when exiting the command by pressing the ESC key, it seems as if the transaction rolls back or fails and all the placed instances are removed.
What I know:
- I am aware the prompt has its own internal transaction method and I am NOT running this inside another transaction.
- I am running this on Revit 2022
- I am sure I can go the idling or IUpdater route, but I would like to reserve those for last case scenarios.
- The family instance I am placing are of the Generic Model Category. I do not have any other updaters that would conflict with the newly created elements especially in this category.
To make troubleshooting simple, I am testing this with an extremely simply example, even the family is just a generic faced based model with nothing in it except a circle. Anyone else experience similar issues or have insight on why the following is not working properly? Thank you.
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Place : IExternalCommand
{
List<ElementId> ids = new List<ElementId>();
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
UIDocument uiDoc = new UIDocument(doc);
UIApplication uiApp = commandData.Application;
Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
ids.Clear();
FamilySymbol symbol = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_GenericModel).OfClass(typeof(FamilySymbol)).Cast<FamilySymbol>().FirstOrDefault(s => s.FamilyName.Equals("testfamily"));
if (symbol != null)
{
app.DocumentChanged += new EventHandler<DocumentChangedEventArgs>(OnDocumentChanged);
uiDoc.PromptForFamilyInstancePlacement(symbol); // Nothing below this line seems to fire, including the TaskDialog. It also seems to not even unsubscribe.
app.DocumentChanged -= new EventHandler<DocumentChangedEventArgs>(OnDocumentChanged);
}
TaskDialog.Show("Count", ids.Count.ToString()); // Does not fire
return Result.Succeeded;
}
void OnDocumentChanged(object sender, DocumentChangedEventArgs e)
{
// I know this fires before the prompt because if I add a message box here, the message box appears right before the command.
ids.AddRange(e.GetAddedElementIds());
}
}
Solved! Go to Solution.