Author Topic: How to update the non active document view?  (Read 2619 times)

0 Members and 1 Guest are viewing this topic.

gswang

  • Newt
  • Posts: 117
How to update the non active document view?
« on: September 22, 2013, 07:42:36 PM »
How to update the non active document view, I used the SetCurrentView method, there will be eNotApplicable error. If the current document, there is no problem with SetCurrentView. :cry:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to update the non active document view?
« Reply #1 on: September 23, 2013, 04:57:40 AM »

How are you trying to do it ?
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.

gswang

  • Newt
  • Posts: 117
Re: How to update the non active document view?
« Reply #2 on: September 23, 2013, 08:02:02 AM »
I use.NET to realize the document Synchronous display, the following two kinds of solutions.
Solution 1, synchronous effect is good, but a long time to process the WM_MOUSEWHEEL message will cause CAD to crash; and then I try to use solution 2 to modify the non current window view, but without success.

Command: registerhook
The code is attached!

Code - C#: [Select]
  1. / / solution 1
  2.                     doc.SendStringToExecute(cmd, false, true, false);
  3.  
  4. / / solution 2
  5.                     using (Transaction tr = doc.TransactionManager.StartTransaction())
  6.                     {
  7.                         ViewTable vt = tr.GetObject(db.ViewTableId, OpenMode.ForWrite) as ViewTable;
  8.  
  9.                         ViewTableRecord vtr = new ViewTableRecord();
  10.                         vtr.Height = max.Y - min.Y;
  11.                         vtr.Width = max.X - min.X;
  12.                         vtr.CenterPoint = new Point2d((min.X + max.X) / 2.0,
  13.                             (min.Y + max.Y) / 2.0);
  14.                         vt.Add(vtr);
  15.  
  16.                         tr.AddNewlyCreatedDBObject(vtr, true);
  17.                         tr.Commit();
  18.  
  19.                         ed.SetCurrentView(vtr);
  20.  
  21.                     }
« Last Edit: September 24, 2013, 05:08:30 AM by gswang »

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: How to update the non active document view?
« Reply #3 on: September 24, 2013, 03:42:30 AM »
I'm guessing this is another situation where if you're going into the windows message loop you've spent so much time banging your head against the wall on the why and the how your current solution is failing that you've gone and complicated the whole issue, but after watching this go on for a couple weeks I finally took a couple hours to play (what a relief after being totally swamped at work lately!)
Thank you TT for showing me the value of the Application.Idle event.  This has been roughly tested but seems to do what you are looking for. It doesn't work well inside the orbit command, but after zooming in or out the other views update. I hope someone else can pitch in as to why this is. This probably also requires some checks and balances to insure you're synchronized, perhaps getting the cvport system variable before going into the update loop would allow you to target the proper viewport in each drawing.

To run, use TESTVIEW command in any drawing and TESTSYNC in any other while in model space and it sync's them up.

