OpenAndActivateDocument, Central model, change Open workset from "specify.

OpenAndActivateDocument, Central model, change Open workset from "specify.

Anonymous
Not applicable
2,688 Views
7 Replies
Message 1 of 8

OpenAndActivateDocument, Central model, change Open workset from "specify.

Anonymous
Not applicable

Hi,

I have a problem to open and detach from a central model, when the central model default workset settings is "specify...", if I do not change the setting, Revit will always show the dialogbox "Open workset" and wait for user input.

How can I change this setting before i open and detach from a central model by the API?

My code:

namespace OpenProject
{
class App : IExternalApplication
{
private static string ModelPathName = @"C:\Users\AWPE\Desktop\Open workset.rvt";

public Result OnStartup(UIControlledApplication a)
{
a.ControlledApplication.ApplicationInitialized += OnApplicationInitialized;
return Result.Succeeded;
}

void OnApplicationInitialized(object sender, ApplicationInitializedEventArgs e)
{
// This does not work, because the sender is
// an Application instance, not UIApplication.

//UIApplication uiapp = sender as UIApplication;

// Sender is an Application instance:

Application app = sender as Application;
// However, UIApplication can be
// instantiated from Application.

UIApplication uiapp = new UIApplication(app);

///ModelPath MyCentralFile = app.GetWorksharingCentralModelPath();
ModelPath modelPathName = ModelPathUtils.ConvertUserVisiblePathToModelPath(ModelPathName);

OpenOptions openoptions = new OpenOptions();
openoptions.Audit = false;
 

openoptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;

// Get info on all the user worksets in the project prior to opening
IList<WorksetPreview> worksets = WorksharingUtils.GetUserWorksetInfo(modelPathName);
IList<WorksetId> worksetIds = new List<WorksetId>();
// Find two predetermined worksets
foreach (WorksetPreview worksetPrev in worksets)
{
worksetIds.Add(worksetPrev.Id);
}

WorksetConfiguration openConfig = new WorksetConfiguration();

// Set list of worksets for opening
openConfig.Open(worksetIds);
openoptions.SetOpenWorksetsConfiguration(openConfig);
uiapp.OpenAndActivateDocument(modelPathName, openoptions, false);
}

public Result OnShutdown(UIControlledApplication a)
{
return Result.Succeeded;
}
}
}

Best regards
awpe

2,689 Views
7 Replies
Replies (7)
Message 2 of 8

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

To be fair, I didn't read any of your code, but the following (VB.NET) snippet from the SDK should be easily modifiable for OpenAndActivateDocument:

(There's a matching C# example in there also.)

 

Private Function OpenDocumentWithWorksets(app As Application, projectPath As ModelPath) As Document
    Dim doc As Document = Nothing
    Try
        ' Get info on all the user worksets in the project prior to opening
        Dim worksets As IList(Of WorksetPreview) = WorksharingUtils.GetUserWorksetInfo(projectPath)
        Dim worksetIds As IList(Of WorksetId) = New List(Of WorksetId)()
        ' Find two predetermined worksets
        For Each worksetPrev As WorksetPreview In worksets
            If worksetPrev.Name.CompareTo("Workset1") = 0 OrElse worksetPrev.Name.CompareTo("Workset2") = 0 Then
                worksetIds.Add(worksetPrev.Id)
            End If
        Next

        Dim openOptions As New OpenOptions()
        ' Setup config to close all worksets by default
        Dim openConfig As New WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets)
        ' Set list of worksets for opening 
        openConfig.Open(worksetIds)
        openOptions.SetOpenWorksetsConfiguration(openConfig)
        doc = app.OpenDocumentFile(projectPath, openOptions)
    Catch e As Exception
        TaskDialog.Show("Open File Failed", e.Message)
    End Try

    Return doc
End Function

 

WorksetConfigurationOption.OpenAllWorksets may be what you need.

 

 


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 3 of 8

Anonymous
Not applicable

Hi Matthew,

 

Thanks for your reply.

 

My problem is not to open All workset automatic.

My problem is to change Open workset from "specify..." because it overrides and breaks OpenAndActivateDocument,

 

I have tried to use WorksetConfigurationOption.OpenAllWorksets in my code.

 

WorksetConfiguration openConfig = new WorksetConfiguration(WorksetConfigurationOption.OpenAllWorksets);

// Set list of worksets for opening

openConfig.Open(worksetIds);

openoptions.SetOpenWorksetsConfiguration(openConfig);

uiapp.OpenAndActivateDocument(modelPathName, openoptions, false);

 

Cheers,
awpe

0 Likes
Message 4 of 8

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

Goes to show I should have read your code! Smiley Wink

I think I've had this issue in the past, come to think of it. Perhaps a related one at least.

I get a 'CentralFileCommunicationException' when opening the file. This is what I've caught in my code that does what you're trying to do.

The exception notes don't make any sense for it. I should have reported the issue, but I support back to Revit 2014 for this particular app, so finding a workaround was the easiest path to take.

My workaround involved the annoying task of having to open, close then retry openAndActivate:

Try

'at least try to open and activate here - works most times, unless 'Specify...'

