Revit Server Browser


#1

Hi everyone,

I have a problem when access a path of Revit file from Revit server
My purpose that allow user pick up file from revit server, then get its path to open this file.
I think It has two way to get the path file

  1. create a string variable, and put the path (ex. RSN://hostname/path/to/model.rvt)
  2. use Open File Dialog to pick up a file and get its path.
    but I don’t like use the first way, it’s manually, So I focus on second way.
    If I use Open File Dialog from System.Windows.Forms, I’m sure cannot access to revit file in Revit server. Therefore I use Open File Dialog from RevitAPIUI.
    I don’t know it can access revit server or not, because at the moment, my code has error.
    image
    Could you please help me fix this code to check my idea.
    In the bad case, If this code work well,but It cannot access revit server file, Do you have any idea about that.
    Thanks in advance.

#2

I cannot really help you, however. I am facing the same problem. I am trying to acces the Open File Dialog from System.Windows.Forms in this manner. Because I had no luck trying to acces the RevitAPIUI File Dialog.

I managed to only acces the Task Dialog through the RevitAPIUI by copy pasting this code

import clr

 #acces RevitAPI and RevitAPIUI
 clr.AddReference('RevitAPI')
 clr.AddReference('RevitAPIUI')
 clr.AddReference("RevitServices")
 import RevitServices
 from RevitServices.Persistence import DocumentManager

 #acces all name-spaces
 from Autodesk.Revit.DB import *
 from Autodesk.Revit.DB.Architecture import *
 from Autodesk.Revit.DB.Analysis import *
 from Autodesk.Revit.UI import TaskDialog, TaskDialogCommonButtons, TaskDialogCommandLinkId, TaskDialogResult

 title = 'Test'

 dialog = TaskDialog(title)
 dialog.MainInstruction = 'Hallo'
 dialog.MainContent = 'Text Content'
 dialog.FooterText = 'Footer text'
 dialog.VerificationText = 'Verification text'
 dialog.TitleAutoPrefix = False
 dialog.AllowCancellation = True
 dialog.CommonButtons = TaskDialogCommonButtons.Ok 
 dialog.DefaultButton = TaskDialogResult.None
 dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, 'Command button text','command button sub text')
 dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, 'Command button text2', 'command button sub text 2')

 result = dialog.Show()

It worked great, I could acces the TaskDialog through Python Dynamo as this image shows:
TaskDialog

However, I want to acces the FileOpenDialog instead of a Dialog. If I give in a FileOpenDialog in this code and type FileOpenDialog.Show(). It says I need to pass an argument into Show(). No idea what argument I should pass. So I resorted to Winforms and copy pasted a file open dialog from the tutorial website. Which is this code;

#!/usr/bin/ipy

 import clr
 clr.AddReference("System.Windows.Forms")
 clr.AddReference("System.Drawing")

 from System.Windows.Forms import Application, Form, StatusBar
 from System.Windows.Forms import ToolBar, ToolBarButton, FolderBrowserDialog
 from System.Windows.Forms import DialogResult

 class IForm(Form):

     def __init__(self):
         self.Text = "FolderBrowserDialog"
  
         toolbar = ToolBar()
         toolbar.Parent = self
         openb = ToolBarButton()

         self.statusbar = StatusBar()
         self.statusbar.Parent = self

         toolbar.Buttons.Add(openb)
         toolbar.ButtonClick += self.OnClicked

         self.CenterToScreen()

     def OnClicked(self, sender, event):
         dialog = FolderBrowserDialog()

         if (dialog.ShowDialog(self) == DialogResult.OK):
             self.statusbar.Text = dialog.SelectedPath
        

 Application.Run(IForm())

This however does not give me the same preview like it does on their website. It returns this instead;

While it should look like this :
file browser

When I copy pasted this code I finally got an OpenFileDialog. My aim is to let the user open an excel file through Dynamo;