Code - C#: [Select]
  1.         static bool valid = false;
  2.         static Document original;
  3.         static HashSet<Document> docs;
  4.         static IEnumerator<Document> enumer;
  5.         static bool updating = false;
  6.         static bool regen = false;
  7.         static ViewTableRecord syncView;
  8.         [CommandMethod("retoggle")]
  9.         public void toggleRegen()
  10.         {
  11.             regen = !regen;
  12.             Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nRegen is {0}", regen ? "enabled" : "disabled");
  13.         }
  14.         [CommandMethod("TestView")]
  15.         public void testView()
  16.         {
  17.             Document doc = Application.DocumentManager.MdiActiveDocument;
  18.             Editor ed = doc.Editor;
  19.             if (valid)
  20.             {
  21.                 ed.WriteMessage("\nDisabling doc_ViewChanged");
  22.                 foreach (Document d in docs)
  23.                 {
  24.                     d.ViewChanged -= doc_ViewChanged;
  25.                 }
  26.                 original = null;
  27.                 docs = null;
  28.                 enumer = null;
  29.                 syncView = null;
  30.                 valid = !valid;
  31.             }
  32.             else
  33.             {
  34.                 ed.WriteMessage("\nEnabling doc_ViewChanged");
  35.                 docs = new HashSet<Document>();
  36.                 docs.Add(doc);
  37.                 doc.ViewChanged += doc_ViewChanged;
  38.                 valid = !valid;
  39.                
  40.             }
  41.         }
  42.         [CommandMethod("TestSync")]
  43.         public void testViewSync()
  44.         {
  45.             Document doc = Application.DocumentManager.MdiActiveDocument;
  46.             Editor ed = doc.Editor;
  47.             if (valid)
  48.             {
  49.                 if (doc == original)
  50.                 {
  51.                     ed.WriteMessage("\nCan't sync original document");
  52.                 }
  53.                 else if (docs.Add(doc))
  54.                 {
  55.                     using (ViewTableRecord vtr = (ViewTableRecord)ed.GetCurrentView())
  56.                     {
  57.                         syncView = new ViewTableRecord();
  58.                         syncView.SetUcs(vtr.UcsOrthographic);
  59.                         syncView.Target = vtr.Target;
  60.                         syncView.ViewDirection = vtr.ViewDirection;
  61.                         syncView.Elevation = vtr.Elevation;
  62.                         syncView.CenterPoint = vtr.CenterPoint;
  63.                         syncView.Height = vtr.Height;
  64.                         syncView.Width = vtr.Width;
  65.                     }
  66.                     original = doc;
  67.                     doc.ViewChanged += doc_ViewChanged;
  68.                     enumer = docs.GetEnumerator();
  69.                     enumer.Reset();
  70.                     Application.Idle += SetViews;
  71.                     updating = true;
  72.                     enumer.MoveNext();
  73.                     Application.DocumentManager.MdiActiveDocument = enumer.Current;
  74.                 }
  75.                 else
  76.                 {
  77.                     ed.WriteMessage("\nDoc already sync'd");
  78.                 }
  79.             }
  80.             else
  81.             {
  82.                 ed.WriteMessage("\nMust call 'TestView' first");
  83.             }
  84.         }
  85.  
  86.         private void SetViews(object sender, EventArgs e)
  87.         {
  88.             Application.Idle -= SetViews;
  89.             if (enumer.Current != original)
  90.             {
  91.                 using (DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument())
  92.                 {
  93.                     using (ViewTable vt = (ViewTable)Application.DocumentManager.MdiActiveDocument.Database.ViewTableId.Open(OpenMode.ForWrite))
  94.                     {
  95.                         using (ViewTableRecord vtr = Application.DocumentManager.MdiActiveDocument.Editor.GetCurrentView())
  96.                         {
  97.                             vtr.SetUcs(syncView.ViewOrthographic);
  98.                             vtr.Target = syncView.Target;
  99.                             vtr.ViewDirection = syncView.ViewDirection;
  100.                             vtr.Elevation = syncView.Elevation;
  101.                             vtr.CenterPoint = syncView.CenterPoint;
  102.                             vtr.Height = syncView.Height;
  103.                             vtr.Width = syncView.Width;
  104.                             Application.DocumentManager.MdiActiveDocument.Editor.SetCurrentView(vtr);
  105.                             if (regen)
  106.                                 Application.DocumentManager.MdiActiveDocument.Editor.Regen();
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.             if (enumer.MoveNext())
  112.             {
  113.                 Application.Idle += SetViews;
  114.                 Application.DocumentManager.MdiActiveDocument = enumer.Current;
  115.             }
  116.             else
  117.             {
  118.                 enumer = null;
  119.                 updating = false;
  120.                 syncView.Dispose();
  121.                 syncView = null;
  122.                 Application.DocumentManager.MdiActiveDocument = original;
  123.                 original = null;
  124.             }
  125.         }
  126.         void doc_ViewChanged(object sender, EventArgs e)
  127.         {
  128.             if (!updating)
  129.             {
  130.                 using (ViewTableRecord vtr = (ViewTableRecord)Application.DocumentManager.MdiActiveDocument.Editor.GetCurrentView())
  131.                 {
  132.                     syncView = new ViewTableRecord();
  133.                     syncView.SetUcs(vtr.UcsOrthographic);
  134.                     syncView.Target = vtr.Target;
  135.                     syncView.ViewDirection = vtr.ViewDirection;
  136.                     syncView.Elevation = vtr.Elevation;
  137.                     syncView.CenterPoint = vtr.CenterPoint;
  138.                     syncView.Height = vtr.Height;
  139.                     syncView.Width = vtr.Width;
  140.                 }
  141.                 original = (Document)sender;
  142.                 enumer = docs.GetEnumerator();
  143.                 enumer.Reset();
  144.                 Application.Idle += SetViews;
  145.                 updating = true;
  146.                 enumer.MoveNext();
  147.                 Application.DocumentManager.MdiActiveDocument = enumer.Current;
  148.             }
  149.         }

gswang

  • Newt
  • Posts: 117
Re: How to update the non active document view?
« Reply #4 on: September 24, 2013, 04:37:35 AM »
Thank you for your reply, you have a point there, I spent too much system resources to process each message, very difficult!
I use Map3D 2010, support ViewChanged?
ViewChanged began to support from which version?

I have tested your program, auto switche document, in this case, press mouse middle key can not drag the views.
« Last Edit: September 24, 2013, 06:58:38 PM by gswang »

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: How to update the non active document view?
« Reply #5 on: September 24, 2013, 12:00:32 PM »
Should have mentioned I'm running on 2013.  I don't understand your comments on trying the program. An alternative approach would be to use object overrule instead of the view changed event.

gswang

  • Newt
  • Posts: 117
Re: How to update the non active document view?
« Reply #6 on: September 24, 2013, 07:03:35 PM »
I have tested your program, the program will automatically switch the document, then, with the mouse middle key can not move the view, and when the data quantity is big, the synchronous effect is not smooth.

(i've tested in autocad 2012.)

Thanks for your help.