Author Topic: Hatch not showing up after creation  (Read 9635 times)

0 Members and 1 Guest are viewing this topic.

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #15 on: July 19, 2019, 04:53:03 AM »
I 'll see to it as soon i get everything to work. Then I 'll post a complete answer.

After further investigation i found out that the code i posted will work perfectly if i call it by command.
Problems start when you try to call the method from a modeless dialog.
I have no idea why it would behave this way, but my advice for everyone is to use this as an command and call it with editor.command().

I 'm still working on this since i would like the method to run in an synchronous fashion and it can't be done while i 'm in a modeless dialog.
I ' ll keep you posted if i find a way to make it work properly.

Thanks everyone for your help. This might be an autocad issue.
Here is the code that works great while called with an command:

Code - C#: [Select]
  1. [CommandMethod("PICKPOINTHATCH")]
  2.         public void TraceBoundaryAndHatch()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.  
  6.             Database db = doc.Database;
  7.             Editor ed = doc.Editor;
  8.  
  9.             PromptPointResult ppr = ed.GetPoint("\nSelect internal point: ");
  10.             if (ppr.Status != PromptStatus.OK) return;
  11.  
  12.             DBObjectCollection objs = ed.TraceBoundary(ppr.Value, true);
  13.             if (objs.Count == 0) return;
  14.  
  15.             using (Transaction tr = doc.TransactionManager.StartTransaction())
  16.             {
  17.                 BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  18.                 BlockTableRecord MSrecord = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  19.  
  20.                 using (Hatch hat = new Hatch())
  21.                 {
  22.                     MSrecord.AppendEntity(hat);
  23.                     tr.AddNewlyCreatedDBObject(hat, true);
  24.                     hat.HatchStyle = HatchStyle.Outer;
  25.                     hat.SetDatabaseDefaults();
  26.                     hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
  27.                     hat.DowngradeOpen();
  28.                     hat.Associative = true;
  29.                     foreach (DBObject obj in objs)
  30.                     {
  31.                         Curve c = obj as Curve;
  32.                         if (c != null)
  33.                         {
  34.                             ObjectId curveId = MSrecord.AppendEntity(c);
  35.                             tr.AddNewlyCreatedDBObject(c, true);
  36.                             hat.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { curveId });
  37.                         }
  38.                     }
  39.  
  40.                     hat.EvaluateHatch(true);
  41.                 }
  42.  
  43.                 tr.Commit();
  44.             }
  45.         }
  46.  



edit kdub: code tag for code=csharp added.
« Last Edit: July 19, 2019, 05:09:53 AM by kdub »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2120
  • class keyThumper<T>:ILazy<T>
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #17 on: July 19, 2019, 06:11:07 AM »

As Mick suggested ...
Please look at this

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUID-A2CD7540-69C5-4085-BCE8-2A8ACE16BFDD-htm.html

I ' ve looked into the example you posted and implemented my method this way.

Code - C#: [Select]
  1. [CommandMethod("TRYHATCH", CommandFlags.Session)]
  2.         public static void TraceBoundaryAndHatch()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             PromptPointResult ppr = ed.GetPoint("\nSelect internal point: ");
  9.             if (ppr.Status != PromptStatus.OK) return;
  10.  
  11.             DBObjectCollection objs = ed.TraceBoundary(ppr.Value, true);
  12.             if (objs.Count == 0) return;
  13.  
  14.             using (DocumentLock acLckDoc = doc.LockDocument())
  15.             {
  16.                 using (Transaction tr = doc.TransactionManager.StartTransaction())
  17.                 {
  18.                     BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  19.                     BlockTableRecord MSrecord = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  20.  
  21.                     using (Hatch hat = new Hatch())
  22.                     {
  23.                         MSrecord.AppendEntity(hat);
  24.                         tr.AddNewlyCreatedDBObject(hat, true);
  25.                         hat.HatchStyle = HatchStyle.Outer;
  26.                         hat.SetDatabaseDefaults();
  27.                         hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
  28.                         hat.DowngradeOpen();
  29.                         hat.Associative = true;
  30.                         foreach (DBObject obj in objs)
  31.                         {
  32.                             Curve c = obj as Curve;
  33.                             if (c != null)
  34.                             {
  35.                                 ObjectId curveId = MSrecord.AppendEntity(c);
  36.                                 tr.AddNewlyCreatedDBObject(c, true);
  37.                                 hat.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { curveId });
  38.                             }
  39.                         }
  40.  
  41.                         hat.EvaluateHatch(true);
  42.                     }
  43.  
  44.                     tr.Commit();
  45.                 }
  46.             }
  47.         }

If I add the CommandFlags.Session. The code behaves incorrectly and does not draw the hatch as i showed in my video.
When i remove it, the code works fine.