import clr
 clr.AddReference("System.Windows.Forms")
 clr.AddReference("System.Drawing")

 from System.Windows.Forms import Application, Form, TextBox
 from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog
 from System.Windows.Forms import DialogResult, ScrollBars, DockStyle

 class IForm(Form):

     def __init__(self):
         self.Text = "OpenDialog"
  
         toolbar = ToolBar()
         toolbar.Parent = self
         openb = ToolBarButton()
         

         self.textbox = TextBox()
         self.textbox.Parent = self
         self.textbox.Multiline = True
         self.textbox.ScrollBars = ScrollBars.Both
         self.textbox.WordWrap = False
         self.textbox.Parent = self
         self.textbox.Dock = DockStyle.Fill
       

         toolbar.Buttons.Add(openb)
         toolbar.ButtonClick += self.OnClicked

         self.CenterToScreen()

     def OnClicked(self, sender, event):
         dialog = OpenFileDialog()
         dialog.Filter = "Excel files (*.xlsx)|*.xlsx"

         if dialog.ShowDialog(self) == DialogResult.OK:
             f = open(dialog.FileName)
             data = f.read()
             f.close()
             self.textbox.Text = data
        

 Application.Run(IForm())

It looked like this and it worked, I am on Windows 10.

open file dialog


#3

@conghau.bdh I’m not certain if this is what your end goal is, but this code will let you select a file and the output will be the path of the selected file:

import clr

clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

dialog = FileOpenDialog("All Revit files (*.rvt, *.rfa, *.rte, *.rft)|*.rvt;*.rfa;*.rte;*.rft")
dialog.Show()
mpath = dialog.GetSelectedModelPath()
path = ModelPathUtils.ConvertModelPathToUserVisiblePath(mpath)

OUT = path

@Claus The FileOpenDialog constructor needs a filter string for file types:
http://www.revitapidocs.com/2018/a67923df-362a-52de-944f-c6ef0532cd59.htm


#4

Thanks for your reply. :slight_smile: I copy pasted your code and got the following;

fileopendialog

Am I missing some import which defines FileOpenDialog?

And about this;

The FileOpenDialog constructor needs a filter string for file types

I don’t understand how to translate the Revit API docs to Python. I have no experience with C#


#5

Did you paste the code exactly? :thinking: It works on my end when directly pasting what I posted above. Here’s the .dyn with it just in case there was error in the copy/pasting… pythonfilepath.dyn (1.7 KB)

The FileOpenDialog Class belongs to the Autodesk.Revit.UI namespace so the from Autodesk.Revit.UI import * line is including it. I don’t have any experience with C# either - the Revit API docs are still incredibly useful. The link I posted to the FileOpenDialog constructor is showing that when you call FileOpenDialog() it takes one argument, a Filter, in the form of a string, explained in this section below:

Parameters

filter
Type: System String
The filter string. See the remarks for Filter for details.

BTW, the functionality of the code I pasted is exactly the same as the OOTB FilePath node, aside from requiring the user to run the code to select the file. I was just showing to demonstrate how to use FileOpenDialog because it sounds as if you and the original post-er are looking to do more with it :slight_smile:


#6

Thanks for your efforts, I think I need to update to Dynamo 1.3 to make it work. At work I am still using 0.9.


#7

@Claus ah actually its more likely your version of Revit. FileOpenDialog Class is not available in the API before 2017 (at least I don’t think so because it is not in the API docs)


#8

You’re correct, I tested it in 2017 and it worked perfectly :grin:


#9

@awilliams Thank you very much. Your script work well. Unfortunately, It cannot show Revit Server or Bim 360 to access. Do you have any idea to deal with it.
I have seen this article and I think It can solve this problem, but how to change it to python and Dynamo, I’m not sure…


#10

@conghau.bdh Ah, I understand better what you are after now! Have a look at the DynaWeb package: https://radumg.github.io/DynaWeb/ :slight_smile:


#11

Let me check. Thank you in advance!


#12

No problem! I am not at a computer with Revit at the moment and can’t recall whether or not that package will help you get the server path or not, but the package nodes allow you to interact with the REST API (there are several sample flles); if the package does require you to have the server path yourself, you will need to get the ServerPath from the ModelPath that the code I posted above supplies, see here on the API docs: http://www.revitapidocs.com/2018.1/c304ffcf-b3ae-46be-e361-a80bec83b5c0.htm
You should get it by this or something close:

import clr

clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

dialog = FileOpenDialog("Revit Files (*.rvt)|*.rvt")
dialog.Show()
mpath = dialog.GetSelectedModelPath()
cpath = mpath.CentralServerPath
spath = ServerPath(cpath, mpath)
OUT = spath