Author Topic: How do you get an objects color  (Read 2765 times)

0 Members and 1 Guest are viewing this topic.

Peter Laker

  • Guest
How do you get an objects color
« on: September 14, 2008, 09:22:15 PM »
I can't work out how to get an existing objects color, I have had a look about & have found stuff working with color books but nothing on how to get the actual color of an existing object. What I'm trying to do is make an object selection by colors, so the user can select any object on the screen & make a filter based on the color of the objects they have just selected then make a selection set based on those colors.

Cases to deal with I think are.
1. Objects color is ByLayer.
2. Objects color is ByBlock.
3. Objects color is ByACI.
4. Objects color is ByColor (True Color & Color Books).

I see there is a IsByPen, do I need to check for this ?



This is what I have so fare, I can check for the 4 conditions.

I'm using AutoCAD 2008 & MVS 2008 Pro with .Net 3.5 C#.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Globalization;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;


public class color
{
   
    [CommandMethod("GETCOLOR")]
    public void GetObjectColor()
{
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        Database db = doc.Database;

        string colorNames = null;

        PromptSelectionOptions ssOpts1 = new PromptSelectionOptions();
        ssOpts1.MessageForAdding = "\nSelect objects: ";
        ssOpts1.AllowDuplicates = false;
        ssOpts1.RejectObjectsOnLockedLayers = true;

        PromptSelectionResult ssRes1 = ed.GetSelection(ssOpts1);

        if (ssRes1.Status == PromptStatus.OK)
        {
            colorNames = GetColorByEnt(ssRes1);
        }

        //...code to select objects by color


        if (ssRes1.Status == PromptStatus.Cancel)
        {
            //if user canceled end
            ed.WriteMessage("User canceled.\n");
            return;
        }
        //check for error status, if error occurred end program
        if (ssRes1.Status == PromptStatus.Error)
        {
            //if we are here user pressed enter
            return;
        }

    }


    //get color name from selected objects
    public static String GetColorByEnt(PromptSelectionResult res)
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        Database db = doc.Database;

        int count = 0;
        string colorNames = null;

        Transaction tr = doc.TransactionManager.StartTransaction();
        try
        {
            ObjectId[] idarray = res.Value.GetObjectIds();
            foreach (ObjectId entid in idarray)
            {
                Entity ent = (Entity)tr.GetObject(entid, OpenMode.ForRead);

                if (ent.Color.IsByLayer == true)
                {
                    //ed.WriteMessage("\nColor IsByLayer: ");
                    ed.WriteMessage("Color Code is : " + ent.Color.ToString(),
                                    "Color is By LAyer");
                }

                if (ent.Color.IsByBlock == true)
                {
                    ed.WriteMessage("\nColor IsByBlock: ");
                }

                if (ent.Color.IsByAci == true)
                {
                    ed.WriteMessage("\nColor IsByAci: ");
                }

                if (ent.Color.IsByColor == true)
                {
                    ed.WriteMessage("\nColor IsByColor: ");
                }

                count++;
            }
        }
        finally
        {
            tr.Dispose();
        }

        //return the colors selected to the calling function
        return colorNames;

    }


}


I have attached a drawing with the colors setup defining the 4 conditions.






« Last Edit: September 15, 2008, 08:49:32 AM by Peter Laker »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: How do you get an objects color
« Reply #1 on: September 15, 2008, 08:12:07 PM »
I don't have time to look at this too much but ColorIndex  seems to be what you need
the following is a function I use to set objects to 0, bylayer,bylayer  for blocks.
255 is byblock
 private static Entity SetEntToZero(Entity ent)
        {
            ent.Layer = "0";
            ent.Linetype = "bylayer";
            ent.ColorIndex = 256;
            return ent;
        }

Peter Laker

  • Guest
Re: How do you get an objects color
« Reply #2 on: September 16, 2008, 04:51:09 PM »
Can only play with this stuff for 2 nights a week & there done this week so wont get back on this till next week now  :-(

Thanks for your reply Bryco  :-)