Hello!
I have a problem when deleting a view.
I'm using this:
doc.Delete(existingView.Value().Id);
existingView.Value() will return the view and the view will have a valid Id.
The error thrown is this:
Autodesk.Revit.Exceptions.ArgumentException: ElementId cannot be deleted. Parameter name: elementId at Autodesk.Revit.DB.Document.Delete(ElementId elementId)
I saw in this post that the condition for this exception is :
The element elementId does not exist in the document -or- ElementId cannot be deleted.
Can someone shred some light on why ElementId cannot be deleted ? What could be the cause ?
I also checked the first case and the element with that id is present in the document.
I'd like to mention that this is happening only on one model ( this model belongs to a client and i cannot share it ).
Thank you in advance for your replies.
Solved! Go to Solution.
Solved by matthew_taylor. Go to Solution.
Hello @schiriac.robert,
Have you tried to delete the view using the Revit UI?
Have you checked that the view is a user visible view?
Perhaps it is the last open view?
Perhaps it is corrupt in some way. Have you audited the model?
Workset issue?
Some checks to do to mitigate this sort of exception:
Check .IsValidObject on the view element.
Check you can delete the element using CanDeleteElement? http://www.revitapidocs.com/2017/e9c8b6a3-5c37-a413-c1aa-c47f8f166a6c.htm
Check it's not the last open view.
Cheers,
-Matt
Hello @matthew.taylor ,
Thank you for your response & sorry for delay in my reply ( was busy with other tickets )
Have you tried to delete the view using the Revit UI?
> Yes. It works
Have you checked that the view is a user visible view?
> Yes
Perhaps it is the last open view?
> No. There are multiple views besides that ( I know the last view can't be deleted so i made sure there are multiple views besides the ones i try to delete)
Perhaps it is corrupt in some way. Have you audited the model?
> I don't know how could be corrupted. I also don't know how to audit the model ( I'm going to find a guide on how to do that and post results )
Workset issue?
> I don't think there was any because i didn't saw anything in the error related to Workset.
Thank you again @matthew_taylor for your response!
Hi @schiriac.robert,
No problem.
You can audit the model when you open it - it's a checkbox on the open dialog.
I'm guessing that there are other things happening before you delete the view in your macro?
When trying to replicate the manual deletion process using the UI, you will need to replicate those also.
Worksets. If the model is workshared, create a detached copy without worksets and see if the issue persists. That will rule it out.
Did you do the checks in the second half of my post?
Cheers,
-Matt
Hi @matthew_taylor,
Did you do the checks in the second half of my post?
> Yes, I forgot to add them
Some checks to do to mitigate this sort of exception:
Check .IsValidObject on the view element.
> returns true.
Check you can delete the element using CanDeleteElement? http://www.revitapidocs.com/2017/e9c8b6a3-5c37-a413-c1aa-c47f8f166a6c.htm
> This returns false ( I don't know the reason )
Check it's not the last open view.
> Is not, i made sure i have multiple views.
I'm guessing that there are other things happening before you delete the view in your macro?
When trying to replicate the manual deletion process using the UI, you will need to replicate those also.
> Not really, just searching for the view i want to delete and get the Id.
Worksets. If the model is workshared, create a detached copy without worksets and see if the issue persists. That will rule it out.
> The issue persists.
I also checked with RevitLookup each view i try to delete and made sure they are not locked, pinned, or ( saw on another forum post that that could be a case )
I've added here 2 RevitLookup prints on the views i try to delete:
https://drive.google.com/drive/folders/0B4O5bUcy_He_SnNMWDdCaUdqWU0?usp=sharing
Thank you for your response and for helping me on this issue!
Have a great day/night!
I don't see you mentioning whether or not the view is the active view or not. Maybe you did. Trying to delete the active view is not possible. Also, you cannot delete any view that is currently being shown in a PreviewControl that you might happen to be using. You have to dispose of that control before deleting the view it might have been showing.
Hi @schiriac.robert,
Sadly, that doesn't spread any light on the issue.
Did you audit the model as I suggested (and explained how to do)?
Have you reduced the issue to a minimum reproducible case with a hard-coded ElementId?
If none of those ideas help, then consider
Otherwise, I'm all out of guesses!
Cheers,
-Matt
p.s. My testing wasn't a dead loss...I figured out a view (not a viewport) may be 'pinned'.
Hi @matthew_taylor , @aksaks,
Thank you all for your help! A colleague of mine figured out the issue.
He told me that there was a method called before deleting the view and that method set the first view (not the ones we were trying to delete) as active but the check was invalid so if the model didn't had other views which were positioned higher than the ones were trying to delete it would select and set as active the first view in that list which was in fact one of the views we were trying to delete.
I don't know how could i missed that but i'm glad he figured it out.
Thank you for all your responses and for your help!
I'm deeply sorry for wasting your time.