
! script name: featureitems.d/featurizeface
! topic: apply features to a face of a part.

!A
!A NAME:
!A
!A 	Featurize the Face of a Part.
!A
!A DESCRIPTION:
!A
!A 	This script defines a plane by three unique points.
!A	It then allows the user to featurize the items in the plane.
!A
!B INSTRUCTIONS:
!B
!B 	FIRST POINT ABS X,Y,Z: Enter the X,Y,Z coordinates for the first
!B 		point that will be used to determine the plane.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	SECOND POINT ABS X,Y,Z: Enter the X,Y,Z coordinates for the second
!B 		point that will be used to determine the plane.
!B
!B 	THIRD POINT ABS X,Y,Z: Enter the X,Y,Z coordinates for the third point
!B 		that will be used to determine the plane.
!B
!C NOTES:
!C
!C 	The three points picked must be unique.
!C
!C
!# see also - points.d/xyzinput.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: featurize face ")

DIM Answer$[1024],Destination$[1024]
DIM Class_dir$[1024],Filename$[1024],Class_name$[1024]
DIM Name$[1024],Face_name$[1024]
DIM A$[1024],B$[1024]
DIM Item_dir$[1024],Item_class$[1024],Amotag_dir$[1024]
DIM Oldname$[1024]
DIM Data$[32000],Namelist$[32000]
DIM D_label$[1024],Sub_label$[1024],F_label$[1024],Choice$[1024],Mask$[32]
DIM Plane_file$[512]
DIM Face_file$[512]
DEG

DeActivateUndo
Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Item_dir$)
Highlight_color= 8
Planar= WsTrue
Min_angle= 90
Units= CurrentUnits
IF Units= WsMetric THEN
	Tolerance= 0.010
ELSE
	Tolerance= 0.005
END IF

Amotag_dir$= FileNameString(Item_dir$,"AmoTags.d")
Plane_file$= FileNameString(Item_dir$,"PlaneProperty/FacePlane")
Face_file$= FileNameString(Item_dir$,"PlanarItems/Face")

Start:!
Existing_items:!
Namelist$=""
Item_group= GrpOfUserItems
Group= PropertyClassGroup(Item_group,"Planar Face Item")
Stat= DeleteEntity(Item_group)
IF NOT GroupCount(Group) THEN Plane_loop

Stat=UserSelectBox("EXISTING FACES","Face selected",0,WsPropGroupSelect,Group)
Pick_item:!
Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT ITEM ID NAME",Name$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Name$
CASE "CANCEL"
        Stat= DeleteEntity(Group)
        GOTO Plane_loop
END SELECT

N1= POS(Name$,")")
IF N1<2 THEN Pick_prop
A$= Name$[1,N1-1]
IF NOT IsStringNumeric(A$) THEN Pick_prop
Num1= VAL(A$)
Face_ent= GroupElement(Group,Num1)
IF NOT ValidGeometry(Face_ent) THEN Start
Stat= DeleteEntity(Group)
LowerSelectBox
F_id_number= UserDataType(Face_ent)
GOTO Start_conc


Plane_loop:!
Done_circles= 0
Stat=SetInputFormat("a99","",0)
Stat=GetValue("DIRECTIONAL FACE NAME",Answer$)
IF ExitScript(Destination$) THEN Leave
Stat= ReplaceString(Answer$,",","-",Face_name$)

Stat=SetInputFormat("n4","")

P1=XyzInput("FIRST POINT",WsXyzInput)
IF ExitScript(Destination$) THEN Leave

P2=XyzInput("SECOND POINT",WsXyzInput)
IF ExitScript(Destination$) THEN Leave

P3=XyzInput("THIRD POINT",WsXyzInput)
IF ExitScript(Destination$) THEN Leave

T1=ThreePtTrans(P1,P2,P3)
Stat=SetWorkplane(T1)

Plane1=ThreePointPlane(P1,P2,P3)

Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)
Stat=DeleteEntity(P3)

Id_number=GetExtUserValue(Plane_file$,"Class id number:")
Stat=GetExtUserString(Plane_file$,"Name:",Name$)
Prop1= ReadUserData(Id_number,Name$,Plane_file$)
Stat=AssocUserData(Plane1,Prop1,Id_number)

