I have an xml file of force plate positions that looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Positions>
<Device name="ForcePlate">
<ParamListGroup name="">
<ParamList name="Plate1">
<Param name="Position_X" value="0.25000000000000006"/>
<Param name="Position_Y" value="0.23000000000000007"/>
<Param name="Position_Z" value="0"/>
</ParamList>
<ParamList name="Plate2">
<Param name="Position_X" value="-0.41000000000000009"/>
<Param name="Position_Y" value="0.090000000000000024"/>
<Param name="Position_Z" value="0"/>
</ParamList>
</ParamListGroup>
</ParamDefinitionList>
</Positions>
I want to rotate the x and y positions about the z-axis by 90 degrees so that the final xml looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Positions>
<Device name="ForcePlate">
<ParamListGroup name="">
<ParamList name="Plate1">
<Param name="Position_X" value="0.23000000000000007"/>
<Param name="Position_Y" value="-0.25000000000000006"/>
<Param name="Position_Z" value="0"/>
</ParamList>
<ParamList name="Plate2">
<Param name="Position_X" value="0.090000000000000024"/>
<Param name="Position_Y" value="0.41000000000000009"/>
<Param name="Position_Z" value="0"/>
</ParamList>
</ParamListGroup>
</Device>
</Positions>
My code looks like this:
import numpy as np
import xml.etree.ElementTree as ET
import os
tree = ET.parse(os.path.relpath(r"C:Usersnpires.OMGOneDrive - OMG PLCWork ProjectsNexus Insight WebinarJohnrotate xcp testtest.xml"))
root = tree.getroot()
# Import the positional data
pos_x_string = [element.attrib["value"] for element in root.findall("Device/ParamListGroup/ParamList/Param")
if element.attrib["name"] == "Position_X"]
pos_y_string = [element.attrib["value"] for element in root.findall("Device/ParamListGroup/ParamList/Param")
if element.attrib["name"] == "Position_Y"]
pos_z_string = [element.attrib["value"] for element in root.findall("Device/ParamListGroup/ParamList/Param")
if element.attrib["name"] == "Position_Z"]
# Get the number of plates
plate_num = len(set(pos_x_string))
# Convert the strings into floats
pos_x = [float(num) for num in pos_x_string]
pos_y = [float(num) for num in pos_y_string]
pos_z = [float(num) for num in pos_z_string]
# Combine the plates into their XYZ positions
pos = {plate:
np.array([[pos_x[plate], pos_y[plate], pos_z[plate]]]).T
for plate in range(plate_num)}
# Define the rotation matrix
rotation_matrix = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]])
# Define the output matrix
pos_rotated = {plate: np.matmul(rotation_matrix, pos[plate])
for plate in range(plate_num)}
# Define the output
output = {"Position_X": [str(pos_rotated[plate][0][0])for plate in range(plate_num)],
"Position_Y": [str(pos_rotated[plate][1][0]) for plate in range(plate_num)],
"Position_Z": [str(pos_rotated[plate][2][0]) for plate in range(plate_num)]}
# Update the XML
for plate in range(plate_num):
for position, element in zip(output["Position_X"], root.findall("Device/ParamListGroup/ParamList/Param")):
if element.attrib["name"] == "Position_X":
element.set("value", output["Position_X"][plate])
for plate in range(plate_num):
for position, element in zip(output["Position_Y"], root.findall("Device/ParamListGroup/ParamList/Param")):
if element.attrib["name"] == "Position_Y":
element.set("value", output["Position_Y"][plate])
for plate in range(plate_num):
for position, element in zip(output["Position_Z"], root.findall("Device/ParamListGroup/ParamList/Param")):
if element.attrib["name"] == "Position_Z":
element.set("value", output["Position_Z"][plate])
# Write to the XML
_ = tree.write(os.path.relpath(r"C:Usersnpires.OMGOneDrive - OMG PLCWork ProjectsNexus Insight WebinarJohnrotate xcp testtest.xml"))
I can do almost everything. I’m just falling at the final hurdle of updating the XML with the new values