Message 1 of 12
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello!
I encountered a very strange problem using Revit API 2018.
A short description of the problem is:
If the DocumentSavingAs event handler writes data to the Extensible Storage of the saving document
(the line of code of this type is "doc.ProjectInformation.SetEntity (entity)"),
then after that all the objects that will be created in this document will not be saved.
It also may cause Revit Error: Missing many elements.
More detailed description in the code below. It is code of entire testing project. There is only one class implementing IExternalApplication.
#region Namespaces using System; using System.Collections.Generic; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Events; using Autodesk.Revit.DB.ExtensibleStorage; using Autodesk.Revit.UI; #endregion namespace RevitTestAddin { /// <summary> /// 2 use cases: /// /// 1. Open existing document /// -> Create 2 or 3 random objects (I created beams) /// -> Save document /// -> Close document /// -> Open document /// -> Everything works fine /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// /// 2. Create new document or open existing document /// -> Save document as to any directory /// -> Create 2 or 3 random objects /// -> Save document /// -> Close document /// -> Open document /// -> The created objects have disappeared !!! /// </summary> class App : IExternalApplication { public static Guid Guid { get; private set; } public static Schema Schema { get; private set; } /// <summary> /// Create Schema to store array of ElementId /// </summary> static App() { Guid = new Guid("d05893e6-c01e-44e9-8fbb-0d140a26015b"); SchemaBuilder schemaBuilder = new SchemaBuilder(Guid); schemaBuilder.SetReadAccessLevel(AccessLevel.Public); schemaBuilder.SetWriteAccessLevel(AccessLevel.Public); schemaBuilder.AddArrayField("ObjectArr", typeof(ElementId)); schemaBuilder.SetSchemaName("ObjectsSchema"); Schema = schemaBuilder.Finish(); } public Result OnStartup(UIControlledApplication a) { a.ControlledApplication.ApplicationInitialized += OnApplicationInitialized; return Result.Succeeded; } void OnApplicationInitialized(object sender, ApplicationInitializedEventArgs e) { Application app = sender as Application; //Add event handlers for DocumentSaving and DocumentSavingAs events //Both of event handlers are execute same method which create Entity and save it into ProjectInfo app.DocumentSaving += DocumentSaving_EventHandler; app.DocumentSavingAs += DocumentSavingAs_EventHandler; } public static void DocumentSaving_EventHandler(object sender, DocumentSavingEventArgs e) { Document doc = e.Document; SaveDocumentObjectIds(doc); } public static void DocumentSavingAs_EventHandler(object sender, DocumentSavingAsEventArgs e) { Document doc = e.Document; SaveDocumentObjectIds(doc); } /// <summary> /// Create Entity /// Save ids of all family instances in document into ProjectInfo /// </summary> /// <param name="doc"></param> private static void SaveDocumentObjectIds(Document doc) { if (!doc.IsFamilyDocument) { Entity entity = new Entity(Schema); IList<ElementId> objectArr = new List<ElementId>(); FilteredElementCollector familyInstanceCollector = new FilteredElementCollector(doc); familyInstanceCollector.OfClass(typeof(FamilyInstance)); foreach (Element elem in familyInstanceCollector) { objectArr.Add(elem.Id); } entity.Set("ObjectArr", objectArr); if (entity.IsValid()) { using (Transaction trans = new Transaction(doc)) { trans.Start("SaveDataIntoDocument"); doc.ProjectInformation.SetEntity(entity); trans.Commit(); } } } } public Result OnShutdown(UIControlledApplication a) { return Result.Succeeded; } } }
Solved! Go to Solution.