void ChangeRenderingTexturePath(Document doc) { // As there is only one material in the sample // project, we can use FilteredElementCollector // and grab the first result Material mat = new FilteredElementCollector(doc).OfClass(typeof(Material)).FirstElement() as Material; // Fixed path for new texture // Texture included in sample files string texturePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "new_texture.png"); using (Transaction t = new Transaction(doc)) { t.Start("Changing material texture path"); using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc)) { Asset editableAsset = editScope.Start(mat.AppearanceAssetId); // Getting the correct AssetProperty AssetProperty assetProperty = editableAsset["generic_diffuse"]; Asset connectedAsset = assetProperty.GetConnectedProperty(0) as Asset; // Getting the right connected Asset if (connectedAsset.Name == "UnifiedBitmapSchema") { AssetPropertyString path = connectedAsset.FindByName(UnifiedBitmap.UnifiedbitmapBitmap) as AssetPropertyString; if (path.IsValidValue(texturePath)) { path.Value = texturePath; } } editScope.Commit(true); } TaskDialog.Show("Material texture path", "Material texture path changed to:\n" + texturePath); t.Commit(); t.Dispose(); } }
/// <summary> /// 更改外观 /// </summary> /// <param name="material"></param> /// <param name="bumpmapImageFilepath"></param> public static void ChangeRenderingTexturePath(Material mat, Document doc, string texturePath, double x, double y) { using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc)) { Asset editableAsset = editScope.Start(mat.AppearanceAssetId); #if VERSION2018 AssetProperty assetProperty = editableAsset["generic_diffuse"]; #else // VERSION2019 AssetProperty assetProperty = editableAsset.FindByName("generic_diffuse"); #endif Asset connectedAsset = assetProperty.GetConnectedProperty(0) as Asset; /*if (connectedAsset == null) * { * * // Add a new default connected asset * assetProperty.AddConnectedAsset("UnifiedBitmap"); * connectedAsset = assetProperty.GetSingleConnectedAsset(); * }*/ if (connectedAsset.Name == "UnifiedBitmapSchema") { // TaskDialog.Show("Tip", "成功"); AssetPropertyString path = connectedAsset.FindByName(UnifiedBitmap.UnifiedbitmapBitmap) as AssetPropertyString; AssetPropertyBoolean scalelock = connectedAsset.FindByName(UnifiedBitmap.TextureScaleLock) as AssetPropertyBoolean; scalelock.Value = false; AssetPropertyDistance sizeY = connectedAsset.FindByName(UnifiedBitmap.TextureRealWorldScaleY) as AssetPropertyDistance; sizeY.Value = y; AssetPropertyDistance sizeX = connectedAsset.FindByName(UnifiedBitmap.TextureRealWorldScaleX) as AssetPropertyDistance; sizeX.Value = x; if (path.IsValidValue(texturePath)) { path.Value = texturePath; } else { throw new Exception("找不到文件:" + texturePath); //TaskDialog.Show("Tip", "文件路径错误"); } } editScope.Commit(true); } }
// 定义修改贴图路径的方法 public string ChangeRenderingTexturePath(Document doc, Material mat, string newPath) { try { using (Transaction t = new Transaction(doc)) { t.Start("更改贴图位置"); using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc)) { Asset editableAsset = editScope.Start(mat.AppearanceAssetId); // Getting the correct AssetProperty AssetProperty assetProperty = editableAsset.FindByName("generic_diffuse"); if (assetProperty is null) { assetProperty = editableAsset.FindByName("masonrycmu_color"); } Asset connectedAsset = assetProperty.GetConnectedProperty(0) as Asset; // getting the right connected Asset if (connectedAsset.Name == "UnifiedBitmapSchema") { AssetPropertyString path = connectedAsset.FindByName(UnifiedBitmap.UnifiedbitmapBitmap) as AssetPropertyString; if (path.IsValidValue(newPath)) { path.Value = newPath; } } editScope.Commit(true); } t.Commit(); t.Dispose(); } return(mat.Name); } catch (Exception) { TaskDialog.Show("错误提示!!!", "材质【" + mat.Name + "】的贴图更改失败,请手动更改材质贴图"); return(null); } }