Revit & Navisworks 二次开发—获取材质贴图

关注公众号及时获取文章更新

Retrieving textures via Navisworks API (no solution)

关于Revit API 获取材质贴图位图 bitMap(可行)

代码如下:

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"
  };

  void FindTextureBitmapPaths( Document doc )
  {
    // 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 ) );
        }
      }
    }
  }

能获取到计算机中位图 bitmap 的路径,但通常是相对路径C:\Program Files (x86)\Common Files\Autodesk Shared\Materials\Textures,这是Revit默认的材质库,当然用户也可以自定义渲染外观的路径;也会出现绝对路径的情况,也就是贴图文件既不在默认材质库文件夹中,也不在用户添加的渲染外观路径下。

对于绝对路径,我们可以直接找到它,而对于相对路径,我需要做一些逻辑判断,首先判断默认的材质库中是否有该文件,如果没有,再读取 Revit.ini (Initialization File 初始化文件)文件来找到用户定义的其他渲染外观路径,接着在这些路径下找到对应的贴图文件。

Revit.ini 路径: C:\Users\11204\AppData\Roaming\Autodesk\Revit\Autodesk Revit 2018 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章