Open a file if central model cannot be found

Open a file if central model cannot be found

Anonymous
Not applicable
3,022 Views
16 Replies
Message 1 of 17

Open a file if central model cannot be found

Anonymous
Not applicable

Hi,

 

I need to open a file with Detach from central option via Revit API.

The problem is that I'm getting CentralFileCommunicationException and opening is failed.

2018-06-01_1827.png

The behavior looks strange to me. I want to open and detach, so it absolutely does not matter for me if central model available or not.

At the same time, if I open this file in UI, I also get this dialog, but I can just close it and continue.2018-06-01 18_25_08-Window.png

Is there a way to open a file with Detach from central and Ignore the fact, that central model is unavailable?

 

I'm using the following code. In the OpenOptions there is no suitable property.

            // open the document
            OpenOptions options = new OpenOptions()
            {
                AllowOpeningLocalByWrongUser = true,
                Audit = false,
                DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets
            };

            Application app = sender as Application;

            // However, UIApplication can be 
            // instantiated from Application.

            UIApplication uiapp = new UIApplication(app);


            var filePath = @"d:\1.rvt";
            var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath);
            try
            {
                uiapp.OpenAndActivateDocument(
                    modelPath, options, true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

Thanks,

Victor.

0 Likes
3,023 Views
16 Replies
Replies (16)
Message 2 of 17

jeremytammik
Autodesk
Autodesk

Dear Victor,

 

 

Sorry to hear that. I passed on your question to the development team...

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 17

eason.kangEDLV4
Autodesk Support
Autodesk Support

Hi,

 

The error is caused by the 3rd argument of the OpenAndActivateDocument method, true means "Do Not Detach".

 

I test the following code snippet with Revit 2018.3 and it works as desired. 

 

 

public void CreateSaveDetachedFile(UIApplication application, string revitFilePath)
{
    //Get information of current opened revit file
    FileInfo filePath = new FileInfo(revitFilePath);
    ModelPath revitCentralFile = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);

    OpenOptions openOptions = new OpenOptions();
    openOptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;
    openOptions.Audit = false;
    openOptions.AllowOpeningLocalByWrongUser = true;

    application.OpenAndActivateDocument(revitCentralFile, openOptions, false);
}

Hope it helps!

 

Cheers,

 


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

0 Likes
Message 4 of 17

kevin_fielding
Advocate
Advocate

I'm experiencing the same issue in Revit 2017. If the Central file doesn't exist or is inaccessible this exception occurs.

 

Can this exception be handled, as in my instance I'm not interested in whether the central file exists, I'm just trying to detached the local and save it as a new central file.

 

K

0 Likes
Message 5 of 17

eason.kangEDLV4
Autodesk Support
Autodesk Support

Hi,

 

The above solution is also working for Revit 2017, please give it a try!

 

https://forums.autodesk.com/t5/revit-api-forum/open-a-file-if-central-model-cannot-be-found/m-p/8059...

 

Cheers,


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

0 Likes
Message 6 of 17

kevin_fielding
Advocate
Advocate

Hi, below is the code I'm implementing. If this is run on a local file where the central file is found and accessible it runs fine, if it is not present an exception is raised.

 

CentralFileCommunicationException

 

The file-based central model could not be reached, because e.g. the network is down or the file server is down.

 

 

// Set the OpenOptions to open the RVT Detached from Central
                OpenOptions oOpenOpts = new OpenOptions();
                oOpenOpts.Audit = false;
                oOpenOpts.AllowOpeningLocalByWrongUser = true;
                oOpenOpts.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;
                                
                // Set open user worksets to an empty list, so all worksets will be closed.
                oWSConfig.Open(oWSIDs);
                // Set the OpenOptions object
                oOpenOpts.SetOpenWorksetsConfiguration(oWSConfig);

                // Open the RVT
                uidetachedDoc = uiapp.OpenAndActivateDocument(oModelPath, oOpenOpts, false);
                detachedDoc = uidetachedDoc.Document;

 

 

0 Likes
Message 7 of 17

eason.kangEDLV4
Autodesk Support
Autodesk Support

The error might be related to this function call, but I didn't see it in your code snippet, it requires Network connection to the central file with my experience.

WorksharingUtils.GetUserWorksetInfo(revitCentralFile)

 

 

If you want to preserve worksets, you can set WorksetConfiguration to OpenLastViewed, it's the default option of the Revit OpenFileDialog with my finding.

DetachCentral.jpg

 

Here is my test code. It works as expected and the result is the same as the Revit OpenFileDialog does.

 

public void CreateSaveDetachedFile2(UIApplication application, string revitFilePath)
{
    //Get information of current opened revit file
    FileInfo filePath = new FileInfo(revitFilePath);
    ModelPath revitCentralFile = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);

    var openConfig = new WorksetConfiguration(WorksetConfigurationOption.OpenLastViewed);

    OpenOptions openOptions = new OpenOptions();
    openOptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;
    openOptions.Audit = false;
    openOptions.AllowOpeningLocalByWrongUser = true;
    openOptions.SetOpenWorksetsConfiguration(openConfig);

    application.OpenAndActivateDocument(revitCentralFile, openOptions, false);
}

