Python Create Rectangle

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

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

clr.AddReference(“RevitNodes”)
import Revit
from Revit.Elements import *

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#general
INPUT = UnwrapElement(IN[0])

#1: get midpoints from DetailCurve (RevitAPI)
MIDPOINT_RVT_API =

for i in INPUT:
point_RVT = i.GeometryCurve.Evaluate(0.5, False)
MIDPOINT_RVT_API.append(point_RVT)

#2: get RevitPoints from RevitAPI Points
MIDPOINT_RVT =

for y in MIDPOINT_RVT_API:
point = y.ToPoint()
MIDPOINT_RVT.append(point)

#3: Translate
POINT_TL =

for z in MIDPOINT_RVT:
#P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
#P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
#P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
#P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
#P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
#P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
#P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
#P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
P1 = z.Translate(-800, 800, 0)
P2 = z.Translate(800, 800, 0)
P3 = z.Translate(800, -800, 0)
P4 = z.Translate(-800, -800, 0)
RECTANGLE = Rectangle.ByCornerPoints(P1, P2, P3, P4)

POINT_TL.append(P1)

OUT = POINT_TL

I get a AttributeError: about the Rectangle.ByCornerPoints.
How to fix it?

The order and sequence of the points matters. I think you want to start with point 3 not point 1, and then go to 2, 1, 4. That said the code is a bit hard to read with the lack of formatting. Can you paste with preformatted text (the </> icons in the gear menu) so that we maintain offsets and such? Also delete the extra point creation lines which are commented out to prevent making them look like you’ve created them repeatedly.

Personally I prefer using the Rectangle.ByPlaneWidthLength for this type of task as it is more reliable and more memory efficient (you don’t have to make 4 points from 12 doubles and keep them in memory, just one plane from 6 doubles).

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

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

clr.AddReference(“RevitNodes”)
import Revit
from Revit.Elements import *

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#general
INPUT = UnwrapElement(IN[0])

#1: get midpoints from DetailCurve (RevitAPI)
MIDPOINT_RVT_API =

for i in INPUT:
point_RVT = i.GeometryCurve.Evaluate(0.5, False)
MIDPOINT_RVT_API.append(point_RVT)

#2: get RevitPoints from RevitAPI Points
MIDPOINT_RVT =

for y in MIDPOINT_RVT_API:
point = y.ToPoint()
MIDPOINT_RVT.append(point)

#3: Translate
POINT_TL =

for z in MIDPOINT_RVT:
#P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
#P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
#P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
#P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
#P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
#P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
#P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
#P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
P1 = z.Translate(-800, 800, 0)
P2 = z.Translate(800, 800, 0)
P3 = z.Translate(800, -800, 0)
P4 = z.Translate(-800, -800, 0)
RECTANGLE = Rectangle.ByCornerPoints(P1, P2, P3, P4)

POINT_TL.append(P1)

OUT = POINT_TL

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

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

clr.AddReference(“RevitNodes”)
import Revit
from Revit.Elements import *

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#general
INPUT = UnwrapElement(IN[0])

#1: get midpoints from DetailCurve (RevitAPI)
MIDPOINT_RVT_API =

for i in INPUT:
Preformatted textpoint_RVT = i.GeometryCurve.Evaluate(0.5, False)
Preformatted textMIDPOINT_RVT_API.append(point_RVT)

#2: get RevitPoints from RevitAPI Points
MIDPOINT_RVT =

for y in MIDPOINT_RVT_API:
Preformatted text point = y.ToPoint()
Preformatted text MIDPOINT_RVT.append(point)

#3: Translate
POINT_TL =

for z in MIDPOINT_RVT:
Preformatted text P1 = z.Translate(-800, 800, 0)
Preformatted text P2 = z.Translate(800, 800, 0)
Preformatted text P3 = z.Translate(800, -800, 0)
Preformatted text P4 = z.Translate(-800, -800, 0)
Preformatted text RECTANGLE = Rectangle.ByCornerPoints(P3, P2, P1, P4)

