english
version "1.0"
identify "%Z%%M% %I% %E%"

#: Copyright (c) 1995, 1997, 2001-2003 by Wayne C. Gramlich.
#, All rights reserved.
#,
#, Permission to use, copy, modify, distribute, and sell this software
#, for any purpose is hereby granted without fee provided that the above
#, copyright notice and this permission are retained.  The author makes
#, no representations about the suitability of this software for any purpose.
#, It is provided "as is" without express or implied warranty.

module parts

#: This module implements some code for managing libraries of parts and
#, packages.

define draw				#: Draw point
    record
	x integer			#: Delta X
	y integer			#: Delta Y
    generate allocate, erase, print

define package				#: A configuration of leads
    record
	description string		#: Long package decription
	draws vector[vector[draw]]	#: Artwork drawing vectors
	file_name string		#: File containing packag definition
	leads vector[package_lead]	#: Lead positions and names
	line_number unsigned		#: Line number in file where defined
	pivet package_lead		#: Lead that rotations pivet around
	name string			#: Short package name (e.g. DIP16)
    generate allocate, erase, identical, print

define package_lead			#: One lead of a package
    record
	name string			#: Name of lead (i.e. pin number)
	package package			#: Package containing lead
	x unsigned			#: X coordinate of lead
	y unsigned			#: Y coordinate of lead
    generate allocate, erase, identical, print

define part_root			#: Root of variaous parts
    record
	name string			#: Part name
	parts vector[part]		#: Parts list
    generate allocate, erase, identical, print

define part				#: Part description
    record
	denominator unsigned		#: Denominator
	description string		#: Long part description
	leads vector[part_lead]		#: Pin names
	name string			#: Short part number (e.g. 74ALS00)
	numerator unsigned		#: Numerator
	flavor string			#: Part flavor (or "")
	package package			#: Package used by part
	vendor string			#: Vendor name (or "")
	vendor_part string		#: Vendor part name (or "")
	vendor_catagory string		#: Vendor catagory (or "")
	vendor_prices vector[price]	#: Vendor prices
    generate address_get, allocate, erase, identical, print

define part_lead			#: One lead of a part
    record
	name string			#: Pin name
	package_lead package_lead	#: Corresponding package lead
	part part			#: Part containing lead
	signal signal			#: Signal type
    generate allocate, erase, identical, print

define part_line			#: One line from a .ptl file
    record
	denominator unsigned		#: Fraction denominator
	description string		#: Textual description of part
	file_name string		#: File name part line is from
	first_number unsigned		#: First number (>1)
	flavor string			#: Part flavor (or "")
	line_number unsigned		#: Line number
	numerator unsigned		#: Fraction numerator
	part part			#: Corresponding part (or ??)
	part_name string		#: Part name from .prt file
	prefix string			#: Part prefix (e.g. N, U, SW, LED)
	quantity unsigned		#: Quantity of parts
	second_number unsigned		#: Second number (or 0)
	vendor string			#: Vendor name (e.g. Jameco)
	vendor_part string		#: Vendor part name (or "")
    generate address_get, allocate, erase, identical, print

define price				#: Part price
    record
	cost unsigned			#: Cost in pennies ($.01)
	quantity unsigned		#: Required quantity
    generate address_get, allocate, erase, identical, print

define signal				#: Signal on pin
    enumeration
	ground				#: Ground (0v)
	ground_implied			#: Implied Ground
	other				#: Other kind of connection
	no_connection			#: No connection
	pull_down			#: Pull-down resistor
	pull_up				#: Pull-up resistor
	ttl_in				#: TTL input
	ttl_open_collector		#: TTL open collector
	ttl_out				#: TTL output
	ttl_tristate			#: TTL tristate
	vcc				#: Vcc (+5V)
	vcc_implied			#: Implied Vcc
    generate print

define board_part			#: One part for a PCB
    record
	name string			#: Part name (e.g. U2, Q2, C2)
	part part			#: Corresponding part number
    generate allocate, erase, identical, print



#: {board_part} routines:

procedure file_merge@board_part
    takes
	file_name string
	board_parts vector[board_part]
	board_part_table table[string, board_part]
	part_root_table table[string, part_root]
	error_stream out_stream
	debug_stream out_stream
    returns logical

    #: This procedure will read all of the board part descriptions from the
    #, file named {file_name} and enter them into {board_part_table}.  Each
    #, new board part is appended to {board_parts} as well.  If any errors
    #, occur,{true}@{logical} is returned; otherwise, {false}@{logical} is
    #, returned.


#: {part_line} routines:

procedure compare@part_line
    takes
	part_line1 part_line
	part_line2 part_line
    returns integer

    #: This procedure will return -1, 0, or 1 depending upon whether
    #, {part_line1} is less than, equal to, or greater than {part_line2}.


procedure parts_list_extract@part_line
    takes
	out_file_name string
	file_names vector[string]
	executable_directory string
	number_count string
	average logical
	error_stream out_stream
	debug_stream out_stream
    returns logical

    #: This procedure will read all of the parts lists files named
    #, by {file_names} and output a sorted parts list file to {out_file_name}.


procedure parts_list_read@part_line
    takes
	ptl_file_name string
	part_root_table table[string, part_root]
	error_stream out_stream
	debug_stream out_stream
    returns vector[part_line]

    #: This procedure will read "file_name" and extract the
    #, parts list information.  If any error occur, an
    #, error message is output to {error_stream} and ?? is
    #, returned.


procedure show@part_line
    takes
	part_line part_line
	out_stream out_stream
    returns_nothing

    #: This procedure will output {part_line} to {out_stream}.


#: {package} routines:

procedure create@package
    takes
	file_name string
	line_number unsigned
    returns package

    #: This procedure will create and return a new package
    #, and containing {line_number}.


procedure file_merge@package
    takes
	file_name string
	packages vector[package]
	package_table table[string, package]
	error_stream out_stream
	debug_stream out_stream
    returns logical

    #: This procedure will read all of the package descriptions from the
    #, file named {file_name} and enter them into {package_table}.  Each
    #, new package is appended to {packages} as well.  If any errors occur,
    #, {true}@{logical} is returned; otherwise, {false}@{logical} is returned.


#: {part} routines:

procedure file_merge@part
    takes
	prt_file_name string
	part_root_table table[string, part_root]
	part_roots vector[part_root]
	parts vector[part]
	package_table table[string, package]
	error_stream out_stream
	debug_stream out_stream
    returns logical

    #: This procedure will read all of the part descriptions from the
    #, file named {prt_file_name} and enter them into {part_root_table}.
    #,  Each new part is appended to {parts} as well.  If any errors occur,
    #, {true}@{logical} is returned; otherwise, {false}@{logical} is returned.


procedure lookup@part
    takes
	part_root_table table[string, part_root]
	name string
	flavor string
    returns part

    #: This procedure will lookup and return the {part} object
    #, corresponding to {name} and {flavor}.  If there is no
    #, matching {part} object, ??@{part} is returned.


procedure insert@part
    takes
	part part
	part_root_table table[string, part_root]
	part_roots vector[part_root]
	parts vector[part]
	error_stream out_stream
	debug_stream out_stream
    returns logical

    #: This procedure will insert {part} into {part_table}, {parts},
    #, {part_root_table}, and {part_roots}.  If there is a duplicate,
    #, an error message is output to {error_stream}, {true}@{logical}
    #, is returned, and no insertions take place.  Otherwise,
    #, {false}@{logical} is returned.


procedure number_append@string
    takes
	buffer string
	number unsigned
    returns_nothing

    #: This procedure will append {number} to {buffer} as decimal number.


#: {price} routines:

procedure check@price
    takes
	price price
	quantity_desired unsigned
    returns unsigned, unsigned, unsigned

    #: This procedure will compute a cost and a quantity based on
    #, {total_quantity} and {price}.


procedure check2@price
    takes
	price1 price
	price2 price
	quantity_desired unsigned
    returns unsigned, unsigned, unsigned

    #: This procedure will compute a cost and a quantity based on
    #, {total_quantity} and {price1} and {price2}.


procedure compare@price
    takes
	price1 price
	price2 price
    returns integer

    #: This procedure will compare {price1} to {price2} and order
    #, it by the quantity.