Change hatchstyle of hatch when creating a new hatch by pickpoint

Change hatchstyle of hatch when creating a new hatch by pickpoint

josip_skopljak
Contributor Contributor
619 Views
1 Reply
Message 1 of 2

Change hatchstyle of hatch when creating a new hatch by pickpoint

josip_skopljak
Contributor
Contributor

Hi,

 

I 've been lately trying to create an hatch with C# with the hatchstyle set to outer.I have used the following code

from this blog: https://www.keanw.com/2010/06/creating-transparent-hatches-in-autocad-using-net.html

 

The code is the same with the only difference is that i set hatchstyle to outer and i append all loops as outermost:

 

 

public class Commands
  {
    static int _index = 1;
    [CommandMethod("TBH")]
    public void TraceBoundaryAndHatch()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;

      // Select a seed point for our boundary
      PromptPointResult ppr = ed.GetPoint("\nSelect internal point: ");
      
      if (ppr.Status != PromptStatus.OK)
        return;
      // Get the objects making up our boundary

      DBObjectCollection objs = ed.TraceBoundary(ppr.Value, false);

      if (objs.Count > 0)
      {
        Transaction tr = doc.TransactionManager.StartTransaction();
        using (tr)
        {
          // We'll add the objects to the model space

          BlockTable bt =(BlockTable)tr.GetObject(doc.Database.BlockTableId,OpenMode.ForRead);
          BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);

          // Add our boundary objects to the drawing and
          // collect their ObjectIds for later use

          ObjectIdCollection ids = new ObjectIdCollection();
          foreach (DBObject obj in objs)
          {
            Entity ent = obj as Entity;
            if (ent != null)
            {
              // Set our boundary objects to be of
              // our auto-incremented colour index

              ent.ColorIndex = _index;

              // Set our transparency to 50% (=127)
              // Alpha value is Truncate(255 * (100-n)/100)

              ent.Transparency = new Transparency(127);
// Add each boundary object to the modelspace // and add its ID to a collection ids.Add(btr.AppendEntity(ent)); tr.AddNewlyCreatedDBObject(ent, true); } } // Create our hatch Hatch hat = new Hatch(); // Solid fill of our auto-incremented colour index hat.SetHatchPattern(HatchPatternType.PreDefined,"SOLID"); hat.ColorIndex = _index++; hat.HatchStyle = HatchStyle.Outer; // Set our transparency to 50% (=127) // Alpha value is Truncate(255 * (100-n)/100) hat.Transparency = new Transparency(127); // Add the hatch to the modelspace & transaction ObjectId hatId = btr.AppendEntity(hat); tr.AddNewlyCreatedDBObject(hat, true); // Add the hatch loops and complete the hatch
hat.Associative = true; foreach (ObjectId id in ids)
{
var newIds = new ObjectIdCollection();
newIds.Add(id);
hat.AppendLoop(HatchLoopTypes.Outermost, newIds);
} hat.EvaluateHatch(true); tr.Commit(); } } } }

 

 

The problem is that setting hatchstyle to outer does not show any results. I get the following:

 

Try1.png

 

Setting the looptype to default results in the boundaries being created but nothing hatched.

I 'd like to achieve this result, but it seems to me that the hatchstyle is just being ignored:

 

Expected resultExpected result

 

Any kind of help would be greatly appreciated. I just cant figure out what the catch is.

Thanks everyone in advance.

 

 

0 Likes
620 Views
1 Reply
Reply (1)
Message 2 of 2

josip_skopljak
Contributor
Contributor

After setting hatchlooptypes to default while appending loops:

 

                       foreach (ObjectId id in ids)
                       {
                           var newIds = new ObjectIdCollection();
                           newIds.Add(id);
                           hat.AppendLoop(HatchLoopTypes.Default,newIds);
                       }

I noticed that while i get only the boundaries created ,  i can click on the outer boundary of the loop and when i go to the properties view and change  any property the hatch appears in a proper way.

I find this a very strange behavior and i m still looking for a way to update the view programatically to show to hatch.

Editor.Regen() did not work unfortunately.

0 Likes