Skip to content

Commit

Permalink
Minor changes to helpers and root node transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
senthurayyappan committed Dec 13, 2024
1 parent 11093cd commit 0cdb6b5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
6 changes: 1 addition & 5 deletions onshape_api/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,7 @@ def download_part_stl(
# TODO: version id seems to always work, should this default behavior be changed?
req_headers = {"Accept": "application/vnd.onshape.v1+octet-stream"}
request_path = f"/api/parts/d/{did}/{wtype}/{wid}/e/{eid}/partid/{partID}/stl"
_query = {
"mode": "binary",
"grouping": True,
"units": "meter",
}
_query = {"mode": "binary", "grouping": True, "units": "meter", "resolution": "coarse"}
response = self.request(
HTTP.GET,
path=request_path,
Expand Down
4 changes: 1 addition & 3 deletions onshape_api/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def transform_mesh(mesh: Mesh, transform: np.ndarray) -> Mesh:
>>> transform_mesh(mesh, transform)
"""

_transform_vectors = partial(
transform_vectors, rotation=transform[:3, :3], translation=transform[0:3, 3:4].T.tolist()
)
_transform_vectors = partial(transform_vectors, rotation=transform[:3, :3], translation=transform[:3, 3].T.tolist())

mesh.v0 = _transform_vectors(mesh.v0)
mesh.v1 = _transform_vectors(mesh.v1)
Expand Down
14 changes: 13 additions & 1 deletion onshape_api/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
MateFeatureData,
MateRelationFeatureData,
MateType,
Occurrence,
Part,
RelationType,
)
Expand Down Expand Up @@ -57,6 +58,7 @@ def get_joint_name(mate_id: str, mates: dict[str, MateFeatureData]) -> str:
def get_robot_link(
name: str,
part: Part,
occurrence: Occurrence,
wid: str,
client: Client,
mate: Optional[Union[MateFeatureData, None]] = None,
Expand Down Expand Up @@ -89,6 +91,9 @@ def get_robot_link(
_link_to_stl_tf = np.eye(4)

if mate is None:
# TODO: this is the root link and we want it to respect the assembly orientation
_assembly_to_link_tf = np.matrix(occurrence.transform).reshape(4, 4)
_link_to_stl_tf[:3, :3] = _assembly_to_link_tf[:3, :3]
_link_to_stl_tf[:3, 3] = np.array(part.MassProperty.center_of_mass).reshape(3)
elif mate.matedEntities[CHILD].parentCS:
_link_to_stl_tf = mate.matedEntities[CHILD].parentCS.part_tf @ mate.matedEntities[CHILD].matedCS.part_to_mate_tf
Expand Down Expand Up @@ -373,6 +378,7 @@ def get_urdf_components(
assembly: Assembly,
graph: DiGraph,
root_node: str,
occurrences: dict[str, Occurrence],
parts: dict[str, Part],
mates: dict[str, MateFeatureData],
relations: dict[str, MateRelationFeatureData],
Expand Down Expand Up @@ -422,7 +428,12 @@ def get_urdf_components(
LOGGER.info(f"Processing root node: {root_node}")

root_link, stl_to_root_tf, _asset = get_robot_link(
name=root_node, part=parts[root_node], wid=assembly.document.wid, client=client, mate=None
name=root_node,
part=parts[root_node],
occurrence=occurrences[root_node],
wid=assembly.document.wid,
client=client,
mate=None,
)

links.append(root_link)
Expand Down Expand Up @@ -476,6 +487,7 @@ def get_urdf_components(
link, stl_to_link_tf, asset = get_robot_link(
child,
parts[child],
occurrences[child],
assembly.document.wid,
client,
topological_mates[mate_key],
Expand Down
8 changes: 6 additions & 2 deletions onshape_api/utilities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def get_sanitized_name(name: str, replace_with: str = "-") -> str:
"""
Sanitize a name by removing special characters, preserving "-" and "_", and
replacing spaces with a specified character. Ensures no consecutive replacement
characters in the result.
characters in the result. Also removes patterns like '<3>' or '<n>' entirely.
Args:
name: Name to sanitize
Expand All @@ -281,7 +281,7 @@ def get_sanitized_name(name: str, replace_with: str = "-") -> str:
Examples:
>>> get_sanitized_name("wheel1 <3>", '-')
"wheel1-3"
"wheel1"
>>> get_sanitized_name("Hello World!", '_')
"Hello_World"
>>> get_sanitized_name("my--robot!!", '-')
Expand All @@ -293,6 +293,10 @@ def get_sanitized_name(name: str, replace_with: str = "-") -> str:
if replace_with not in "-_":
raise ValueError("replace_with must be either '-' or '_'")

# Remove patterns like <3> or <n> where n is a number
name = re.sub(r" <\d+>", "", name)

# Remove unwanted characters and sanitize
sanitized_name = "".join(char if char.isalnum() or char in "-_ " else "" for char in name)
sanitized_name = sanitized_name.replace(" ", replace_with)
sanitized_name = re.sub(f"{re.escape(replace_with)}{{2,}}", replace_with, sanitized_name)
Expand Down

0 comments on commit 0cdb6b5

Please sign in to comment.