I got this error what does it mean? Exception: A managed exception was thrown by Revit or by one of its external applications

I try to create stirrup using

Rebar.CreateFromCurvesAndShape(Document, RebarShape, RebarBarType, RebarHookType (Start Hook), RebarHookType (End Hook), Element, XYZ (norm), IList<Curves>, RebarHookOrientation (Start), RebarHookOrientation (End))

but I got this error Exception: A managed exception was thrown by Revit or by one of its external applications. What is the cause for this error because I already try to check each of my input to these function it work just fine but when used together with these function it shown this error.

This is my full code

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

# Import Revit Services
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import Curve as RCurve
from Autodesk.Revit.DB.Structure import *

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

clr.AddReference("RevitNodes")
import Revit
from Revit.Elements import *
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

import System
import System.Array
from System.Collections.Generic import *

#get the document
doc = DocumentManager.Instance.CurrentDBDocument

#Function
def str(x1, y1, b, h, cover, shape, type, host):
    nx1 = x1 - (b / 2) + cover
    nx2 = x1 + (b / 2) - cover
    ny1 = y1 - (h / 2) + cover
    ny2 = y1 + (h / 2) - cover
    nz1 = 0
    count = 0
    point_list = []
    outLine = []
    while (count < 1):
	    #Create Point
	    pt1 = XYZ(nx1, ny1, nz1).ToPoint()
	    pt2 = XYZ(nx2, ny1, nz1).ToPoint()
	    pt3 = XYZ(nx2, ny2, nz1).ToPoint()
	    pt4 = XYZ(nx1, ny2, nz1).ToPoint()
	    pt6 = XYZ(0, 0, 0)
	    #Create Point List
	    point_list.append(pt1)
	    point_list.append(pt2)
	    point_list.append(pt3)
	    point_list.append(pt4)
	    #Use Point List Create PolyCurves
	    polycurve1 = PolyCurve.ByPoints(point_list, True)
	    outLine.append(polycurve1)
	    #Make polycurve in form of curve
	    curve1 = PolyCurve.Curves(polycurve1)
	    #Get rebar hook from rebar shape
	    shook = RebarShape.GetDefaultHookAngle(shape, 0)
	    ehook = RebarShape.GetDefaultHookAngle(shape, 1)
	    #Get orient data from rebar shape
	    orients = RebarShape.GetDefaultHookOrientation(shape, 0)
	    oriente = RebarShape.GetDefaultHookOrientation(shape, 1)
	    #Somehow the result from GetDefaultHookAngle is result as 135, which is angle but I preferred to get ID. Therefore, add constant to equal value as ID
	    id1 = shook + 93441
	    id2 = ehook + 93441
	    #Assign int as element id
	    hookid1 = ElementId(id1)
	    hookid2 = ElementId(id2)
	    #Get rebar hook family by id
	    hooks = doc.GetElement(hookid1)
	    hooke = doc.GetElement(hookid2)
	    #UnwrapElement
	    uhooks = UnwrapElement(hooks)
	    uhooke = UnwrapElement(hooke)
	    #Start Transaction
	    TransactionManager.Instance.EnsureInTransaction(doc)
	    #Create Stirrup base on curves and shape
	    #Function : Rebar.CreateFromCurvesAndShape(Document, RebarShape, RebarBarType, RebarHookType (Start Hook), RebarHookType (End Hook), Element, XYZ (norm), IList<Curves>, RebarHookOrientation (Start), RebarHookOrientation (End))
	    stirrup1 = Rebar.CreateFromCurvesAndShape(doc, shape, type, uhooks, uhooke, host, pt6, curve1, orients, oriente)
	    stirrup_list.append(stirrup1)
	    #End
	    TransactionManager.Instance.TransactionTaskDone()
	    count = count + 1
    return stirrup_list

pointx = IN[0]
pointy = IN[1]
width = IN[2]
height = IN[3]
covering = IN[4]
rebarshape = UnwrapElement(IN[5])
rebartype = UnwrapElement(IN[6])
element = UnwrapElement(IN[7])

stirrup_list = []

