Memory error

I was creating to split the duct where there is clash. Its almost working perfectly but some time its throwing memory error as shown below. please help me to identify the issue.
image

code is below:

import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 
import math
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

from System.Collections.Generic import List
from System.Collections.Generic import Dictionary
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

Felem = UnwrapElement(IN[0])
Selem = UnwrapElement(IN[1])
from Autodesk.Revit.UI.Selection import *

#element angle property

def angle(pt1, pt2):
	y1 = pt1.Y
	y2 = pt2.Y
	a = pt1.DistanceTo(pt2)
	o = y2 - y1
	sinT = o / a
	ang = math.asin(sinT)
	return ang
#end element angle property	

#selecting first element and retreiving values
ref1 = uidoc.Selection.PickObject(ObjectType.Element)
eid = ref1.ElementId
elem1 = doc.GetElement(eid)
elem1curve= elem1.Location.Curve

elem1sp1 = elem1curve.GetEndPoint(0)
elem1sp2 = elem1curve.GetEndPoint(1)

# End selecting first element and retreiving values

# Selecting second element and retreiving values

ref2 = uidoc.Selection.PickObject(ObjectType.Element)
eid2 = ref2.ElementId
elem2 = doc.GetElement(eid2)
elem2curve= elem2.Location.Curve

elem2sp1 = elem2curve.GetEndPoint(0)
elem2sp2 = elem2curve.GetEndPoint(1)

elem2Width = elem2.GetParameters("Main Primary Width")[0].AsDouble()
elem2Depth = elem2.GetParameters("Main Primary Depth")[0].AsDouble()
elem2bottom = elem2.GetParameters("Bottom")[0].AsDouble()
elem2top = elem2.GetParameters("Top")[0].AsDouble()

# End selecting first element and retreiving values

# Find intersection and  split point
elem1bb = elem1.BoundingBox[doc.ActiveView]
elem1outline = Outline(elem1bb.Min, elem1bb.Max)
elem2bb = elem2.BoundingBox[doc.ActiveView]
elem2outline = Outline(elem2bb.Min, elem2bb.Max)
outlineintersect = elem1outline.Intersects(elem2outline, 0).ToString()
if outlineintersect == "True" :
	temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
	temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
	templine = Line.CreateBound(temppt1 , temppt2)
	results = clr.Reference[IntersectionResultArray]()
	result = elem1curve.Intersect(templine,results)
	iResult = IntersectionResult()
	intersectionpoint = results.get_Item(0).XYZPoint
	newcurve1 = Line.CreateBound(intersectionpoint, elem1sp2)
	newpoint1 = newcurve1.GetEndParameter(0) + elem2Width
	tof = newcurve1.IsInside(newpoint1)
	normParam1 = newcurve1.ComputeNormalizedParameter(newpoint1)
	evaluatedPoint1 = newcurve1.Evaluate(normParam1,tof)
	TransactionManager.Instance.EnsureInTransaction(doc)
	newductid1 = elem1.SplitStraight(evaluatedPoint1)
	TransactionManager.Instance.TransactionTaskDone()
	elem1a = doc.GetElement(newductid1)
	elem1List = [elem1 , elem1a]
	if len (elem1List) > 1:
		for elem in elem1List:
			elem1curve= elem.Location.Curve
			elem1sp1 = elem1curve.GetEndPoint(0)
			elem1sp2 = elem1curve.GetEndPoint(1)
			elem1bb = elem.BoundingBox[doc.ActiveView]
			elem1outline = Outline(elem1bb.Min, elem1bb.Max)
			outlineintersect = elem1outline.Intersects(elem2outline, 0).ToString()
			if outlineintersect == "True" :
					temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
					temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
					templine = Line.CreateBound(temppt1 , temppt2)
					results = clr.Reference[IntersectionResultArray]()
					result1 = elem1curve.Intersect(templine,results)
					iResult1 = IntersectionResult()
					intersectionpoint = results.get_Item(0).XYZPoint
					newcurve1 = Line.CreateBound(intersectionpoint, elem1sp1)
					newpoint = newcurve1.GetEndParameter(0) + elem2Width
					tof = newcurve1.IsInside(newpoint)
					normParam = newcurve1.ComputeNormalizedParameter(newpoint1)
					evaluatedPoint = newcurve1.Evaluate(normParam , tof)
					TransactionManager.Instance.EnsureInTransaction(doc)
					elem.SplitStraight(evaluatedPoint)
					TransactionManager.Instance.TransactionTaskDone()
					newduct = elem					
			comment = "Found list"
	else:
		newduct = "No clash - Duct not splitted"
		
