TheSwamp

Code Red => .NET => Topic started by: Kralj_klokan on July 16, 2019, 06:31:05 PM

Title: Hatch not showing up after creation
Post by: Kralj_klokan on July 16, 2019, 06:31:05 PM
Hi,

I ' m new to this forum and i have a problem while creating a hatch using traceboundaries().
After i creating my hatch and setting hatchstyle to outer i does not show up. The border polylines is displayed but the hatch only shows up if i click on any object and change its property.
I have no idea what the issue could be. I 've set all my system variables to the default. I tried it on various drawings and even on other autocad versions.
Once the hatch appears everything is working fine.

This issue is driving me nuts. Here is my Hatch creation code. Maybe someone sees something i don't.  :x :x :x
Code - C#: [Select]
  1.   Document doc = Application.DocumentManager.MdiActiveDocument;
  2.             Database db = doc.Database;
  3.             Editor ed = doc.Editor;
  4.             PromptPointResult ppr = ed.GetPoint("\nSelect internal point: ");
  5.             if (ppr.Status != PromptStatus.OK) return;
  6.  
  7.             DBObjectCollection objs = ed.TraceBoundary(ppr.Value, true);
  8.             if (objs.Count == 0) return;
  9.  
  10.             using (Transaction tr = doc.TransactionManager.StartTransaction())
  11.             {
  12.                 BlockTable acBlkTbl;
  13.                 acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  14.  
  15.                 // Open the Block table record Model space for write
  16.                 BlockTableRecord acBlkTblRec;
  17.                 acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  18.  
  19.                 using (Hatch hat = new Hatch())
  20.                 {
  21.                     acBlkTblRec.AppendEntity(hat);
  22.                     tr.AddNewlyCreatedDBObject(hat, true);
  23.                     hat.HatchStyle = HatchStyle.Outer;
  24.                     hat.SetDatabaseDefaults();
  25.                     hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
  26.  
  27.                     hat.Associative = true;
  28.                     foreach (DBObject obj in objs)
  29.                     {
  30.                         Curve c = obj as Curve;
  31.                         if (c != null)
  32.                         {
  33.                             ObjectId curveId = acBlkTblRec.AppendEntity(c);
  34.                             tr.AddNewlyCreatedDBObject(c, true);
  35.                             hat.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { curveId });
  36.                         }
  37.                     }
  38.                     hat.EvaluateHatch(true);
  39.                     var ds = new DrawStream();
  40.                 }
  41.                 tr.Commit();
  42.             }
  43.  
Any help would be greatly appreciated since i 'm working on this for days.


edit kdub: code tag for code=csharp added.
Title: Re: Hatch not showing up after creation
Post by: gile on July 17, 2019, 02:00:39 AM
Hi,

Your code works for me (tested on A2019).
You can try adding:
Code - C#: [Select]
  1. hat.DowngradeOpen();
just after:
Code - C#: [Select]
  1. hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 17, 2019, 03:41:27 AM
Hi,

Your code works for me (tested on A2019).
You can try adding:
Code - C#: [Select]
  1. hat.DowngradeOpen();
just after:
Code - C#: [Select]
  1. hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");

I 've tried your suggestion but unfortunately still no result. It could be some graphics card setting or something like that.
This is really getting ridiculous. I 'm also using AutoCAD 2019.

Is the hatch created with hatchstyle.outer?

Ty for your help anyway.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 17, 2019, 04:13:43 AM
I 've recorded my behaviour to show a picture how its working.
It's uploaded on vimeo on this link https://vimeo.com/user100893766/review/348539021/7a97fe464e (https://vimeo.com/user100893766/review/348539021/7a97fe464e)
I also attached the video.

So i pick a point inside of the area i want to hatch. The hatch does not show up. But if i click on the polyline that surrounds the hatch  twice quickly it suddenly appears.

 :uglystupid2: :uglystupid2: :uglystupid2:
Title: Re: Hatch not showing up after creation
Post by: Atook on July 17, 2019, 12:30:07 PM
Have you tried Editor.Regen() once you're done creating the hatch and committing the transaction?
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 17, 2019, 12:37:41 PM
Hi Atook,

