Author Topic: Change Hatch Gradient Colors  (Read 2259 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Change Hatch Gradient Colors
« on: September 26, 2010, 02:10:12 PM »
I have no problem setting the gradient color to a hatch and applying it to an entity.

I am having trouble trying to change the colors of the gradient fill.

Any ideas would be greatly appreciated.


Thanks

**************EDIT***************

I got it thanks anyway I will post later if someone wants me to.
« Last Edit: September 26, 2010, 02:35:48 PM by Jeff H »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change Hatch Gradient Colors
« Reply #1 on: September 26, 2010, 08:49:11 PM »
< ... >
**************EDIT***************

I got it thanks anyway I will post later if someone wants me to.

Probably a good idea Jeff ... every bit helps :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Change Hatch Gradient Colors
« Reply #2 on: September 26, 2010, 09:15:17 PM »
Sure thing boss
I just need to do some clean-up

kaefer

  • Guest
Re: Change Hatch Gradient Colors
« Reply #3 on: September 27, 2010, 03:41:42 AM »
Sure thing boss
I just need to do some clean-up

As per http://through-the-interface.typepad.com/through_the_interface/2007/07/applying-a-grad.html or reading TFM (Autodesk.AutoCAD.DatabaseServices Namespace > Hatch Class) it's the Hatch.GradientOneColorMode Property for switching between one and two color mode. In the latter case the Hatch.SetGradientColors Method is then used to supply an array of  2 gradient color defintions (restriction to 2 colors as of 2010). Example:

Code: [Select]
            hat.GradientOneColorMode <- false
            hat.SetGradientColors
                [|  new GradientColor(Color.FromRgb(0uy, 0uy, 255uy), 0.f )
                    new GradientColor(Color.FromRgb(255uy, 255uy, 153uy), 1.f) |]

Cheers, Thorsten



Jeff H

  • Needs a day job
  • Posts: 6150
Re: Change Hatch Gradient Colors
« Reply #4 on: September 27, 2010, 04:51:40 AM »
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 Link

I 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

Code: [Select]

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; }
        }
    }
}

« Last Edit: September 27, 2010, 06:08:43 AM by Jeff H »