Beispiel #1
0
def create_directshape(room_collection):
    if room_collection:
        document = room_collection[0].Document
        direct_shapes_col = FilteredElementCollector(document).OfClass(DirectShape).ToElements()
        if direct_shapes_col:
            for shape in direct_shapes_col:
                document.Delete(shape.Id)
        datashape = []
        bad_rooms = []
        for room in room_collection:
            try:
                calculator = SpatialElementGeometryCalculator(document)
                geometry_result = calculator.CalculateSpatialElementGeometry(room)
                room_solid = geometry_result.GetGeometry()
                geometry_objects = List[GeometryObject]()
                geometry_objects.Add(room_solid)
                room_name = room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()

                ds_type = get_directshape_type(room_name)
                ds = DirectShape.CreateElementInstance(document,
                                                       ds_type.Id,
                                                       ElementId(BuiltInCategory.OST_GenericModel),
                                                       room_name,
                                                       Transform.Identity)
                ds.SetTypeId(ds_type.Id)
                ds.SetShape(geometry_objects)
                datashape.append(ds)
            except Exception:
                bad_rooms.append(room)
        return datashape, bad_rooms
Beispiel #2
0
def create_directshape(room_collection, view_name):
    if room_collection:
        document = room_collection[0].Document
        delete_class_elements(DirectShape, document)
        delete_class_elements(DirectShapeType, document)
        datashape = []
        bad_rooms = []
        view = create_view(document, view_name)
        data_colors = get_color_from_colorscheme(document.ActiveView)
        if data_colors:
            for room in room_collection:
                try:
                    calculator = SpatialElementGeometryCalculator(document)
                    geometry_result = calculator.CalculateSpatialElementGeometry(room)
                    room_solid = geometry_result.GetGeometry()
                    geometry_objects = List[GeometryObject]()
                    geometry_objects.Add(room_solid)
                    room_name = room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()

                    ds_type = get_directshape_type(room_name)
                    ds = DirectShape.CreateElementInstance(document, ds_type.Id, \
                        ElementId(BuiltInCategory.OST_GenericModel), \
                        room_name, Transform.Identity)
                    ds.ApplicationId = uiapp.ActiveAddInId.ToString()
                    ds.ApplicationDataId = room.UniqueId
                    ds.SetTypeId(ds_type.Id)
                    ds.SetShape(geometry_objects)
                    view.SetElementOverrides(ds.Id, get_overidegraphics(data_colors[room_name]))
                    datashape.append(ds)
                except Exception:
                    bad_rooms.append(room)
            return room_collection
        else:
            return 'Перейдите на вид,\r\nгде применена цветовая схема'
Beispiel #3
0
def calculate_elements_in_room(room_collector, param_name):
    elements_in_rooms = []
    calculator = SpatialElementGeometryCalculator(doc)
    bad_rooms = []
    for room in room_collector:
        room_number = room.get_Parameter(
            BuiltInParameter.ROOM_NUMBER).AsString()
        room_elements = []
        try:
            cal_geometry = calculator.CalculateSpatialElementGeometry(room)
            solid = cal_geometry.GetGeometry()
            for face in solid.Faces:
                for sub in cal_geometry.GetBoundaryFaceInfo(face):
                    el = doc.GetElement(
                        sub.SpatialBoundaryElement.HostElementId)
                    set_value_to_parameter(el, param_name, room_number)
                    room_elements.append(el)
            elements_in_rooms.append(room_elements)
        except Exception:
            bad_rooms.append(room)
    return elements_in_rooms, bad_rooms