Yes, i 've tried putting Editor.Regen() after everything. I also tried typing manually regen in the command prompt. Still the hatch does not show.
I m wondering now the whole day how it is working for Gile but not for me.

The hatch wont show until I double click on the outer polyline and then press ESC.

In fact, even if i change the hatchlooptype being appended to Outermost, the hatch will show but not with the proper HatchStyle(Outer).
A hatch created in such manner will get the desired hatchstyle also only if i press twice on the polyline or do something with it.

I know, it's really weird.
Title: Re: Hatch not showing up after creation
Post by: kdub on July 17, 2019, 04:15:09 PM
I dropped the code into a Method and compiled with VS2019 Win10.
Netloaded debug build into AC2020 Vanilla.
Ran in Modelspace,
Worked as expected.


Changed to Paperspace Layout, toggled to Modelspace.

Worked as expected.

In both cases I ran the command twice .. once to select outer space, once to select inner space.

Title: Re: Hatch not showing up after creation
Post by: kdub on July 17, 2019, 04:27:14 PM
What does the DrawStream Class do ??

I did comment it out.


Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 18, 2019, 06:08:26 AM
What does the DrawStream Class do ??

I did comment it out.

The DrawStream class is useless. It 's just a c/p error. By your image i can see that the HatchStyle is not set to outer. I would expect that the smallest circle is not hatched if the hatchstyle is set to outer.
Could there be a difference since I call my method by button click, while you call it with a command?
Title: Re: Hatch not showing up after creation
Post by: kdub on July 18, 2019, 06:31:51 AM
First time I selected the outer circle .. it filled the outer

Second time I selected the inner circle .. it filled the inner.

I didn't check if selecting the inner filled both.

I haven't fully read the docs so I'm not certain if the result is correct of not, but it made sence at the time ; and I'm too brain dead (GMT +12 ) at the moment to investigate further.

added:
Perhaps you could do some manual Hatches from the command line to test the expected default behaviour.
Title: Re: Hatch not showing up after creation
Post by: MickD on July 18, 2019, 06:51:29 AM
....
Could there be a difference since I call my method by button click, while you call it with a command?

Have you tried locking the document? Some times this is required when using dialog to give the CAD system full control of the editor.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 18, 2019, 07:21:53 AM
....
Could there be a difference since I call my method by button click, while you call it with a command?

Have you tried locking the document? Some times this is required when using dialog to give the CAD system full control of the editor.

I 've tried your suggestion but still the same problem. Reinstalling autocad also did nothing.
Only if i edit the outer polyline (move it around, set the transparancy in the property view to 51 instead of 50...) the hatch shows.

It's also interesting that if I set the appended loop type to outermost. The hatch shows, but still the smallest circle get hatched. And if i then edit the outer polyline, the inner hatch disappears and everything looks as it should

Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 18, 2019, 01:33:47 PM
After some investigation i finally came to the answer.

When I try to run this code with an command it works without any problems.
Since I was just calling the method to execute, there must be some issue with it.

I had just to restructure my code to call the code as an command and now it's working great.

Thank you all for your help.
Title: Re: Hatch not showing up after creation
Post by: kdub on July 18, 2019, 03:27:56 PM
For the benefit of anyone reading this thread later, perhaps you should post your revised code in full so we understand what you have changed.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan on July 18, 2019, 05:32:01 PM
I 'll see to it as soon i get everything to work. Then I 'll post a complete answer.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.
Title: Re: Hatch not showing up after creation
Post by: kdub on July 19, 2019, 05:13: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
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.
Title: Re: Hatch not showing up after creation
Post by: kdub on July 19, 2019, 06:23:45 AM
Kralj_klokan,

Can you please use Code Tags

Title: Re: Hatch not showing up after creation
Post by: kdub 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.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.
Title: Re: Hatch not showing up after creation
Post by: gile 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.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.
Title: Re: Hatch not showing up after creation
Post by: gile 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.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.


Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.
Title: Re: Hatch not showing up after creation
Post by: kdub on July 20, 2019, 08:57:03 PM

Show us the code you are using from the button callback/event please.
Title: Re: Hatch not showing up after creation
Post by: Kralj_klokan 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.