
! script name: datatable.d/toolholechart
! topic: tool list hole chart

!A
!A NAME:
!A
!A 	Tool List Hole Chart
!A
!A DESCRIPTION:
!A
!A 	This script generates a hole chart from the tool list
!A
!B NOTES:
!B
!B 	This script is a template only. A user may copy and modify
!B 	this script to produce custom charts. Weber systems is not
!B 	responsible for customization of charts for particular needs.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: tool list hole chart ")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[1024]
DIM B$[1024]
DIM Descr$[256]
DIM Toolname$[256]
DIM Table_name$[256]
DIM Format$[32]

! StdWorkplane

ActivateUndo
A= GetDefault(WsPlacesAfter)
Format$= "decimal "&VAL$(A)

Add_z_column= 1
Add_depths= 1
Units= CurrentUnits

Grid_color= 14

Entry_color= 8
Entry_text_height= GetDefault(WsTextHeight)
Entry_text_width= GetDefault(WsTextWidth)

Header_color= 14
Header_ltype= 9
Head_text_height= 2.0*Entry_text_height
Head_text_width= 2.0*Entry_text_width
Header_slant= TAN(30)

Label_color= 8
Label_ltype= 0
Label_text_height= 1.5*Entry_text_height
Label_text_width= 1.5*Entry_text_width
Label_slant= 0


Tool_group= UserDataGroup(WsToolData)
IF NOT ValidGeometry(Tool_group) THEN Leave
Number= GroupCount(Tool_group)

Holes_group= GrpByEntType(WsHoleEnt)
IF NOT ValidGeometry(Holes_group) THEN Leave

Hole_count=0
FOR I=1 TO Number
	Tool= GroupElement(Tool_group,I)
	Hole_locations= GrpByTool(Holes_group,Tool)
IF ValidGeometry(Hole_locations) THEN 
	Hole_count= Hole_count+GroupCount(Hole_locations)
	Stat=DeleteEntity(Hole_locations)
END IF
NEXT I
IF NOT Hole_count THEN Leave

Nr= Hole_count+2
Nc= 4 + Add_z_column+ Add_depths
Table_name$= "TOOL LIST HOLE CHART"
Table1= FindNamedEntity(Table_name$)
IF ValidGeometry(Table1) THEN
   Stat= EraseEntity(Table1)
   Stat= ReDimTableRows(Table1,Nr)
   Stat= ReDimTableColumns(Table1,Nc)
   Stat= BlankDataTable(Table1)
ELSE
Table_pos:Stat=SetInputFormat("n4","")
        Pc=XyzInput("TABLE LOCATION ",WsXyzInput)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Pc) THEN Table_pos
   	Table1= CreateBlankTable(Pc,Table_name$,Nr,Nc,WsUpperLeft,"  ")
   	Stat= DeleteEntity(Pc)
END IF

Table_pos=1
Stat= InsertIntoTable(Table1,"Synergy Hole Chart",Table_pos,2)


Depth_pos= 5
Table_pos=2
Stat= InsertIntoTable(Table1,"#",Table_pos,1)
Stat= InsertIntoTable(Table1,"TOOL",Table_pos,2)
Stat= InsertIntoTable(Table1,"X",Table_pos,3)
Stat= InsertIntoTable(Table1,"Y",Table_pos,4)
IF Add_z_column THEN
	Stat= InsertIntoTable(Table1,"Z",Table_pos,5)
	Depth_pos= 6
END IF
IF Add_depths THEN
	Stat= InsertIntoTable(Table1,"Depth",Table_pos,Depth_pos)
END IF

Table_pos=3
Number= GroupCount(Tool_group)
FOR I= 1 TO Number
	Tool= GroupElement(Tool_group,I)
	Stat=GetUserDataName(Tool,Toolname$)
	IF Stat=1 THEN Next_tool
	Stat=GetUserString(Tool,"Tool id name:",Toolname$)
	Hole_locations= GrpByTool(Holes_group,Tool)
	IF NOT ValidGeometry(Hole_locations) THEN Next_tool
	Hole_count= GroupCount(Hole_locations)

	Stat= InsertIntoTable(Table1,A$,Table_pos,1)
	Stat= InsertIntoTable(Table1,I,"integer",Table_pos,1)
	Stat= InsertIntoTable(Table1,Toolname$,Table_pos,2)
	IF Hole_count THEN
		A$= VAL$(Hole_count)&" locations"
       		Stat= InsertIntoTable(Table1,A$,Table_pos+1,2)
	END IF

	FOR J=1 TO Hole_count
		H1= GroupElement(Hole_locations,J)
		Pt= ParameterPt(H1,0.0)
       		Stat= InsertIntoTable(Table1,XPointValue(Pt),Format$,Table_pos,3)
       	 	Stat= InsertIntoTable(Table1,YPointValue(Pt),Format$,Table_pos,4)
		IF Add_z_column THEN
       	 	Stat= InsertIntoTable(Table1,ZPointValue(Pt),Format$,Table_pos,5)
		END IF
		IF Add_depths THEN
        	Depth= EntityValue(H1,WsHoleDepth)
       	 	Stat= InsertIntoTable(Table1,Depth,Format$,Table_pos,Depth_pos)
		END IF
		Stat= DeleteEntity(Pt)
        	Table_pos= Table_pos+1
	NEXT J
Next_tool:!
Stat= DeleteEntity(Hole_locations)
NEXT I

Stat= NameEntity(table1,Table_name$)
Stat= TableTextOrigin(Table1,WsRightCenter)
Stat= TableTextOrigin(Table1,0,2,WsLeftCenter)
Stat= TableTextOrigin(Table1,0,1,WsCenterText)

Stat= SetTableColors(Table1,Entry_color)

Stat= TableTextSizes(Table1,1,0,Head_text_width,Head_text_height,Header_slant)
Stat= TableTextOrigin(Table1,1,0,WsCenterText)
Stat= SetTableColors(Table1,1,0,Header_color)
Stat= SetTableLType(Table1,1,0,Header_ltype)

Stat= TableTextSizes(Table1,2,0,Label_text_width,Label_text_height,Label_slant)
Stat= TableTextOrigin(Table1,2,0,WsCenterText)
Stat= SetTableColors(Table1,2,0,Label_color)
Stat= SetTableLType(Table1,2,0,Label_ltype)

Stat= SetColor(Table1,Grid_color)
Stat= PlotEntity(Table1)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Tool_group)
Stat= DeleteEntity(Holes_group)

ActivateUndo
Stat= AdvanceUndoNumber("hole chart")
Stat= AddItemToUndo(WsUBCreateMode,Table1)

Leave:!
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Holes_group)
Stat= DeleteEntity(Tool_group)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