Hope it helps.

 


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

Message 8 of 17

e.capasso
Participant
Participant

Thanks,

this solved the problem.

 

var openConfig = new WorksetConfiguration(WorksetConfigurationOption.OpenLastViewed);
0 Likes
Message 9 of 17

kevin_fielding
Advocate
Advocate

I've tried OpenLastView and CloseAllWorksets. Unfortunately I can't find a way of opening a local file were the central file is inaccessible without an exception being raised, below is my code.

 

 

ModelPath oModelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(strRVTFullPath);

                // Set the OpenOptions to open the RVT Detached from Central
                OpenOptions oOpenOpts = new OpenOptions();
                oOpenOpts.Audit = false;
                oOpenOpts.AllowOpeningLocalByWrongUser = true;
                oOpenOpts.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;

                // Set WorksetConfiguration so that all user created worksets are closed on file open.
                WorksetConfiguration oWSConfig = new WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets);
                oOpenOpts.SetOpenWorksetsConfiguration(oWSConfig);

                // Open the RVT
                try
                {
                    uidetachedDoc = uiapp.OpenAndActivateDocument(oModelPath, oOpenOpts, false);
                }
                catch (Autodesk.Revit.Exceptions.CentralFileCommunicationException)
                {
                    ????
                }

 

 

Any thoughts?

 

K

0 Likes
Message 10 of 17

e.capasso
Participant
Participant

Using the try-catch block like you did you're assuming the exception is raised only by the OpenAndActiveDocument,

