Asset |
| Exception | Condition |
|---|---|
| ArgumentNullException | A non-optional argument was null |
| InvalidOperationException | The asset property is not editable. -or- Cannot check validity for a property not being edited in AppearanceAssetEditScope. -or- Asset property is already connected to one asset. |
public void CopyAndAddConnectedAsset(Material sourceMaterial, Material targetMaterial) { Document doc = targetMaterial.Document; // Get the appearance asset of the source material ElementId otherAppearanceAssetId = sourceMaterial.AppearanceAssetId; AppearanceAssetElement otherAssetElem = doc.GetElement(otherAppearanceAssetId) as AppearanceAssetElement; Asset otherAsset = otherAssetElem.GetRenderingAsset(); // Get the connected asset of the source material appearance asset (that contains the bitmap) AssetProperty otherGenericDiffuseProperty = otherAsset.FindByName(Generic.GenericDiffuse); Asset otherGenericDiffuseConnectedAsset = otherGenericDiffuseProperty.GetSingleConnectedAsset(); using (Transaction t = new Transaction(doc, "Change a connected asset by a copy")) { t.Start(); using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc)) { ElementId appearanceAssetId = targetMaterial.AppearanceAssetId; Asset editableAsset = editScope.Start(appearanceAssetId); // returns an editable copy of the appearance asset AssetProperty genericDiffuseProperty = editableAsset.FindByName(Generic.GenericDiffuse); // Find the connected asset (with a shortcut to get the only one) Asset genericDiffuseConnectedAsset = genericDiffuseProperty.GetSingleConnectedAsset(); if (genericDiffuseConnectedAsset == null) { // Target material has only a color for �GenericDiffuse� // This will assign a bitmap to it instead // by making a copy of the asset and use it as connected asset genericDiffuseProperty.AddCopyAsConnectedAsset(otherGenericDiffuseConnectedAsset); } editScope.Commit(true); } t.Commit(); } }