F_id_number= NextItemNumber
Face_ent= ReadUserData(F_id_number,Face_name$,Face_file$)
Stat=AssocUserData(Plane1,Face_ent,F_id_number)
Stat=PutUserString(Face_ent,"Name:",Face_name$)

Start_conc:!
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Start_cir:!

New_item:!
D_label$="PROPERTY CLASSES"
Choice$="CLASS SELECTED"
Stat=DirListBox(Amotag_dir$,"*",D_label$,Choice$,WsAscending)

Stat=SetInputFormat("a99","")
Stat=GetValue("CHOOSE TYPE OF FEATURE , PLANE ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="PLANE" THEN
	LowerSelectBox
	IF NOT Done_circles THEN
		Stat= DeleteEntity(Face_ent)
		Stat= DeleteEntity(Plane1)
		Stat= DeleteEntity(Prop1)
	END IF
	GOTO Plane_loop
END IF
IF Answer$="CANCEL" THEN Leave
Answer$= TRIM$(Answer$)
Class_dir$= FileNameString(Amotag_dir$,Answer$)
Item_class$= Answer$

Ask_file:!
D_label$= Item_class$
Choice$= Item_class$&" SELECTED"
Stat=DirListBox(Class_dir$,"*",D_label$,Choice$,WsAscending)

File_name:stat= SetInputFormat("a99","")
Stat= GetValue("CHOOSE SPECIFIC FEATURE ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Start_cir
IF NOT LEN(Answer$) THEN File_name
Oldname$= Answer$
Filename$=FileNameString(Class_dir$,Oldname$)

LowerSelectBox

Edit_item:!
Stat=GetExtUserString(Filename$,"Name:",Name$)
Item_ent= ReadUserData(1,Name$,Filename$)
Stat= DisplayMessage(" ")
Input_stat= MultiEditDataStr(Item_ent,":")
IF (ExitScript(Destination$)) THEN Leave_script
IF Input_stat=27 THEN Leave
IF Input_stat=1 THEN Edit_item
Stat=GetUserString(Item_ent,"Name:",Name$)

Circle_dia:!
Stat=SetInputFormat("n4=","")
Diameter=InputRadius("CIRCLE DIAMETER",WsDiaInput)
IF ExitScript(Destination$) THEN Leave

Pick_entity:!
Group=CreateGroup("TEMPGROUP")

Vnum=PickView("SELECT VIEW FOR SELECTION BOX")
IF (ExitScript(Destination$)) THEN Leave
Stat= SetInputFormat("n4b","")
Stat=PickBox(Group,Vnum,WsHighlightPen)
IF (ExitScript(Destination$)) THEN 
	Stat=DeleteEntity(Group)
	IF POS(Destination$,"SCRIPTSTART") THEN
		IF NOT Done_circles THEN
			Stat= DeleteEntity(Face_ent)
			Stat= DeleteEntity(Plane1)
			Stat= DeleteEntity(Prop1)
		END IF
		GOTO Plane_loop
	END IF
	IF POS(Destination$,"BACKUP") THEN Start_cir
	GOTO Leave
END IF

IF NOT GroupCount(Group) THEN 
	Stat=DeleteEntity(Group)
	GOTO Start_cir
END IF


Cgroup= ConcentricGroups(Group,Diameter,Tolerance,WsFalse,Planar,Min_angle)
Stat=DeleteEntity(Group)
IF NOT GroupCount(Cgroup) THEN Start_cir

Stat= MakeDistinctItems(Cgroup,Item_ent)
Stat= AssocUserData(Cgroup,Face_ent,F_id_number)
Stat= SetSelectedFlag(Cgroup,WsTrue)
Stat= PlotEntity(Cgroup)

Done_circles= 1
Stat=DeleteSubGroups(Cgroup)

GOTO Pick_entity

Leave:!
LowerSelectBox
UnSelectAll
IF NOT Done_circles THEN
	Stat= DeleteEntity(Face_ent)
	Stat= DeleteEntity(Plane1)
	Stat= DeleteEntity(Prop1)
END IF
Stat=DeleteEntity(Group)
Stat=DeleteEntity(Item_ent)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)
Stat=DeleteEntity(P3)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
