open a rvt file

open a rvt file

Joris.vd.Meulen
Collaborator Collaborator
10,151 Views
6 Replies
Message 1 of 7

open a rvt file

Joris.vd.Meulen
Collaborator
Collaborator

Hi, 

 

I'm fiddling with API to create a button which opens a certain file. 

 

I've got the whole ribbon, button working however. opening a file though .... 

What am I missing / does open command need a return Result.succesful ?

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;

namespace OpenLib
{
public Document OpenDocumentFile(
	string openlib = "T:\Revit 2016\My Lib.rvt";
    )
}

 

I tried without result

 

private static Document OpenDocumentFile(ModelPath modelPath)
    {
        ModelPath modelPath = "T:\Revit 2016\My Lib.rvt";

        Document openedDoc = application.OpenDocumentFile(modelPath);

        return openedDoc;
    }   

 

love python coding
0 Likes
Accepted solutions (1)
10,152 Views
6 Replies
Replies (6)
Message 2 of 7

BobbyC.Jones
Advocate
Advocate
Accepted solution

It's hard to give you exact advise, as what you've shared will not compile.  So as a better starting point, here's some example code that will compile.  I've also included a method that will open the document in the Revit editor, in case that's what you were looking for.  Let us know if that doesn't get you started down the correct path...pun may or may not be intended.

 

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class OpenMyLib : IExternalCommand
    {
        private const string Path = @"T:\Revit 2016\My Lib.rvt";

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //opens a document
            commandData.Application.Application.OpenDocumentFile(Path);

            //opens a document in the editor
            //commandData.Application.OpenAndActivateDocument(Path);

            return Result.Succeeded;
        }
    }
--
Bobby C. Jones
Message 3 of 7

Anonymous
Not applicable

It's kind of hard to tell without all the code. Here is some code that works fine that maybe you can use?

 

namespace Revit.ribbon
{
    [TransactionAttribute(TransactionMode.Manual), RegenerationAttribute(RegenerationOption.Manual)]
    public class Open : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIApplication App = commandData.Application;
            Document doc = App.ActiveUIDocument.Document;
            UIDocument uidoc = new UIDocument(doc);

            String modelPath = @"C:\Revit Projects\Project1.rvt";
            App.OpenAndActivateDocument(modelPath);
                       
            return Result.Succeeded;
        }
    }
    
}

and then I call it with a ribbon button:

PushButtonData itemData22 = new PushButtonData("itemName22", "Testing Addin", AddInPath, "Revit.ribbon.Open");
            PushButton item22 = group1.AddPushButton(itemData22) as PushButton;
            item18.ToolTip = "Testing a new addin";
0 Likes
Message 4 of 7

Joris.vd.Meulen
Collaborator
Collaborator

@BobbyC.Jones yes! open and activate is the one I need.

 

What is the purpose of opendocumentfile (without activate)?

 

 

love python coding
0 Likes
Message 5 of 7

BobbyC.Jones
Advocate
Advocate

Hello @Joris.vd.Meulen 

 

I'm glad that's what you needed!

 

We use OpenDocumentFile() for importing schedules and for batch processing.  We have a file with many pre-configured schedules and a tool that allows a user to import desired schedules into their current project.  And for batch processing it's faster to open the documents without opening them in the editor.

--
Bobby C. Jones
0 Likes
Message 6 of 7

Anonymous
Not applicable

Thank you for your useful guide. But I have 1 more question: 

  • After opening a new file and active the view of that file. How can I get control of that file ? Or how I can get that file save as and close then return to the first file.

Thank you so much Smiley Happy

0 Likes
Message 7 of 7

MERLET_J
Participant
Participant

I have the same problem

0 Likes