Skip to main content

Full API Part Import Example

This page provides a sample python script and sample XML files for using the API to import a part.

Python Script

import_full_part_example.py
import requests
import xml.etree.ElementTree as ET

TdsAddress = "https://192.168.1.100" # insert your TDS server

User = "admin"
Pass = "spotweld"


def read_file(file_name):
with open(file_name) as f:
return f.read()


# Get the authentication token
tokenXml = requests.post(
f"{TdsAddress}/users/get_token",
verify=False,
data=f"User[login]={User}&User[password]={Pass}",
)
token = ET.fromstring(tokenXml.text).text

def post(endpoint: str, data: str, id_path: str = None):
response = requests.post(
f"{TdsAddress}/{endpoint}",
verify=False,
data=data,
headers={"Authorization": f"Token: {token}"},
)
if id_path is not None:
response_xml = ET.fromstring(response.text)
return response_xml.find(id_path).text

part_xml = read_file("part.xml")
weld1_xml = read_file("weld1.xml")
weld2_xml = read_file("weld2.xml")
weld3_xml = read_file("weld3.xml")
image1_xml = read_file("image1.xml")
image2_xml = read_file("image2.xml")
hotspot1_xml = read_file("hotspot1.xml")
hotspot2_xml = read_file("hotspot2.xml")
hotspot3_xml = read_file("hotspot3.xml")
route_xml = read_file("route.xml")
route_items_xml = read_file("route_items.xml")


# create part
part_id = post("parts", part_xml, "Part/id")

# upload the welds
weld1_id = post("welds", weld1_xml.format(part_id=part_id), "Weld/id")
weld2_id = post("welds", weld2_xml.format(part_id=part_id), "Weld/id")
weld3_id = post("welds", weld3_xml.format(part_id=part_id), "Weld/id")

# upload the images
image1_id = post("images", image1_xml.format(part_id=part_id), "Image/id")
image2_id = post("images", image2_xml.format(part_id=part_id), "Image/id")

# upload the hotspots
post("hot_spots", hotspot1_xml.format(image_id=image2_id, weld_id=weld1_id))
post("hot_spots", hotspot2_xml.format(image_id=image1_id, weld_id=weld2_id))
post("hot_spots", hotspot3_xml.format(image_id=image1_id, weld_id=weld3_id))

# upload the route
route_id = post("routes", route_xml.format(part_id=part_id), "Route/id")

# upload the route_items
post("route_items", route_items_xml.format(route_id=route_id, weld_id=weld1_id, image_id=image2_id))
post("route_items", route_items_xml.format(route_id=route_id, weld_id=weld2_id, image_id=image1_id))
post("route_items", route_items_xml.format(route_id=route_id, weld_id=weld3_id, image_id=image1_id))

Part XML File

part.xml
<request>
<Part>
<group_id>0</group_id>
<name>Import Part</name>
<measurement_type>rswa-steel</measurement_type>
<meta/>
</Part>
</request>

Weld XML Files

Do not remove the {part_id} section, as it is included intentionally. The provided python code will automatically replace it with the proper part ID.

weld1.xml
<request>
<Weld>
<category_id>0</category_id>
<name>Weld 1</name>
<slots>1</slots>
<part_id>{part_id}</part_id>
<stack_front>800</stack_front>
<stack_back>900</stack_back>
<diam_min>3000</diam_min>
<robot></robot>
<schedule></schedule>
<indentation_max>640</indentation_max>
<thickness_min>1020</thickness_min>
<meta/>
</Weld>
</request>
weld2.xml
<request>
<Weld>
<category_id>0</category_id>
<name>Weld 2</name>
<slots>1</slots>
<part_id>{part_id}</part_id>
<stack_front>1200</stack_front>
<stack_back>900</stack_back>
<diam_min>3000</diam_min>
<robot></robot>
<schedule></schedule>
<indentation_max>640</indentation_max>
<thickness_min>900</thickness_min>
<meta/>
</Weld>
</request>
weld3.xml
<request>
<Weld>
<category_id>0</category_id>
<name>Weld 3</name>
<slots>1</slots>
<part_id>{part_id}</part_id>
<stack_front>800</stack_front>
<stack_back>1200</stack_back>
<diameter_min>3400</diameter_min>
<diameter_target>3700</diameter_target>
<robot></robot>
<schedule></schedule>
<indentation_max>640</indentation_max>
<thickness_min>1020</thickness_min>
<meta/>
</Weld>
</request>

Image XML Files

Do not remove the {part_id} section, as it is included intentionally. The provided python code will automatically replace it with the proper part ID.

The <thumbnail> and <data> fields contain large base64-encoded binary blobs. Download the full files:

image1.xml
<request>
<Image>
<part_id>{part_id}</part_id>
<width>1098</width>
<height>1000</height>
<position>1</position>
<crc32>843768659</crc32>
<thumbnail><!-- base64-encoded data omitted --></thumbnail>
<data><!-- base64-encoded data omitted --></data>
<meta />
</Image>
</request>
image2.xml
<request>
<Image>
<part_id>{part_id}</part_id>
<width>799</width>
<height>533</height>
<position>2</position>
<crc32>678848275</crc32>
<thumbnail><!-- base64-encoded data omitted --></thumbnail>
<data><!-- base64-encoded data omitted --></data>
<meta />
</Image>
</request>

Hotspot XML Files

Do not remove the {image_id} or {weld_id} sections, as they are included intentionally. The provided python code will automatically replace it with the proper IDs.

hotspot1.xml
<request>
<image_id>{image_id}</image_id>
<HotSpot>
<weld_id>{weld_id}</weld_id>
<data>
{{"lx":452, "ly":288, "ts": [{{"x":400, "y":236}}, {{"x":415, "y":221}}]}}
</data>
</HotSpot>
</request>
hotspot2.xml
<request>
<image_id>{image_id}</image_id>
<HotSpot>
<weld_id>{weld_id}</weld_id>
<data>
{{"lx":722, "ly":584, "ts": [{{"x":650, "y":512}}, {{"x":665, "y":497}}]}}
</data>
</HotSpot>
</request>
hotspot3.xml
<request>
<image_id>{image_id}</image_id>
<HotSpot>
<weld_id>{weld_id}</weld_id>
<data>
{{"lx":933, "ly":527, "ts": [{{"x":864, "y":446}}, {{"x":881, "y":429}}]}}
</data>
</HotSpot>
</request>

Route XML File

Do not remove the {part_id} section, as it is included intentionally. The provided python code will automatically replace it with the proper part ID.

route.xml
<request>
<Route>
<part_id>{part_id}</part_id>
<name>Route</name>
<measurement_type>rswa-steel</measurement_type>
<comment />
</Route>
</request>

Route Items XML File

Do not remove the {route_id}, {weld_id} or {image_id} sections, as they are included intentionally. The provided python code will automatically replace it with the proper IDs.

route_items.xml
<request>
<route_id>{route_id}</route_id>
<RouteItem>
<weld_id>{weld_id}</weld_id>
<image_id>{image_id}</image_id>
</RouteItem>
</request>