Author Topic: Hatching Around Text with C# .Net  (Read 3383 times)

0 Members and 1 Guest are viewing this topic.

deckeresq

  • Guest
Hatching Around Text with C# .Net
« on: June 26, 2015, 05:23:29 PM »
Hey all,

I've been searching for a few weeks now, but unfortunately haven't been able to find an answer to my question. I'm attempting to hatch an area and detect islands (and not hatch them -- the island will always be MTEXT). I saw http://www.theswamp.org/index.php?topic=38156.0#lastPost, but it always throws an Exception for me, as has every other similar piece of code I've found. I'm able to get the ObjectId of the MTEXT and the PLINE, but when I try to append the loop to multiple IDs, as follows:

Code - C#: [Select]
  1. foreach (ObjectId id in ids) //ids is an ObjectIdCollection
  2. {
  3.     if (id != ObjectId.Null)
  4.     {
  5.         acHatch.AppendLoop(HatchLoopTypes.Outermost, new ObjectIdCollection() { id });
  6.     }
  7. }
  8.  

I get an "InvalidInput" Exception on the second run of the loop. Here's the code that currently works, except that it hatches through text (coll only ever has 1 ObjectId):

Code - C#: [Select]
  1. private void AddHatch(Transaction tr, BlockTableRecord acBlkTblRec, MaxHatch hatchCommand, ObjectIdCollection coll)
  2.         {          
  3.             try
  4.             {
  5.                 Hatch acHatch = new Hatch();
  6.                 acHatch.SetDatabaseDefaults();
  7.                 acHatch.SetHatchPattern(HatchPatternType.PreDefined, hatchCommand.HatchCode);
  8.                 acHatch.HatchStyle = HatchStyle.Outer;
  9.                 acHatch.ColorIndex = hatchCommand.HatchColor;
  10.                 acHatch.PatternAngle = hatchCommand.HatchAngle;                        
  11.                 acHatch.PatternScale = hatchCommand.HatchScale;                
  12.  
  13.                 //have to call SetHatchPattern again due to AutoCAD bug (http://forums.autodesk.com/t5/net/hatch-pattern-scale-problem/td-p/2057211), confirmed on http://adndevblog.typepad.com/autocad/2012/07/hatch-using-the-autocad-net-api.html
  14.                 acHatch.SetHatchPattern(HatchPatternType.PreDefined, hatchCommand.HatchCode);
  15.  
  16.                 acBlkTblRec.AppendEntity(acHatch);
  17.                 tr.AddNewlyCreatedDBObject(acHatch, true);
  18.  
  19.                 acHatch.Associative = true;
  20.  
  21.  
  22.                 acHatch.AppendLoop(HatchLoopTypes.Outermost, coll);
  23.  
  24.                 acHatch.EvaluateHatch(true);
  25.             }
  26.             catch (System.Exception ex)
  27.             {
  28.                 HandleError("AddHatch", ex.Message, false);
  29.             }
  30.         }
  31.  

Any ideas?

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Hatching Around Text with C# .Net
« Reply #1 on: June 29, 2015, 01:11:45 PM »
Without trying to run your code I can tell you how I did it.  I have a Polyline(polyline) around the area I want to hatch and a DBText(text) entity inside so first I create a Region(reg) from the Polyline.
Code - C#: [Select]
  1. var tempcol = new DBObjectCollection { polyline };
  2. var regions = Region.CreateFromCurves(tempcol);
  3. var reg = (Region)regions[0];
  4. reg.SetDatabaseDefaults();
  5. modelSpace.AppendEntity(reg);
  6. tr.AddNewlyCreatedDBObject(reg, true);

Then I create the Hatch(hat).
Code - C#: [Select]
  1. var hat = new Hatch();
  2. hat.SetDatabaseDefaults();
  3. hat.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
  4. hat.Associative = false;
  5. hat.AppendLoop(HatchLoopTypes.External, new ObjectIdCollection(new[] { reg.ObjectId }));  //Put the Hatch inside the polyline
  6. hat.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection(new[] { text.ObjectId }));   //Let the Hatch know there are islands inside
  7. modelSapce.AppendEntity(hat);
  8. tr.AddNewlyCreatedDBObject(hat, true);
Revit 2019, AMEP 2019 64bit Win 10

deckeresq

  • Guest
Re: Hatching Around Text with C# .Net
« Reply #2 on: July 13, 2015, 12:42:29 PM »
Thanks for the suggestion, MexicanCustard! Unfortunately, I get the same "eInvalidInput" Exception at this line:

Code - C#: [Select]
  1. hat.AppendLoop(HatchLoopTypes.External, new ObjectIdCollection(new[] { reg.ObjectId }));  //Put the Hatch inside the polyline

Any ideas on what might be causing that?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Hatching Around Text with C# .Net
« Reply #3 on: July 13, 2015, 06:29:28 PM »
deckeresq,

Which AutoCAD build ?

Could you post a drawing with just the offending parts ?

Could you post a ZIP of your solution ? ... just to save everyone re-creating your code.

Regards,
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.

deckeresq

  • Guest
Re: Hatching Around Text with C# .Net
« Reply #4 on: July 14, 2015, 11:01:49 AM »
Kerry,

I'm working with AutoCAD 2014 and the Interop.AutoCAD 1.0.0.0 reference in .Net 4.0.

Here is a drawing:

http://imgur.com/OL0hnWJ

Unfortunately, I can't post a ZIP of the solution due to workplace restrictions (this is for work). I'd be happy to provide any sort of background required, though!

Thanks for looking into this! It's been killing me for weeks.