I miserably failed at what I was attempting
That is what I get for not thinking or planning it out and just start coding.
The plan was grab all the block references and add a bounding box, then with the gradient hatch have the three colors alternate from inside to outside then skip and repeat.
Here is a link to a picture that the three methods at the bottom will do for adding a bounding box. For a spline it seems to work well and not having to traverse the curve a million times to get a tight box.
Here is the LinkI added a constructor to the a class with command attributes. It seems to work okay for anything you need initialized or checked for each document any of the commands are called.
This is not anything to be proud of but it changes the color and I need to do school work so it still needs cleaning.
Thanks for the link kaefer
using System;
using System.Collections.Generic;
using System.Collections;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
[assembly: CommandClass(typeof(HpadCadBlockFinder.MyCommands))]
namespace HpadCadBlockFinder
{
public class MyCommands
{
GradientColor[] gcs;
GradientColor[] gcs1;
GradientColor[] gcs2;
List<HatchHolder> hHold = new List<HatchHolder>();
public MyCommands()
{
gcs = new GradientColor[2];
gcs1 = new GradientColor[2];
gcs2 = new GradientColor[2];
//GradientColor[] gcs3 = new GradientColor[2];
gcs[0] = new GradientColor(Color.FromRgb(0, 0, 255), 0); gcs[1] = new GradientColor(Color.FromRgb(255, 255, 0), 1);
gcs1[0] = new GradientColor(Color.FromRgb(255, 255, 0), 0); gcs1[1] = new GradientColor(Color.FromRgb(255, 0, 0), 1);
gcs2[0] = new GradientColor(Color.FromRgb(255, 0, 0), 0); gcs2[1] = new GradientColor(Color.FromRgb(0, 0, 255), 1);
//gcs3[0] = new GradientColor(Color.FromRgb(255, 255, 0), 0); gcs3[1] = new GradientColor(Color.FromRgb(255, 0, 0), 1);
}
[CommandMethod("BlockFinder")]
public void MyCommand()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btrMs = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord btr = bt["C"].GetObject(OpenMode.ForRead) as BlockTableRecord;
ObjectIdCollection objID = btr.GetBlockReferenceIds(true, false);
foreach (ObjectId oId in objID)
{
////A Class to hold Object Id's
HatchHolder hh = new HatchHolder();
hh.brefID = oId;
BlockReference bref = oId.GetObject(OpenMode.ForRead) as BlockReference;
//// A funtion to build the bounding box and return the ObjectId
hh.pLineBound = BoundBox(oId, db, bt, btrMs);
Hatch hat = new Hatch();
hat.SetDatabaseDefaults();
hat.HatchObjectType = HatchObjectType.GradientObject;
hat.SetGradient(GradientPatternType.PreDefinedGradient, "SPHERICAL");
hat.GradientOneColorMode = false;
hat.SetGradientColors(gcs);
ObjectId hatId = btrMs.AppendEntity(hat);
tr.AddNewlyCreatedDBObject(hat, true);
hat.Associative = true;
hat.AppendLoop(HatchLoopTypes.Default, hh.pLineBound);
hat.EvaluateHatch(true);
hh.hatchFill = hat.ObjectId;
hHold.Add(hh);
}
tr.Commit();
}
//////Just threw in if statements to test
using (Transaction tr = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < hHold.Count; i++)
{
ObjectId hId = hHold[i].hatchFill;
Entity ent = tr.GetObject(hId, OpenMode.ForRead) as Entity;
Hatch hch = ent as Hatch;
hch.UpgradeOpen();
if (i == 0)
{
hch.SetGradientColors(gcs);
}
else if (i == 1)
{
hch.SetGradientColors(gcs1);
}
else if (i == 2)
{
hch.SetGradientColors(gcs2);
}
else if (i == 3)
{
hch.SetGradientColors(gcs);
}
else if (i == 4)
{
hch.SetGradientColors(gcs1);
}
else if (i == 5)
{
hch.SetGradientColors(gcs2);
}
else
{
hch.SetGradientColors(gcs);
}
////Change the color
hch.Associative = true;
ObjectIdCollection objidcoll = hHold[i].pLineBound;
hch.AppendLoop(HatchLoopTypes.Default, objidcoll);
hch.EvaluateHatch(true);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.UpdateScreen();
System.Threading.Thread.Sleep(10);
}
tr.Commit();
}
}
public static ObjectIdCollection BoundBox(ObjectId brefID, Database db, BlockTable bt, BlockTableRecord btrMs)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(brefID, OpenMode.ForRead) as Entity;
Point3d min = ent.GeometricExtents.MinPoint;
Point3d max = ent.GeometricExtents.MaxPoint;
Polyline pl = new Polyline();
pl.SetDatabaseDefaults();
pl.AddVertexAt(0, new Point2d(min.X, min.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(max.X, min.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(max.X, max.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(min.X, max.Y), 0, 0, 0);
pl.Closed = true;
btrMs.AppendEntity(pl);
tr.AddNewlyCreatedDBObject(pl, true);
tr.Commit();
ObjectIdCollection objIds = new ObjectIdCollection();
objIds.Add(pl.ObjectId);
return objIds;
}
}
[CommandMethod("BoundBoxExplode")]
public void BoundBoxExplode()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult per = ed.GetEntity("Select a Entity\n");
if (per.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btrMs = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
DBObjectCollection dbObjColl = new DBObjectCollection();
ent.Explode(dbObjColl);
foreach (Entity entExplode in dbObjColl)
{
Point3d min = entExplode.GeometricExtents.MinPoint;
Point3d max = entExplode.GeometricExtents.MaxPoint;
DocumentCollection docs = Application.DocumentManager;
Polyline pl = new Polyline();
pl.SetDatabaseDefaults();
pl.AddVertexAt(0, new Point2d(min.X, min.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(max.X, min.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(max.X, max.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(min.X, max.Y), 0, 0, 0);
pl.Closed = true;
btrMs.AppendEntity(pl);
tr.AddNewlyCreatedDBObject(pl, true);
}
tr.Commit();
}
}
}
[CommandMethod("BoundBoxSpline")]
public void BoundBoxSpline()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult per = ed.GetEntity("Select a Spline\n");
if (per.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btrMs = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);
Spline sp = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Spline;
Application.SetSystemVariable("PLINECONVERTMODE", 1);
Polyline pl = sp.ToPolylineWithPrecision(99) as Polyline;
Point3d min = pl.GeometricExtents.MinPoint;
Point3d max = pl.GeometricExtents.MaxPoint;
Polyline ply = new Polyline();
ply.SetDatabaseDefaults();
ply.AddVertexAt(0, new Point2d(min.X, min.Y), 0, 0, 0);
ply.AddVertexAt(1, new Point2d(max.X, min.Y), 0, 0, 0);
ply.AddVertexAt(2, new Point2d(max.X, max.Y), 0, 0, 0);
ply.AddVertexAt(3, new Point2d(min.X, max.Y), 0, 0, 0);
ply.Closed = true;
btrMs.AppendEntity(ply);
tr.AddNewlyCreatedDBObject(ply, true);
tr.Commit();
}
}
}
public class HatchHolder
{
public ObjectIdCollection pLineBound { get; set; }
public ObjectId hatchFill { get; set; }
public ObjectId brefID { get; set; }
}
}
}