Cut Methods Database Handler in TCL: ASCII File Management
CUT_METHODS.TCL/n###############################################################################/n# cut_methods.tcl - DBC Event Handler for Cut Methods database stored /n# as ascii file/n###############################################################################/n# REVISIONS/n# Date Who Reason/n# 03/24/99 Murthy Mandaleeka Initial/n# 05/19/99 Murthy Mandaleeka New Environment Variable/n# 07/21/99 Subhash Included dbc_general_ascii.tcl file/n# 07/21/99 Subhash Used general functions for/n# opening and reading dat files./n# 08/06/99 Subhash Removed some unwanted comments/n# 08/20/99 Subhash Added procedure /n# ASC_file_execute_query_check_attr/n# to search for the records with the/n# same mode as the operation./n# This procedure is a modification of/n# ASC_file_execute_query/n# 11/11/2003 rlm Add ASC_output_header/n# 01/30/2004 rlm Move ASC_write to dbc_ascii_general/n# 02/12/2004 rlm Add last revision information/n# 06/28/2011 Peter Mao 6556892 Add DBC_ask_library_values/n# 08/10/2020 Yusheng Chen 9847704 Initialize file_unit in DBC_retrieve/n##############################################################################/n#/n# The format of each line in the text file is:/n#/n# cut_method_index - Integer /n# machine_mode - String /n# cut_method - String /n#-------------------------------------------------------------------------------/n/n#---------------------------------------------/n# Non DBC Global Variables/n#---------------------------------------------/n#MOM_set_debug_mode ON/n#set dbFileName /'[MOM_ask_env_var UGII_CAM_LIBRARY_FEEDS_SPEEDS_DATA_DIR]cut_methods.dat/'/nset DEBUG 0/nset fPointer /'/'/n/n#---------------------------------------------/n# Global variables set by DBC for Input/Output/n#---------------------------------------------/nset dbc_lhs_exp /'/'/nset dbc_rhs_exp /'/'/nset dbc_relop /'/'/nset dbc_query /'/'/nset dbc_subqry1 /'/'/nset dbc_subqry2 /'/'/nset dbc_boolop /'/'/nset dbc_class_name /'/'/nset dbc_attr_count 0/nset dbc_attr_id /'/'/nset dbc_query_count 0/nset dbc_cutmthd_libref /'/'/nset dbc_var_list /'/'/nset dbc_machine_mode 'MILL'/nset asc_debug 0/n/n/n#---------------------------------------------/nproc ASC_t_create_filename {env_var_name filename} ///n{/n#/n# Creates a complete filename by evaluating an environment variable/n# for the directory information/n#/n set env_var [MOM_ask_env_var $env_var_name]/n if { $env_var == /'/' } ///n {/n set message 'Can't read environment variable $env_var_name'/n MOM_abort '/n $message'/n }/n/n set fname /'/'/n set fname [append fname $env_var $filename]/n/n return $fname/n/n}/n/n#---------------------------------------------/nproc MOM__boot {} {/n#---------------------------------------------/n/n# source some general procedures/n/n#/n# dbc_ascii_general.tcl/n#/n set filename ///n [ASC_t_create_filename 'UGII_UG_LIBRARY_DIR' 'dbc_ascii_general.tcl']/n/n/n if { [catch {source $filename}] == '1' } ///n {/n set message 'Can't load .tcl file: $filename'/n MOM_abort '/n $message'/n }/n/n}/n/nproc MOM__halt {} ///n{/n/n}/n/n/n#------------------------------------------------------------/nproc DBC_init_db { } ///n{/n global DEBUG/n global dataBase/n global lastRowNumber/n global dbFileName/n global fPointer/n global asc_file_name/n/n set asc_part_units /'/'/n/n set data_unit 0/n set mode 0/n/n/n#/n# Set the filename for ASCII Data File/n#/n ASC_set_data_file_name/n/n#/n# And load the file into memory/n#/n# ASC_load_data_file $asc_file_name $data_unit $mode/n# Since we are now reading the records one by one, no need to load/n# the data file. ( Changed on 20-Aug-99 )/n/n/n}/n/n#---------------------------------------------/nproc ASC_set_data_file_name {} {/n#---------------------------------------------/n global dbc_part_units/n global asc_file_name/n global asc_part_units/n/n MOM_ask_part_units ;# writes to dbc_part_units/n/n set env_var UGII_CAM_LIBRARY_FEEDS_SPEEDS_DATA_DIR/n set asc_file_name [ASC_t_create_filename '$env_var' 'cut_methods.dat']/n set asc_part_units $dbc_part_units/n/n}/n/n#---------------------------------------------/nproc DBC_translate_att_alias {} {/n#---------------------------------------------/n global DEBUG/n global dbc_alias/n global dbc_id/n/n # The input dbc_id is the column in the RSET/n set dbc_id '/$dataBase($dbc_id)'/n/n}/n/n#------------------------------------------------------------/nproc DBC_create_criterion { } {/n#------------------------------------------------------------/n global DEBUG/n global dbc_lhs_exp/n global dbc_rhs_exp/n global dbc_relop/n global dbc_query/n/n# set dbc_query '/[$dbc_lhs_exp/] $dbc_relop /[$dbc_rhs_exp/]'/n/n ASC_create_criterion/n/n/n}/n/n#------------------------------------------------------------------------/nproc DBC_create_query { } {/n#------------------------------------------------------------------------/n global DEBUG/n global dbc_query/n global dbc_subqry1/n global dbc_subqry2/n global dbc_boolop/n set dbc_query '$dbc_subqry1 $dbc_boolop $dbc_subqry2'/n/n ASC_create_query/n/n}/n#---------------------------------------------/nproc ASC_file_execute_query_check_attr { file_name file_unit } ///n{/n#/n# Executes query on cut_methods file. This procedure is derived from/n# ASC_file_execute_query.It is slightly modified so that an attribute/n# of the record can be checked for some value and then populated to Rset/n#/n# Input filename - name of the database file, in this case cut_methods.dat/n#/n# file_unit - Unit of the records, in this case none/n#/n# Returns 0 -> Succesful Loading/n# 1 -> File open error/n#/n global asc_debug/n/n global dbc_class_name/n global dbc_query/n global dbc_query_count/n global dbc_attr_count/n global dbc_attr_aliases/n global dbc_attr_id/n/n global asc_database/n global asc_database_count/n global dbc_machine_mode/n/n global asc_next_line/n if { $asc_debug == '1' } {/n puts 'ASC_file_execute_query'/n puts 'dbc_class_name = $dbc_class_name'/n puts 'dbc_query = $dbc_query'/n }/n/n# The following procedure writes to the variables/n# dbc_attr_count, dbc_attr_aliases/n/n DBC_load_rset_data/n/n#/n# Open the file/n#/n set ret_code [catch {open $file_name 'r'} fp]/n if { $ret_code } ///n {/n return 1/n }/n/n/n ASC_init_database/n#/n# Loop over all the DATA records/n#/n# The db_row variable has to be initialized (see also ASC_translate_att_alias)/n/n set db_row 0/n set rset_row 0/n/n/n set asc_next_line /'/'/n while {[ASC_read_next_db_object $fp $file_name $file_unit $db_row] >= 0} ///n {/n/n set local_dbc_machine_mode [ASC_ask_att_val MODE $db_row /'/' 0 flag]/n/n if {[string compare $dbc_machine_mode $local_dbc_machine_mode] == 0 ||/n [string compare $dbc_machine_mode 'ALL'] == 0 } ///n {/n/n set dum [catch {expr $dbc_query} ret]/n if { $ret == '1' } ///n {/n ASCi_write_to_rset fp $dbc_attr_count dbc_attr_aliases ///n $rset_row $db_row/n incr rset_row/n incr dbc_query_count/n }/n/n catch {unset asc_database}/n set asc_database_count 0/n }/n/n }/n/n if { $asc_debug == '1' } {/n puts 'End of ASC_file_execute_query '/n puts 'Count: $dbc_query_count'/n }/n/n close $fp/n/n return 0/n/n}/n/n#------------------------------------------------------------------------/nproc DBC_execute_query { } {/n#------------------------------------------------------------------------/n global asc_units/n global file_unit/n global asc_file_name/n/n set file_unit $asc_units(unknown)/n ASC_file_execute_query_check_attr $asc_file_name $file_unit/n/n}/n#---------------------------------------------------------------------/nproc DBC_retrieve { } {/n#---------------------------------------------------------------------/n global DEBUG/n global dbc_libref/n global dbc_retrieve_var_list/n global dataBase/n/n global dbc_machine_mode/n global dbc_name/n global dbc_description/n global dbFileName/n global fPointer/n global asc_debug/n global asc_database_count/n global asc_file_name/n global file_unit/n/n# intialize file_unit/n if { ![info exists file_unit] } {/n global dbc_part_units/n global asc_units/n/n MOM_ask_part_units ;# writes to dbc_part_units/n/n if { $dbc_part_units == 'metric' } {/n set file_unit $asc_units(mm)/n } else {/n set file_unit $asc_units(inch)/n }/n }/n/n if { $asc_debug == '1' } ///n {/n puts ' ==========================================='/n puts ' procedure DBC_retrieve for cut methods'/n puts ' libref -> $dbc_libref'/n }/n/n#/n# Look for the desired libref/n#/n/n set found 2/n/n set found [ASC_file_find_object_by_att $asc_file_name $file_unit ///n LIBRF $dbc_libref db_row]/n/n if { $found == 2 } ///n {/n set message 'Error retrieving Cut Method from external library.'/n set message '$message /n Cut Method with the library reference $dbc_libref'/n set message '$message /n does not exist in the'/n set message '$message /n ASCII Data File: $asc_file_name'/n MOM_abort '/n $message'/n }/n/n if { $found == 1 } ///n {/n set message 'Error retrieving Cut Method from external library.'/n set message '$message /n ASCII Data File: $asc_file_name'/n set message '$message /n does not exist.'/n MOM_abort '/n $message'/n }/n/n#/n# Get the Machining Mode/n#/n set dbc_machine_mode [ASC_ask_att_val MODE $db_row /'/' 0 flag]/n/n#/n# Get the Cut Method Name/n#/n set dbc_name [ASC_ask_att_val NAME $db_row /'/' 0 flag]/n/n}/n/n/n#------------------------------------------------------------------------/nproc DBC_write { } {/n#------------------------------------------------------------------------/nglobal asc_database_name/n/n set asc_database_name 'cut_methods.dat'/n ASC_write/n}/n/n#---------------------------------------------------------------------/nproc ASC_output_header { fileid } {/n#---------------------------------------------------------------------/nglobal dbc_attr_count/nglobal dbc_attr_aliases/nglobal dbc_attr_id/nglobal dbc_logname/n/n/n set daytime [clock seconds]/n set out_daytime [clock format $daytime -format '%a %b %d %Y %I:%M %p']/n/n set headerMessage '# CUT_METHODS.DAT/n##########################################################################/n#/n# PURPOSE:/n#/n# This is the database file used for defining Cut Method in/n# Machining method. The library reference is used for Feeds and/n# Speeds Specifications. /n#/n#/n# REVISED:/n# $dbc_logname $out_daytime/n#/n##########################################################################/n##########################################################################/n## /n## The following key words for Attribute ids are defined/n##/n## LIBREF cutmthd_libref - Unique record identifier (Library Reference)/n## MODE machine_mode - Machine mode - MILL/n## - DRILL/n## - LATHE/n## NAME name - Name of Cutting Method /n## (appears on the label in method view)/n#########################################################################/n# --------+-------------------------------+---------------------'/n/n/n puts $fileid '$headerMessage'/n/n# load the database structure/n DBC_load_rset_data/n/n/n# output FORMAT line/n set format_line 'FORMAT'/n for {set i 0} {$i < $dbc_attr_count} {incr i} ///n {/n DBC_ask_attr_id $dbc_attr_aliases($i)/n set format_line '$format_line $dbc_attr_id'/n }/n puts $fileid $format_line/n puts $fileid '#--------------------------------------------------------------------------------'/n}/n/nproc DBC_ask_library_values {} ///n{/n global dbc_libref/n global dbc_db_ids_count/n global dbc_db_ids/n global dbc_event_error/n global asc_file_name/n global asc_units/n global dbc_return_status/n global dbc_db_ids_value/n/n# Do not change this string/n set NO_MATCH 'No matching record found for $dbc_libref in $asc_file_name'/n/n set dbc_return_status 0/n#/n# Look for the desired libref from the file directly/n#/n set found [ASC_file_find_object_by_att $asc_file_name $asc_units(unknown) ///n LIBRF $dbc_libref db_row]/n/n#/n# and set the desired values/n#/n if {$found == 1 || $found == 2} {/n set dbc_event_error $NO_MATCH/n set dbc_return_status 1/n } else {/n for { set inx 0 } { $inx < $dbc_db_ids_count } { incr inx } ///n {/n set dbc_db_ids_value($dbc_db_ids($inx)) ///n [ASC_ask_att_val $dbc_db_ids($inx) $db_row /'/' /'/' flag]/n }/n }/n
原文地址: https://www.cveoy.top/t/topic/nQNu 著作权归作者所有。请勿转载和采集!