//---------------------------------------------------------- public static void set_appearance_asset(Document doc, Material material) { try { var asset = new FilteredElementCollector(doc).OfClass(typeof(AppearanceAssetElement)).First(x => x.Name == "Generic"); AppearanceAssetElement assetElem = asset as AppearanceAssetElement; AppearanceAssetElement assetElemNew = assetElem.Duplicate(material.Name + DateTime.Now.ToString("ddMMyyHHmmss")); AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc); Asset editableAsset = editScope.Start(assetElemNew.Id); AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset.FindByName("generic_diffuse") as AssetPropertyDoubleArray4d; genericDiffuseProperty.SetValueAsColor(material.Color); AssetPropertyDouble genericTransparency = editableAsset.FindByName("generic_transparency") as AssetPropertyDouble; genericTransparency.Value = Convert.ToDouble(material.Transparency) / 100; editScope.Commit(true); material.AppearanceAssetId = assetElemNew.Id; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//---------------------------------------------------------- public void SetColorAppearanceAsset(Document doc, Material material) { try { var list = new FilteredElementCollector(doc).OfClass(typeof(AppearanceAssetElement)).ToList(); List <string> name = new List <string>(); foreach (AppearanceAssetElement e in list) { name.Add(e.Name); } string newName = ""; for (var i = 0; i < 10000000; i++) { if (name.Any(x => x == material.Name + "(" + i.ToString() + ")") == false) { newName = material.Name + "(" + i.ToString() + ")"; break; } } AppearanceAssetElement assetElem = list[0] as AppearanceAssetElement; AppearanceAssetElement assetElemNew = assetElem.Duplicate(newName); AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(doc); Asset editableAsset = editScope.Start(assetElemNew.Id); AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset["generic_diffuse"] as AssetPropertyDoubleArray4d; genericDiffuseProperty.SetValueAsColor(material.Color); AssetPropertyDouble genericTransparency = editableAsset["generic_transparency"] as AssetPropertyDouble; genericTransparency.Value = Convert.ToDouble(material.Transparency) / 100; editScope.Commit(true); material.AppearanceAssetId = assetElemNew.Id; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Enable tint color. /// </summary> private void EnableTintColor() { using (Transaction transaction = new Transaction(m_document, "Enable tint color")) { transaction.Start(); using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(m_document)) { Asset editableAsset = editScope.Start(m_currentAppearanceAssetElementId); // If the material supports tint but it is not enabled, it will be enabled first with a value (255 255 255) AssetPropertyBoolean tintToggleProp = editableAsset["common_Tint_toggle"] as AssetPropertyBoolean; AssetPropertyDoubleArray4d tintColorProp = editableAsset["common_Tint_color"] as AssetPropertyDoubleArray4d; tintToggleProp.Value = true; tintColorProp.SetValueAsColor(new Color(255, 255, 255)); editScope.Commit(true); } transaction.Commit(); } }
/// <summary> /// Edit tint color property. /// </summary> /// <param name="lighter">Increase the tint color property or not.</param> internal void EditMaterialTintColorProperty(bool lighter) { using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(m_document)) { Asset editableAsset = editScope.Start(m_currentAppearanceAssetElementId); AssetPropertyDoubleArray4d metalColorProp = editableAsset["common_Tint_color"] as AssetPropertyDoubleArray4d; Color color = metalColorProp.GetValueAsColor(); byte red = color.Red; byte green = color.Green; byte blue = color.Blue; // Increment factor (value related to 255) int factor = 25; if (lighter) { red = (byte)LimitValue(red + factor); green = (byte)LimitValue(green + factor); blue = (byte)LimitValue(blue + factor); } else { red = (byte)LimitValue(red - factor); green = (byte)LimitValue(green - factor); blue = (byte)LimitValue(blue - factor); } if (metalColorProp.IsValidValue(color)) { metalColorProp.SetValueAsColor(new Color(red, green, blue)); } editScope.Commit(true); } }