Python truss code

I am very new to Python but am attempting to create a script to parametrically model a truss in Revit with a given span, depth and number of struts. I have a few issues with my code. Being able to see all the internal points created from the iteration, drawing lines between these points and then applying a family to the lines created.
This is the output I get:

This is my code:

Enable Python ‘Common Language Runtime’ (.NET) support

import clr

Enable access to IronPython modules

import sys
sys.path.append(‘C:\Program Files (x86)\IronPython 2.7\Lib’)

Load DesignScript

clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Load Revit API

clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
#from Autodesk.Revit.DB.Architecture import *
#from Autodesk.Revit.DB.Electrical import *
#from Autodesk.Revit.DB.Mechanical import *
#from Autodesk.Revit.DB.Plumbing import *
from Autodesk.Revit.DB.Structure import *

Load Revit UI

Gives access to the Revit User Interface (buttons; dialogues etc)

clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.UI import *

Load System .NET

import System
from System.Collections.Generic import *

Load Revit Nodes

Gives access to Dynamo Revit Nodes

Performs geometry conversion between Dynamo and Revit

clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

Load Revit Services

Gives access to the Revit document

clr.AddReference(‘RevitServices’)
import RevitServices

Get document, UI and application objects

from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
app=DocumentManager.Instance.CurrentUIApplication.Application

Allows transactions in Revit

from RevitServices.Transactions import TransactionManager
#--------------- REFERENCES END ---------------

#--------------- COLLECTOR SAMPLE START ---------------

Get Elements by built-in category (Instance)

elems = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_DetailComponents).WhereElementIsNotElementType().ToElements()

Get Elements by built-in category (Type)

#elems = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_DetailComponents).WhereElementIsElementType().ToElements()

Get Elements by multiple Classes

#typeList = ListSystem.Type
#typeList.Add(ViewSection)
#typeList.Add(ViewDrafting)
#emcf = ElementMulticlassFilter(typeList)
#elems = FilteredElementCollector(doc).WherePasses(emcf).WhereElementIsNotElementType().ToElements()
#--------------- COLLECTOR SAMPLE END ---------------

#--------------- INPUT START ---------------
#ensure ‘element’ is a list to satisfy ‘For Loop’ if only one object is passed.
def tolist(obj1):
if hasattr(obj1,“iter”): return obj1
else: return [obj1]

#Preparing input from Dynamo to Revit
#Set the script to work to 10 decimal places
from decimal import Decimal, getcontext
#How to Limit Decimal Places in Python Without Rounding - Solved - Beautiful Programming
getcontext().prec = 10
#--------------- INPUT END ---------------
guid=Revit_API_Revit_API_Developers_Guide_Revit_Geometric_Elements_Geometry_GeometryObject_Class_Curves_Curve_creation_html
span = int(IN[1])
span = Decimal(span)
depth = IN[2]
#ST = [Element.Name.GetValue(UnwrapElement(IN[3]).Symbol) for i in elems]
#level = doc.ActiveView.GenLevel
#ST = FilteredElementCollector(doc).OfClass(FamilySymbol).OfCategory(BuiltInCategory.OST_StructuralFraming).ToElements()[0]

def seePoint(xyz):
a = Point.Create(xyz).ToProtoType()
return a

#Get truss points
#Set how many points are needed to create the struts
Strutpoints = IN[5]-(3)
#Set the distance between each strut
Strutdist = int(IN[5])
Strutdist = Decimal(Strutdist)
Strutdist = Decimal(span/Strutdist)
#Formatted_Strutdist = “{:.10f}”.format(Strutdist)
#Set edge points
a = XYZ.Zero
b = app.Create.NewXYZ(span,0,0)
c = app.Create.NewXYZ(Strutdist,0,depth)
d = app.Create.NewXYZ(span-Strutdist,0,depth)
#See edge points
ax = seePoint(a)
bx = seePoint(b)
cx = seePoint(c)
dx = seePoint(d)
#Set internl points
#e = app.Create.NewXYZ(Strutdist2,0,0)
#f = app.Create.NewXYZ(Strutdist
3,0,depth)
#g = app.Create.NewXYZ(Strutdist*4,0,0)

#e = app.Create.NewXYZ(Strutdist2,0,0)
#f = app.Create.NewXYZ(Strutdist
3,0,depth)
#g = app.Create.NewXYZ(Strutdist*4,0,0)

internal_points =
for i in range(2, Strutpoints + 2):
point = app.Create.NewXYZ(Strutdist*i,0,depth if i % 2==0 else 0)
internal_points.append(point)
for point in internal_points:
point_x = seePoint(point)
#See internal points
#ex = seePoint(e)
#fx = seePoint(f)
#gx = seePoint(g)

BC = Line.CreateUnbound(a,b)
TC = Line.CreateUnbound(c,d)
S1 = Line.CreateUnbound(a,c)
S2 = Line.CreateUnbound(b,d)

#--------------- CODE ---------------
#--------------- TRANSACTION START---------------

#TransactionManager.Instance.EnsureInTransaction(doc)

#BCx = doc.Create.NewFamilyInstance(a,ST,level,Structure.StructuralType(1))
#TCx = doc.Create.NewFamilyInstance(c,ST,level,Structure.StructuralType(1))

#TransactionManager.Instance.TransactionTaskDone()
#--------------- TRANSACTION END---------------
#--------------- OUTPUT ---------------
OUT = ax,bx,cx,dx,internal_points,point_x,BC,TC,S1,S2
#OUT = a,b,ax,bx,cx,dx,BC,TC

@jake.styles ,

you can set your code here </> … looks like chatGPT, first handle your frame, than go step by step…


