Hi there,
I am trying to create a script that will take an input Polyline which represents a linked floor’s boundaries as an input.
Using these curves the script then opens and creates a new generic family document and creates a sweep (using a temporary profile atm) following this complete boundary edge.
This portion of the script seems to be working well, as if I output the sweep variable it shows me a sweep element with an element ID.
import clr
import sys
import os
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
Document = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
# Create a new family document using a specific family template
template_path = "C:\\ProgramData\\Autodesk\\RVT 2021\\Family Templates\\English\\Metric Generic Model.rft"
doc = app.NewFamilyDocument(template_path)
# Get the input curves from Dynamo
DSpolycurve = IN[0]
# Turn the Dynamo curves in Revit Lines
RevitLines = [x.ToRevitType() for x in DSpolycurve.Curves()]
# Create a curvearray represnting the path using the revit Lines
patharr = CurveArray()
for RVT_lin in RevitLines:
patharr.Append(RVT_lin)
TransactionManager.Instance.EnsureInTransaction(doc)
# Get three points from input floor curves
DSpts = [Curve.StartPoint for Curve in DSpolycurve.Curves()]
threepts = [x.ToRevitType() for x in DSpts[0:3]]
# Create sketchplane using 3 points
pl = Plane.CreateByThreePoints(threepts[0],threepts[1],threepts[2])
sketchPlane = Autodesk.Revit.DB.SketchPlane.Create(doc,pl)
# Create curveloops for profile creation
Profile_arrarr = CurveArrArray()
Profile_arr = CurveArray()
# Create basic points for test profile
pnt1 = XYZ(0,0,0)
pnt2 = XYZ(0.21,0,0)
pnt3 = XYZ(0.21,0.21,0)
pnt4 = XYZ(0,0.21,0)
# Append to curvearray
Profile_arr.Append(Line.CreateBound(pnt1, pnt2))
Profile_arr.Append(Line.CreateBound(pnt2, pnt3))
Profile_arr.Append(Line.CreateBound(pnt3, pnt4))
Profile_arr.Append(Line.CreateBound(pnt4, pnt1))
Profile_arrarr.Append(Profile_arr)
# Create a profile using previously defined curveloop
profile = doc.Application.Create.NewCurveLoopsProfile(Profile_arrarr)
trans = SubTransaction(doc)
trans.Start()
# Create sweep
sweep = doc.FamilyCreate.NewSweep(True, patharr,sketchPlane, profile, 0, ProfilePlaneLocation.Start);
trans.Commit()
TransactionManager.Instance.TransactionTaskDone()
OUT = sweep
My issue arises when I try to add these few lines of code to the script to save the family document to my desktop:
# Define Save as options
SaveAsOpt = SaveAsOptions()
SaveAsOpt.OverwriteExistingFile = True
# Define new filepath
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
file_path = os.path.join(desktop, "Fire Barrier.rfa")
# Save the family document to the user's desktop
doc.SaveAs(file_path, SaveAsOpt)
OUT = doc
I’ve tried all sorts to try and get it to close the transaction but can’t seem to get it to work.
Is it something to do with me background opening the family document to create the sweep?
A couple of other similar forum posts (albiet not very well detailed and neither solved) can be found here:
Any suggestions appreciated!