RevitPythonShell: Curves

<<< Return to Nathan's Revit API Notebook

Introduction

Model curves can be created 2 ways within the family editor:

  1. NewModelCurve(): Uses Revit curve geometry (lines, arcs) and a sketch plane.
  2. NewCurveByPoints(): Uses an array of points to create a spline curve.

Model Curves

New model curves need to be defined using a Revit curve geometry and a sketch plane. Both need to be defined within the code before using the NewModelCurve() method.

Drawing a Line

A line is defined using XYZ values and then converting using NewModelCurve()

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
 
t = Transaction(doc, 'Create Line')
 
t.Start()
 
#Create a sketch plane
origin = XYZ.Zero
normal = XYZ.BasisZ
 
plane = app.Create.NewPlane(normal, origin)
skplane = doc.FamilyCreate.NewSketchPlane(plane)
 
#Create line vertices
lnStart = XYZ(0,0,0)
lnEnd = XYZ(20,20,0)
 
#create NewLine()
line = app.Create.NewLine(lnStart, lnEnd, True)
 
#create NewModelCurve()
crv = doc.FamilyCreate.NewModelCurve(line, skplane)
 
t.Commit()
 
__window__.Close()
RPS-Curves-CreateLineCurve.jpg

Drawing an Arc

An arc is created using the same technique as a line. In this case, the NewArc() method is used and requires parameters for plane, radius, start angle, and end angle.

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
 
t = Transaction(doc, 'Create Line')
 
t.Start()
 
#Create a sketch plane
origin = XYZ.Zero
normal = XYZ.BasisZ
 
plane = app.Create.NewPlane(normal, origin)
skplane = doc.FamilyCreate.NewSketchPlane(plane)
 
#Define arc parameters
startAngle = 0
endAngle = .5* math.pi
radius = 10
 
#create NewArc()
arc = app.Create.NewArc(plane, radius, startAngle, endAngle)
 
#create NewModelCurve()
crv = doc.FamilyCreate.NewModelCurve(arc, skplane)
 
t.Commit()
 
__window__.Close()
RPS-Curves-CreateArcCurve.jpg

Curve By Points

Curve Wave

We can create a complex array of reference points with a for loop and string them together with a NewCurveByPoints()

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application 
 
t = Transaction(doc, 'Create a sine wave curve.')
 
t.Start()
 
refptarr = ReferencePointArray()
 
#use for loop to create a series of points
for i in range(0,20):
    x = i*2
    y = i*2
    #z is controlled using sine
    z = math.sin(i)*2
 
    myXYZ = XYZ(x,y,z)
    refPt = doc.FamilyCreate.NewReferencePoint(myXYZ)
    refptarr.Append(refPt)
 
crv = doc.FamilyCreate.NewCurveByPoints(refptarr)
 
t.Commit()
 
__window__.Close()
RPS-Curves-CreateComplexSpline.jpg

Spiral Curve

We can create a spiral curve by having x controlled by sine, y controlled by cosine, and z increment by i.

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
 
t = Transaction(doc, 'This is my new transaction')
 
t.Start()
 
refptarr = ReferencePointArray()
 
#use for loop to create a series of points
for i in range(0,20):
    x = math.sin(i)*5
    y = math.cos(i)*5
    z = i
 
    myXYZ = XYZ(x,y,z)
    refPt = doc.FamilyCreate.NewReferencePoint(myXYZ)
    refptarr.Append(refPt)
 
crv = doc.FamilyCreate.NewCurveByPoints(refptarr)
 
t.Commit()
 
__window__.Close()
RPS-Curves-CreateSpiralCurve.jpg

Wave Curve Array

By we can create an array of wave curves using a nested for loop

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application 
 
t = Transaction(doc, 'wave curve array')
 
t.Start()
 
for i in range(0,20):
    #nesting Reference Points and Reference Point Array
    refptarr = ReferencePointArray()
    for j in range(0,20):
        x = i * 10
        y = j * 10
        z = (10*math.cos(i)) + (10*math.sin(j))
 
        myXYZ = XYZ(x,y,z)
        refPoint = doc.FamilyCreate.NewReferencePoint(myXYZ)
        refptarr.Append(refPoint)
 
    crv = doc.FamilyCreate.NewCurveByPoints(refptarr)
 
t.Commit()
 
__window__.Close()
RPS-Curves-CurveArray.jpg
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License