AppearanceAssetElements can' be Delete

AppearanceAssetElements can' be Delete

Sean_Page
Collaborator Collaborator
518 Views
3 Replies
Message 1 of 4

AppearanceAssetElements can' be Delete

Sean_Page
Collaborator
Collaborator

I am trying to create a simple utility to clean up Families. Things like deleting all materials and Fill patterns.  These work as expected, but for some reason, even after deleting the materials, the AppearanceAssetElement's are not allowed to be deleted.

 

I have validated that I am getting the assets, and they should not be linked to anything else, and they have no dependent elements either. Is there something I am missing as to why these can't be deleted, but can be "purged" from the model?

 

using(FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(AppearanceAssetElement)))
{
	foreach(AppearanceAssetElement asset in collector.ToElements())
	{
		try
		{
			doc.Delete(asset.Id);
		}
		catch(Exception ex)
		{
			continue;
		}
	}
}

 

 I eleven attempted to use the GetSubelements() and DeleteSubelements() methods on the Asset class, but those did not return any elements either.

 

My quick test and results in Dynamo for show:

spage_0-1701382778890.png

 

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
519 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

Might you possibly need an AppearanceAssetEditScope or some such thing?

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 4

Sean_Page
Collaborator
Collaborator

Thanks Jeremy, I did look at that and if that is the pathway I don't understand how. The Asset provided by the EditScope is not an Element so it does not have an Id for deletion.

If I enter into the EditScope and then try to delete it still says it's not valid for deletion.  I also thought maybe it was because there were related properties, but there aren't any that it returns.

I feel like there must be somewhere else that the element containing the Asset lives, but that is what I can't find thus far.

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 4 of 4

GaryOrrMBI
Collaborator
Collaborator

I'm finally getting to this...

I'm not sure as to your entire process but I am able to delete material assets from a project document using this (I haven't tested it extensively but it works on a quick test run, hopefully you can convert the VB to reverse engineer it for your specific needs):

	'Purge unused document Material Assets
	Public Sub PurgeUnusedMatAssets(thisDoc As RDB.Document)
		Dim printString As String = "Material Assets to purge" & vbCrLf

		'first, collect all materials to generate lists of Assets in use:
		Dim matCollector As RDB.FilteredElementCollector = New RDB.FilteredElementCollector(thisDoc)
		Dim allMats As IList(Of RDB.Material) = matCollector.OfClass(GetType(RDB.Material)).OfType(Of RDB.Material).ToList

		Dim appAsset As New List(Of RDB.ElementId)
		Dim struAsset As New List(Of RDB.ElementId)
		Dim thermAsset As New List(Of RDB.ElementId)
		For Each thisMat As RDB.Material In allMats
			If thisMat.AppearanceAssetId <> RDB.ElementId.InvalidElementId Then
				If appAsset.Contains(thisMat.AppearanceAssetId) = False Then
					appAsset.Add(thisMat.AppearanceAssetId)
				End If
			End If
			If thisMat.StructuralAssetId <> RDB.ElementId.InvalidElementId Then
				If struAsset.Contains(thisMat.StructuralAssetId) = False Then
					struAsset.Add(thisMat.StructuralAssetId)
				End If
			End If
			If thisMat.ThermalAssetId <> RDB.ElementId.InvalidElementId Then
				If thermAsset.Contains(thisMat.ThermalAssetId) = False Then
					thermAsset.Add(thisMat.ThermalAssetId)
				End If
			End If
		Next

		Using thisTrans As New RDB.Transaction(thisDoc, "Purge Material Assets")
			thisTrans.Start()

			'collect document Appearance Asset Elements
			Dim assetCollector As New RDB.FilteredElementCollector(thisDoc)
			Dim allAppearanceAssets As IList(Of RDB.AppearanceAssetElement) = assetCollector.OfClass(GetType(RDB.AppearanceAssetElement)) _
			.OfType(Of RDB.AppearanceAssetElement).ToList
			'process the collection
			For Each thisAssetElem As RDB.AppearanceAssetElement In allAppearanceAssets
				'test for in use
				If appAsset.Contains(thisAssetElem.Id) = False Then
					printString += thisAssetElem.Name & vbCrLf
					Try
						thisDoc.Delete(thisAssetElem.Id)
						printString += vbTab & "Purged" & vbCrLf
					Catch ex As Exception
						printString += vbTab & "Purge Failed" & vbCrLf
					End Try
				End If
			Next

			'collect document Property Set Elements
			Dim pSetCollector As New RDB.FilteredElementCollector(thisDoc)
			Dim allPropertyAssets As IList(Of RDB.PropertySetElement) = pSetCollector.OfClass(GetType(RDB.PropertySetElement)) _
			.OfType(Of RDB.PropertySetElement).ToList
			'process the collection
			For Each thisAssetElem As RDB.PropertySetElement In allPropertyAssets
				If struAsset.Contains(thisAssetElem.Id) = False AndAlso thermAsset.Contains(thisAssetElem.Id) = False Then
					printString += thisAssetElem.Name & vbCrLf
					Try
						thisDoc.Delete(thisAssetElem.Id)
						printString += vbTab & "Purged" & vbCrLf
					Catch ex As Exception
						printString += vbTab & "Purge Failed" & vbCrLf
					End Try
				End If
			Next

			thisTrans.Commit()
		End Using

		RUI.TaskDialog.Show("Material Assets", printString)

	End Sub

 

Before running:

GaryOrrMBI_0-1702146902513.png

the taskdialog created:

GaryOrrMBI_1-1702146982590.png

The result in the document:

GaryOrrMBI_2-1702147055161.png

 

-G

Gary J. Orr
GaryOrrMBI (MBI Companies 2014-Current)
aka (past user names):
Gary_J_Orr (GOMO Stuff 2008-2014);
OrrG (Forum Studio 2005-2008);
Gary J. Orr (LHB Inc 2002-2005);
Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes