Author Topic: Get more information from entities that implement DrawableOverrule  (Read 260 times)

0 Members and 1 Guest are viewing this topic.

ken2023

  • Mosquito
  • Posts: 7
I have rewritten the appearance of Hatch by adding some text at the centroid position of the bounding rectangle.

I know I can write the text content to XData or XRecord, but I'm a bit curious if I can directly read the text inside Hatch?



Code - C#: [Select]
  1. namespace Test
  2. {
  3.     public class HatchDrawRule : DrawableOverrule
  4.     {
  5.         public override bool WorldDraw(Drawable drawable, WorldDraw wd)
  6.         {
  7.             if (drawable.Bounds == null) return false;
  8.             Extents3d b = (Extents3d)drawable.Bounds;
  9.             if (b.MinPoint.Z != 0) return false;
  10.  
  11.             var style = new TextStyle();
  12.             style.TextSize = (b.MaxPoint.Y - b.MinPoint.Y) / 4;
  13.  
  14.             var center = new Point3d((double)(b.MinPoint.X + b.MaxPoint.X) / 2, (double)(b.MinPoint.Y + b.MaxPoint.Y) / 2, 0);
  15.             wd.Geometry.Text(center, Vector3d.ZAxis, Vector3d.XAxis, "Hatch", true, style);
  16.  
  17.             return base.WorldDraw(drawable, wd);
  18.         }
  19.     }
  20.  
  21.     public class CustomHatchClass
  22.     {
  23.         private static HatchDrawRule _hatchDrawRule;
  24.         [CommandMethod("AllowCustomHatch")]
  25.         public void AllowCustomHatch()
  26.         {
  27.             if (_hatchDrawRule == null)
  28.             {
  29.                 _hatchDrawRule = new HatchDrawRule();
  30.                 Overrule.AddOverrule(RXObject.GetClass(typeof(Hatch)), _hatchDrawRule, false);
  31.             }
  32.             Overrule.Overruling = true;
  33.             Acap.DocumentManager.MdiActiveDocument.Editor.Regen();
  34.         }
  35.  
  36.         [CommandMethod("GetData")]
  37.         public void GetData()
  38.         {
  39.             var doc = Acap.DocumentManager.MdiActiveDocument;
  40.             var pr = doc.Editor.GetSelection();
  41.             if (pr.Status == ZwSoft.ZwCAD.EditorInput.PromptStatus.OK)
  42.             {
  43.                 foreach (ObjectId id in pr.Value.GetObjectIds())
  44.                 {
  45.                     using (var tran = doc.Database.TransactionManager.StartTransaction())
  46.                     {
  47.                         var ent = tran.GetObject(id, OpenMode.ForRead) as Entity;
  48.                         if (ent is Hatch hatch)
  49.                         {
  50.                             //Can I read the text within Hatch directly?
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }


Besides, is there a good method to help me calculate the position of the text?
For example, I would like to adjust the position of the "Hatch" in the following image.
Although I gained the center point of Hatch approximately, the text did not truly center.

edit Kerry: add code tags [code=csharp]
https://www.theswamp.org/index.php?topic=48309.0
« Last Edit: March 20, 2024, 01:27:27 AM by kdub_nz »

n.yuan

  • Bull Frog
  • Posts: 348
Re: Get more information from entities that implement DrawableOverrule
« Reply #1 on: March 21, 2024, 01:09:58 PM »
I do not think you can have access the text presented by the DrawableOverrule, because it is a visual presentation dynamically generated at the time when the Overruled hatch needs to be drawn/redrawn, it could be any shape and it happens to a look like a text.

So, you can save the Text with XData/ExtensionDictionary to the overruled Hatch, of course. Or, maybe, for it to be a bit  simpler, you can expose a Dictionary<ObjectId, string> from the Overrule class as static property, and save the text value used for visual presentation in the dictionary for later easy access?