Beispiel #4
0
def convertRoomsToHBZones(rooms, boundaryLocation=1):
    """Convert rooms to honeybee zones.

    This script will only work from inside Dynamo nodes. for a similar script
    forRrevit check this link for more details:
    https://github.com/jeremytammik/SpatialElementGeometryCalculator/
        blob/master/SpatialElementGeometryCalculator/Command.cs
    """
    rooms = tuple(_getInternalElements(rooms))
    if not rooms:
        return []

    # create a spatial element calculator to calculate room data
    doc = rooms[0].Document
    options = SpatialElementBoundaryOptions()
    options.SpatialElementBoundaryLocation = getBoundaryLocation(
        boundaryLocation)
    calculator = SpatialElementGeometryCalculator(doc, options)

    opt = Options()
    _ids = []
    _zones = range(len(rooms))
    _surfaces = {}  # collect hbSurfaces so I can set adjucent surfaces
    for zoneCount, room in enumerate(rooms):
        # initiate zone based on room id
        _zone = HBZone(room.Id)

        # assert False, room.Id
        elementsData = calculator.CalculateSpatialElementGeometry(room)

        _roomGeo = elementsData.GetGeometry()
        # This can be the same as finish wall or in the center of the wall
        roomDSFaces = tuple(face.ToProtoType()[0] for face in _roomGeo.Faces)

        assert _roomGeo.Faces.Size == len(roomDSFaces), \
            "Number of rooms elements ({}) doesn't match number of faces ({}).\n" \
            "Make sure the Room is bounded.".format(_roomGeo.Faces.Size,
                                                    len(roomDSFaces))

        for count, face in enumerate(_roomGeo.Faces):
            # base face is useful to project the openings to room boundary
            _baseFace = roomDSFaces[count]

            # Revit is strange! if two roofs have a shared wall then it will be
            # duplicated! By checking the values inside the _collector
            _collector = []

            _boundaryFaces = elementsData.GetBoundaryFaceInfo(face)

            if len(_boundaryFaces) == 0:
                # initiate honeybee surface
                _ver = tuple(v.PointGeometry for v in _baseFace.Vertices)

                _hbSurface = HBSurface("%s:%s" % (_zone.name, createUUID()),
                                       _ver)

                _zone.addSurface(_hbSurface)
                continue

            for boundaryFace in _boundaryFaces:
                # boundaryFace is from type SpatialElementBoundarySubface
                # get the element (Wall, Roof, etc)
                boundaryElement = doc.GetElement(
                    boundaryFace.SpatialBoundaryElement.HostElementId)

                # initiate honeybee surface
                _ver = tuple(v.PointGeometry for v in _baseFace.Vertices)

                if _ver in _collector:
                    continue

                _hbSurface = HBSurface(
                    "%s:%s" % (_zone.name, boundaryElement.Id), _ver)

                _collector.append(_ver)

                if boundaryElement.Id not in _surfaces:
                    _surfaces[boundaryElement.Id] = _hbSurface
                else:
                    # TODO: set adjacent surface
                    pass

                # Take care of curtain wall systems
                # I'm not sure how this will work with custom Curtain Wall
                if getParameter(boundaryElement, 'Family') == 'Curtain Wall':
                    _elementIds, _coordinates = \
                        extractPanelsVertices(boundaryElement, _baseFace, opt)

                    for count, coordinate in enumerate(_coordinates):
                        if not coordinate:
                            print "{} has an opening with less than " \
                                "two coordinates. It has been removed!" \
                                .format(childElements[count].Id)
                            continue

                        # create honeybee surface - use element id as the name
                        _hbfenSurface = HBFenSurface(_elementIds[count],
                                                     coordinate)

                        # add fenestration surface to base honeybee surface
                        _hbSurface.addFenestrationSurface(_hbfenSurface)
                else:
                    # check if there is any child elements
                    childElements = getChildElemenets(boundaryElement)

                    if childElements:

                        _coordinates = exctractGlazingVertices(
                            boundaryElement, _baseFace, opt)

                        for count, coordinate in enumerate(_coordinates):

                            if not coordinate:
                                print "{} has an opening with less than " \
                                    "two coordinates. It has been removed!" \
                                    .format(childElements[count].Id)
                                continue

                            # create honeybee surface - use element id as the name
                            _hbfenSurface = HBFenSurface(
                                childElements[count].Id, coordinate)
                            # add fenestration surface to base honeybee surface
                            _hbSurface.addFenestrationSurface(_hbfenSurface)

                # add hbsurface to honeybee zone
                _zone.addSurface(_hbSurface)
                boundaryElement.Dispose()

        _zones[zoneCount] = _zone

        # clean up!
        elementsData.Dispose()

    calculator.Dispose()
    return _zones
