Parsing AssetProperty to UnifiedBitmap?

Anonymous

Parsing AssetProperty to UnifiedBitmap?

Anonymous
Not applicable

I'm fairly new to the Revit API + some aspects of C#.


I'm having some problems getting the final varaibles I require from a material in order to start a transaction within Revit. The post by Jeremy Tammick has been very useful.

https://thebuildingcoder.typepad.com/blog/2017/10/material-texture-path.html

 

However the line :

AssetPropertyString path = connectedAsset[
            UnifiedBitmap.UnifiedbitmapBitmap] 
              as AssetPropertyString;

is causing me this error
"Cannot apply indexing with [] to an expression of type 'Autodesk.Revit.DB.Visual.AssetProperty'.

 

Which makes sense to me because connected asset is an instance and not a List, IList or an Array. Innevitably I am trying to access and edit the Bitmaps link, offset and size parameters. I think I need to somehow pass the UnifiedBitmap to a variable, but can't seem to find the function in the API.

Any help would be amazing.

0 Likes
Reply
860 Views
3 Replies
Replies (3)

jeremy_tammik
Autodesk
Autodesk

Welcome to the Revit API! 

 

I am glad you find the blog post useful.

 

You say, connected asset is an instance...

 

In Revit, one would usually use the word 'instance' to refer to a family instance element in the BIM model.

 

In C#, everything is an object and hence an instance of some class. Hence, every single List, IList or Array object is indeed an instance.

 

So I do not understand your statement at all.

 

In the blog post, connectedAsset is an Asset object:

 

        Asset connectedAsset = aProperty
          .GetConnectedProperty( 0 ) as Asset;

 

So, it is hard to say anything more just looking at your question so far.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

Anonymous
Not applicable

Sorry, you are correct in your phrasing.

I guess I should be saying, that connectedAsset isnt a collection, not an instance. So I am getting errors as connected_asset is not a collection.

I made the change you just recomended. I've made the change you mentioned and it still doesnt seem to be working.

Asset connected_asset = asset_property.GetConnectedProperty(0) as Asset;
														if(connected_asset.Name == "UnifiedBitmapSchema")
{
AssetPropertyString bitmap_path = connected_asset[UnifiedBitmap.UnifiedbitmapBitmap] as AssetPropertyString;
}

Errors :

Argument 1: cannot convert from 'string' to 'int' (CS1503) -

The best overloaded method match for 'Autodesk.Revit.DB.Visual.AssetProperties.this[int]'


I really want to be able to get and set t he image path, image width, height and offsets within Revit.

Does this help frame my problem?

0 Likes

Anonymous
Not applicable

Hey Jeremy,

I've taken and edited your code slightly in the marco editor and it still produces the error that I've been talking about. Is it possible that this is a limitation of the macro editor?

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Visual;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace Test
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("BE0FCACF-2A26-4DB0-95A1-2FF7F4070582")]
	public partial class ThisApplication
	{
		private void Module_Startup(object sender, EventArgs e)
		{

		}

		private void Module_Shutdown(object sender, EventArgs e)
		{

		}

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion
		public void TestA()
		{
			Document doc = ActiveUIDocument.Document;
			string[] targetMaterialNames = {

		    // A standard Revit material, with 
    		// textures in standard paths. 
    		"Brick, Common",

		    // A material with a single image from 
    		// another non-material library path
    		"Local Path Material"
  			};

    // Find materials
    FilteredElementCollector fec 
      = new FilteredElementCollector( doc );

    fec.OfClass( typeof( Material ) );

    IEnumerable<Material> targetMaterials 
      = fec.Cast<Material>().Where<Material>( mtl => 
        targetMaterialNames.Contains( mtl.Name ) );

    foreach( Material material in targetMaterials )
    {
      // Get appearance asset for read
      ElementId appearanceAssetId = material
        .AppearanceAssetId;

      AppearanceAssetElement appearanceAssetElem 
        = doc.GetElement( appearanceAssetId )
          as AppearanceAssetElement;

      Asset asset = appearanceAssetElem
        .GetRenderingAsset();

      // Walk through all first level assets to find 
      // connected Bitmap properties.  Note: it is 
      // possible to have multilevel connected 
      // properties with Bitmaps in the leaf nodes.  
      // So this would need to be recursive.

      int size = asset.Size;
      for( int assetIdx = 0; assetIdx < size; assetIdx++ )
      {
        AssetProperty aProperty = asset[assetIdx];

        if( aProperty.NumberOfConnectedProperties < 1 )
          continue;

        // Find first connected property.  
        // Should work for all current (2018) schemas.  
        // Safer code would loop through all connected
        // properties based on the number provided.

        Asset connectedAsset = aProperty
          .GetConnectedProperty( 0 ) as Asset;

        // We are only checking for bitmap connected assets. 
             
        if( connectedAsset.Name == "UnifiedBitmapSchema" )
        {
          // This line is 2018.1 & up because of the 
          // property reference to UnifiedBitmap
          // .UnifiedbitmapBitmap.  In earlier versions,
          // you can still reference the string name 
          // instead: "unifiedbitmap_Bitmap"

          AssetPropertyString path = connectedAsset[
            UnifiedBitmap.UnifiedbitmapBitmap] 
              as AssetPropertyString;

          // This will be a relative path to the 
          // built -in materials folder, addiitonal 
          // render appearance folder, or an 
          // absolute path.

          TaskDialog.Show( "Connected bitmap", 
            String.Format( "{0} from {2}: {1}", 
              aProperty.Name, path.Value, 
              connectedAsset.LibraryName ) );
        }
      }
    }
  }
			
			
			
			
		
	}
}

 

0 Likes