Author Topic: Keeping associated hatches associated  (Read 152 times)

0 Members and 1 Guest are viewing this topic.

Kralj_klokan

  • Mosquito
  • Posts: 20
Keeping associated hatches associated
« on: November 11, 2020, 04:09:14 PM »
Hi,

I' m creating associated hatches and everything is working perfectly.

My problem is when the user tries to change Associative to false through quick properties or any other way it removes associativity and this generates trouble with my code.

What I would like to do is to use ObjectOverrule to prevent the user from making this change to the entity.

When i create an object i put its associative entities Handles into its Xdata.

Then i try during the overruling to reappend the loops to the hatch but this works unreliably and causes exceptions.
 
When i try to apoend a loop it throws an eInvalidLayer exception which is caused by the polyline being already opened.

Did anyone else come across a similar problem or does anyone have an idea how to solve this issue?

This is what I m trying:

Code - C#: [Select]
  1.  
  2.   public override void Close(DBObject dbObject)
  3.         {
  4.             try
  5.             {
  6.                 Document doc = Application.DocumentManager?.MdiActiveDocument;
  7.  
  8.                 var db = doc?.Database;
  9.                 var ed = doc?.Editor;
  10.                 var lMan = new LayerManagement();
  11.  
  12.                 if (dbObject is Hatch)
  13.                 {
  14.                     Hatch hatch = dbObject as Hatch;
  15.  
  16.                     if (hatch != null && hatch.IsWriteEnabled && hatch.IsModified && !hatch.IsErased)
  17.                     {
  18.                         var rb = hatch.GetXDataForApplication("ASCD").AsArray();
  19.                         hatch.SetLayerId(lMan.GetLayerObjectId(rb[2].Value.ToString()), true);
  20.                         hatch.ColorIndex = 256;
  21.                         hatch.LineWeight = LineWeight.ByLayer;
  22.                         hatch.Associative = true;
  23.                         if (!hatch.Associative || hatch.GetAssociatedObjectIds().Count == 0)
  24.                         {
  25.                             var allHandlesString = rb[3].Value.ToString();
  26.  
  27.                             var allHandles = allHandlesString.Split(';');
  28.  
  29.                             if (allHandles.Count() == 1)
  30.                             {
  31.                                 long result = Convert.ToInt64(allHandles[0], 16);
  32.                                 ObjectId newObjectId = db.GetObjectId(false, new Handle(result), 0);
  33.                                 hatch.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { newObjectId });
  34.                             }
  35.                             else
  36.                             {
  37.                                 for (int i = allHandles.Count() - 1; i > -1; i--)
  38.                                 {
  39.                                     if (String.IsNullOrWhiteSpace(allHandles[i])) continue;
  40.                                     long result = Convert.ToInt64(allHandles[i], 16);
  41.                                     ObjectId newObjectId = db.GetObjectId(false, new Handle(result), 0);
  42.                                     hatch.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { newObjectId });
  43.                                 }
  44.                             }
  45.                         }
  46.                         hatch.Annotative = AnnotativeStates.True;
  47.                         hatch.HatchStyle = HatchStyle.Outer;
  48.                         hatch.EvaluateHatch(true);
  49.                     }
  50.                 }
  51.  
  52.                 else if (dbObject is BlockReference)
  53.                 {
  54.                     BlockReference bReference = dbObject as BlockReference;
  55.  
  56.                     if (bReference != null && bReference.IsWriteEnabled  && bReference.IsModified && bReference.IsErased == false)
  57.                     {
  58.                         var rb = bReference.GetXDataForApplication("ASCD").AsArray();
  59.                         bReference.SetLayerId(lMan.GetLayerObjectId(rb[2].Value.ToString()), true);
  60.                         bReference.ColorIndex = 256;
  61.                         bReference.LineWeight = LineWeight.ByLayer;
  62.                     }
  63.                 }
  64.  
  65.                 else
  66.                 {
  67.                     Entity pline = dbObject as Entity;
  68.  
  69.                     if (pline != null && pline.IsWriteEnabled && pline.IsModified && !pline.IsErased)
  70.                     {
  71.                         var rb = pline.GetXDataForApplication("ASCD").AsArray();
  72.                         var pl = pline.GetPersistentReactorIds();
  73.                         pline.SetLayerId(lMan.GetLayerObjectId(rb[2].Value.ToString()), true);
  74.                         pline.ColorIndex = 256;
  75.                         pline.LineWeight = LineWeight.ByLayer;
  76.                         if (rb.Length > 5 && rb[5].Value.ToString() != "IsAlone" && pline.GetPersistentReactorIds().Count == 0)
  77.                         {
  78.                             throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.CloseModifyAborted);
  79.                         }
  80.                     }
  81.                 }
  82.  
  83.                 base.Close(dbObject);
  84.             }
  85.       }
  86. }
  87.  
  88.  

Any help would be appreciated.


« Last Edit: November 11, 2020, 04:37:52 PM by Kralj_klokan »

nobody

  • Swamp Rat
  • Posts: 850
  • .net stuff
Re: Keeping associated hatches associated
« Reply #1 on: November 14, 2020, 04:35:40 PM »
One solution would be an event that tracked changes to hatches. When they change, the even generates a border around that hatches then checks linework to see if it matches it.  If it does, it reassigns it as the border for the associative hatch.

This seems really difficult with so many ways to trim, break, edit boundaries and break the associative relationship.

Kralj_klokan

  • Mosquito
  • Posts: 20
Re: Keeping associated hatches associated
« Reply #2 on: November 15, 2020, 05:21:46 AM »
Thank you for your help.

I have been also thinking about this, but it seemed to me that there has to be a better way.

I finally decided to save the handle of the hatch to the associated polyline Xdata and made a overrule when erasing, so that if someone deletes the hatch there are no leftover polylines.
I also disabled grip stretch of the hatch through the GripOverrule.

Unfortunately i could not find a better way.