else:
	newduct = "No clash - Duct not splitted"
#End Find intersection and  split point
newElevation = float(elem2top) + 0.083
newduct.LookupParameter("Bottom").Set(newElevation)
OUT = newduct,float(newElevation),elem2top
1 Like

@SHIBUJOSE ,

just for better reading

import clr
import sys
sys.path.append(‘C:\Program Files (x86)\IronPython 2.7\Lib’)
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import math
clr.AddReference(“RevitAPI”)
clr.AddReference(“RevitAPIUI”)

import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

from System.Collections.Generic import List
from System.Collections.Generic import Dictionary
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument

Felem = UnwrapElement(IN[0])
Selem = UnwrapElement(IN[1])

from Autodesk.Revit.UI.Selection import *

#element angle property

def angle(pt1, pt2):
	y1 = pt1.Y
	y2 = pt2.Y
	a = pt1.DistanceTo(pt2)
	o = y2 - y1
	sinT = o / a
	ang = math.asin(sinT)
	return ang
#end element angle property

#selecting first element and retreiving values
ref1 = uidoc.Selection.PickObject(ObjectType.Element)
eid = ref1.ElementId
elem1 = doc.GetElement(eid)
elem1curve= elem1.Location.Curve

elem1sp1 = elem1curve.GetEndPoint(0)
elem1sp2 = elem1curve.GetEndPoint(1)

#End selecting first element and retreiving values
#Selecting second element and retreiving values

ref2 = uidoc.Selection.PickObject(ObjectType.Element)
eid2 = ref2.ElementId
elem2 = doc.GetElement(eid2)
elem2curve= elem2.Location.Curve

elem2sp1 = elem2curve.GetEndPoint(0)
elem2sp2 = elem2curve.GetEndPoint(1)

elem2Width = elem2.GetParameters(“Main Primary Width”)[0].AsDouble()
elem2Depth = elem2.GetParameters(“Main Primary Depth”)[0].AsDouble()
elem2bottom = elem2.GetParameters(“Bottom”)[0].AsDouble()
elem2top = elem2.GetParameters(“Top”)[0].AsDouble()

#End selecting first element and retreiving values
#Find intersection and split point

elem1bb = elem1.BoundingBox[doc.ActiveView]
elem1outline = Outline(elem1bb.Min, elem1bb.Max)
elem2bb = elem2.BoundingBox[doc.ActiveView]
elem2outline = Outline(elem2bb.Min, elem2bb.Max)
outlineintersect = elem1outline.Intersects(elem2outline, 0).ToString()

if outlineintersect == “True” :
	temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
	temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
	templine = Line.CreateBound(temppt1 , temppt2)
	results = clr.ReferenceIntersectionResultArray
	result = elem1curve.Intersect(templine,results)
	iResult = IntersectionResult()
	intersectionpoint = results.get_Item(0).XYZPoint
	newcurve1 = Line.CreateBound(intersectionpoint, elem1sp2)
	newpoint1 = newcurve1.GetEndParameter(0) + elem2Width
	tof = newcurve1.IsInside(newpoint1)
	normParam1 = newcurve1.ComputeNormalizedParameter(newpoint1)
	evaluatedPoint1 = newcurve1.Evaluate(normParam1,tof)
	TransactionManager.Instance.EnsureInTransaction(doc)
	newductid1 = elem1.SplitStraight(evaluatedPoint1)
	TransactionManager.Instance.TransactionTaskDone()

elem1a = doc.GetElement(newductid1)
elem1List = [elem1 , elem1a]

