RevitPythonShell: Custom Form Creation

<<< Return to Nathan's Revit API Notebook

Introduction

This examples here show how to create a class which add some additional form creation methods. The examples will use the process for creating a NewLoftForm() as a basis for creating two new form methods for added flexibility in the main code.

The basic class will look as follows…

class FormCreate:
 
    #Method to create a surface from corner points
    def SurfaceFromCorners(self, c1, c2, c3, c4):
 
    #Method to create a surface from a grid of points
    def SurfaceFromPoints(self, points, u, v):

Surface from Corner Points

We will define the SurfaceFromCorners() method using the following technique

  1. Method accepts 4 XYZ corner points c1, c2, c3, c4
  2. Method defines two straight curves to define edge using reference points.
  3. Method draws a loft between two curves and outputs a surface.
    def SurfaceFromCorners(self, c1, c2, c3, c4):
 
        refarrarr = ReferenceArrayArray()
 
        #reference point array 1
        refptarr1 = ReferencePointArray()
        refptarr1.Append(doc.FamilyCreate.NewReferencePoint(c1))
        refptarr1.Append(doc.FamilyCreate.NewReferencePoint(c2))
        crv1 = doc.FamilyCreate.NewCurveByPoints(refptarr1)
 
        refarr1 = ReferenceArray()
        refarr1.Append(crv1.GeometryCurve.Reference)
        refarrarr.Append(refarr1)
 
        #reference point array 2
        refptarr2 = ReferencePointArray()
        refptarr2.Append(doc.FamilyCreate.NewReferencePoint(c4))
        refptarr2.Append(doc.FamilyCreate.NewReferencePoint(c3))
        crv2 = doc.FamilyCreate.NewCurveByPoints(refptarr2)
 
        refarr2 = ReferenceArray()
        refarr2.Append(crv2.GeometryCurve.Reference)
        refarrarr.Append(refarr2)
 
        #Corner surface property
        CornerSurface = doc.FamilyCreate.NewLoftForm(True, refarrarr)

Surface from Point Grid

    def SurfaceFromPointGrid(self, points, u, v):
 
        refarrarr = ReferenceArrayArray()
 
        index = 0
        for i in range(0, u):
            refptarr = ReferencePointArray()
            for j in range(0, v):
                point = points[index]
                refptarr.Append(doc.FamilyCreate.NewReferencePoint(point))
                index = index + 1
 
            crv = doc.FamilyCreate.NewCurveByPoints(refptarr)
 
            refarr = ReferenceArray()
            refarr.Append(crv.GeometryCurve.Reference)
            refarrarr.Append(refarr)
 
        PointsSurface = doc.FamilyCreate.NewLoftForm(True, refarrarr)

Using FormCreate()

Now that we have a new FormCreate class with two methods, we are ready to access it to create some forms.

Access SurfaceFromCorners
c1 = XYZ(0,0,0)
c2 = XYZ(50,0,20)
c3 = XYZ(50,50,0)
c4 = XYZ(0,50,20)
 
srf = FormCreate()
srf.SurfaceFromCorners(c1,c2,c3,c4)
Access SurfaceFromPointGrid
points = []
u = 20
v = 20
 
for i in range(0,u):
    for j in range(0,v):
        x = i * 10
        y = j * 10
        z = (10*math.cos(i)) + (10*math.sin(j))
        points.append(XYZ(x,y,z))
 
#Instantiating a FormCreate() object        
srf = FormCreate()
 
#Accessing the SurfaceFromPointGrid method
srf.SurfaceFromPointGrid(points, u, v)

Summary

The final code with the custom FormCreate() class is as follows

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
#DEFINE A CLASS called 'FormCreate' ---------------------------------------------
 
class FormCreate:
 
    #Method to create a surface from corner points
    def SurfaceFromCorners(self, c1, c2, c3, c4):
 
        refarrarr = ReferenceArrayArray()
 
        #reference point array 1
        refptarr1 = ReferencePointArray()
        refptarr1.Append(doc.FamilyCreate.NewReferencePoint(c1))
        refptarr1.Append(doc.FamilyCreate.NewReferencePoint(c2))
        crv1 = doc.FamilyCreate.NewCurveByPoints(refptarr1)
 
        refarr1 = ReferenceArray()
        refarr1.Append(crv1.GeometryCurve.Reference)
        refarrarr.Append(refarr1)
 
        #reference point array 2
        refptarr2 = ReferencePointArray()
        refptarr2.Append(doc.FamilyCreate.NewReferencePoint(c4))
        refptarr2.Append(doc.FamilyCreate.NewReferencePoint(c3))
        crv2 = doc.FamilyCreate.NewCurveByPoints(refptarr2)
 
        refarr2 = ReferenceArray()
        refarr2.Append(crv2.GeometryCurve.Reference)
        refarrarr.Append(refarr2)
 
        #Corner surface property
        CornerSurface = doc.FamilyCreate.NewLoftForm(True, refarrarr)
 
    #Method to create a surface from a grid of points  
    def SurfaceFromPointGrid(self, points, u, v):
 
        refarrarr = ReferenceArrayArray()
 
        index = 0
        for i in range(0, u):
            refptarr = ReferencePointArray()
            for j in range(0, v):
                point = points[index]
                refptarr.Append(doc.FamilyCreate.NewReferencePoint(point))
                index = index + 1
 
            crv = doc.FamilyCreate.NewCurveByPoints(refptarr)
 
            refarr = ReferenceArray()
            refarr.Append(crv.GeometryCurve.Reference)
            refarrarr.Append(refarr)
 
        PointsSurface = doc.FamilyCreate.NewLoftForm(True, refarrarr)
 
#MAIN CODE  -----------------------------------------------------------------
 
#main code demonstrates the use of SurfaceFromPointGrid
def main():
 
    t = Transaction(doc, 'Surface from point grid.')
 
    t.Start()
 
    points = []
    u = 20
    v = 20
 
    for i in range(0,u):
        for j in range(0,v):
            x = i * 10
            y = j * 10
            z = (10*math.cos(i)) + (10*math.sin(j))
            points.append(XYZ(x,y,z))
 
    #Instantiating a FormCreate() object        
    srf = FormCreate()
 
    #Accessing the SurfaceFromPointGrid method
    srf.SurfaceFromPointGrid(points, u, v)
 
    t.Commit()
 
    __window__.Close()
 
if __name__=="__main__":
    main()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License