DocumentChanged event produces bug when used in conjunction with Document.ExportImage

DocumentChanged event produces bug when used in conjunction with Document.ExportImage

Anonymous
Not applicable
795 Views
4 Replies
Message 1 of 5

DocumentChanged event produces bug when used in conjunction with Document.ExportImage

Anonymous
Not applicable

Hello everyone, I was recently working on an addin for Revit that tries to export a preview image of a certain view when ever the user makes any changed to the model. everything went according to plan except for a tiny little bug that despite it's insignificance, renders the whole addin completely useless. so the addin is supposed to capture a preview every time that the DocumentChanged event is triggered, but somehow this process is getting in the way of Revit and preventing it from changing some of the instance parameters (e.g. whenever I tried to change the top constraint of a wall from "level3"to "unconstrained", the parameter reverts back to "level3" as soon as I click it). it is extremely difficult for me to figure out a workaround and this is why I'm asking for your help.  What I do know is this: the issue is not caused by the act of subscribing to the event. rather it is the byproduct of both the event subscription as well as the doc.ExportImage method. if I comment out this line the problem disappears immediately, but then obviously no previews will get exported. any help is greatly appreciated and thanks in advance...

 

here is my method:

public static void OnDocumentChanged(object sender, DocumentChangedEventArgs e)
        {
            

            string path = GlobalVariables6.m_path + "/frame-" + frameNumber.ToString() + ".jpg";            
            var opt = new ImageExportOptions
            {
                Zoom = GlobalVariables6.m_zoom,
                ZoomType = ZoomFitType.Zoom,
                FilePath = path,
                FitDirection = FitDirectionType.Horizontal,
                HLRandWFViewsFileType = ImageFileType.JPEGLossless,
                ImageResolution = ImageResolution.DPI_600,
                ExportRange = ExportRange.SetOfViews,
            };
            IList<ElementId> viewId = new List<ElementId>();
            viewId.Add(view.Id);
            opt.SetViewsAndSheets(viewId);            
            GlobalVariables6.m_document.ExportImage(opt);
            //TaskDialog.Show("nasim", "the preview has been saved");
            frameNumber++;
        }

 

0 Likes
Accepted solutions (1)
796 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor

Why are you taking the Document from a variable rather that from the EventArgs: e.GetDocument? Where is ViewID coming from? How are you subscribing / unsubscribing to the event? 

 

Exporting an image in response to every document change, does this really seem like a good idea? 

 

Message 3 of 5

Anonymous
Not applicable

thank you very much for your time, quite frankly I wasn't aware of the fact that I could have just used the event arguments to gain access to the document, I really appreciate you pointing that out. as for the ViewId, I used a filteredElementCollector to get a list of all the views in the document and then looped through them to get a specific view id. I subscribed and unsubscribe to the event using the "Execute" methods in two separate IExternalCommands which can be controlled by the user within Revit (sort of like ON and OFF switches). the main idea behind this addin is to accurately track progress. another alternative would be to export an image every 2 minuets or so.  what would be the best course of action? thanks in advance.

0 Likes
Message 4 of 5

Anonymous
Not applicable

what I suspect is happening is that every time the user makes a change to the model, the ImageExport method is run simultaneously and this causes all kinds of issues and conflicts, would it be feasible to incorporate the Idling event into the code so that it only gets fired up when Revit is able to receive API calls?

0 Likes
Message 5 of 5

RPTHOMAS108
Mentor
Mentor
Accepted solution

You could use Idling event or put timer on a separate thread to raise an external event periodically.

 

I don't recommend either of these things for what you are doing because what I've noticed in the past using the UI is that exporting an image at 600 dpi isn't an instantaneous thing.