How to get Vector of Column

I add 2 properties (as key)

WidthColumn and LengthColumn I think you will need it :wink:

import clr 
import sys 
import System 
# Add references 
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

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

# Add references to 'RevitNodes' and 'RevitServices' for Revit-specific functionality
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

def get_DatasColumns_from_CAD(importinstance, lst_layerName=[]):
    datas = []  # Initialize an empty list to store data
    # Define vectors for X-axis and Z-axis
    vect_X = DS.Vector.XAxis()
    vect_axis = DS.Vector.ZAxis()
    # Create an Options object for importing geometry
    opt = Options()
    # Get the geometry of the import instance
    geoSet = importinstance.get_Geometry(opt)
    # Iterate through the geometry set
    for geo in geoSet:
        if isinstance(geo, GeometryInstance):
            for g in geo.GetInstanceGeometry():
                if isinstance(g, DB.PolyLine):
                    # Get the graphics style and name of the current polyline
                    gStyle = doc.GetElement(g.GraphicsStyleId)
                    gstyleName = gStyle.GraphicsStyleCategory.Name
                    # Check if the layer name is in the specified list
                    if gstyleName in lst_layerName:
                        # Create a DesignScript polygon from the polyline
                        ds_polygon = DS.Polygon.ByPoints([p.ToPoint() for p in g.GetCoordinates()])
                        # Explode the polygon into a list of DesignScript curves
                        lst_ds_curves = [curve for curve in ds_polygon.Explode() if curve.Length > 0.001]
                        # sort curves by length
                        sort_curves = sorted(lst_ds_curves, key=lambda x: x.Length)
                        # Find the shortest and longest curve in the list
                        sortest_curve, longest_curve  = sort_curves[0], sort_curves[-1]
                        # Create a DesignScript surface from the perimeter points of the polyline
                        ds_surface = DS.Surface.ByPerimeterPoints([p.ToPoint() for p in list(g.GetCoordinates())[:3]])
                        # Calculate the center point of the surface
                        pt_ds_center = ds_surface.PointAtParameter(0.5, 0.5)
                        # Get the direction of the longest curve
                        vector_direction = longest_curve.Direction
                        # Determine the clockwise factor based on the cross product with the X-axis
                        factor_clockwise = 1 if vector_direction.Cross(vect_X).Z < 0 else -1
                        # Calculate the angle from the X-axis
                        angle_from_BasisX = vector_direction.AngleWithVector(vect_X) * factor_clockwise
                        
                        # Append the data to the list
                        datas.append({
                            "Layer": gstyleName,
                            "Geometry": ds_polygon,
                            "Center": pt_ds_center,
                            "Direction": vector_direction,
                            "Angle": angle_from_BasisX,
                            "WidthColumn" : sortest_curve.Length,
                            "LengthColumn" : longest_curve.Length,
                            "longest_curve": longest_curve,
                        })
                        
    return datas

# Unwrap the import instance and layer name list from the Revit input
importinstance = UnwrapElement(IN[0])
lst_layerName = IN[1]

# Call the function and assign the result to the output variable
OUT = get_DatasColumns_from_CAD(importinstance, lst_layerName)
4 Likes