result = str(pointx, pointy, width, height, covering, rebarshape, rebartype, element)

OUT = stirrup_list

Testing stirrup scripts_r9.dyn (21.2 KB)

On a quick glance, this appears to be the first issue you’ll need to fix:
while (count < 1):

and then you do this:

count = count + 1

Which means count is never less than 1, so your while loops forever, and you’re opening a never-ending number of transactions. Revit is most likely handling this however, since a never-ending loop will always crash your application rather than throw an exception.

@Thomas_Mahon I have set started with count = 0 and used while loop while (count < 1) and at the end I use count = count + 1 according to my understand it will goes only one loop per function call isnt it? because I have try to erase this line and nothing work for me

Hello @somphon
a few comments

1/ str() is a Python Built-in Function https://docs.python.org/2.7/library/functions.html#str it is better to rename your function

2/ UnwrapElement() is not necessary here

hooks = doc.GetElement(hookid1)
hooke = doc.GetElement(hookid2)
#UnwrapElement
uhooks = UnwrapElement(hooks)
uhooke = UnwrapElement(hooke)

Objects did not go through the Dynamo Wrapper, more information here https://github.com/DynamoDS/Dynamo/wiki/Python-0.6.3-to-0.7.x-Migration

3/ Beware of API mixing
you can’t use a DynamoProtoType object in a Revit API method, you must convert them
(curv1 can’t be use in Rebar.CreateFromCurvesAndShapestrong)

#Use Point List Create PolyCurves
polycurve1 = PolyCurve.ByPoints(point_list, True)
outLine.append(polycurve1)
#Make polycurve in form of curve
curve1 = PolyCurve.Curves(polycurve1)
#...//...
#...//...
#...//...
#Start Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
#Create Stirrup base on curves and shape
stirrup1 = Rebar.CreateFromCurvesAndShape(doc, shape, type, uhooks, uhooke, host, pt6, curve1, orients, oriente)

and you can replace
curve1 = PolyCurve.Curves(polycurve1)
by
curve1 = polycurve1.Curves()

4/ in according with the API doc you can replace

#Get rebar hook from rebar shape
shook = RebarShape.GetDefaultHookAngle(shape, 0)
ehook = RebarShape.GetDefaultHookAngle(shape, 1)
#Get orient data from rebar shape
orients = RebarShape.GetDefaultHookOrientation(shape, 0)
oriente = RebarShape.GetDefaultHookOrientation(shape, 1)

by

#Get rebar hook from rebar shape
shook = shape.GetDefaultHookAngle( 0)
ehook = shape.GetDefaultHookAngle( 1)
#Get orient data from rebar shape
orients = shape.GetDefaultHookOrientation(0)
oriente = shape.GetDefaultHookOrientation(1)
2 Likes

@c.poupin I have tried many change to my code but it still show the same error is it because of rebar hook type? because when I try manually create stirrup with rebar shape browser it didnt allow u to make a change in rebar hook type when create but function Rebar.CreateFromCurvesAndShape, we must input both rebar shape and rebar hook type is it conflict between input? Is there any way to set hook type both start and end to default? the default I mean reading rebar shape family hook type data so that it didnt conflict

The exact problem which is causing that exception is your PolyCurve, You can’t use a Dynamo Curve to Revit API the Dynamo has it own API which belongs to Design Script but Revit API was created in C#. In Iron Python You can use both the APIs but if you want to use a Revit API method, Your overloads or arguments have to belong to Revit API but you are trying to use the Design Script API.

use PolyLine of Revit API insterd of PolyCurve but if you don’t provide proper curve with closed loop, It will provide you null. So Better use Rebar.CreateFromCurves and set the shape Id separately.

stirrup1 = Rebar.CreateFromCurves(12 Overloads)

shapeParam = rebar.get_Parameter(BuiltInParameter.REBAR_SHAPE)
shape = shapeParam.Set(shape.Id)
curve1 = List[Curve]()

c1 = Line.CreateBound((Start Point Of Your Curve).ToXyz(), (End Point Of Your Curve).ToXyz())
# You can add as much as curve you want or try creating polyline

curve1.Add(c1)