wpf window and autocad not working synchronously

wpf window and autocad not working synchronously

essam-salah
Collaborator Collaborator
1,417 Views
5 Replies
Message 1 of 6

wpf window and autocad not working synchronously

essam-salah
Collaborator
Collaborator

hi

the plugin functionalities are all ok but the problem is when we do something like.

AddAcadLine() then UpdateWpfWindow() then AddAcadLine() UpdateWpfWindow() ,

what we see is:

UpdateWpfWindow()  then UpdateWpfWindow() then AddAcadLine() AddAcadLine() .

 

the scenario is we try to add/delete ACAD Objects and we want to see the notifications in the wpfWindow after each add/delete process.

we use aApp.Application.ShowModelessWindow(wpfWindow); for this example.

and C#/VS2019 and Autocad/Civil3d 2018.

thanks in advance, any help will be appreciated.

 

0 Likes
Accepted solutions (1)
1,418 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

You did not say how the methods AddCadxxx() and UpddateWpfWinodw() is called, but yes, using modeless window is the cause of the issue. When making changes to the drawing from a modeless window, it is strongly recommended that you call SendStringToExecute() from the modeless window.

 

So, you should wrap you drawing change/update code in custom command method or methods, and use SendStringToExecute() in the modeless window user interaction event handler, where you could also add CommandEnded event handler to MdiActiveDocument, so that when the custom command method is done, you can update the modeless window view (as you can see, in this way, you only update your window when the command action is completed), then remove the CommandEnded handler.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6

FRFR1426
Collaborator
Collaborator

It's because when the focus is on your modeless window, AutoCAD is running in the application context. In this mode, the drawing window is not refreshed until it's got focus. You can use SendStringToExecute() like advised by Norman, or you can use ExecuteInCommandContextAsync() like this:

 

 

void DrawLineButtonOnClick(object sender, RoutedEventArgs e)
{
    if (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.IsApplicationContext)
        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.ExecuteInCommandContextAsync(AddLineCallback, null);
    else
        AddLineCallback();
}

static Task AddLineCallback(object o = null)
{
    // Add your line

    return Task.CompletedTask;
}

 

 

Always test if you are in application context because in this case, the callback is not executed.

 

And ExecuteInCommandContextAsync() is async, so if you want to run code after the call, you need to await the method.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 4 of 6

essam-salah
Collaborator
Collaborator

hi @norman.yuan 

thanks for reply, about calling SendStringToExecute(), where we can open Transaction and DocumentLock ?

i mean should we call SendStringToExecute() from Trans/DocLock or we do it in the other place the custom acad Cmd?

0 Likes
Message 5 of 6

norman.yuan
Mentor
Mentor
Accepted solution

Sorry for the late reply. I was thinking to put together some code to demonstrate my suggest would be much better for anyone who is interested in this topic. I decided to publish a working project in my blog here so that with all code for a better readability:

 

https://drive-cad-with-code.blogspot.com/2021/03/modeless-formwindow-do-something-and.html 

 

Hope it helps.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 6

essam-salah
Collaborator
Collaborator

hi @norman.yuan  many thanks for your effort sir,

i just made the same project you published but in wpf, here:

essamce/AcadWpfModelessDialog (github.com)

0 Likes