Announcement

Collapse
No announcement yet.

Windows Form completes and then can't undo

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Windows Form completes and then can't undo

    I have written a Revit macro in C# that will upload a set of viewsheet parameters from a text file. I let the use pick which parameters from their viewsheet map to which fields in the text file, and then hit the OK button to make all of the changes. This is set up to use a single transaction, but I could do it so each sheets changes are a single transaction just as easily.

    When the routine is finished, the form window stays there . . . and after I manually close it I cannot undo the transaction from the menu (or with ctrl-z). Has anyone done something similar? Not sure what I'm doing wrong. While the form is up, the undo button is enabled, but I can't get to it while the form is up, and ctrl-z does not work. After the form closes the undo button is disabled. I originally had this working just by reading the text file, and hardcoding which parameters would change (to get my particular task finished), and the undo functionality worked (though I did it by sheet, rather than all in one) so I think it must be something to do with the form.

    Here is the code that calls the form, and then should dispose it when finished (but for some reason doesn't, though the routine works otherwise):
    Code:
    [COLOR=#008000][B]namespace[/B][/COLOR] SheetPropertyMaintenance
    {
        [Autodesk.Revit.Attributes.[COLOR=#191970][B]Transaction[/B][/COLOR](Autodesk.Revit.Attributes.TransactionMode.Manual)]
        [Autodesk.Revit.DB.Macros.[COLOR=#191970][B]AddInId[/B][/COLOR]([COLOR=#0000ff]"ACFA52A7-3E01-49C9-989A-D26DDB475C40"[/COLOR])]
        [COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#000080]partial[/COLOR] [COLOR=#ff0000]class[/COLOR] ThisApplication
        {
            [COLOR=#0000ff][B]private[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]Module_Startup[/B][/COLOR]([COLOR=#ff0000]object[/COLOR] sender, EventArgs e)
            {
    
            }
    
            [COLOR=#0000ff][B]private[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]Module_Shutdown[/B][/COLOR]([COLOR=#ff0000]object[/COLOR] sender, EventArgs e)
            {
    
            }
    
            [COLOR=#008000]#[B]region[/B] Revit Macros generated code[/COLOR]
            [COLOR=#0000ff][B]private[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]InternalStartup[/B][/COLOR]()
            {
                [B]this[/B].Startup += [COLOR=#008b8b][B]new[/B][/COLOR] System.[COLOR=#191970][B]EventHandler[/B][/COLOR](Module_Startup);
                [B]this[/B].Shutdown += [COLOR=#008b8b][B]new[/B][/COLOR] System.[COLOR=#191970][B]EventHandler[/B][/COLOR](Module_Shutdown);
            }
            [COLOR=#008000]#[B]endregion[/B][/COLOR]
            [COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]LoadDrawingNumbers[/B][/COLOR]()
            {
                [COLOR=#008000]//Load the form[/COLOR]
    
                Document doc = [B]this[/B].ActiveUIDocument.Document;
                SheetParametersForm f = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#191970][B]SheetParametersForm[/B][/COLOR](doc);
                [COLOR=#0000ff][B]if[/B][/COLOR] (f.[COLOR=#191970][B]ShowDialog[/B][/COLOR]()==DialogResult.OK) f.[COLOR=#191970][B]Dispose[/B][/COLOR]();
                [COLOR=#0000ff][B]else[/B][/COLOR] f.[COLOR=#191970][B]Dispose[/B][/COLOR]();
             }
        }
    }
    The code for the OK button is here:
    Code:
             [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]BCommandClick[/B][/COLOR]([COLOR=#ff0000]object[/COLOR] sender, EventArgs e)
            {
                [COLOR=#008000]//Here is where we read the file (skipping the first line which is the labels)[/COLOR]
                [COLOR=#008000]//And set the parameter values in revit based on the text file[/COLOR]
                FilteredElementCollector Sheets = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#191970][B]FilteredElementCollector[/B][/COLOR](doc).[COLOR=#191970][B]OfClass[/B][/COLOR]([COLOR=#008b8b][B]typeof[/B][/COLOR](ViewSheet));
                StreamReader reader = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#191970][B]StreamReader[/B][/COLOR](FileName.Text);
                [COLOR=#ff0000]string[/COLOR] newline;
                [COLOR=#ff0000]string[/COLOR] [] linearray;
                newline = reader.[COLOR=#191970][B]ReadLine[/B][/COLOR](); [COLOR=#008000]// this is the label line so we don't want to use it[/COLOR]
                [COLOR=#008000]// we want a single transaction for the operation, so that one undo undoes the entire operation[/COLOR]
                [COLOR=#008000][B]using[/B][/COLOR] (Transaction t = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#191970][B]Transaction[/B][/COLOR](doc))
                {
                    t.[COLOR=#191970][B]Start[/B][/COLOR]([COLOR=#0000ff]"Load Parameters From File"[/COLOR]);
                    [COLOR=#0000ff][B]while[/B][/COLOR] ((newline = reader.[COLOR=#191970][B]ReadLine[/B][/COLOR]()) != [B]null[/B])
                    {
                        linearray = newline.[COLOR=#191970][B]Split[/B][/COLOR]([COLOR=#ff00ff]'\t'[/COLOR]);
                        [COLOR=#008000]//find a sheet with the key field specified (preferable method, not working yet)[/COLOR]
    [COLOR=#008000]/*[/COLOR]
    [COLOR=#008000]                    var query = from element in Sheets[/COLOR]
    [COLOR=#008000]                        where element.get_Parameter(KeyParameter.Text).AsString() == linearray[0][/COLOR]
    [COLOR=#008000]                        select element;[/COLOR]
    [COLOR=#008000]                    ViewSheet v = (ViewSheet) query.ElementAt(0);[/COLOR]
    [COLOR=#008000]*/[/COLOR]
                        [COLOR=#008000]//query above does not work, for now just loop through the sheets each time[/COLOR]
                        [COLOR=#0000ff][B]foreach[/B][/COLOR] (ViewSheet v [COLOR=#0000ff][B]in[/B][/COLOR] Sheets)
                        {
                            [COLOR=#ff0000]string[/COLOR] kParam = v.[COLOR=#191970][B]get_Parameter[/B][/COLOR](KeyParameter.Text).[COLOR=#191970][B]AsString[/B][/COLOR]();
                            [COLOR=#0000ff][B]if[/B][/COLOR](kParam == linearray[[COLOR=#00008b]0[/COLOR]]) //This is the sheet we are looking for
                            {
                                [COLOR=#0000ff][B]for[/B][/COLOR] ([COLOR=#ff0000][B]int[/B][/COLOR] i = [COLOR=#00008b]0[/COLOR]; i<LBRevitParameters.Items.Count; i++)
                                {
                                    [COLOR=#ff0000]string[/COLOR] sParam = LBRevitParameters.[COLOR=#191970][B]GetItemText[/B][/COLOR](LBRevitParameters.Items[i]);
                                       v.[COLOR=#191970][B]get_Parameter[/B][/COLOR](sParam).[COLOR=#191970][B]Set[/B][/COLOR](linearray[i+[COLOR=#00008b]1[/COLOR]]);
                                }
                            }
                        }
                    }
                    reader.[COLOR=#191970][B]Close[/B][/COLOR]();
                    t.[COLOR=#191970][B]Commit[/B][/COLOR]();
                }
                [COLOR=#000080]return[/COLOR];

Related Topics

Collapse

Working...
X