Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

DataStorage does not get stored in document

christostsar
Contributor

DataStorage does not get stored in document

christostsar
Contributor
Contributor

I've read the information and followed the instructions in https://thebuildingcoder.typepad.com/blog/2012/05/devblog-devcamp-element-and-project-wide-data.html... to create a DataStorage with project-wide settings. The problem is, after the call to DataStorage.Create(), I do get a new data storage object with an ElementId, but then this element does not exist in the document when I try to retrieve it or snoop it.

 

Here I make the transaction:

 

 

 

RevitProjectExportSettings exportProjectSettings = new RevitProjectExportSettings(exportOptions);
RevitProjectDataStorage dataStorage = new RevitProjectDataStorage();
using (Transaction t = new Transaction(doc, "Write project settings"))
{
   t.Start();
   dataStorage.writeProjectSettings(doc, exportProjectSettings);
   t.Commit();
}

 

 

 

 Here is writeProjectSettings() routine:

 

 

 

public void writeProjectSettings(Document doc, RevitProjectExportSettings settings)
{
	var settingDataStorage = getSettingsDataStorage(doc);

	if (settingDataStorage is null)
	{
		// Here I do get a new DataStorage element with a proper ElementId,
		// but when I snoop the database later it does not exist.
		settingDataStorage = DataStorage.Create(doc);
	}

	Entity settingsEntity = new Entity(RevitProjectExportSettingsSchema.getSchema());

	// Save all settings to the entity.
	settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_FILE_NAME, settings.ExportFileName);
	settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_FILE_PATH, settings.ExportPath);
	settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_MODE, settings.ExportMode);
	settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_UNIT_SET, settings.UnitSet);

	// Identify settings data storage.
	Entity idEntity = new Entity(DataStorageUniqueIdSchema.getSchema());
	idEntity.Set("Id", dataStorageId);

	settingDataStorage.SetEntity(idEntity);
	settingDataStorage.SetEntity(settingsEntity);
}

 

 

 

Here is the getSettingsDataStorage() routine:

 

 

 

private DataStorage getSettingsDataStorage(Document doc)
{
	FilteredElementCollector collector = new FilteredElementCollector(doc);
	var dataStorages = collector.OfClass(typeof(DataStorage));

	// Find our data storage identified by containing our specific settings schema.
	foreach (DataStorage dataStorage in dataStorages)
	{
		Entity settingIdEntity = dataStorage.GetEntity(DataStorageUniqueIdSchema.getSchema());
		if (!settingIdEntity.IsValid()) continue;

		// Check that the id matches our storage id.
		var id = settingIdEntity.Get<Guid>("Id");
		if (id.Equals(dataStorageId))
		{
			return dataStorage;
		}
	}

	return null;
}

 

 

Reply
Accepted solutions (2)
371 Views
3 Replies
Replies (3)

RPTHOMAS108
Mentor
Mentor
Accepted solution

Have you stepped through the code all the way to t.commit?

What API interface are you calling this from, are you returning Result.Succeeded?

 

The example is quite old there is now the ..DB.ExtensibleStorage.ExtensibleStorageFilter which will allow you to filter for elements containing your schema id such as the DataStorage you are trying to find.

renencon
Enthusiast
Enthusiast
Accepted solution

I suppose when say you want to create project-wide settings you refer to a single location within the document to store your exportProjectSettings.


Also, in your writeProjectSettings routine the fields in your schema if they are strings and named as listed below
EXPORT_FILE_NAME
EXPORT_FILE_PATH
EXPORT_MODE
EXPORT_UNIT_SET


and they should have been created in your schema creation/lookup routing as bellow

SchemaBuilder schemaBuilder = new SchemaBuilder(<<YOUR-SCHEMA-GUID here>>);
schemaBuilder.AddSimpleField("EXPORT_FILE_NAME", typeof(string));

 

then replace the lines below
settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_FILE_NAME, settings.ExportFileName);
settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_FILE_PATH, settings.ExportPath);
settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_MODE, settings.ExportMode);
settingsEntity.Set(RevitProjectExportSettingsSchema.EXPORT_UNIT_SET, settings.UnitSet);

 

with


settingsEntity.Set<string>(“EXPORT_FILE_NAME”, settings.ExportFileName);
settingsEntity.Set<string>(“EXPORT_FILE_PATH”, settings.ExportPath);
settingsEntity.Set<string>(“EXPORT_MODE”, settings.ExportMode);

settingsEntity.Set<string>(“EXPORT_UNIT_SET”, settings.UnitSet);

 

I do not think the DataStorageUniqueIdSchema entity is required here, and the lines below can be removed.


// Identify settings data storage.
Entity idEntity = new Entity(DataStorageUniqueIdSchema.getSchema());
idEntity.Set("Id", dataStorageId);

settingDataStorage.SetEntity(idEntity);

 

Now, your getSettingsDataStorage routine I would suggest something like the one below

 

private DataStorage getSettingsDataStorage(Document doc)
{

FilteredElementCollector collector = new FilteredElementCollector(doc);

Guid RevitProjectExportSettingsSchemaGuid = RevitProjectExportSettingsSchema.getSchema().GUID;
ElementFilter ElementFilter =

new LogicalAndFilter(
new ExtensibleStorageFilter(RevitProjectExportSettingsSchemaGuid),
new ElementClassFilter(typeof(DataStorage)));


var dataStorages = collector.OfClass(typeof(DataStorage)).WherePasses(ElementFilter);
if (dataStorages.FirstOrDefault() is DataStorage dataStorage && dataStorage.IsValidObject)
{
return dataStorage;
}
return null;

}

 

I hope the above makes sense.

0 Likes

christostsar
Contributor
Contributor

The problem was the Result.Succeeded as suggested. I did not know that the returned result had any impact on the transactions if they were committed.

 

Thanks a lot for the info on the new filters, I am definitely going to incorporate those.