#Enable Python ‘Common Language Runtime’ (.NET) support
import clr

# Enable access to IronPython modules
import sys
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")

# Load DesignScript
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *

# Load Revit API
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
#from Autodesk.Revit.DB.Architecture import *
#from Autodesk.Revit.DB.Electrical import *
#from Autodesk.Revit.DB.Mechanical import *
#from Autodesk.Revit.DB.Plumbing import *
from Autodesk.Revit.DB.Structure import *

Load Revit UI
Gives access to the Revit User Interface (buttons; dialogues etc)
clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.UI import *

# Load System .NET
import System
from System.Collections.Generic import *

#Load Revit Nodes
#Gives access to Dynamo Revit Nodes
#Performs geometry conversion between Dynamo and Revit
clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

Load Revit Services
Gives access to the Revit document
clr.AddReference(‘RevitServices’)
import RevitServices

Get document, UI and application objects
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
app=DocumentManager.Instance.CurrentUIApplication.Application

Allows transactions in Revit
from RevitServices.Transactions import TransactionManager
#--------------- REFERENCES END ---------------

#--------------- COLLECTOR SAMPLE START ---------------

# Get Elements by built-in category (Instance)
#elems = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_DetailComponents).WhereElementIsNotElementType().ToElements()

#Get Elements by built-in category (Type)
elems = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_DetailComponents).WhereElementIsElementType().ToElements()

#Get Elements by multiple Classes
#typeList = ListSystem.Type
#typeList.Add(ViewSection)
#typeList.Add(ViewDrafting)
#emcf = ElementMulticlassFilter(typeList)
#elems = FilteredElementCollector(doc).WherePasses(emcf).WhereElementIsNotElementType().ToElements()
#--------------- COLLECTOR SAMPLE END ---------------

#--------------- INPUT START ---------------
#ensure ‘element’ is a list to satisfy ‘For Loop’ if only one object is passed.
def tolist(obj1):
if hasattr(obj1,“iter”): return obj1
else: return [obj1]

#Preparing input from Dynamo to Revit
#Set the script to work to 10 decimal places
from decimal import Decimal, getcontext
#How to Limit Decimal Places in Python Without Rounding - Solved - Beautiful Programming
getcontext().prec = 10
#--------------- INPUT END ---------------
guid=Revit_API_Revit_API_Developers_Guide_Revit_Geometric_Elements_Geometry_GeometryObject_Class_Curves_Curve_creation_html
span = int(IN[1])
span = Decimal(span)
depth = IN[2]
#ST = [Element.Name.GetValue(UnwrapElement(IN[3]).Symbol) for i in elems]
#level = doc.ActiveView.GenLevel
#ST = FilteredElementCollector(doc).OfClass(FamilySymbol).OfCategory(BuiltInCategory.OST_StructuralFraming).ToElements()[0]

def seePoint(xyz):
a = Point.Create(xyz).ToProtoType()
return a

#Get truss points
#Set how many points are needed to create the struts
Strutpoints = IN[5]-(3)
#Set the distance between each strut
Strutdist = int(IN[5])
Strutdist = Decimal(Strutdist)
Strutdist = Decimal(span/Strutdist)
#Formatted_Strutdist = “{:.10f}”.format(Strutdist)
#Set edge points
a = XYZ.Zero
b = app.Create.NewXYZ(span,0,0)
c = app.Create.NewXYZ(Strutdist,0,depth)
d = app.Create.NewXYZ(span-Strutdist,0,depth)
#See edge points
ax = seePoint(a)
bx = seePoint(b)
cx = seePoint(c)
dx = seePoint(d)
#Set internl points
#e = app.Create.NewXYZ(Strutdist2,0,0)
#f = app.Create.NewXYZ(Strutdist3,0,depth)
#g = app.Create.NewXYZ(Strutdist*4,0,0)

#e = app.Create.NewXYZ(Strutdist2,0,0)
#f = app.Create.NewXYZ(Strutdist3,0,depth)
#g = app.Create.NewXYZ(Strutdist*4,0,0)

internal_points = 
for i in range(2, Strutpoints + 2):
point = app.Create.NewXYZ(Strutdist*i,0,depth if i % 2==0 else 0)
internal_points.append(point)
for point in internal_points:
point_x = seePoint(point)
#See internal points
#ex = seePoint(e)
#fx = seePoint(f)
#gx = seePoint(g)

BC = Line.CreateUnbound(a,b)
TC = Line.CreateUnbound(c,d)
S1 = Line.CreateUnbound(a,c)
S2 = Line.CreateUnbound(b,d)

#--------------- CODE ---------------
#--------------- TRANSACTION START---------------

#TransactionManager.Instance.EnsureInTransaction(doc)

#BCx = doc.Create.NewFamilyInstance(a,ST,level,Structure.StructuralType(1))
#TCx = doc.Create.NewFamilyInstance(c,ST,level,Structure.StructuralType(1))

#TransactionManager.Instance.TransactionTaskDone()
#--------------- TRANSACTION END---------------
#--------------- OUTPUT ---------------
OUT = ax,bx,cx,dx,internal_points,point_x,BC,TC,S1,S2
#OUT = a,b,ax,bx,cx,dx,BC,TC

here some usefull links
https://primer.dynamobim.org/10_Custom-Nodes/10-6_Python-Templates.html

1 Like

Thank you, I will. It is my first time coding, I tried chatGPT but it did not work in the slightest. It probably looks AI because I have taken bits from forums in the same way AI would haha.

If you’ve never coded before I’d suggest something more basic.

Maybe create a single object of x length.

3 Likes