// Color Bodies by native CAD Color Attribute (Mechanical only)
// debugger;
var branch = DS.Tree.FirstActiveBranch;
var geomObj = branch.PrototypeGroup;
var protos = branch.Prototypes;
var numProtos = protos.Count;
for (var idx = 1; idx <= numProtos; idx++) 
{
    var proto = protos.Item(idx);
    scriptcode.changeActiveObject(proto.ID);
    var drawable = DS.Graphics.Drawable(proto);
    var bodyId = proto.TopoId;
    var part = proto.Part;
    
    var attrs = part.AttributesByTopo(bodyId);
    var numAttrs = attrs.Count;
    for (var attrIdx = 1; attrIdx <= numAttrs; attrIdx++)
    {
        var attr = attrs.Item(attrIdx);
        var name = attr.Name;
        // check if attribute name contains the string "Color"
        if(name.substring(0, 6) == "Color:")
        {
           var RGB = attr.Value;
           var red = RGB.Item(1);
           var green = RGB.Item(2);
           var blue = RGB.Item(3);
           var color = 0x10000 * blue;   //65536 * blue;
           color += 0x100 * green; //256 * green;
           color += red;

           // Pure GREEN is reserved for highlighting so need
           // to set the color we are trying to push to something
           // other than 00FF00
           // if (color == 0xFF00) color = 0xFF01;
            
           drawable.Material.Color = color;
        }
    }
}
scriptcode.changeActiveObject(geomObj.ID);


DS.Graphics.Redraw(1)