AddCopyAsConnectedAsset Method


Asset Property Add Copy As Connected Asset Method

Makes a copy of the asset and connects it to this property.

Namespace: Autodesk.Revit.DB.Visual
Assembly: RevitAPI (in RevitAPI.dll) Version: 27.0.4.0 (27.0.4.0)
Syntax
public void AddCopyAsConnectedAsset(
	Asset pRenderingAsset
)

Parameters

pRenderingAsset Asset
The asset to duplicate and associate with this property as a connected asset.
Exceptions
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.
Example
C#
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();
   }
}
See Also
ArchiLabs Desktop

Put AI to work inside Revit.

Give Desktop your instructions and project files to build Revit families, update model elements, and create views and schedules. Works directly in Revit and AutoCAD.

Explore Desktop →