Prod.: Engine, ver.: 6, ID: 60000296, HowTo : How can I get all the collors that are used in a drawing (visible entities)

HowTo : How can I get all the collors that are used in a drawing (visible entities)

Article60000296
TypeHowTo
ProductEngine
Version6
Date Added10/30/2007
Submitted by???
Keywords

Subject

How can I get all the collors that are used in a drawing (visible entities)

Summary

I want to get the colors from entities that are drawn in the screen !

Solution

See thecode below. It is in C# 2005 using the vdFramed control.
The onDrawFigure event is called, and the from the render of this event you can get the PenStyle.Color.

System.Collections.Generic.Dictionary<int, System.Drawing.Color> dictionarycolor;
private bool countcolors = false;

public Form1()
{
    InitializeComponent();
}

 

private void Form1_Load(object sender, EventArgs e)
{
    vdFC.BaseControl.ActiveDocument.FreezeEntityDrawEvents.Push(
false);
    vdFC.BaseControl.ActiveDocument.OnDrawFigure +=
new VectorDraw.Professional.vdObjects.vdDocument.FigureDrawEventHandler(ActiveDocument_OnDrawFigure);
   // this event is needed. The render of this event has the PenStyle.Color
}

private void button3_Click(object sender, EventArgs e)
{
   //create a dictionary to filter dublicate colors
   dictionarycolor = new Dictionary<int, Color>();
    countcolors =
true;
    vdFC.BaseControl.ActiveDocument.Open(vdFC.BaseControl.ActiveDocument.GetOpenFileNameDlg(0,
"", 0) as string);
    vdFC.BaseControl.ActiveDocument.Redraw(
true);
    countcolors =
false;
   //put the colors from dictionary in a simple array of System colors.
   System.Drawing.Color[] usedcolors = new Color[dictionarycolor.Count];
   int i = 0;
   foreach (System.Collections.Generic.KeyValuePair<int,System.Drawing.Color> var in dictionarycolor)
    {
  
   usedcolors.SetValue(var.Value,i ); i++;
    }
   MessageBox.Show("different colors used : "+ i.ToString());
}

void ActiveDocument_OnDrawFigure(object sender, VectorDraw.Render.vdRender render, ref bool cancel)
{
   if (countcolors)
    {
      int key = render.PenStyle.color.GetHashCode();
      if(dictionarycolor.ContainsKey(key)) return;
        dictionarycolor.Add(key, render.SystemPenColor);
//add the color to the dictionary if doesn't already exist
   }
}