I need to use Rebar.CreateFromCurves method in Python but I am getting some errors. I dont know how to resolve. It will be helpful if somebody solve it – Developers

I’ll give the source code here

import clr

import sys

clr.AddReference(‘RevitAPI’)
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

clr.AddReference(‘RevitNodes’)
import Revit
from Revit.Elements import *

clr.ImportExtensions(Revit.GeometryConversion)

import System
from System.Collections.Generic import List,IList

clr.AddReference(‘DSCoreNodes’)
from DSCore.List import Flatten

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

clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

surface = IN[0]
sideOffsetDistance = IN[1]
topOffsetDistance = IN[2]
bottomOffsetDistance = IN[3]
offsetFromFace = IN[4]
rebarType = UnwrapElement(IN[5])
selectedElement = UnwrapElement(IN[6])
rebarHookType = UnwrapElement(IN[7])

dummyOffsetDistance = 0
u = 0.5;
v = u;

tempList = [Surface.PerimeterCurves(edge) for edge in surface]

edgePerimeterCurves = Flatten(tempList)

firstEdgeCurve = edgePerimeterCurves[0]
secondEdgeCurve = edgePerimeterCurves[1]
thirdEdgeCurve = edgePerimeterCurves[2]
fourthEdgeCurve = edgePerimeterCurves[3]

def getCurvePoint(curve, key) :
if isinstance(curve, Curve) and key == “sp” :
return curve.StartPoint
elif isinstance(curve, Curve) and key == “ep” :
return curve.EndPoint
else :
return None

startPointFirstEdgeCurve = getCurvePoint(firstEdgeCurve, “sp”)
endPointFirstEdgeCurve = getCurvePoint(firstEdgeCurve, “ep”)

startPointSecondEdgeCurve = getCurvePoint(secondEdgeCurve, “sp”)
endPointSecondEdgeCurve = getCurvePoint(secondEdgeCurve, “ep”)

startPointThirdEdgeCurve = getCurvePoint(thirdEdgeCurve, “sp”)
endPointThirdEdgeCurve = getCurvePoint(thirdEdgeCurve, “ep”)

startPointForthEdgeCurve = getCurvePoint(fourthEdgeCurve, “sp”)
endPointForthEdgeCurve = getCurvePoint(fourthEdgeCurve, “ep”)

def offsetPoint(startPoint, endPoint, offset) :

x1 = startPoint.X
y1 = startPoint.Y
z1 = startPoint.Z  
x2 = endPoint.X
y2 = endPoint.Y
z2 = endPoint.Z
    
# Direction Vector

dx = (x2 - x1)
dy = (y2 - y1)
dz = (z2 - z1)

""" directionVector = Vector.ByCoordinates(dx, dy, dz) """

# Magnitude

magnitudeOfDirectionVector = (dx*dx + dy*dy + dz*dz) ** 0.5

# Normalized Direction Vector

ndx = dx/magnitudeOfDirectionVector
ndy = dy/magnitudeOfDirectionVector
ndz = dz/magnitudeOfDirectionVector

""" normalizedDirectionVector = Vector.ByCoordinates(ndx, ndy, ndz) """

# Scalling the normal to the Side Offset Distance

sx = offset * ndx
sy = offset * ndy
sz = offset * ndz

""" scaledVectorPoint = Vector.ByCoordinates((sideOffsetDistance * ndx), (sideOffsetDistance * ndy), (sideOffsetDistance*ndz)) """

# Point at the given distance on the first edge

return Point.ByCoordinates(x1 + sx, y1 + sy, z1 + sz)

edgeOneOffsetPointOne = offsetPoint(startPointFirstEdgeCurve, endPointFirstEdgeCurve, sideOffsetDistance)
edgeOneOffsetPointTwo = offsetPoint(endPointFirstEdgeCurve, startPointFirstEdgeCurve, sideOffsetDistance)

edgeTwoOffsetPointOne = offsetPoint(startPointSecondEdgeCurve, endPointSecondEdgeCurve, topOffsetDistance)
edgeTwoOffsetPointTwo = offsetPoint(endPointSecondEdgeCurve, startPointSecondEdgeCurve, topOffsetDistance)

edgeThreeOffsetPointOne = offsetPoint(endPointThirdEdgeCurve, startPointThirdEdgeCurve, sideOffsetDistance)

edgeThreeOffsetPointTwo = offsetPoint(startPointThirdEdgeCurve, endPointThirdEdgeCurve, sideOffsetDistance)

edgeFourOffsetPointOne = offsetPoint(endPointForthEdgeCurve, startPointForthEdgeCurve, topOffsetDistance)
edgeFourOffsetPointTwo = offsetPoint(startPointForthEdgeCurve, endPointForthEdgeCurve, topOffsetDistance)

boundingPointOne = offsetPoint(edgeOneOffsetPointOne, edgeThreeOffsetPointOne, topOffsetDistance)

boundingPointTwo = offsetPoint(edgeOneOffsetPointTwo, edgeThreeOffsetPointTwo, topOffsetDistance)

boundingPointThree = offsetPoint(edgeThreeOffsetPointTwo, edgeOneOffsetPointTwo, bottomOffsetDistance)

boundingPointFour = offsetPoint(edgeThreeOffsetPointOne, edgeOneOffsetPointOne, bottomOffsetDistance)

boundingBox = Rectangle.ByCornerPoints(boundingPointOne, boundingPointTwo, boundingPointThree, boundingPointFour)

rectangleCurveOffset = Curve.Offset(boundingBox, dummyOffsetDistance)
geometryOne = Geometry.Explode(rectangleCurveOffset)

surfaceNormal = Surface.NormalAtParameter(surface[0], u, v)
surfaceNormalReversed = Vector.Reverse(surfaceNormal)
surfaceNormalReversedPoint = surfaceNormalReversed.AsPoint()
surfaceNormalRecersedXYZ = XYZ(surfaceNormalReversedPoint.X, surfaceNormalReversedPoint.Y, surfaceNormalReversedPoint.Z)

translatedGeometryRebarCurve = Geometry.Translate(geometryOne[0], surfaceNormal, -offsetFromFace)

translatedGeometryRebarCurveIList = ListCurve

translatedGeometryRebarCurveIList.Add(translatedGeometryRebarCurve.ToRevitType())

“””except Exception as ex :
dialog = TaskDialog(“Header”)
dialog.MainInstruction = “Exception In Translating Curve”
dialog.MainContent = ex.ToString()

dialog.CommonButtons = TaskDialogCommonButtons.Close;
dialog.DefaultButton = TaskDialogResult.Close; """

doc = DocumentManager.Instance.CurrentDBDocument

TransactionManager.Instance.EnsureInTransaction(doc)

try :
rebar = Rebar.CreateFromCurves(doc, RebarStyle.Standard, rebarType, rebarHookType, rebarHookType, selectedElement, surfaceNormalRecersedXYZ, translatedGeometryRebarCurveIList, RebarHookOrientation.Right, RebarHookOrientation.Right, True, TrueDynamo File)

except Exception as ex:
dialog = TaskDialog(“Header”)
dialog.MainInstruction = “Exception In CreateFromCurves”
dialog.MainContent = ex.ToString()

dialog.CommonButtons = TaskDialogCommonButtons.Close;
dialog.DefaultButton = TaskDialogResult.Close;

dialog.Show()

TransactionManager.Instance.TransactionTaskDone()

OUT = geometryOne # Nevermind the OUT

I also attached the dynamo file with this

Read more here: Source link