|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from typing import List, Tuple |
| 3 | +import adsk, adsk.fusion, adsk.core |
| 4 | +from .link import Link |
| 5 | +from .joint import Joint |
| 6 | + |
| 7 | +class Robot(): |
| 8 | + """ |
| 9 | + Store robot link joint information into a bigraph structure |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, robot: adsk.fusion.Design): |
| 13 | + self.robot = robot |
| 14 | + self.rootComp = robot.rootComponent |
| 15 | + self.name = self.get_robot_name() |
| 16 | + self.nodes = self.get_links() |
| 17 | + self.edges = self.get_joints() |
| 18 | + self.loop_joints, self.tree_joints = self.seperate_joints(self.edges) |
| 19 | + |
| 20 | + def get_robot_name(self, ) -> str: |
| 21 | + """ |
| 22 | + Robot name is the root component name |
| 23 | + """ |
| 24 | + robot_name = self.rootComp.name.split()[0] |
| 25 | + return robot_name |
| 26 | + |
| 27 | + def get_links(self, ) -> List[Link]: |
| 28 | + link_list = [] |
| 29 | + occs: adsk.fusion.OccurrenceList = self.rootComp.allOccurrences |
| 30 | + |
| 31 | + # try to solve the nested components problem |
| 32 | + # but still not fully tested |
| 33 | + for occ in occs: |
| 34 | + # TODO: it seems use occ.joints.count will make it usable with occurrences? Test it |
| 35 | + if occ.component.joints.count > 0: |
| 36 | + # textPalette.writeText(str(occ.fullPathName)) |
| 37 | + continue |
| 38 | + else: |
| 39 | + # Only occurrence contains zero joint and has zero childOccurrences |
| 40 | + # can be seen as a link |
| 41 | + if occ.childOccurrences.count > 0: |
| 42 | + # textPalette.writeText(str(occ.fullPathName)) |
| 43 | + # textPalette.writeText(str(occ.childOccurrences.count)) |
| 44 | + continue |
| 45 | + else: |
| 46 | + # textPalette.writeText(str(occ.fullPathName)) |
| 47 | + # textPalette.writeText(str(occ.childOccurrences.count)) |
| 48 | + if occ.isLightBulbOn: |
| 49 | + # only the occurrence light bulb on that the occurrence will be exported |
| 50 | + link_list.append(Link(occ)) # add link objects into link_list |
| 51 | + |
| 52 | + return link_list |
| 53 | + |
| 54 | + def get_joints(self, ) -> List[Joint]: |
| 55 | + joint_list = [] |
| 56 | + |
| 57 | + for joint in self.rootComp.allJoints: |
| 58 | + joint_list.append(Joint(joint)) # add joint objects into joint_list |
| 59 | + |
| 60 | + return joint_list |
| 61 | + |
| 62 | + def get_loop_joints(self,) -> List[Joint]: |
| 63 | + return self.loop_joints |
| 64 | + |
| 65 | + def get_tree_joints(self, ) -> List[Joint]: |
| 66 | + return self.tree_joints |
| 67 | + |
| 68 | + |
| 69 | + def get_graph(self, ): |
| 70 | + pass |
| 71 | + |
| 72 | + def seperate_joints(self, joint_list: List[Joint]) -> Tuple[List[Joint], List[Joint]]: |
| 73 | + """ |
| 74 | + Seperate robot joints into two parts: loop joints, tree joints |
| 75 | + |
| 76 | + Returns: |
| 77 | + loop_joints: List[Joint] |
| 78 | + joints construct closed loop |
| 79 | + it should be noted by user explicitly by name it with prefix "loop" |
| 80 | + tree_joints: List[Joint] |
| 81 | + edges connects nodes(links) like a spanning tree |
| 82 | + """ |
| 83 | + loop_joints = [] |
| 84 | + tree_joints = [] |
| 85 | + for joint in joint_list: |
| 86 | + if joint.name.startswith("loop"): |
| 87 | + loop_joints.append(joint) |
| 88 | + else: |
| 89 | + tree_joints.append(joint) |
| 90 | + return loop_joints, tree_joints |
| 91 | + |
| 92 | + def is_connected_graph(self, ): |
| 93 | + pass |
0 commit comments