/// <summary>
 /// 自定义方法,用递归找到包含贴图信息:Asset包含的AssetProperty有多种类型,其中Asset、Properties
 /// 和Reference这三种必须递归处理。贴图信息的AssetProperty名字是unifiedbitmap_Bitmap,类型是String。
 /// </summary>
 /// <param name="ap"></param>
 /// <returns></returns>
 private Autodesk.Revit.DB.Visual.Asset FindTextureAsset(AssetProperty ap)
 {
     Autodesk.Revit.DB.Visual.Asset result = null;
     if (ap.Type == AssetPropertyType.Asset)
     {
         if (!IsTextureAsset(ap as Autodesk.Revit.DB.Visual.Asset))
         {
             for (int i = 0; i < (ap as Autodesk.Revit.DB.Visual.Asset).Size; i++)
             {
                 if (null != FindTextureAsset((ap as Autodesk.Revit.DB.Visual.Asset)[i]))
                 {
                     result = FindTextureAsset((ap as Autodesk.Revit.DB.Visual.Asset)[i]);
                     break;
                 }
             }
         }
         else
         {
             result = ap as Autodesk.Revit.DB.Visual.Asset;
         }
         return(result);
     }
     else
     {
         for (int j = 0; j < ap.NumberOfConnectedProperties; j++)
         {
             if (null != FindTextureAsset(ap.GetConnectedProperty(j)))
             {
                 result = FindTextureAsset(ap.GetConnectedProperty(j));
             }
         }
         return(result);
     }
 }
        /// <summary>
        /// 自定义方法,判断Asset是否包含贴图信息
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        private bool IsTextureAsset(Autodesk.Revit.DB.Visual.Asset asset)
        {
            AssetProperty assetProprty = GetAssetProprty(asset, "assettype");

            if (assetProprty != null && (assetProprty as AssetPropertyString).Value == "texture")
            {
                return(true);
            }
            return(GetAssetProprty(asset, "unifiedbitmap_Bitmap") != null);
        }
 /// <summary>
 /// 自定义方法,根据名字获取对应的AssetProprty
 /// </summary>
 /// <param name="asset"></param>
 /// <param name="propertyName"></param>
 /// <returns></returns>
 private AssetProperty GetAssetProprty(Autodesk.Revit.DB.Visual.Asset asset, string propertyName)
 {
     for (int i = 0; i < asset.Size; i++)
     {
         if (asset[i].Name == propertyName)
         {
             return(asset[i]);
         }
     }
     return(null);
 }
        /// <summary>
        /// 设置材质
        /// </summary>
        /// <remarks>
        //可以为每个单独的输出网格调用OnMaterial方法
        ///即使材质尚未实际更改。 因此通常
        ///有利于存储当前材料并仅获取其属性
        ///当材质实际更改时。
        /// </remarks>
        public void OnMaterial(MaterialNode node)
        {
            if (currentMaterialId != node.MaterialId)
            {
                currentMaterialId   = node.MaterialId;
                currentColor        = node.Color;
                currentTransparency = node.Transparency;
                swMtl.Write(strNewmtl, currentMaterialId.IntegerValue.ToString(),
                            (currentColor.Red / 256.0).ToString(), (currentColor.Green / 256.0).ToString(), (currentColor.Blue / 256.0).ToString(),
                            currentTransparency);
                if (node.HasOverriddenAppearance)
                {
                    currentAsset = node.GetAppearanceOverride();
                }
                else
                {
                    currentAsset = node.GetAppearance();
                }

                try
                {
                    //取得Asset中贴图信息
                    string textureFile = (FindTextureAsset(currentAsset as AssetProperty)["unifiedbitmap_Bitmap"] as AssetPropertyString).Value.Split('|')[0];
                    //用Asset中贴图信息和注册表里的材质库地址得到贴图文件所在位置
                    string texturePath = Path.Combine(textureFolder, textureFile.Replace("/", "\\"));
                    //写入贴图名称
                    swMtl.Write("map_Kd " + Path.GetFileName(texturePath) + "\n");
                    //如果贴图文件真实存在,就复制到相应位置
                    if (File.Exists(texturePath))
                    {
                        File.Copy(texturePath, Path.Combine(Path.GetDirectoryName(filePath), textureName), true);
                    }
                }
                catch (Exception e)
                {
                    Debug.Print("{0} Second exception.", e.Message);
                }
            }
            Debug.Print($"Material {node.NodeName}");
        }
Esempio n. 5
0
        /// <summary>
        /// 设置当前材质
        /// </summary>
        /// <param name="uidMaterial"></param>
        void SetCurrentMaterial(string uidMaterial, MaterialNode node)
        {
            if (!_materials.ContainsKey(uidMaterial))
            {
                RMaterial       material = CurrentDocument.GetElement(uidMaterial) as RMaterial;
                Color           c        = material.Color;
                MaterialBuilder m        = null;
                try
                {
                    if (material.Transparency != 0)
                    {
                        m = new MaterialBuilder()
                            .WithAlpha(SharpGLTF.Materials.AlphaMode.BLEND)
                            .WithDoubleSide(true)
                            .WithMetallicRoughnessShader()
                            .WithChannelParam(KnownChannel.BaseColor, new Vector4(c.Red / 256f, c.Green / 256f, c.Blue / 256f, 1 - (material.Transparency / 128f)));
                    }
                    else
                    {
                        m = new MaterialBuilder()
                            .WithDoubleSide(true)
                            .WithMetallicRoughnessShader()
                            .WithChannelParam(KnownChannel.BaseColor, new Vector4(c.Red / 256f, c.Green / 256f, c.Blue / 256f, 1));
                    }
                    Autodesk.Revit.DB.Visual.Asset currentAsset;
                    if (node.HasOverriddenAppearance)
                    {
                        currentAsset = node.GetAppearanceOverride();
                    }
                    else
                    {
                        currentAsset = node.GetAppearance();
                    }

                    try
                    {
                        //取得Asset中贴图信息
                        Autodesk.Revit.DB.Visual.Asset findAssert = FindTextureAsset(currentAsset as AssetProperty);
                        if (findAssert != null)
                        {
                            string textureFile = (findAssert["unifiedbitmap_Bitmap"] as AssetPropertyString).Value.Split('|')[0];
                            //用Asset中贴图信息和注册表里的材质库地址得到贴图文件所在位置
                            string textureName = textureFile.Replace("/", "\\");

                            string texturePath = Path.Combine(_textureFolder, textureName);

                            m.WithChannelImage(KnownChannel.BaseColor, texturePath);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
                catch (Exception e)
                {
                }

                _materials.Add(uidMaterial, m);
            }
            _currentMaterial = _materials[uidMaterial];
        }