1. 简单测试
测试让选中的图形描红
[CommandMethod("SeleDemo")]
public void SeleDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult psr = ed.GetSelection();
if (psr.Status == PromptStatus.OK)
{
SelectionSet sSet = psr.Value;
this.ChangeColor(sSet);
}
}
private void ChangeColor(SelectionSet sSet)
{
ObjectId[] ids = sSet.GetObjectIds();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
ent.ColorIndex = 1;
}
trans.Commit();
}
}
2. 选择集API说明
3. 案例说明
[CommandMethod("SeleDemo")]
public void SeleDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//PromptSelectionResult psr = ed.SelectAll();
TypedValue[] values = new TypedValue[]{
new TypedValue((int)DxfCode.Start,"circle")
};
SelectionFilter filter = new SelectionFilter(values);
//PromptSelectionResult psr = ed.GetSelection(filter);
//if (psr.Status == PromptStatus.OK)
//{
// SelectionSet sSet = psr.Value;
// this.ChangeColor(sSet);
//}
PromptSelectionResult psr = ed.GetSelection(filter);
List<ObjectId> ids = new List<ObjectId>();
if (psr.Status == PromptStatus.OK)
{
SelectionSet sSet = psr.Value;
List<Point3d> points = this.GetPoint(sSet);
for (int i = 0; i <points.Count; i++)
{
PromptSelectionResult ss1 = ed.SelectCrossingWindow(points.ElementAt(i), points.ElementAt(i));
ids.AddRange(ss1.Value.GetObjectIds());
}
}
this.ChangeColor(ids);
}
private List<Point3d> GetPoint(SelectionSet sSet)
{
List<Point3d> points = new List<Point3d>();
ObjectId[] ids = sSet.GetObjectIds();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);
Point3d center = ((Circle)ent).Center;
double radius = ((Circle)ent).Radius;
points.Add(new Point3d(center.X + radius, center.Y, center.Z));
}
trans.Commit();
}
return points;
}
private void ChangeColor(SelectionSet sSet)
{
ObjectId[] ids = sSet.GetObjectIds();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i <ids.Length; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
ent.ColorIndex = 1;
}
trans.Commit();
}
}
private void ChangeColor(List<ObjectId> ids)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Count; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
ent.ColorIndex = 3;
}
trans.Commit();
}
}