InternalException when using Schema.EraseSchemaAndAllEntities()

InternalException when using Schema.EraseSchemaAndAllEntities()

Anonymous
Not applicable
3,024 Views
8 Replies
Message 1 of 9

InternalException when using Schema.EraseSchemaAndAllEntities()

Anonymous
Not applicable

Hi,

 

I am getting an InternalException when I run this method. I have tried it with the argument overrideWriteAccessWithUserPermission set to false and it also trows an InternalException.

 

public void DeleteAllSchemas()
{
      using (Transaction trans = 
                  new Transaction(_doc, "Delete Schemas Transaction"))
      {
          trans.Start();
          IList<Schema> schemas = Schema.ListSchemas();

          foreach (Schema schema in schemas)
          {
               Schema.EraseSchemaAndAllEntities(schema, true);  
          }

          trans.Commit();
      }
}

  Any idea on how to adress this issue?

 

Thanks

 

Marc

Accepted solutions (1)
3,025 Views
8 Replies
Replies (8)
Message 2 of 9

Ning_Zhou
Advocate
Advocate

did a quick test and works OK, my test model have schemas but not yet attach ES to any element, is it because you still have ES attached to some elements? or it simply doesn't matter?

Message 3 of 9

jeremytammik
Autodesk
Autodesk

Dear Marc,

Thank you for your query.

Basically, the Revit SDK ExtensibleStorageUtility shows you how to use this method.

Ning, I believe it should work perfectly well even if entities using the schema still exist.

One thing that strikes me as rather strange is that the Schema.EraseSchemaAndAllEntities method will delete storage of this schema in *all* open documents.

In your code, you only have a transaction open on the document stored in the _doc variable.

I wonder how this will affect other documents if you have no trnasaction open for them.

I would suggest running this with only one document open in the current Revit session.

Can you please confirm that this is in fact the case, please?

Thank you.

Here is a list of The Building Coder topics related to this topic:

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.23

The last one, especially, may be of use.

I hope this helps.

Best regards,



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 9

Anonymous
Not applicable

Hi Jeremy,

 

Thanks for your help. 

My document have one linked project in it, but it tried to run the code in a document with no links and I keep getting the same error. This is the code that I've been using for testing.

 

public void CreateESDSchema()
{
    using (Transaction trans = new Transaction(_doc, "Create Schema"))
    {
        trans.Start();

        Schema schema = Schema.Lookup(_schemaGuid);

        if (schema == null)
        {
            SchemaBuilder schemaBuilder = new SchemaBuilder(_schemaGuid);

            schemaBuilder.SetSchemaName("Tracking Elements");
            schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
            schemaBuilder.SetWriteAccessLevel(AccessLevel.Public);

            FieldBuilder fieldBuilder = schemaBuilder
                .AddSimpleField("LinkedInstanceElementID", typeof(int));
            fieldBuilder.SetDocumentation("Instance Element Unique ID");

            fieldBuilder = schemaBuilder
                .AddSimpleField("LinkedTypeElementID", typeof(int));
            fieldBuilder.SetDocumentation("Type Element Unique ID");

            fieldBuilder = schemaBuilder
                .AddArrayField("InstanceParameters", typeof(string));
            fieldBuilder.SetDocumentation("List of instance parameters");

            fieldBuilder = schemaBuilder
                .AddArrayField("TypeParameters", typeof(string));
            fieldBuilder.SetDocumentation("List of type parameters");

            fieldBuilder = schemaBuilder
                .AddSimpleField("LinkedDocument", typeof(string));
            fieldBuilder.SetDocumentation("Document where element is linked");

            schema = schemaBuilder.Finish(); // register the Schema object
        }

        trans.Commit();
    }

}


public void SetDataLinkedInsElemID(int id)
{
    using (Transaction trans = new Transaction(_doc, "Store Data trans"))
    {
        trans.Start();
        Schema schema = Schema.Lookup(_schemaGuid);        

        Entity entity = new Entity(schema);
        entity.Set<int>(schema.GetField("ESD_LinkedInstanceElementID"), id);

        _modelElementInstance.SetEntity(entity); 

        trans.Commit();
    }
}


public void DeleteSchema()
{
       Schema schema = Schema.Lookup(_schemaGuid);
       Schema.EraseSchemaAndAllEntities(schema, true);            
}

 

The variable _modelElementInstance it's an element that I manually pick. 

 

If I run this when deleting the Schema I get the InternalException.

 

CreateESDSchema();
SetDataLinkedInsElemID(123456);
DeleteSchema();

 

In one of your articles (http://thebuildingcoder.typepad.com/blog/2013/08/deleting-and-updating-extensible-storage-schema.htm...) someone have a similar issue. Can you confirm if the issue is caused because the linked files?

 

Regarding my code for testing, am I doing something wrong? 

 

I highgly apreciate your help.

 

Thanks again,

 

Marc

 

0 Likes
Message 5 of 9

jeremytammik
Autodesk
Autodesk

Dear Marc,

 

Revit handles element ids in extensible storage intelligently.

 

You may need to populate them all, at least with an InvalidElementId.

 

Please let us know whether this helps.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 6 of 9

Anonymous
Not applicable

HI Jeremy,

 

Thanks again for your help.

 

The type of fields used are just an example and I don't think is causing the error. The problem is that I can't delete the schema from the document if I want to.

 

 

I knew that Revit handles elements ids intelligently but just with elements within that document.

This addin is intended to track elements from linked projects, so I couldn't use this feature.

 

Please, let me know if you find any other issue that maybe causing that problem.

 

Thanks

 

Marc

 

 

0 Likes
Message 7 of 9

Anonymous
Not applicable
Accepted solution

Hi,

 

I finally found that the InternalException was caused by having projects linked in the document.

 

I have to Unload all linked files first and run the method Schema.EraseSchemaAndAllEntities(schema, false) and then Load the files again.

 

This is the final custom method that worked for me including the transaction.

 

public void DeleteSchema()
{
    using (Transaction trans = new Transaction(_doc, "Delete Schema Transaction"))
    {
        trans.Start();
        Schema schema = Schema.Lookup(_schemaGuid);
        Schema.EraseSchemaAndAllEntities(schema, false); 
        trans.Commit();
    }                       
}

 

Thanks for all your help!

 

Marc

0 Likes
Message 8 of 9

jeremytammik
Autodesk
Autodesk

Dear Marc,

 

Thank you for the confirmation!

 

To ensure that your work does not go unnoticed, here is a blog post reiterating the result:

 

http://thebuildingcoder.typepad.com/blog/2013/11/erasing-extensible-storage-with-linked-files.html

 

Thank you very much!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 9 of 9

Ning_Zhou
Advocate
Advocate

just noticed that even if all links are unloaded, but if i zoom to that element before using Schema.EraseSchemaAndAllEntities() then same exception occurred again:

ElementId id = ent.Get<ElementId>("MyField");
if (id != ElementId.InvalidElementId) uidoc.ShowElements(id);

0 Likes