Obtain Generic Appearance Color on MEP Curve Objects

Obtain Generic Appearance Color on MEP Curve Objects

pmeigneux
Advocate Advocate
648 Views
2 Replies
Message 1 of 3

Obtain Generic Appearance Color on MEP Curve Objects

pmeigneux
Advocate
Advocate

Hello everybody,

 

I would like to obtain the generic appearance color on the MEP curve objects. At this time, I only succeed to retrieve this information on the surface element by using this sample of code.

 

 

      private Color GetAppearenceColor(Document doc, Face face)
        {
            Color appearenceColor = Color.InvalidColorValue;

            Material material = doc.GetElement(face.MaterialElementId) as Material;

            AppearanceAssetElement appearanceElem = material.Document.GetElement(material.AppearanceAssetId) as AppearanceAssetElement;
            Asset theAsset = appearanceElem.GetRenderingAsset();

            AssetProperty colorProperty = theAsset["generic_diffuse"];
            Type type = colorProperty.GetType();
            object apVal = null;
            try
            {
                // using .net reflection to get the value  
                var prop = type.GetProperty("Value");
                if (prop != null &&
                    prop.GetIndexParameters().Length == 0)
                {
                    apVal = prop.GetValue(colorProperty);
                }
                else
                {
                    apVal = "<No Value Property>";
                }
            }
            catch (Exception ex)
            {
                apVal = ex.GetType().Name + "-" + ex.Message;
            }

            if (apVal is DoubleArray)
            {
                var doubles = apVal as DoubleArray;

                byte r = (byte)(doubles.get_Item(0) * 255);
                byte g = (byte)(doubles.get_Item(1) * 255);
                byte b = (byte)(doubles.get_Item(2) * 255);

                appearenceColor = new Color(r,g,b);
            }

            return appearenceColor;
        }

 

Could help us to find a similar approach to get this color information on the MEP curve elements?

 

Thanks for your help,

 

Philippe

0 Likes
Accepted solutions (1)
649 Views
2 Replies
Replies (2)
Message 2 of 3

JimJia
Alumni
Alumni

Dear Philippe,

 

You can get the material of MEP Curve using Element.GetMaterialIds() or Category.Material.

 

please see codes below:

 

private Color GetAppearenceColor(Document doc, Element element)
        {
            ICollection<ElementId> ids = element.GetMaterialIds(false);
            MEPCurve mep = element as MEPCurve;

            Color appearenceColor = Color.InvalidColorValue;

            if (ids.Count > 0)
            {
                Material material = doc.GetElement(ids.ElementAt(0)) as Material;
                //You can also use Category.Material
                //Material material = mep.Category.Material;
                AppearanceAssetElement appearanceElem = material.Document.GetElement(material.AppearanceAssetId) as AppearanceAssetElement;
                Asset theAsset = appearanceElem.GetRenderingAsset();
                AssetProperty colorProperty = theAsset["generic_diffuse"];
                Type type = colorProperty.GetType();
                object apVal = null;
                try
                {
                    // using .net reflection to get the value 
                    var prop = type.GetProperty("Value");
                    if (prop != null && prop.GetIndexParameters().Length == 0)
                    {
                        apVal = prop.GetValue(colorProperty);
                    }
                    else { apVal = "<No Value Property>"; }
                }
                catch (Exception ex)
                {
                    apVal = ex.GetType().Name + "-" + ex.Message;
                }
                if (apVal is DoubleArray)
                {
                    var doubles = apVal as DoubleArray;
                    byte r = (byte)(doubles.get_Item(0) * 255);
                    byte g = (byte)(doubles.get_Item(1) * 255);
                    byte b = (byte)(doubles.get_Item(2) * 255);
                    appearenceColor = new Color(r, g, b);
                }
            }
            return appearenceColor;
        }

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 3 of 3

JimJia
Alumni
Alumni
Accepted solution

Dear Philippe,

 

You can get the material of MEP Curve using Element.GetMaterialIds() or Category.Material.

 

please see codes below:

 

private Color GetAppearenceColor(Document doc, Element element)
        {
            ICollection<ElementId> ids = element.GetMaterialIds(false);
            MEPCurve mep = element as MEPCurve;

            Color appearenceColor = Color.InvalidColorValue;

            if (ids.Count > 0)
            {
                Material material = doc.GetElement(ids.ElementAt(0)) as Material;
                //You can also use Category.Material
                //Material material = mep.Category.Material;
                AppearanceAssetElement appearanceElem = material.Document.GetElement(material.AppearanceAssetId) as AppearanceAssetElement;
                Asset theAsset = appearanceElem.GetRenderingAsset();
                AssetProperty colorProperty = theAsset["generic_diffuse"];
                Type type = colorProperty.GetType();
                object apVal = null;
                try
                {
                    // using .net reflection to get the value 
                    var prop = type.GetProperty("Value");
                    if (prop != null && prop.GetIndexParameters().Length == 0)
                    {
                        apVal = prop.GetValue(colorProperty);
                    }
                    else { apVal = "<No Value Property>"; }
                }
                catch (Exception ex)
                {
                    apVal = ex.GetType().Name + "-" + ex.Message;
                }
                if (apVal is DoubleArray)
                {
                    var doubles = apVal as DoubleArray;
                    byte r = (byte)(doubles.get_Item(0) * 255);
                    byte g = (byte)(doubles.get_Item(1) * 255);
                    byte b = (byte)(doubles.get_Item(2) * 255);
                    appearenceColor = new Color(r, g, b);
                }
            }
            return appearenceColor;
        }

 


Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes