How to add your knowledge

Example 06a: Using Python - Create Sine Wave From Selected Elements

    Table of contents
    No headers

    Note: you must have Python installed to use the Python script node.  See Installation and Getting Started.

    Goal: Learn about Python scripting and the Python Script node. Learn how to pass information into that node, have the python script create some elements and then pass execution back to Dynamo.

    This example covers how to user the Python Script Node in Dynamo. Don’t worry if you are not a programmer! Learning how to program in Python is the basis for another class, we will just be covering the highlights here. The Python Scripting Node requires IronPython to be installed on the machine.

    1. From Vasari 2.5, go to Open/Family…and open Mass with Loaded Families.rfa from the Samples directory
    2. Go to Add-ins tab and launch Dynamo
    3. From the Dynamo File Menu, go to File/Samples/ 6. Create Sine Wave From Selected Points. You should see this workspace appear:

     

    ex6-1.png

    A Python Script Node can take in input from other Dynamo nodes

     

    1. Now place two points using the Vasari Sketching Gallery
    2. Select each point using the Point By Selection Node in Dynamo
    3. Press Run. You should see a line and a sine wave be plotted between the two entered points. These elements all came from the Python Script inside of Dynamo.
    4. Experiment with moving the points or the straight line, notice how the sine wave is updated to follow the new position? This is because Dynamo and the Python node can watch specific elements and then re-run the workspace to keep things in sync based on your changes.

    ex6-2.png

    Sine Wave plotted by the Python Script Node inside Dynamo

    1. Below is the Python script. We will not dissect it in detail now but take a look at how the beginPoint and endPoint variables, get assigned objects from IN. The IN and OUT ports map to variable in the script.

     

    #derived from Nathan Miller's example #http://theprovingground.wikidot.com/...-api-py-curves

     

    import math

     

    doc = __revit__.ActiveUIDocument.Document

    app = __revit__.Application

     

    beginPoint = IN[0]

    endPoint = IN[1]

    lineRefPointArray = ReferencePointArray()

    lineRefPointArray.Append(beginPoint)

    lineRefPointArray.Append(endPoint)

    crv = doc.FamilyCreate.NewCurveByPoints(lineRefPointArray)

    crvRef = crv.GeometryCurve

    refptarr = ReferencePointArray()

     

    #use for loop to create a series of points

    steps = 20

    for i in range(0,steps+1):

        pt = crvRef.Evaluate(float(i)/steps,1) # returns and XYZ

        x = pt.X

        y = pt.Y

        z = pt.Z + math.sin(i)*steps

        myXYZ = XYZ(x,y,z)

        refPt = doc.FamilyCreate.NewReferencePoint(myXYZ)

        refptarr.Append(refPt)

    crv2 = doc.FamilyCreate.NewCurveByPoints(refptarr)

     

    Python script to make a sine wave between two points

    1. Now for a variation on a theme. The nice thing about Python scripts is they can be altered fairly easily to suit new conditions.
    2. Draw a new Model Spline in the graphics view.
    3. From the Dynamo File Menu, go to File/Samples/ 6.1 Create Sine Wave From Selected Curve. You should see this workspace appear:

     

    ex6-3.png

    Using Python script to make a sine wave along a curve

     

    1. Select the curve using the Curve By Selection Node in Dynamo
    2. Press Run. You should see a sine wave be plotted along the curve.
    3. Here is the Python script. The program flow is very similar, all we are doing is passing in a different object into IN and then creating new XYZs along the spline curve instead of in a strait line.

     

    #derived from Nathan Miller's example #http://theprovingground.wikidot.com/...-api-py-curves

     

    import math

     

    doc = __revit__.ActiveUIDocument.Document

    app = __revit__.Application

     

    crv = IN

    crvRef = crv.GeometryCurve

    refptarr = ReferencePointArray()

     

    #use for loop to create a series of points

    steps = 20

    for i in range(0,steps+1):

        pt = crvRef.Evaluate(float(i)/steps,1) # returns and XYZ

        x = pt.X

        y = pt.Y

        z = pt.Z + math.sin(i)*steps

        myXYZ = XYZ(x,y,z)

        refPt = doc.FamilyCreate.NewReferencePoint(myXYZ)

        refptarr.Append(refPt)

    crv2 = doc.FamilyCreate.NewCurveByPoints(refptarr)crv2 = doc.FamilyCreate.NewCurveByPoints(refptarr)

     

    Python script to make a sine wave along a curve