Beispiel #5
0
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, SpatialElementGeometryCalculator, \
    UnitUtils, DisplayUnitType, StorageType, BuiltInParameter
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

room_col = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms)
calculator = SpatialElementGeometryCalculator(doc)

TransactionManager.Instance.EnsureInTransaction(doc)
report = []
for room in room_col:
    param = room.LookupParameter('Комментарии')
    room_name = room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()
    result = calculator.CalculateSpatialElementGeometry(room)
    room_solid = result.GetGeometry()
    for face in room_solid.Faces:
        faceNormal = face.FaceNormal
        if faceNormal.Z < 0:
            value = round(
                UnitUtils.ConvertFromInternalUnits(
                    face.Area, DisplayUnitType.DUT_SQUARE_METERS), 2)
            if param:
                if param.StorageType == StorageType.String:
                    param.Set(str(value))
Beispiel #6
0
def analyze_rooms(rooms, boundary_location=1):
    """Convert revit rooms to dynosaur rooms.

    This script will only work from inside Dynamo nodes. for a similar script
    forRrevit check this link for more details:
    https://github.com/jeremytammik/SpatialElementGeometryCalculator/
        blob/master/SpatialElementGeometryCalculator/Command.cs
    """
    # unwrap room elements
    # this is not the right way of logging in python and probably any other language
    log = []
    rooms = tuple(_get_internalelement_collector(rooms))
    if not rooms:
        return []

    # create a spatial element calculator to calculate room data
    doc = rooms[0].Document
    options = SpatialElementBoundaryOptions()
    options.SpatialElementBoundaryLocation = get_boundary_location(boundary_location)
    calculator = SpatialElementGeometryCalculator(doc, options)

    opt = Options()
    element_collector = []
    room_collector = range(len(rooms))
    # surface_collector = {}  # collect hbSurfaces so I can set adjucent surfaces
    for room_count, revit_room in enumerate(rooms):
        # initiate zone based on room id
        element_collector.append([])
        new_room = room.create_room(revit_room.Id)

        # calculate spatial data for revit room
        revit_room_spatial_data = calculator.CalculateSpatialElementGeometry(revit_room)
        # get the geometry of the room
        revit_room_geometry = revit_room_spatial_data.GetGeometry()
        # Cast revit room faces to dynamo geometry using ToProtoType method
        room_faces_dyn = \
            tuple(face.ToProtoType()[0] for face in revit_room_geometry.Faces)

        assert revit_room_geometry.Faces.Size == len(room_faces_dyn), \
            "Number of rooms elements ({}) doesn't match number of faces ({}).\n" \
            "Make sure the Room is bounded.".format(revit_room_geometry.Faces.Size,
                                                    len(room_faces_dyn))

        for count, face in enumerate(revit_room_geometry.Faces):
            # base face is useful to project the openings to room boundary
            base_face_dyn = room_faces_dyn[count]

            # Revit is strange! if two roofs have a shared wall then it will be
            # duplicated! By checking the values inside the _collector I try to avoid
            # that.
            _collector = []

            boundary_faces = revit_room_spatial_data.GetBoundaryFaceInfo(face)

            if len(boundary_faces) == 0:
                # There is no boundary face! I don't know what does this exactly mean
                # in the Revit world but now that there is no boundary face we can just
                # use the dynamo face and create the surface!
                face_vertices = tuple(v.PointGeometry for v in base_face_dyn.Vertices)

                new_surface = surface.create_surface(
                    "%s_%s" % (new_room['name'], create_uuid()),
                    new_room['name'],
                    face_vertices
                )

                room.add_surface_to_room(new_room, new_surface)
                continue

            for boundary_face in boundary_faces:
                # boundary_face is a SpatialElementBoundarySubface
                # we need ti get the element (Wall, Roof, etc) first
                boundary_element = doc.GetElement(
                    boundary_face.SpatialBoundaryElement.HostElementId
                )

                # initiate honeybee surface
                face_vertices = tuple(v.PointGeometry for v in base_face_dyn.Vertices)

                # TODO(mostapha) This should be done once when all the surfaces are
                # created.
                if face_vertices in _collector:
                    continue

                new_surface = surface.create_surface(
                    "%s_%s" % (new_room['name'], boundary_element.Id),
                    new_room['name'],
                    face_vertices
                )

                # TODO(mostapha) This should be done once when all the surfaces are
                # created.
                _collector.append(face_vertices)

                # collect element id for each face.
                # this can be part of the object itself. I need to think about it before
                # adding it to the object. Trying to keep it as light as possible
                element_collector[room_count].append(doc.GetElement(
                    boundary_face.SpatialBoundaryElement.HostElementId
                ))

                # I'm not sure how this works in Revit but I assume there is an easy
                # way to find the adjacent surface for an element. Let's hope this time
                # revit doesn't let me down!
                # if boundary_element.Id not in surface_collector:
                #     surface_collector[boundary_element.Id] = new_surface
                # else:
                #     # TODO(mostapha): set adjacent surface
                #     pass
                # ----------------------- child surfaces --------------------------- #
                # time to find child surfaces (e.g. windows!)
                # this is the reason dynosaur exists in the first place

                # Take care of curtain wall systems
                # This will most likely fail for custom curtain walls
                if get_parameter(boundary_element, 'Family') == 'Curtain Wall':
                    # get cooredinates and element ids for this curtain wall.
                    _elementIds, _coordinates = extract_panels_vertices(
                        boundary_element, base_face_dyn, opt)

                    for count, coordinate in enumerate(_coordinates):
                        if not coordinate:
                            log.append("{} has an opening with less than "
                                       "two coordinates. It has been removed!"
                                       .format(_elementIds[count]))
                            continue

                        # create honeybee surface - use element id as the name
                        new_fen_surface = surface.create_fen_surface(
                            _elementIds[count],
                            new_surface['name'],
                            coordinate)

                        # get element and add it to the collector
                        elm = boundary_element.Document.GetElement(_elementIds[count])
                        element_collector[room_count].append(elm)

                        # add fenestration surface to base surface
                        surface.add_fenestration_to_surface(new_surface, new_fen_surface)
                else:
                    # collect child elements for non-curtain wall systems
                    childelement_collector = get_child_elemenets(boundary_element)

                    if childelement_collector:

                        _coordinates = exctract_glazing_vertices(
                            boundary_element,
                            base_face_dyn,
                            opt
                        )

                        for count, coordinate in enumerate(_coordinates):

                            if not coordinate:
                                log.append("{} in {} has an opening with less than "
                                           "two coordinates. It has been removed!"
                                           .format(
                                               childelement_collector[count].Id,
                                               new_room['name']
                                           ))

                            # create honeybee surface - use element id as the name
                            new_fen_surface = surface.create_fen_surface(
                                childelement_collector[count].Id,
                                new_room['name'],
                                coordinate
                            )

                            # add fenestration surface to base honeybee surface
                            element_collector[room_count].append(
                                childelement_collector[count]
                            )
                            # add fenestration surface to base surface
                            surface.add_fenestration_to_surface(new_surface,
                                                                new_fen_surface)

                # add surface to dynosaur room
                room.add_surface_to_room(new_room, new_surface)
                # clean up!
                boundary_element.Dispose()

        room_collector[room_count] = new_room

        # clean up!
        revit_room_spatial_data.Dispose()

    calculator.Dispose()
    return room_collector, element_collector, log