if len (elem1List) > 1:
	for elem in elem1List:
		elem1curve= elem.Location.Curve
		elem1sp1 = elem1curve.GetEndPoint(0)
		elem1sp2 = elem1curve.GetEndPoint(1)
		elem1bb = elem.BoundingBox[doc.ActiveView]
		elem1outline = Outline(elem1bb.Min, elem1bb.Max)
		outlineintersect = elem1outline.Intersects(elem2outline, 0).ToString()
	if outlineintersect == “True” :
		temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
		temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
		templine = Line.CreateBound(temppt1 , temppt2)
		results = clr.ReferenceIntersectionResultArray
		result1 = elem1curve.Intersect(templine,results)
		iResult1 = IntersectionResult()
		intersectionpoint = results.get_Item(0).XYZPoint
		newcurve1 = Line.CreateBound(intersectionpoint, elem1sp1)
		newpoint = newcurve1.GetEndParameter(0) + elem2Width
		tof = newcurve1.IsInside(newpoint)
		normParam = newcurve1.ComputeNormalizedParameter(newpoint1)
		evaluatedPoint = newcurve1.Evaluate(normParam , tof)
		TransactionManager.Instance.EnsureInTransaction(doc)
		elem.SplitStraight(evaluatedPoint)
		TransactionManager.Instance.TransactionTaskDone()
		newduct = elem
		comment = “Found list”
	else:
		newduct = “No clash - Duct not splitted”

	else:
		newduct = “No clash - Duct not splitted”
#End Find intersection and split point
newElevation = float(elem2top) + 0.083
newduct.LookupParameter(“Bottom”).Set(newElevation)

OUT = newduct,float(newElevation),elem2top

double else: does not work!

i would recomment post your code again </> #use this!

Hi,

If statement should be inside the for loop (if outlineintersect == “True” : ). You have put it outside the for loop. that is why you seeing two else.

image

1 Like

Hello,
try to release objects

			# previous code
			if outlineintersect == "True" :
					temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
					temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
					templine = Line.CreateBound(temppt1 , temppt2)
					results = clr.Reference[IntersectionResultArray]()
					result1 = elem1curve.Intersect(templine,results)
					iResult1 = IntersectionResult()
					intersectionpoint = results.get_Item(0).XYZPoint
					newcurve1 = Line.CreateBound(intersectionpoint, elem1sp1)
					newpoint = newcurve1.GetEndParameter(0) + elem2Width
					tof = newcurve1.IsInside(newpoint)
					normParam = newcurve1.ComputeNormalizedParameter(newpoint1)
					evaluatedPoint = newcurve1.Evaluate(normParam , tof)
					TransactionManager.Instance.EnsureInTransaction(doc)
					elem.SplitStraight(evaluatedPoint)
					TransactionManager.Instance.TransactionTaskDone()
					newduct = elem
					# Release Objects
					templine.Dispose()
					newcurve1.Dispose()
					evaluatedPoint.Dispose()
					results.Dispose()
			# rest of code
1 Like

unfortunately issue not resolved

A little code review
I could not test without a rvt file, so it’s possible there are some errors

import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 
import math
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

from System.Collections.Generic import List
from System.Collections.Generic import Dictionary
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

Felem = UnwrapElement(IN[0])
Selem = UnwrapElement(IN[1])
from Autodesk.Revit.UI.Selection import *

#element angle property

def angle(pt1, pt2):
	y1 = pt1.Y
	y2 = pt2.Y
	a = pt1.DistanceTo(pt2)
	o = y2 - y1
	sinT = o / a
	ang = math.asin(sinT)
	return ang
#end element angle property	

#selecting first element and retreiving values
ref1 = uidoc.Selection.PickObject(ObjectType.Element)
eid = ref1.ElementId
elem1 = doc.GetElement(eid)
elem1curve= elem1.Location.Curve

elem1sp1 = elem1curve.GetEndPoint(0)
elem1sp2 = elem1curve.GetEndPoint(1)

# End selecting first element and retreiving values

# Selecting second element and retreiving values

ref2 = uidoc.Selection.PickObject(ObjectType.Element)
eid2 = ref2.ElementId
elem2 = doc.GetElement(eid2)
elem2curve= elem2.Location.Curve

elem2sp1 = elem2curve.GetEndPoint(0)
elem2sp2 = elem2curve.GetEndPoint(1)

