Skip to content

Commit

Permalink
Merge pull request #72 from 2twenity/main
Browse files Browse the repository at this point in the history
topologicpy.Speckle Updates
  • Loading branch information
wassimj authored Nov 22, 2024
2 parents fb6ae03 + 27280d5 commit 290e20c
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 4 deletions.
128 changes: 124 additions & 4 deletions src/topologicpy/Speckle.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@
from specklepy.api.wrapper import StreamWrapper
from specklepy.api import operations
from specklepy.objects import Base
from specklepy.objects.other import Collection
from specklepy.objects.other import RenderMaterial
from specklepy.transports.server import ServerTransport
from topologicpy.Topology import Topology
from specklepy.objects.geometry import (Mesh, Point, Polyline)

from topologicpy.Topology import Topology
from topologicpy.Vertex import Vertex
from topologicpy.Face import Face
from topologicpy.Cell import Cell
from topologicpy.CellComplex import CellComplex
from topologicpy.Graph import Graph
from topologicpy.Dictionary import Dictionary

from collections import deque
from typing import List

class Speckle:

@staticmethod
Expand Down Expand Up @@ -108,7 +120,7 @@ def BranchesByStream(client, stream):
return branches

@staticmethod
def ClientByURL(url="speckle.xyz", token=None):
def ClientByURL(url="https://app.speckle.systems/", token=None):
"""
Parameters
----------
Expand Down Expand Up @@ -379,7 +391,7 @@ def mesh_to_native(speckle_mesh, scale=1.0):
transport = ServerTransport(stream.id, client)
last_obj_id = commit.referencedObject
speckle_mesh = operations.receive(obj_id=last_obj_id, remote_transport=transport)
print(speckle_mesh)
# print(speckle_mesh)
return mesh_to_native(speckle_mesh["@display_value"])

@staticmethod
Expand Down Expand Up @@ -506,4 +518,112 @@ def StreamsByClient(item):
DESCRIPTION.
"""
return item.stream.list()
return item.stream.list()

#######################################################
# NEW METHODS TO PROCESS IFC

@staticmethod
def SpeckleObject(client, stream, branch, commit):
transport = ServerTransport(stream.id, client)
last_obj_id = commit.referencedObject
speckle_mesh = operations.receive(obj_id=last_obj_id, remote_transport=transport)

return speckle_mesh

@staticmethod
def get_name_from_IFC_name(name) -> str:
"Function is used to get clean speckle element name"

import re
mapping = {
"IFC_WALL_REG": r"(Wall)",
"IFC_SLAB_REG": r"(Slab)",
"IFC_WINDOW_REG": r"(Window)",
"IFC_COLUMN_REG": r"(Column)",
"IFC_DOOR_REG": r"(Door)",
"IFC_SPACE_REG": r"(Space)"
}

for expression in mapping.values():
res = re.findall(expression, name)
if res:
return res[0]


@staticmethod
def TopologyBySpeckleObject(obj):

def from_points_to_vertices_3D(points) -> List[Vertex]:
points_array_length = len(points)
i = 0
topologic_vertices = []
while i < points_array_length:
x = points[i]
y = points[i+1]
z = points[i+2]
v = Vertex.ByCoordinates(x, y, z)
topologic_vertices.append(v)

i+=3
return topologic_vertices

def make_faces_from_indices(vertices, face_indices) -> List[Face]:
"Helping function used to make a triangular faces"
l = len(face_indices)
i = 0
triangles = []

while i < l:
curr = face_indices[i]
i+=1
lis = face_indices[i:curr+i]
triangles.append(lis)
i+=curr

topologic_vertices = from_points_to_vertices_3D(vertices)
faces_list = []

for triangle in triangles:
v1 = topologic_vertices[triangle[0]]
v2 = topologic_vertices[triangle[1]]
v3 = topologic_vertices[triangle[2]]
f = Face.ByVertices([v1, v2, v3])
faces_list.append(f)

return faces_list

#Stack used for the tree traversal
stack = deque()
stack.append(obj)

#list of coordinates
coords = []

while len(stack) > 0:
head = stack.pop()

if isinstance(head, Collection):
for el in head.elements:
stack.append(el)

elif isinstance(head, Base):

if hasattr(head, '@displayValue'):
meshes = head['@displayValue']
# print(head.name)
if isinstance(meshes, List):
full_name = head.name
short_name = Speckle.get_name_from_IFC_name(full_name)
speckle_id = head.id

mesh_faces = []
for mesh in meshes:
# print(mesh.get_serializable_attributes())
faces = make_faces_from_indices(vertices=mesh.vertices,
face_indices=mesh.faces)
mesh_faces.append(faces)

yield [short_name, full_name, speckle_id, mesh_faces]

# stack.pop()
29 changes: 29 additions & 0 deletions tests/test_15Speckle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys
sys.path.append("/workspaces/topologicpy/src")

import topologicpy
from topologicpy.Speckle import Speckle

def run_test(token):
TOKEN = token

client = Speckle.ClientByURL(url="https://app.speckle.systems/", token=TOKEN)

streams = Speckle.StreamsByClient(client)
stream = streams[1]

branches = Speckle.BranchesByStream(client=client, stream=stream)
branch = branches[0]

commits = Speckle.CommitsByBranch(branch=branch)
commit = commits[0]

obj = Speckle.SpeckleObject(client=client, stream=stream, branch=branch, commit=commit)

toplogy_obj = Speckle.TopologyBySpeckleObject(obj)

for top in toplogy_obj:
print(top)

#To run test enter your token and select desired stram, branch, and commit
# run_test()

0 comments on commit 290e20c

Please sign in to comment.