while it may be raised by some other function. try to use the Exception to find which function called the exception

 

                ModelPath oModelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(strRVTFullPath);
                try {
                // Set the OpenOptions to open the RVT Detached from Central
                OpenOptions oOpenOpts = new OpenOptions();
                oOpenOpts.Audit = false;
                oOpenOpts.AllowOpeningLocalByWrongUser = true;
                oOpenOpts.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;

                // Set WorksetConfiguration so that all user created worksets are closed on file open.
                WorksetConfiguration oWSConfig = new WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets);
                oOpenOpts.SetOpenWorksetsConfiguration(oWSConfig);

                // Open the RVT
                uidetachedDoc = uiapp.OpenAndActivateDocument(oModelPath, oOpenOpts, false);
                }
                catch (Exception e)
                {
// The below gives you which method threw the exception e.TargetSite
0 Likes
Message 11 of 17

kevin_fielding
Advocate
Advocate

Prior to posting I debugged the code extensively, and found that the OpenAndActivateDocument method was the cause and that the CentralFileCommunicationException was the exception raised.

 

This was confirmed with your code suggestion. 

 

 

 

0 Likes
Message 12 of 17

eason.kangEDLV4
Autodesk Support
Autodesk Support

Could you share your whole test procedure step-by-step? I cannot reproduce the issue you're facing.

 

In the same time, could you consider providing a none-confidential reproducible test case? This need to debug.

 

A none-confidential reproducible test case is a way to describe an API problem which contains following items:

  • A short exact description of what you are trying to achieve.
  • The behavior you observe versus what you expect, and why this is a problem.
  • A complete yet minimal Revit sample model to run a test in.
  • A complete yet minimal macro embedded in the sample model or Visual Studio solution with add-in manifest that can be compiled, loaded, run and debugged with a single click to analyze its behavior live in the sample model.
  • Detailed step-by-step instructions for reproducing the issue, e.g. which element to pick, what command to launch etc.

Note. Please remove confidential data or information from anything you want to provide to us.

 

If your reproducible test case cannot be posted here, please consider providing it in these ways:

  • ForumPrivate Messenger: Help > Private Messenger > How do I send a private message?
  • Revit Product Support: Contact product support, pass your reproducible test case to him/her quoting this forum link and tell him/her its a Revit API case, then it will be forwarded to my team.

Cheers,


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

0 Likes
Message 13 of 17

AnatolyCEL
Enthusiast
Enthusiast

Hi,

I have the same problem. I need to open project with detach from central, but saved central model path is not exist any more. Now openAndActivate command rise exception and not open the project. How open it with API?

Copy SmallBuilding - R2018.rvt to c:\temp

Open Project2018Macro.rvt in Revit 2018

Enable Macro

Start Macro Manager

Run OpenSmallBuildingProject.Run

 

Thanks,

Anatoly.

0 Likes
Message 14 of 17

eason.kangEDLV4
Autodesk Support
Autodesk Support

Hi @AnatolyCEL @kevin_fielding,

 

I'm sorry to hear that, and thank you for your reproducible model, @AnatolyCEL!

 

Finally, I figure out how to create the detached model from worksharing based on the model you provided.

 

Here are the steps I used:

  1. Open the original model in DetachAndPreserveWorksets and CloseAllWorksets mode via the OpenDocumentFile Method (ModelPath, OpenOptions).
  2. Save it as new central model in your local machine and close the opened document.
  3. Open the new central model with the same option with the OpenAndActivateDocument Method (ModelPath, OpenOptions, Boolean), then your will see Revit open it as detached model.
  4. Save it to somewhere you want.

 

And here you go, the code snippet:

public void CreateDetachedFile(UIApplication application, string revitFilePath)
{
    //Get information of current opened revit file
    var fileInfo = new FileInfo(revitFilePath);

    // Open the original model with DetachAndPreserveWorksets option
    ModelPath revitCentralFile = ModelPathUtils.ConvertUserVisiblePathToModelPath(fileInfo.FullName);

    var openConfig = new WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets);
    var openOptions = new OpenOptions();
    openOptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;
    openOptions.Audit = false;
    openOptions.AllowOpeningLocalByWrongUser = true;
    openOptions.SetOpenWorksetsConfiguration(openConfig);

    var openedDoc = application.Application.OpenDocumentFile(revitCentralFile, openOptions);

    var options = new SaveAsOptions();
    options.OverwriteExistingFile = true;

    var saveConfig = new WorksharingSaveAsOptions();
    saveConfig.SaveAsCentral = true;
    saveConfig.OpenWorksetsDefault = SimpleWorksetConfiguration.LastViewed;

    options.SetWorksharingOptions(saveConfig);

    var newFilename = Path.Combine(
                            fileInfo.DirectoryName,
                            string.Format(
                                "{0}_{1}{2}",
                                Path.GetFileNameWithoutExtension(fileInfo.Name),
                                "new centeral",
                                Path.GetExtension(fileInfo.Name)
                            )
    );

    var modelPathNewCtentral = ModelPathUtils.ConvertUserVisiblePathToModelPath(newFilename);

    //Save it as new central model
    openedDoc.SaveAs(modelPathNewCtentral, options);
    openedDoc.Close(true);

    // Open the model with detached from central option
    application.OpenAndActivateDocument(modelPathNewCtentral, openOptions, false);
}

Hope it helps!

 

Cheers,

 

 


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

0 Likes
Message 15 of 17

jeremytammik
Autodesk
Autodesk

Dear Eason,

 

Thank you very much for your research and complete solution!

 

Can you please put together an edited description of the solution and publish that as a blog post for future reference?

 

I can happily publish it for you on The Building Coder, if you like.

 

Thank you!

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 16 of 17

AnatolyCEL
Enthusiast
Enthusiast

Hi Eason,

Thank you, very much. It helps.

Anatoly.

0 Likes
Message 17 of 17

eason.kangEDLV4
Autodesk Support
Autodesk Support

Hi Jeremy,

 

Sure, it's my pleasure! I will post a new reply here after handling some Forge backlog.

 

@AnatolyCEL

I'm glad it helps! 🙂

 

Cheers,


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

0 Likes