Section creation with python

I am trying to create sectionviews from a list of walls using Python, but when run (without errors) nothing is created in Revit?

The Python code:
import clr

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

clr.AddReference(‘RevitServices’)
import RevitServices

from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

from RevitServices.Transactions import TransactionManager

clr.AddReference(“RevitAPI”)
from Autodesk.Revit.DB import *

wall = list(UnwrapElement(IN[0]))
secTypeName = IN[1]

idx =
sectionLst =
lvlName =
test =

#Finds the BuildingName in the first part of the filename
lstSplit =
bygn =
try:
rvt_file_name = doc.Title
except:
rvt_file_name = “Unkown” # file may not be saved
lstSplit = rvt_file_name.Split(“_”)
bygn = lstSplit[0].ToString()

value =

viewFamilyTypes = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
vftLst =
for vf in viewFamilyTypes:
if vf.LookupParameter(“Type Name”).AsString() == secTypeName:
vftLst.append(vf.Id)

Determine section box

for i in wall:

idx.append(wall.index(i))

lc = i.Location
line = lc.Curve

p = line.GetEndPoint(0)
q = line.GetEndPoint(1)
v = q - p

bb = i.get_BoundingBox(None)
minZ = bb.Min.Z
maxZ = bb.Max.Z

w = v.GetLength()
h = maxZ - minZ
d = i.WallType.Width
offset = 100 / 304.8
offsetHeight = 200 / 304.8
offsetFront = 1 / 304.8	

min = XYZ(-0.5*w - offset, - offset, - offsetFront - 0.5*d)
max = XYZ(0.5*w + offset, h + offsetHeight, offsetFront + 0.5*d)

midpoint = p + 0.5*v
walldir = v.Normalize()
up = XYZ.BasisZ
viewdir = walldir.CrossProduct(up)

t = Transform.Identity
t.Origin = midpoint
t.BasisX = walldir
t.BasisY = up
t.BasisZ = viewdir

sectionBox = BoundingBoxXYZ()
sectionBox.Transform = t
sectionBox.Min = min
sectionBox.Max = max

#Base Constraint value
DB = i.GetParameters("Base Constraint")
for y in DB:
	value = y.AsValueString()



TransactionManager.Instance.EnsureInTransaction(doc)


newSection = ViewSection.CreateSection(doc, vftLst[0], sectionBox)

newParameterValue = newSection.LookupParameter('View Name')
newParameterValue.Set(bygn +"_"+ value +"_"+ wall.index(i).ToString())

sectionLst.append(newSection)
test = idx[-1] +1	

TransactionManager.Instance.TransactionTaskDone()

OUT = test.ToString() + " sectionviews have been created", sectionLst

Can you re-paste your code and make sure the whole thing gets formatted this time? That’ll make it much easier to read.

According to the output, you created all those new sections. Have you confirmed they aren’t in the project? Have you searched for them in the browser? Confirmed sections are visible and within the scope of your views?

I don’t know why it formats it wierd, so i tried with a screenshot

I have tried both making a list of all sections in Revit and looked through all views available in the file, but they arn’t there

If it helps, when i try to run the script and have a view list scheduel open in Revit i can see the section views being created in a flash then they are gone

Press this button before pasting your code.

image

It should look like this

It will preserve tabs and other formatting things which we can’t recreate on our side unless we rewrite your code or from screenshot.

1 Like

Thanks, here i have it formattet

Code:

import clr

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

clr.AddReference('RevitServices')
import RevitServices

from RevitServices.Persistence import DocumentManager
doc =  DocumentManager.Instance.CurrentDBDocument

from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

wall = list(UnwrapElement(IN[0]))
secTypeName = IN[1]

idx = []
sectionLst = []
sectionData = []
lvlName = []
test = []

#Finds the BuildingName in the first part of the filename
lstSplit = []
bygn = []
try:
    rvt_file_name = doc.Title
except:
    rvt_file_name = "Unkown" # file may not be saved
lstSplit = rvt_file_name.Split("_")
bygn = lstSplit[0].ToString()

value = []

#Collect ViewFamilyTypes
viewFamilyTypes = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
vftLst = []
for vf in viewFamilyTypes:
	if vf.LookupParameter("Type Name").AsString() == secTypeName:
		vftLst.append(vf.Id)

#Determine section box
for index, i in enumerate(wall):
	idx.append(index)
	
	lc = i.Location
	line = lc.Curve
	
	p = line.GetEndPoint(0)
	q = line.GetEndPoint(1)
	v = q - p
	
	bb = i.get_BoundingBox(None)
	minZ = bb.Min.Z
	maxZ = bb.Max.Z
	
	w = v.GetLength()
	h = maxZ - minZ
	d = i.WallType.Width
	offset = 100 / 304.8
	offsetHeight = 200 / 304.8
	offsetFront = 1 / 304.8	
	
	min = XYZ(-0.5*w - offset, - offset, - offsetFront - 0.5*d)
	max = XYZ(0.5*w + offset, h + offsetHeight, offsetFront + 0.5*d)
	
	midpoint = p + 0.5*v
	walldir = v.Normalize()
	up = XYZ.BasisZ
	viewdir = walldir.CrossProduct(up)
	
	t = Transform.Identity
	t.Origin = midpoint
	t.BasisX = walldir
	t.BasisY = up
	t.BasisZ = viewdir
	
	sectionBox = BoundingBoxXYZ()
	sectionBox.Transform = t
	sectionBox.Min = min
	sectionBox.Max = max
	
	#Base Constraint value
	DB = i.GetParameters("Base Constraint")
	for y in DB:
		value = y.AsValueString()

	#Create data list
	sectionData.append((sectionBox, bygn, value, index))

#Start transaction
TransactionManager.Instance.EnsureInTransaction(doc)

#Create sections for transaction
for data in sectionData:
	sectionBox, bygn, value, wallIndex = data
	
	#Create new section
	newSection = ViewSection.CreateSection(doc, vftLst[0], sectionBox)
	
	if not newSection:
		raise Exception("Section couldn't be created")
	
	#Set View Name
	newParameterValue = newSection.LookupParameter("View Name")
	newParameterValue.Set(bygn + "_" + value + "_" + wallIndex.ToString())
	
	sectionLst.append(newSection)
	test = idx[-1] + 1
	
#End transaction
TransactionManager.Instance.TransactionTaskDone()

OUT = test.ToString() + " sectionviews have been created", sectionLst
1 Like

Look into Archilab’s solution code also.

1 Like

Your code works for me. Are you sure you don’t get any Revit errors after running and the elements don’t exist? Can you select any of the sections by Id?

I have not been able to find the solution on my end, but when i ran it on the same project on a different computer it worked just fine, so my guess is i somehow have tainted the model through testing.