Python script Path error

Hi All,

I have created a python script for duct support. But the problem is that if I change the family path location and after that run the script that time one error massage is coming regarding the family location. I am not understand why script is not taking the family from the changes location. I have attached error massage snap as well as whole script. Please help me.

image

# import libraries
import clr
import sys
import math
import System

clr.AddReference('RevitServices')
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
from RevitServices import *
from RevitServices.Persistence import DocumentManager
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
from Autodesk.Revit.DB.Mechanical import *
from Autodesk.Revit.DB.Plumbing import *
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *
from RevitServices.Transactions import TransactionManager

pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
family_path = 'G:\dynamo practice\hanger'

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference('System.Windows.Forms.DataVisualization')
from System.Windows import Forms
from System.Windows.Forms import Application, Form, Button, Panel, Label, ComboBox, DialogResult, ListBox, TextBox,OpenFileDialog
from System.Windows.Forms import Button, AnchorStyles, StatusBar, ListView, View, ColumnHeader, ListViewItem, SortOrder, FormBorderStyle,FolderBrowserDialog, DialogResult
from System.Drawing import Size, Point, Font, Color

import os
from System.IO import Directory, Path, SearchOption

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#dist = IN[1]
#od = dist
distfromEdge = IN[2]


def itof(inc):
	ft = (inc / 12.00)
	return ft
	
	
	
def findPath ():
	dialog = FolderBrowserDialog()
	s = dialog.ShowDialog()
	if s == DialogResult.OK:
		path = dialog.SelectedPath
	return path



#========================================Element angle property =======================================================



def angle(pt1, pt2):
	x1 = pt1.X
	x2 = pt2.X
	y1 = pt1.Y
	y2 = pt2.Y
	if round(x1, 2) == round(x2, 2):
		ang =  math.radians(90)
	elif round(y1, 2) == round(y2, 2):
		ang = math.radians(0)
	else:
		a = pt1.DistanceTo(pt2)
		o = y2 - y1
		sinT = o / a
		ang = math.asin(sinT)
	return ang


#========================================End Element angle property =====================================================	

#==================================   Retreiving Duct shapes   ======================================
def shape(el):
	cm = el.ConnectorManager.Connectors
	for i in cm:
		sh = i.Shape.ToString()
	return sh
#=============================    End Retreiving Duct shapes   ======================================

#======================================== Z axis  =======================================================================

def zaxis(obj):
	i_point = obj.Location.Point
	s_point = XYZ(i_point.X,i_point.Y,i_point.Z+1)
	za = Line.CreateBound(i_point,s_point)
	return za
	
#======================================      Collect hanger family        =================================================

def findFname (duct):
	cr1 = FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements()
	if shape(duct) == "Rectangular":
		for i in cr1:
			if i.FamilyName.ToString() == "sh_rec_hanger_type-1":
				familyName = i
	elif shape(duct) == "Round":
		for i in cr1:
			if i.FamilyName.ToString() == "sh_rnd_hanger_type-1":
				familyName = i
	else:
		pass
	return familyName
		
#======================================  End Collect hanger family        =================================================


	
#======================================== SELECT DUCT    ===================================================================

selIds = uidoc.Selection.GetElementIds()
ducts = []
dName = []
if selIds:
	idList = []
	for id in selIds:
		dt = doc.GetElement(id)
		if dt.Category.Name.ToString() == "Ducts" :
			ducts.append(dt)
		else:
			pass
else:
	ducts = []

rectangleDuctList = []
roundDuctList = []
#==========================================================================================================================
#if len(ducts) != 0:
hangerList = ["sh_rec_hanger_type-1","sh_rnd_hanger_type-1"]
familyFilterName = []
lenhangerList = len(hangerList)
familyFilter = FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements()
for fn in familyFilter:
	familyFilterName.append(fn.FamilyName)	
		
for hangerName in hangerList:
	if hangerName not in familyFilterName:
		family_path = findPath()
		break
if family_path:
	for ha in hangerList:
		fname = family_path + "\\" + ha + ".rfa"
		t = Transaction(doc, "place hanger")
		t.Start()
		doc.LoadFamily(fname)
		doc.Regenerate()
		t.Commit()
else:
	pass
familyCollector = FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements()
for j in familyCollector:
	if j.FamilyName in hangerList:
		t = Transaction(doc, "make active")
		t.Start()
		if j.IsActive == False:
			j.Activate()
			doc.Regenerate()
				
		else:
			pass
		t.Commit()
	else:
		pass
