How to force readOnly for opening a RVT(and RFA) file?

Hello everybody
I create a simple script to open catalogue model :

# -*- coding: utf-8 -*-
# Overture de fichier
model_path = "C:\\folder\\__Catalogue.rvt"
 __revit__.OpenAndActivateDocument(model_path)

How can I force the opening to be in “Read Only” mode please ?

Have you tried setting the file to be read only? In the command prompt the instruction would be:
attrib +r "C:\folder\__Catalogue"
You can use os.chmod() function if you want to do it from within a Python script.
…but Revit is extremely annoying and limited when a file is read-only, it might be better to copy the file to a temporary location and open the temporary file.

2 Likes

Thank you Maw-Wan, it was I already do, but everybody can change this by mistake.
So I search a solution with the open action of Revit API.

For memory, my code actually look like that :

# -*- coding: utf-8 -*-
# Overure de fichier selon milésime REVIT
from pyrevit import HOST_APP
from pathlib import Path
import os, stat


model_path = ""
ver = int(HOST_APP.version)
for i in range(ver , 2020, -1):
    moa = "C:\\folder\\__"+ str(i) +" Catalogue.rvt"
    my_file = Path(moa)
    if my_file.exists():
        model_path = moa
        
        # voir http://techarttiki.blogspot.com/2008/08/read-only-windows-files-with-python.html
        if i<ver:
            os.chmod(moa, stat.S_IREAD)
        else:
            os.chmod(moa, stat.S_IWRITE)
        break

my_file = Path(model_path)
if model_path != "":
    __revit__.OpenAndActivateDocument(model_path)
else:
    print "Pas de fichier trouvé."

It would be more reliable to have IT change the permissions to the directory that they reside in so that so only you (or a specific account type, ie. admin) can save changes to files in that directory. I know it’s not a very Revit thing to do, but it is very effective.

1 Like

It is not to limit writing to a user, it’s to limit for REVIT version.
We ave many project in many REVIT version.
My goal is to make sure that no one converts a library from a given version to a higher version (making it unusable for REVIT from an earlier version)