Changes cannot be shown until the external command ends

Changes cannot be shown until the external command ends

Anonymous
Not applicable
1,071 Views
8 Replies
Message 1 of 9

Changes cannot be shown until the external command ends

Anonymous
Not applicable

I have a command that is triggered by button in a ribbon. This command creates multiple ViewPlans automatically, the issue that I'm facing is that when I run the command the form shows fine I create the new Views but the changes are not shown until I close the form and the external command ends.

 

How can I do the same task but being able to see the changes instantly in the background without having to close the form?  I have seen that in other addins but I don't know what I'm doing different.

 

Here is some of my code.

 

    [Transaction(TransactionMode.Automatic)]    
    public class Command : IExternalCommand
    {
        Result IExternalCommand.Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements) 
        {
            ViewCreator myViews = new ViewCreator(commandData);            

            try
            {
                ProjectSetupForm myForm = new ProjectSetupForm(myViews);
                myForm.ShowDialog();
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }  
    }

 

ViewCreator is my class where I have all my properties and methods.

I create all de ViewPlans inside the form with all the data that the user inputs.

 

I really apreciate your help.

Thanks,

 

Marc

 

0 Likes
Accepted solutions (1)
1,072 Views
8 Replies
Replies (8)
Message 2 of 9

ollikat
Collaborator
Collaborator

Hi.

 

Document.Regenerate() function usually does what you are looking for.

Message 3 of 9

Anonymous
Not applicable

Thanks for your help ollikat I really appreciate it.

 

I have tried to use the Document.Regenerate() method but it doesn't seams to work for my program.

 

Do you think that it has something to do with opening the form in modal? 

 

If I try to show the form modless like this

 

myForm.Show();

 

The program crashes in this line of code when I use ViewPlan.Create method.

 

ViewPlan vPlan = ViewPlan.Create(_doc, vft.Id, levelID);

 

Thanks again for your help!

0 Likes
Message 4 of 9

ollikat
Collaborator
Collaborator

Hi

 

Sorry to hear that Regenerate doesn't help.

 

No I don't think that modal dialog has any effect on your issue...at least directly. You might be able to do some magic with modeless dialog and idling event etc. but tha's not my strongest area currently.

 

As far as I know, only option you have left is that you commit the transaction where you have created those objects. It should refresh the project up to date.

Although I have to admit that I'm not perfectly aware how those view plans should appear. Someone else in this forum was askin whether changing the selection trough API would cause property palette to update, but that wasn't possible while external command was executing. So maybe there's something similar here (?).

Message 5 of 9

Anonymous
Not applicable

Hi,

 

Thanks again for your help! I'll try to play with it and see how it goes. No luck so far.

 

If I find a way to fix it I'll reply back the solution.

 

Thanks

0 Likes
Message 6 of 9

Joe.Ye
Alumni
Alumni

 

Hi,

 

Because you are using the automatic transaction model. So the model will not get updated until the command end and the automatica transaction commited.

 

The solution is to use the manual external transaction mode. And start the transaction before changing the model. Then just afte the change, Commit the transaction immediately in the handler of the button. 

 

This should be able to update the model when the form is visible.

 

 


______________________________________________________________

If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!



Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network
Message 7 of 9

Anonymous
Not applicable

Hi

 

Thanks for your help. I can't get the model update until the command ends. This is what I've changed so far, but is not updating the model because all changes are happening inside the form.

How can I make sure that I commit those changes multiple times inside the form?

 

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        Result IExternalCommand.Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements) 
        {
            Document _doc = commandData.Application.ActiveUIDocument.Document;

            ViewCreator myViews = new ViewCreator(commandData);            

            try
            {
                using (Transaction trans = new Transaction(_doc,"CreateViews"))
                {
                    trans.Start();

                    ProjectSetupForm myForm = new ProjectSetupForm(myViews);
                    myForm.ShowDialog();

                    trans.Commit();
                }
                
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }  

 

Thanks again for any help provided!

 

Marc

0 Likes
Message 8 of 9

Joe.Ye
Alumni
Alumni
Accepted solution
Hi Marc,

Would you try start transaction in the button handler, and commit it within the button handler too? You need to pass in the current document to ProjectSetupForm class. So transaction can be started within class ProjectSetupForm.

Hope this helps.


Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network
Message 9 of 9

Anonymous
Not applicable

Hi,

 

Thanks again! This time worked perfectly! For some reason I thought that I had to do the transaction within the Command class. 

 

I changed the transaction to Manual

 

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        Result IExternalCommand.Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            ViewCreator myViews = new ViewCreator(commandData);          

            try
            {
                ProjectSetupForm myForm = new ProjectSetupForm(myViews);
                myForm.ShowDialog();
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }
    }

 

And then inside my ViewCreator class I did the transaction with the method that creates every view.

 


using (Transaction trans = new Transaction(_doc, "CreateViews")) { trans.Start(); // Code ... trans.Commit(); }

 

Thanks a lot for your help!!

 

Marc

0 Likes