8.1.2. Revit Python Script Templete#

8.1.2.1. Import Library#

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
clr.AddReference('System')
from System.Collections.Generic import List
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System 
from System.Collections.Generic import *
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')

using Autodesk.Revit.DB;
using Dynamo.Graph.Nodes;
using Autodesk.DesignScript.Geometry;
using Autodesk.DesignScript.Runtime;
using Revit.GeometryConversion;
using RevitServices.Persistence;

8.1.2.2. App,UIApp,Document,UIDocument#

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = DocumentManager.Instance.CurrentUIApplication.Application
doc = DocumentManager.Instance.CurrentDBDocument;
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument;
uiapp = DocumentManager.Instance.CurrentUIApplication;
app = DocumentManager.Instance.CurrentUIApplication.Application;

8.1.2.3. Function#

8.1.2.3.1. Convert To List#

def tolist(obj1):
	if hasattr(obj1,'__iter__') : return obj1
	else : return [obj1]
element = UnwrapElement(tolist(IN[0]))

8.1.2.3.2. Flatten#

def flatten(x):
    result = []
    for el in x:
        if hasattr(el, "__iter__") and not isinstance(el, basestring):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

8.1.2.3.3. Transaction#

import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
#Do some action in a Transaction
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)
#Do Action
TransactionManager.Instance.TransactionTaskDone()
using Autodesk.Revit.DB;
using RevitServices.Persistence;
using RevitServices.Transactions;
doc = DocumentManager.Instance.CurrentDBDocument
Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
TransactionManager.Instance.EnsureInTransaction(doc);
// Do some thing
TransactionManager.Instance.TransactionTaskDone();
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Application = Autodesk.Revit.ApplicationServices.Application;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    try
    {
        _commandData = commandData;
        _uiapp = commandData.Application;
        _uidoc = commandData.Application.ActiveUIDocument;
        _doc = commandData.Application.ActiveUIDocument.Document;
        _app = commandData.Application.Application;
        using (Autodesk.Revit.DB.Transaction tran = new Autodesk.Revit.DB.Transaction(_doc,"Acton"))
        {
            // Do some thing
            tran.Commit();
        }
        Action();
    }
    catch (Exception e)
    {
        message = e.ToString();
        return Result.Failed;
    }
    return Result.Succeeded;
}

8.1.2.4. Reference#