elem2Width = elem2.GetParameters("Main Primary Width")[0].AsDouble()
elem2Depth = elem2.GetParameters("Main Primary Depth")[0].AsDouble()
elem2bottom = elem2.GetParameters("Bottom")[0].AsDouble()
elem2top = elem2.GetParameters("Top")[0].AsDouble()

# End selecting first element and retreiving values

# Find intersection and  split point
elem1bb = elem1.BoundingBox[doc.ActiveView]
elem1outline = Outline(elem1bb.Min, elem1bb.Max)
elem2bb = elem2.BoundingBox[doc.ActiveView]
elem2outline = Outline(elem2bb.Min, elem2bb.Max)
TransactionManager.Instance.EnsureInTransaction(doc)
outlineintersect = elem1outline.Intersects(elem2outline, 0)
#if outlineintersect == "True" :
if outlineintersect:
	temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
	temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
	templine = Line.CreateBound(temppt1 , temppt2)
	resultsA = clr.Reference[IntersectionResultArray]()
	result = elem1curve.Intersect(templine,resultsA)
	if result1 == SetComparisonResult.Overlap: 
		#iResult = IntersectionResult()
		#intersectionpoint = resultsA.get_Item(0).XYZPoint
		intersectionpoint = resultsA.Value[0].XYZPoint
		newcurve1 = Line.CreateBound(intersectionpoint, elem1sp2)
		newpoint1 = newcurve1.GetEndParameter(0) + elem2Width
		tof = newcurve1.IsInside(newpoint1)
		normParam1 = newcurve1.ComputeNormalizedParameter(newpoint1)
		evaluatedPoint1 = newcurve1.Evaluate(normParam1,tof)
		#TransactionManager.Instance.EnsureInTransaction(doc)
		newductid1 = elem1.SplitStraight(evaluatedPoint1)
		#TransactionManager.Instance.TransactionTaskDone()
		# Regenerate doc
		doc.Regenerate()
		elem1a = doc.GetElement(newductid1)
		elem1List = [elem1 , elem1a]
		if len (elem1List) > 1:
			for elem in elem1List:
				elem1curve= elem.Location.Curve
				elem1sp1 = elem1curve.GetEndPoint(0)
				elem1sp2 = elem1curve.GetEndPoint(1)
				elem1bb = elem.BoundingBox[doc.ActiveView]
				elem1outline = Outline(elem1bb.Min, elem1bb.Max)
				outlineintersect = elem1outline.Intersects(elem2outline, 0)
				#if outlineintersect == "True" :
				if outlineintersect:
						temppt1 = XYZ(elem2sp1.X , elem2sp1.Y , elem1sp1.Z)
						temppt2 = XYZ(elem2sp2.X , elem2sp2.Y , elem1sp2.Z)
						templine = Line.CreateBound(temppt1 , temppt2)
						resultsB = clr.Reference[IntersectionResultArray]()
						result1 = elem1curve.Intersect(templine,resultsB)
						if result1 == SetComparisonResult.Overlap: 
							# iResult1 = IntersectionResult()
							#intersectionpoint = resultsB.get_Item(0).XYZPoint
							intersectionpoint =  resultsB.Value[0].XYZPoint
							newcurve1 = Line.CreateBound(intersectionpoint, elem1sp1)
							newpoint = newcurve1.GetEndParameter(0) + elem2Width
							tof = newcurve1.IsInside(newpoint)
							normParam = newcurve1.ComputeNormalizedParameter(newpoint1)
							evaluatedPoint = newcurve1.Evaluate(normParam , tof)
							#TransactionManager.Instance.EnsureInTransaction(doc)
							elem.SplitStraight(evaluatedPoint)
							doc.Regenerate()
							#TransactionManager.Instance.TransactionTaskDone()
							newduct = elem					
				comment = "Found list"
		else:
			newduct = "No clash - Duct not splitted"
	else:
		newduct = "No clash - Duct not splitted"
		
else:
	newduct = "No clash - Duct not splitted"
#End Find intersection and  split point
newElevation = float(elem2top) + 0.083
newduct.LookupParameter("Bottom").Set(newElevation)
TransactionManager.Instance.TransactionTaskDone()
OUT = newduct,float(newElevation),elem2top
1 Like