Jump to content

Exporting project keywords in a .txt file


---
 Share

Recommended Posts

Hello everyone,

I want to learn if it is possible or not to export project keywords and write on  a .txt file or at least, to access the keywords as parameters. We use project keywords (which includes some info about part number, serial number, inspector info etc.) in every part is scanned and inspected. Therefore, in our script, we need to use project keywords filled by inspector in a separate file (as marking tag of the part). Is there a way to do that?

Many thanks,

Canset

Link to comment
Share on other sites

Canset,

I've recently begun utilizing project keywords in our scan templates as well (they've proven particularly handy for autofilling information on report pages!). You can access project keywords in the Script Editor by right-clicking and selecting Insert--> Element Value (or hitting F2 as a keyboard shortcut). In the window that appears, select the Project Object group, and "project" in the list, then in the keyword window at the bottom click on "User-defined keywords" to access all the available project keywords. In the screenshot below I've inserted the "Part" keyword, and it appears in the script as the "gom.app.project.get ('user_part')" string variable. From here you could use standard python open/write methods to write the keyword variables to a text file in whatever format you choose. I hope this helps!

Regards,

Michael

Capture.PNG.374fbed6967b1fab59b3733ec7bd3834.PNG

EDIT: I should point out that this method works for at lease ATOS V8 through 2020, we haven't updated to ATOS 2021 so I don't know if the Script Editor interface has changed significantly in that version.

Edited
Link to comment
Share on other sites

Hello Michael,

We also need project keywords both for reporting purposes and use .txt files for kind of separate tag file  of the part. Actually after creating this subject, I accessed and edit the keyword variables through script.(gom.sys.set_project_keywords). However, your way of accessing element values looks like much more useful.

Many thanks for your help,

Canset

Link to comment
Share on other sites

  • 2 years later...

even if the topic is allready closed/answered:

as we have to restructure our keywords-settings we had to find a solution to use our existing user-keywords for the new set. 

This script will read all keywords, extract only "user_keywords", get keywords value and write them into a *.txt file which can be used to set the new keywords.

Maybe it's helpfull to someone:

 

import gom
import os
import time
from datetime import datetime
import re

# read all existing keywords (gom.app.project.__doc__) and write into a .txt file:
# set filename (date+time+partname+_Keywords_export) and path to the new file
t = time.localtime()
d = gom.Date(t.tm_mday, t.tm_mon, t.tm_year)
dt = time.strftime("20%y-%m-%d__%H-%M")+"__"+str(gom.app.project.get ('user_part_nr')+"_Keywords_export") # Keyword "user_part_nr" has to be set in existing keywords, otherwise use your own keyword 
logname_preset = str(dt)
print (logname_preset)
path = ('C:/gom/temp/keywords') #set your own path
print (path)
TmpDir=path
FNam=logname_preset+'.txt'
print(TmpDir+"/"+FNam)
fpOut=open(TmpDir+"/"+FNam, "a")

fpOut.write(
str(gom.app.project.__doc__))
fpOut.close()

file = (TmpDir+"/"+FNam)
print (file)

# Input+Output files
input_file = file
output_file = TmpDir+"/"+logname_preset+"_user_Keywords_extracted"+".txt"


# read and extract lines
def process_file(input_file, output_file):
	results = []  # temp_list
	
	with open(input_file, "r", encoding="ISO-8859-1") as file:
		for line in file:
			# ignore scanbox auto-entries
			if line.startswith("user_defined"):
				continue
			# exctract User-Keyword only
			if line.startswith("user_"):
				parts = line.split("|")
				field_name = parts[0].strip()
				variable_name = field_name[5:]  # remove "user_"
				description = parts[1].strip() if len(parts) > 1 else "no description" 
				# get keywords value
				try:
					value = gom.app.project.get(field_name)
				except (KeyError, AttributeError, TypeError) as e:
					value = f"error: {e}"
				# save results in list
				results.append(f"{variable_name};	{value};	{description};	{field_name}")
	
	# write result in new txt file
	with open(output_file, "w", encoding="ISO-8859-1") as file:
		file.write("\n".join(results))

# run script
process_file(input_file, output_file)
print(f"file has been created successfully: {output_file}")


Best regards

Michel

Link to comment
Share on other sites

 Share

×
×
  • Create New...