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#.
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.