#==========================================================================================================================

dcidList = []
if len(ducts) != 0:
	t = Transaction(doc, "place hanger")
	ductLengthList = []
	raList = []
	countList = []
	evaluatedPointList = []
	for du in ducts:
		ductLength = du.GetParameters("Length")[0].AsDouble()
		#dist = IN[1]
		od = IN[1]
		if ductLength <= IN[2] * 2 :
			dist = ductLength / 2
			ra = 1
		else:
			dist = IN[2]
			realductLength = ductLength - (IN[2] * 2)
			
			ra = int((realductLength / IN[1]) +2)		
			#ra = int((ductLength / IN[1]) +1)
	
		count = 0
		while count < ra:
			if count == 1:
				dist = ductLength - IN[2]
			elif count == 2:
				dist = IN[2] + od
			else :
				dist = dist
			line1 = du.Location.Curve	
			st_pt1 = line1.GetEndPoint(0)
			end_pt1 = line1.GetEndPoint(1)
			level = du.ReferenceLevel
			ang = angle(st_pt1, end_pt1)
			ductAngle = ang + math.radians(90)
			st_ppt1 = line1.GetEndParameter(0)
			end_ppt1 = line1.GetEndParameter(1)
			paramCalc = st_ppt1 + dist
			
			tof = line1.IsInside(paramCalc)
			normParam =line1.ComputeNormalizedParameter(paramCalc)
			evaluatedPoint = line1.Evaluate(normParam,tof)
			
			s_point = XYZ(evaluatedPoint.X,evaluatedPoint.Y,evaluatedPoint.Z+2)
			
			axis = Line.CreateBound(evaluatedPoint,s_point)
			o = du.GetParameters("Bottom Elevation")[0].AsDouble()
			if shape(du) == "Rectangular":
				w = du.GetParameters("Width")[0].AsDouble()
				fname = findFname(du)
				#familyName = "sh_rec_hanger_type-1"
				ductShape = "duct width"
			elif shape(du) == "Round":
				w = du.GetParameters("Diameter")[0].AsDouble()
				fname = findFname(du)
				#familyName = "sh_rnd_hanger_type-1"
				ductShape = "Diameter"
			else:
				pass
			t.Start()
			dc = doc.Create.NewFamilyInstance(evaluatedPoint,fname,level,StructuralType.NonStructural)
			dcid = dc.Id
			ElementTransformUtils.RotateElement(doc,dcid,axis,ductAngle)			
			#dc.LookupParameter("duct width").Set(w)
			dc.LookupParameter("Offset").Set(o)	
			dc.LookupParameter(ductShape).Set(w)
			dc.LookupParameter("Offset").Set(o)
			t.Commit()
			count +=1
			dist +=  od
else:
	ducts = "Nothing selected"
		

#=================================== Retreiving linked models ===========================================================

OUT = ducts

sh_rec_hanger_type-1.rfa (340 KB) sh_rnd_hanger_type-1.rfa (364 KB)

is the family open, or blocked from reading\writing?

1 Like

Hello
@ranjit.chatterjee in addition to the remark of @FabioDeAgostini

at the line :
family_path = 'G:\dynamo practice\hanger'

try to add a raw string
family_path = r'G:\dynamo practice\hanger'

oh sure,
@c.poupin is right. You can also write

family_path = ‘G:\\dynamo practice\\hanger’

with double slashes

1 Like

Thank you so much. Now perfectly working.

Hi Guys…
I am getting error in line 242…
Can anyone share me Revit model with working script…
My email : hubligopal@gmail.com

Hello @hubligopal and welcome

your family must have this parameters :

  • “duct width”
  • “Diameter”

Note
it’s better start a new topic, with screenshot of error (traceback) and the content of Python Script rather than dig up an old

Thank you for your reply
Please find the error

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 242, in
AttributeError: ‘NoneType’ object has no attribute ‘Set’

Because your familyType (family to be placed) in your model has no parameter “duct width” or “Diameter” parameter, the script need to be adapted to your project

Hi,

There is parameter " duct width"

Please find the link along with python and letme know.
Thanks for your support.

I am using same Python script stated above

Please read my previous post

Create a new topic with your script and your model, your problem is not related with this topic (“Python script Path error”)

I close this one

1 Like