Preformatted text POINT_TL.append(P1)

OUT = POINT_TL

It did not work with sequence 3-2-1-4.

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

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

clr.AddReference(“RevitNodes”)
import Revit
from Revit.Elements import *

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#general
INPUT = UnwrapElement(IN[0])

#1: get midpoints from DetailCurve (RevitAPI)
MIDPOINT_RVT_API =

for i in INPUT:
point_RVT = i.GeometryCurve.Evaluate(0.5, False)
MIDPOINT_RVT_API.append(point_RVT)

#2: get RevitPoints from RevitAPI Points
MIDPOINT_RVT =

for y in MIDPOINT_RVT_API:
point = y.ToPoint()
MIDPOINT_RVT.append(point)

#3: Translate
POINT_TL =

for z in MIDPOINT_RVT:
#P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
#P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
#P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
#P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
#P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
#P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
#P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
#P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
P1 = z.Translate(-800, 800, 0)
P2 = z.Translate(800, 800, 0)
P3 = z.Translate(800, -800, 0)
P4 = z.Translate(-800, -800, 0)
RECTANGLE = Rectangle.ByCornerPoints(P1, P2, P3, P4)

POINT_TL.append(P1)


OUT = POINT_TL

What is the warning message?

1 Like

Hi, you forgot a line for conversions and Using Dynamo’s Geometry.translate, you are missing the Geometry argument

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion) # Line is Missing
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

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

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

elt=UnwrapElement(IN[0])

Point_Revit=elt.Location.Curve.Evaluate(0.5,False)
Point_Dynamo=Point_Revit.ToPoint()
P_new=Geometry.Translate(Point_Dynamo,0.8,0,0)
OUT = Point_Revit,Point_Dynamo,P_new

cordially
christian.stan

image
This is the error message.

import sys
import clr
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 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

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

#clr.AddReference('RevitNodes')
#import Revit
#from Revit.Elements import *

#clr.AddReference('RevitServices')
#import RevitServices
#from RevitServices.Persistence import DocumentManager
#from RevitServices.Transactions import TransactionManager

#general
INPUT = UnwrapElement(IN[0])

#1: get midpoints from DetailCurve (RevitAPI)
MIDPOINT_RVT_API = []

for i in INPUT:
    point_RVT = i.GeometryCurve.Evaluate(0.5, False)
    MIDPOINT_RVT_API.append(point_RVT)

#2: get RevitPoints from RevitAPI Points
MIDPOINT_RVT = []

for y in MIDPOINT_RVT_API:
    point = y.ToPoint()
    MIDPOINT_RVT.append(point)

#3: Translate Points By 800 in every direction
POINT_TL = []

for z in MIDPOINT_RVT:
    #P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
    #P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
    #P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
    #P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z)
    #P1 = Point.ByCoordinates(z.X - 800, z.Y + 800, z.Z)
    #P2 = Point.ByCoordinates(z.X + 800, z.Y + 800, z.Z)
    #P3 = Point.ByCoordinates(z.X + 800, z.Y - 800, z.Z)
    #P4 = Point.ByCoordinates(z.X - 800, z.Y - 800, z.Z) 
    P1 = z.Translate(-800, 800, 0)
    P2 = z.Translate(800, 800, 0)
    P3 = z.Translate(800, -800, 0)
    P4 = z.Translate(-800, -800, 0)
    RECTANGLE = Rectangle.ByCornerPoints(P1, P2, P3, P4)
    POINT_TL.append(RECTANGLE)

OUT = POINT_TL

Looks like it might be namespace collision (meaning it’s loading rectangle from something other than the Dynamo geometry library) or that the method isn’t spelled correctly. Comment out all your code and try OUT = dir(Rectangle)

1 Like

You must have class conflict as mentioned by Mr. Jacob
if this helps

DS=Autodesk.DesignScript.Geometry

rec=DS.Rectangle.ByCornerPoints(P1,P2,P3,P4)

cordially
christian.stan

Thank you so much!

1 Like