When i try to call the command with
Code - C#: [Select]
  1.  Editor.Command("TRYHATCH")
. It gives me eInvalidInput error message. Which is i think expected since I m callling the command from a button on a modeless dialog.


If i try to call the command with
Code - C#: [Select]
  1.  Document.SendStringToExecute("TRYHATCH\n", true, false, true)
everything works fine but then i have the async method problem while I want the code to continue with the nextline only when the command is finished.

Maybe I 'm doing something wrong?

Sorry for the code tags. I 'll include them from now on.
« Last Edit: July 19, 2019, 06:29:46 AM by Kralj_klokan »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2120
  • class keyThumper<T>:ILazy<T>
Re: Hatch not showing up after creation
« Reply #18 on: July 19, 2019, 06:23:45 AM »
Kralj_klokan,

Can you please use Code Tags

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2120
  • class keyThumper<T>:ILazy<T>
Re: Hatch not showing up after creation
« Reply #19 on: July 19, 2019, 06:31:52 AM »

Sorry, I'm lost regarding what you are trying to do.

Are you calling this from another module ??

It's way past my bedtime ... perhaps someone will be able to assist you over(my)night.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #20 on: July 19, 2019, 06:46:12 AM »

Sorry, I'm lost regarding what you are trying to do.

Are you calling this from another module ??

It's way past my bedtime ... perhaps someone will be able to assist you over(my)night.

GMT+1 here.

Good night :)

I ' m actually trying to run this code from a modeless dialog in a synchronous way.

I want to be able to click on the button wait for the command to finish and then call  the next line of code.
So far i have implemented various methods where i could just instantiate the class like this example.

Code - C#: [Select]
  1. var jig = new HatchJig(new Matrix3d(), new Plane(200, 200, 200, 200), new Polyline() /*{ Color = color }*/, hat);
  2. jig.RunHatchJig(layerName, color);

This example works fine and everything gets drawn the appropriate way.

When i try to execute the code for hatching by pickpoint this way. I get the error i posted the video about.
Code - C#: [Select]
  1. var hatch = new PickPointHatch(layerName, color);
  2. hatch.TraceBoundaryAndHatch();
I 'm puzzled why every other method works fine this way but not this one.
After i found out that it would work if i called a command  i ignored this error and tried to call a command. The problem is that i can't get it to be synchronous.

When I call the method like this Document.SendStringToExecute() the result is good. But its async (other code gets executed while the command is still running).

I 'm looking for a way to call the command either by this way
Code - C#: [Select]
  1. var hatch = new PickPointHatch(layerName, color);
  2. hatch.TraceBoundaryAndHatch();
and to work fine.

or to call it somehow with
Code - C#: [Select]
  1. Editor.Command()
or
Code - C#: [Select]
  1. Document.SendStringToExecute()
while being synchronous.

I ' m not an native english speaker and sorry if you have a hard time understanding my problem.
« Last Edit: July 19, 2019, 06:57:19 AM by Kralj_klokan »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Hatch not showing up after creation
« Reply #21 on: July 19, 2019, 08:19:34 AM »
Hi,

It's a good practice to wrap your method in a CommandMethod and call it with SendStringToExevute() from a modeless dialog (typically a palette set). This way you let AutoCAD taking care of locking the document and setting the focus to the editor.
Speaking English as a French Frog

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #22 on: July 19, 2019, 08:25:36 AM »
Hi,

It's a good practice to wrap your method in a CommandMethod and call it with SendStringToExevute() from a modeless dialog (typically a palette set). This way you let AutoCAD taking care of locking the document and setting the focus to the editor.

I have no problem with doing it that way. But what if i want to wait when the command is over to do some more code. Should i listen for a CommandEnded() event or something like that? Let's say i want to have some values updated when i execute this command.
« Last Edit: July 19, 2019, 08:38:13 AM by Kralj_klokan »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Hatch not showing up after creation
« Reply #23 on: July 19, 2019, 10:10:55 AM »
As SendStringToExecute runs asynchronously, you have to also wrap the jig process in the same CommandMethod or another one.

If you do not want to use SendStringToExecute, you are responsible of locking the document (in a using statement) and setting the focus to the editor window before calling the methods.
Speaking English as a French Frog

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #24 on: July 19, 2019, 11:11:40 AM »
As SendStringToExecute runs asynchronously, you have to also wrap the jig process in the same CommandMethod or another one.

If you do not want to use SendStringToExecute, you are responsible of locking the document (in a using statement) and setting the focus to the editor window before calling the methods.

Thank you for your help.

I think that i now correctly lock the document and set the focus to the dwg. But still if i try to call the code like this
Code - C#: [Select]
  1. hatch.TraceBoundaryAndHatch();
