This commit is contained in:
Alan Rynne 2021-10-29 22:10:03 +02:00
Родитель fcec358540
Коммит 34061fd12a
1 изменённых файлов: 55 добавлений и 43 удалений

Просмотреть файл

@ -46,32 +46,38 @@ class SpeckleMeshDiff:
# query for latest x commits in diff branch # query for latest x commits in diff branch
# read commit message & parse # read commit message & parse
# return url if found # return url if found
existing_diff_commit = self.check_existing_commits() existing_diff_commit = None
if existing_diff_commit is not None: if existing_diff_commit is not None:
url = f"{self.host}/streams/{self.stream_id}/commits/{existing_diff_commit}" url = f"{self.host}/streams/{self.stream_id}/commits/{existing_diff_commit}"
print(f"Returning existing diff: {url}") print(f"Returning existing diff: {url}")
return url return url
print("Did not find existing diff, processing commits now....") print("Did not find existing diff, fetching commits now....")
diff_base = self.compare_meshes() # get meshes from commits
previous_commit = self.receive_data(
self.client, self.stream_id, self.commit_prev)
previous_meshes = self.get_all_meshes(previous_commit)
current_commit = self.receive_data(
self.client, self.stream_id, self.commit_current)
current_meshes = self.get_all_meshes(current_commit)
print("Comparing meshes...")
diff_base = self.compare_meshes(current_meshes, previous_meshes)
print("Diffing was successfull, sending to Speckle") print("Diffing was successfull, sending to Speckle")
res = self.send_diff_data(diff_base) diff_commit_id = self.send_data(
self.client,
self.stream_id,
self.diff_branch,
diff_base,
self.commit_current + "-" + self.commit_prev)
print("Successfully sent data to Speckle") print("Successfully sent data to Speckle")
return res diff_url = f"{self.host}/streams/{self.stream_id}/commits/{diff_commit_id}"
return diff_url
def receive_data(self, stream_id: str, commit_id: str) -> Base: def check_existing_commits(self) -> bool or None:
"""Get the data from a commit on the Speckle server"""
transport = ServerTransport(self.client, stream_id)
commit = self.client.commit.get(stream_id, commit_id)
res = operations.receive(commit.referencedObject, transport)
# if grasshopper, will be nested under data: res["data"]
# if rhino/autocad/revit, will be sent with layers or categories
return res
def check_existing_commits(self) -> Any:
"""Checks if a specific diff commit already exists in the diff_branch""" """Checks if a specific diff commit already exists in the diff_branch"""
branch_commits: Branch = self.client.branch.get( branch_commits: Branch = self.client.branch.get(
self.stream_id, self.diff_branch, 50) self.stream_id, self.diff_branch, 50)
@ -82,19 +88,12 @@ class SpeckleMeshDiff:
return None return None
def compare_meshes(self): def compare_meshes(self, current_meshes: List[Mesh], previous_meshes: List[Mesh]) -> Base:
""" """
Compares the meshes from the first commit against the second, and sends the result to the `diff` branch. Compares the meshes from the first commit against the second, and sends the result to the `diff` branch.
It returns the commit url of the diff. It returns the commit url of the diff.
""" """
# get meshes from commits
previous_commit = self.receive_data(self.stream_id, self.commit_prev)
previous_meshes = self.get_all_meshes(previous_commit)
current_commit = self.receive_data(self.stream_id, self.commit_current)
current_meshes = self.get_all_meshes(current_commit)
# pre process meshes in the current commit to check for same object ID (this means obj hasn't changed) - skip these # pre process meshes in the current commit to check for same object ID (this means obj hasn't changed) - skip these
# if object id has changed, check for application id - if these are the same, compare these objects directly # if object id has changed, check for application id - if these are the same, compare these objects directly
matched_current_indices = [] matched_current_indices = []
@ -220,31 +219,44 @@ class SpeckleMeshDiff:
base["ref"] = ref_meshes base["ref"] = ref_meshes
return base return base
def send_diff_data(self, @staticmethod
diff_object: Base def send_data(client: SpeckleClient, stream_id: str, branch: str, diff_object: Base, message: str) -> str:
): """Sends a Base object to a specified branch"""
"""Sends the data resulting from the diff operation to the 'diff' branch"""
# create a branch if necessary # create a branch if necessary
branches = self.client.branch.list(self.stream_id) branches = client.branch.list(stream_id)
has_res_branch = any(b.name == self.diff_branch for b in branches) has_res_branch = any(b.name == branch for b in branches)
if not has_res_branch: if not has_res_branch:
self.client.branch.create( client.branch.create(
self.stream_id, name=self.diff_branch, description="all your stream diff results" stream_id, name=branch, description="This branch was created by the AEC Tech Masterclass App"
) )
transport = ServerTransport( transport = ServerTransport(
client=self.client, stream_id=self.stream_id) client=client, stream_id=stream_id)
object_id = operations.send(base=diff_object, transports=[transport]) object_id = operations.send(base=diff_object, transports=[transport])
commit_id = self.client.commit.create( commit_id = client.commit.create(
self.stream_id, stream_id,
object_id, # object id object_id, # object id
self.diff_branch, branch,
message=self.commit_current + "-" + self.commit_prev message
) )
diff_url = f"{self.host}/streams/{self.stream_id}/commits/{commit_id}"
return diff_url return commit_id
@staticmethod
def receive_data(client: SpeckleClient, stream_id: str, commit_id: str) -> Base:
"""Get the data from a commit on the Speckle server"""
transport = ServerTransport(client, stream_id)
commit = client.commit.get(stream_id, commit_id)
res = operations.receive(commit.referencedObject, transport)
# if grasshopper, will be nested under data: res["data"]
# if rhino/autocad/revit, will be sent with layers or categories
return res
@staticmethod @staticmethod
def get_all_meshes(child: Base) -> List[Mesh]: def get_all_meshes(child: Base) -> List[Mesh]:
@ -274,7 +286,7 @@ class SpeckleMeshDiff:
return meshes return meshes
@staticmethod @staticmethod
def get_all_points(meshes: List[Mesh]): def get_all_points(meshes: List[Mesh]) -> List[Point]:
"""Returns a flat list of vertices of all the meshes in a list""" """Returns a flat list of vertices of all the meshes in a list"""
points = [] points = []
for mesh in meshes: for mesh in meshes:
@ -287,7 +299,7 @@ class SpeckleMeshDiff:
return points return points
@staticmethod @staticmethod
def find_point(current: Point, points: List[Point]): def find_point(current: Point, points: List[Point]) -> bool:
"""Attempts to find a specific point in a list. Returns True if successful""" """Attempts to find a specific point in a list. Returns True if successful"""
for point in points: for point in points:
if (point.x == current.x and point.y == current.y and point.z == current.z): if (point.x == current.x and point.y == current.y and point.z == current.z):