Hi all,
The next script shows how to rename your parts in 3-matic based on an excel file. As input, it requires an excel file with:
- column A: current part names
- column B: new part names to which you want to rename
The second part of the script creates an example excel which can be used as input for the main function rename_xlsx.
# This script will look for objects defined in column A of an excel file
# and rename them with names of column B
# Requirement: Package openpyxl
# Input:
# - location of the folder containing the excel file
# - Name (string) of the input excel
# - Name (string) of the output excel
# - The range: until which row of the excel you want to look.
# Output:
# - Object tree will be renamed
# - a new excel file will be written containing info if object were found and renamed or not.
# Author: Kristof Godelaine & Sofie Desiron (Materialise)
# Version: 2.0 (May 2019)
from openpyxl import load_workbook
from openpyxl import Workbook
import trimatic
def rename_xlsx(xlsx_loc,name_xlsx_inp,name_xlsx_out,range_xlx):
#finding excell files
inp_xlsx = xlsx_loc + name_xlsx_inp
out_xlsx = xlsx_loc + name_xlsx_out
#Loading the excell info
wb = load_workbook(filename = inp_xlsx)
#sheet 1 of the excell will be used
ws1 = wb.active
for x in range(1,range_xlx+1):
#coordinate of the position in the excell
colA_posX = 'A'+ str(x)
colB_posX = 'B'+ str(x)
colC_posX = 'C'+ str(x)
colD_posX = 'D'+ str(x)
#Check if cell A is not empty
name_A = ws1[colA_posX].value
name_B = ws1[colB_posX].value
if name_A:
#Look for object A
temp = trimatic.find_object(name_A)
if temp:
ws1[colC_posX] = 'Found'
#If object A is found rename witch xlsx value of cell B
#Check if cell B is not empty
if name_B:
#Do not rename if name in cell A and B is equal
if name_B == name_A:
ws1[colD_posX] = 'Not renamed - name cell A = name cell B'
#Rename if name in cell A and B is different
else:
temp.name = name_B
ws1[colD_posX] = 'Renamed'
#If cell B is empty do not rename
else:
ws1[colD_posX] = 'Not renamed - No info in cell B'
#Object A is not found
else:
ws1[colC_posX] = 'Not Found'
ws1[colD_posX] = 'Not renamed - item is not found'
#If cell A is empty do not rename
else:
print(" Cell A is blank")
#write an excell file
wb.save(filename = out_xlsx)
print('------------------------------------------------------------')
print("Renaming script has been completed")
print("Excell file with detection rate has been saved")
print('------------------------------------------------------------')
##auxiliary functions for runnable example
# Input:
# - location to save auxiliary excel file
# - the number and the size of the boxes used for the example
# Output:
# - a set of boxes will be created in 3-matic
# - an excel file will be written containing the current parts names and the part names which will be used to rename
#Create set of boxes in 3-matic. Returns a list of the boxes created in 3-matic
def create_boxes(number_of_boxes, box_size):
#Clear 3-matic
trimatic.clear()
distance_corner_points = box_size + int(box_size/2)
total_distance = distance_corner_points*number_of_boxes
#create boxes
for k in range(0,total_distance,distance_corner_points):
trimatic.create_box_part((k,0,0),box_size,box_size,box_size)
#get list of boxes
parts = trimatic.get_parts()
return parts
#Create excel file to run example
def create_example_files(xlsx_loc, number_of_boxes, box_size):
parts = create_boxes(number_of_boxes, box_size)
#creating workbook
wb = Workbook()
#activating workbook
ws = wb.active
#writing information to worksheet
for k in range(1, number_of_boxes+1):
position_current_name = "A" + str(k)
position_new_name = "B" + str(k)
ws[position_current_name] = str(parts[k-1].name)
ws[position_new_name] = "part_" + str(k)
#save excel file
wb.save(filename=xlsx_loc + 'input.xlsx')
#Example
#xlsx_loc = r"C:\Renaming Script\\"
#name_xlsx_inp = 'input.xlsx'
#name_xlsx_out = 'output.xlsx'
#number_of_boxes = range_xlx = 10
#box_size = 3
#create_example_files(xlsx_loc, number_of_boxes, box_size)
#rename_xlsx(xlsx_loc, name_xlsx_inp, name_xlsx_out, range_xlx)
#NOTE: Make sure the output directory (xlsx_loc) exists