Delete Materials is not working

Delete Materials is not working

Anonymous
Not applicable
683 Views
1 Reply
Message 1 of 2

Delete Materials is not working

Anonymous
Not applicable

Hello, everyone!

 

Can anyone help me with problem. Delete element by Id is not working.

 

That's my code:

 

    Private Sub DataTransferStartButton_Click(sender As Object, e As EventArgs) Handles DataTransferStartButton.Click

        Dim App As Autodesk.Revit.UI.UIApplication = myCD.Application()
        Dim UIDoc As UIDocument = App.ActiveUIDocument
        Dim Doc As Document = App.ActiveUIDocument.Document

        App.OpenAndActivateDocument("C:\Users\Pashin.Evgeniy\Desktop\001_Проба.rfa")

        Dim tr As New Transaction(Doc, "DeleteAllElements")
        tr.Start()

        Try
            Dim coll As FilteredElementCollector = New FilteredElementCollector(Doc).OfClass(GetType(Material))
            For Each dtFillPaternElement As Autodesk.Revit.DB.Material In coll
                Doc.Delete(dtFillPaternElement.Id)
                MsgBox("I kill material with name " & dtFillPaternElement.Name.ToString)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        tr.Commit()
        tr.Dispose()

    End Sub

I'm using ExternalCommand and myForm.

 

myForm.ShowDialog( ) is not working!!!

 

I'm using myForm.Show( ) - and It's better...

 

Family project is opening but materials is not deleted!!! Why? Transaction is not completed!!! Why?

0 Likes
684 Views
1 Reply
Reply (1)
Message 2 of 2

jeremytammik
Autodesk
Autodesk

You cannot modify the iteration variables within the loop.

 

Always terminate the filtered element collector iteration before starting to modify anything at all.

 

I suggest the following:

 

1. Collect all the materials to delete and store their element ids.

2. Open the transaction.

3. Delete the elements with one single call to the Document.Delete method taking an ICollection of ElementIds argument.

4. Commit the transaction.

 

Cheers,

 

Jeremy



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

0 Likes