It still does not show the hatch. The exact same code will work if i call it with SendStringToExecute  This is the code i came up with:

Code - C#: [Select]
  1. public void TraceBoundaryAndHatch()
  2.         {
  3.             Document doc = Application.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.             bool ContinueHatch = true;
  7.  
  8.             using (DocumentLock acLckDoc = doc.LockDocument())
  9.             {
  10.                 Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
  11.                 while (ContinueHatch)
  12.                 {
  13.                     PromptPointResult ppr = ed.GetPoint("\nSelect internal point: ");
  14.                     if (ppr.Status != PromptStatus.OK) ContinueHatch = false;
  15.  
  16.                     if (ContinueHatch)
  17.                     {
  18.                         DBObjectCollection objs = ed.TraceBoundary(ppr.Value, true);
  19.                         if (objs.Count == 0) return;
  20.  
  21.  
  22.                         using (Transaction tr = doc.TransactionManager.StartTransaction())
  23.                         {
  24.                             BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  25.                             BlockTableRecord MSrecord = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  26.  
  27.                             using (Hatch hat = new Hatch())
  28.                             {
  29.                                 MSrecord.AppendEntity(hat);
  30.                                 tr.AddNewlyCreatedDBObject(hat, true);
  31.                                 hat.HatchStyle = HatchStyle.Outer;
  32.                                 hat.SetDatabaseDefaults();
  33.                                 hat.Color = color;
  34.                                 hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
  35.                                 hat.DowngradeOpen();
  36.                                 hat.Associative = true;
  37.                                 foreach (DBObject obj in objs)
  38.                                 {
  39.                                     Curve c = obj as Curve;
  40.                                     if (c != null)
  41.                                     {
  42.                                         ObjectId curveId = MSrecord.AppendEntity(c);
  43.                                         tr.AddNewlyCreatedDBObject(c, true);
  44.                                         hat.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { curveId });
  45.                                     }
  46.                                 }
  47.                             }
  48.  
  49.                             tr.Commit();
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.         }

Could it be that AutoCAD does some more things while executing a command? Maybe some kind of redraw or regen? If i manually call regen or redraw the hatch still will not show.
The only way it shows is if i try to edit its outer boundary. Or maybe the way i append the loop is not correct? Still i can't explain why the same code works when called as a command with SendStringToExecute() but not if I just call the method from code?.

I can only be grateful for your patience.


« Last Edit: July 19, 2019, 11:15:06 AM by Kralj_klokan »

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #25 on: July 20, 2019, 06:30:00 PM »
Hi everyone,

The conclusion is that code posted is working fine. When I type the command in the command prompt its working perfectly. As i wished to call the command from a button on a palette i found out that it has to be something that has to do with the palette.
I even tried calling the same code from a modal dialog or simply immediately when initializing the application and everything worked.

The only explanation must that when calling this kind of code from a palette results in errors and is not reliable.
If anyone tries to do the same thing i did i can only suggest that he tries using a modal dialog while creating his plugin since it seems that it's more reliable.

Thanks everyone for your help.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2120
  • class keyThumper<T>:ILazy<T>
Re: Hatch not showing up after creation
« Reply #26 on: July 20, 2019, 08:57:03 PM »

Show us the code you are using from the button callback/event please.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Kralj_klokan

  • Newt
  • Posts: 23
Re: Hatch not showing up after creation
« Reply #27 on: July 21, 2019, 03:30:49 AM »

Show us the code you are using from the button callback/event please.
This is the way i show my tool palette (WPF):

Code - C#: [Select]
  1.             var palette = new ToolPaletteViewModel();
  2.             palette.AddVisual("", new ToolPaletteView() { DataContext = palette });
  3.             palette.Visible = true;
  4.             palette.Dock = DockSides.Right;
  5.  

This is my xaml for the button:
Code - XML: [Select]
  1. <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" Margin="5"/>

And when the button is pressed the code is:

Code - C#: [Select]
  1.         internal void StartHatchingByPickPoint(string layerName, Autodesk.AutoCAD.Colors.Color color)
  2.         {
  3.             var hatch = new PickPointHatch(layerName, color);
  4.             PickPointHatch.TraceBoundaryAndHatch();
  5.         }
  6.  

If I call the last bit of code i posted anywhere but on the toolpalette it will work. Ridicolous.

Some more info:

If i put the loop to be appended as outermost, the hatch is being displayed but without the islands. If i click on the hatch editor i see that the hatchstyle is set to outer. But toggling from normal to outer does nothing . The hatch stays the same.
If i click on the outer polyline twice and then press Cancel the hatch will display the right way and the islands show. Also, moving the hatch a little will also work. As if autocad recalculates the hatch if i move/edit the associated borders.
« Last Edit: July 21, 2019, 03:39:32 AM by Kralj_klokan »