Catch ex As Exceptions.CentralFileCommunicationException
    Dim tempDoc As DB.Document = m_app.OpenDocumentFile(newModelPath, openOptions)
    Dim saveasOptions As New DB.SaveAsOptions
    saveasOptions.OverwriteExistingFile = True
    Dim wsOpts As New DB.WorksharingSaveAsOptions
    wsOpts.OpenWorksetsDefault = DB.SimpleWorksetConfiguration.AllWorksets
    wsOpts.SaveAsCentral = True
    saveasOptions.Compact = True
    saveasOptions.SetWorksharingOptions(wsOpts)
    'MySaveAsTool.SaveAs is just a wrapper catching likely exceptions, and reporting back.
    If Not MySaveAsTool.SaveAs(tempDoc, newPath, saveasOptions, errMsg2) Then
       Return UI.Result.Failed
    End If
    If tempDoc.Close(False) Then
       'it worked
    End If
End Try
'retry OpenAndActivate

It's probably worth reporting this bug, I guess!

 

 

By the way, have you checked the WorksetId values that you're adding? There may be something telling there, such as WorksetId.InvalidWorksetId


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi Matt

 

Thanks again for your reply,

 

I have tried to use OpenDocumentFile, but I can not get the function to work on a Central model  that has been saved with the setting "Open workset from specify..." the file stop open at 10%.

 

my new code:

 

        void OnApplicationInitialized(object sender, ApplicationInitializedEventArgs e)
        {
                // This does not work, because the sender is
                // an Application instance, not UIApplication.

                //UIApplication uiapp = sender as UIApplication;

                // Sender is an Application instance:

                Application app = sender as Application;
                // However, UIApplication can be
                // instantiated from Application.

                UIApplication uiapp = new UIApplication(app);

                ///ModelPath MyCentralFile = app.GetWorksharingCentralModelPath();
                ModelPath modelPathName = ModelPathUtils.ConvertUserVisiblePathToModelPath(ModelPathName);

                WorksetConfiguration openConfig = new WorksetConfiguration(WorksetConfigurationOption.OpenAllWorksets);

                OpenOptions openoptions = new OpenOptions();
                openoptions.Audit = false;
                openoptions.SetOpenWorksetsConfiguration(openConfig);
                openoptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;

                //// Get info on all the user worksets in the project prior to opening
                //IList<WorksetPreview> worksets = WorksharingUtils.GetUserWorksetInfo(modelPathName);
                //IList<WorksetId> worksetIds = new List<WorksetId>();
                //// Find two predetermined worksets
                //foreach (WorksetPreview worksetPrev in worksets)
                //{
                //    worksetIds.Add(worksetPrev.Id);
                //}

                //// Set list of worksets for opening
                //openConfig.Open(worksetIds);
                app.OpenDocumentFile(modelPathName, openoptions);
            //uiapp.OpenAndActivateDocument(modelPathName, openoptions, false);
        }

 

Cheers,
awpe

0 Likes
Message 6 of 8

matthew_taylor
Advisor
Advisor

Hi @Anonymous,

No problem.

I didn't set any worksetConfigurationOption value.

 

I suggest you get this working as a standalone externalCommand before doing whatever you're doing with your events.

This works with the file you supplied.

 

<Transaction(TransactionMode.Manual)> _
<Regeneration(RegenerationOption.Manual)> _
<Journaling(JournalingMode.UsingCommandData)> _
Public Class NonTransactionCommand
    Implements UI.IExternalCommand
    Public Function Execute(ByVal commandData As UI.ExternalCommandData, ByRef message As String, ByVal elements As DB.ElementSet) As UI.Result Implements UI.IExternalCommand.Execute
        Dim app As ApplicationServices.Application = commandData.Application.Application
        Execute = UI.Result.Failed
        Dim newModelPath As DB.ModelPath = DB.ModelPathUtils.ConvertUserVisiblePathToModelPath("C:\Users\change me!\Desktop\Open workset.rvt")

        Dim openOptions As New DB.OpenOptions
        openOptions.DetachFromCentralOption = _
               DB.DetachFromCentralOption.DetachAndPreserveWorksets
        openOptions.Audit = True
        Dim tempDoc As DB.Document = app.OpenDocumentFile(newModelPath, openOptions)
        MsgBox(tempDoc.PathName)
        Execute = UI.Result.Succeeded

        Return Execute
    End Function
End Class

 [Edit: stripped out unnecessary code]


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 7 of 8

Troy_Gates
Advocate
Advocate

I don't believe there is a way to specify the worksets you want open when opening a model other than previous, all, or none. But you could open with all worksets closed, then open the worksets you want opened after the model is opened using the DocumentOpened event handler.

0 Likes
Message 8 of 8

aleksei.melnikov
Contributor
Contributor

I have a similar issue when I am opening workshared model using OpenAndActivateDocument.

 

In spite of chosen WorksetConfigurationOption (OpenLastViewed, OpenAllWorksets, CloseAllWorksets or specifying using .Open()) Revit shows "Opening Worksets" dialog which is locking script execution. And the chosen WorksetConfigurationOption has no any effect on "Opening Worksets" dialog content, it always shows all the worksets enabled.

 

Is it a common behaviour of OpenAndActivateDocument to always show "Opening workset" dialog?

Could there be any way to skip this dialog and set worksets programmatically?

 

0 Likes