
! script name: items.d/itemtable
! topic: makes a table listing similar items

!A
!A NAME:
!A
!A 	Item Table
!A
!A DESCRIPTION:
!A
!A 	This script creates a table of the item flags present in a file and
!A 	gives a count of each item.  Use this function to generate a Bill of
!A 	Materials.  You can also generate any other type of table that can be
!A 	used within Synergy, or exported for use by other software.
!A
!B INSTRUCTIONS:
!B
!B 	TABLE LOCATION: Enter the location of the upper-left corner of
!B 	the table.
!B
!C NOTES:
!C	
!C 	By default, the table will list the quantity, the category or class
!C 	name and the item flag's name.  Additional datafields may be added
!C 	to the table by adding the parameter list for the CreateItemTable
!C 	function.  For example, if cost and vendor are datafields present
!C 	in all your items, you can modify the script to read:
!C
!C 		Table1= CreateItemTable(Pc,":",WsUpperLeft,"Cost","Vendor")
!C
!C 	With these modifications, the fields "Cost" and "Vendor" will appear
!C 	in the table.
!C
!# see also - htdocs/help.d/items.htm
!# see also - htdocs/help.d/property.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: item table ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Class$[256]
DIM A$[256]
DIM B$[256]
DIM File$[256]
DIM Table_name$[256]
DIM Table_title$[256]
DIM Table_file$[256]

ActivateUndo
Table_title$= "BILL OF MATERIALS"
Table_file$= "itemtable.html"
IF ARGC>1 THEN
	Answer$=ARGV(1)
        Html_table=VAL(Answer$)
	IF ARGC>2 THEN Table_title$= ARGV(2)
	IF Html_table THEN Table_name$= "BILL OF MATERIALS(HTML)"
ELSE
	Table_name$= "BILL OF MATERIALS"
	Html_table=0
END IF


Tab$= CHR$(9)
Lf$= CHR$(10)

Start:!
Grid_color= 18
Entry_color= 12
Entry_text_height= GetDefault(WsTextHeight)
Entry_text_width= GetDefault(WsTextWidth)

Header_color= 17
Head_text_height= 1.2*Entry_text_height
Head_text_width= 1.2*Entry_text_width
Header_slant= TAN(20)


Table1= FindNamedEntity(Table_name$)
IF ValidGeometry(Table1) THEN
        Pc= TableEdgeParaPt(Table1,0,1)
        Stat= DeleteEntity(Table1)
ELSE
	IF Html_table THEN
		Pc= PointXYZ(0,0,0)
	ELSE
        	Center_point:Stat=SetInputFormat("n4","")
        	Pc=XyzInput("TABLE LOCATION",WsXyzInput)
        	IF (ExitScript(Destination$)) THEN Leave
        	IF NOT ValidGeometry(Pc) THEN Center_point
	END IF
END IF


PlotOff
Table1= CreateItemTable(Pc,":",WsUpperLeft)
Stat= DeleteEntity(Pc)
IF NOT ValidGeometry(Table1) THEN Leave

Set_table_props:!
Stat= TableTextOrigin(Table1,0,0,WsLeftCenter)
Stat= TableTextOrigin(Table1,0,1,WsCenterText)
Stat= TableTextOrigin(Table1,1,0,WsCenterText)
Stat= SetTableColors(Table1,0,0,Entry_color)
Stat= SetTableColors(Table1,1,0,Header_color)
Stat= TableTextSizes(Table1,1,0,Head_text_width,Head_text_height,Header_slant)
Stat= SetColor(Table1,Grid_color)
Stat= NameEntity(table1,Table_name$)
PlotOn

ActivateUndo
Stat= AdvanceUndoNumber("BOM table")
Stat= AddItemToUndo(WsUBCreateMode,Table1)

IF Html_table THEN
        LOAD "datatable.d/exporthtml: 0,"&Table_name$&",1,"&Table_title$&","&Table_file$
ELSE
        Stat= PlotEntity(Table1)
END IF


Leave:!
PlotOn
Stat=SetInputFormat("n4","")
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
