
! script name: vpksolids.d/vpktaperface
! var_solids: apply taper to a list of faces

!A
!A NAME:
!A
!A 	Taper Face VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script applies taper to a list of faces.
!A
!A~ image location - images/pktaperface.gif
!A
!B INSTRUCTIONS:
!B
!B	TAPER ANGLE: Input the taper angle.
!B
!B	PICK FACE TO BE TAPERED: Make body, Unpick ,Clear
!B
!B	PICK REFERENCE ENTITY FOR THIS FACE: Pick the reference entity for
!B 		this face. The reference entity is usually an edge which
!B 		remains fixed while the taper is applied to the face.
!B
!B NOTES:
!B
!B	The taper direction is determined by the positive z axis of
!B	the current workplane.
!B
!B	Taper may be applied to a horizontal face. It must be remembered
!B	that is this case the face is already at 90 degrees. So that
!B	a taper of 80 degrees will cant the face 10 degrees from the 
!B	horizontal. It may be necessary to input angles like 280
!B	to achieve some taper conditions.
!B
!B	Taper can usually be applied to a single face if it makes
!B	sharp corners with its adjacent faces. If the face is curved
!B	in z, the isocline curves should be created to split the face.
!B	The taper can then be applied to the lower face with the isocline
!B	curve as the reference entity.
!B
!B	Taper should be applied before radius blends if possible. It
!B	is much easier to apply taper to faces having sharp intersections.
!B	If blends are already in place, Delete Blends may remove the
!B	blend radius so taper is easier to apply.
!B		
!B	The following methods are available:
!B
!B		0,1) PK_taper_method_isocline_c (default)
!B		2)   PK_taper_method_curve_c
!B		3)   PK_taper_method_surface_c
!B		4)   PK_taper_method_offset_c
!B
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: taper faces ")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
REAL Face_array(100)
REAL Ref_array(100)
DIM Name$[256]
DIM Refname$[256]
DIM Facename$[256]
DIM Bodyname$[256]
DIM Angle_var$[256]

ActivateUndo
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Pick_color=2
Ref_color=4

Stat= GLPlotOn
Start:!
Method=NamedVarVal("solid taper face method",WsPKSolidsVars,1.0)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Angle)
Angle=GetValue("TAPER ANGLE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)

Num_faces= 0
Mark_group=CreateGroup("TEMPGROUP")
Ref_group=CreateGroup("TEMPGROUP")


Face_loop:!
Stat=SetInputFormat("a1",",A,U,C,")
E1=WPKPickEntity("PICK FACE TO BE TAPERED: Apply, Unpick ,Clear ",WsPKPickFace,Answer$)
IF (ExitScript(Destination$)) THEN Leave

SELECT Answer$
CASE "A"
	GOSUB Apply_taper
	GOTO Start
CASE "U"
	Num_faces= Num_faces-1
	IF Num_faces<0 THEN Num_faces=0
	N1= GroupCount(Mark_group)
	E1= GroupElement(Mark_group,N1)
	Stat=RemoveFromGrp(Mark_group,N1)
	Stat= DeleteEntity(E1)
	
	N1= GroupCount(Ref_group)
        E1= GroupElement(Ref_group,N1)
        Stat=RemoveFromGrp(Ref_group,N1)
        Stat= DeleteEntity(E1)

	PlotAll
	GOTO Face_loop
CASE "C"
	Stat= DeleteGroup(Mark_group,WsTrue)
	Stat= DeleteGroup(Ref_group,WsTrue)
	PlotAll
	GOTO Start
END SELECT

IF Num_faces= 99 THEN Start
IF NOT WPKIsEntity(E1) THEN Face_loop

UVpoint=LastUVSurfPick(E1)
IF NOT ValidGeometry(UVpoint) THEN face_loop
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
Stat= DeleteEntity(UVPoint)

Face1= WPKEntityTag(E1)
FOR I= 1 TO Num_faces
	IF Face1= Face_array(I) THEN Face_loop
NEXT I
Num_faces= Num_faces+1
Face_array(Num_faces)= Face1
Pt= SurfParameterPt(E1,U,V)
Stat= SetColor(Pt,Pick_color,WsTrue)
Stat= AddToGroup(Mark_group,Pt)

E1=WPKPickEntity("PICK REFERENCE ENTITY FOR THIS FACE",WsPKPickAll)
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Pick_face

Ref1= WPKEntityTag(E1)
Ref_array(Num_faces)= Ref1

Pt= ParameterPt(E1,0.5)
Stat= SetColor(Pt,Ref_color,WsTrue)
Stat= AddToGroup(Ref_group,Pt)
GOTO Face_loop

Apply_taper:!
Stat= DeleteGroup(Mark_group,WsTrue)
Stat= DeleteGroup(Ref_group,WsTrue)
Face1= Face_array(1)
Bodyname$= WPKBodyName(Face1)

WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("TAPER: taper faces")
V1= FreeParameter("Method",Method)
IF No_angle_var THEN
	V2= FreeParameter("Taper angle",Angle)
ELSE
	V2= FreeParameter("Taper angle",Angle_var$)
END IF
RecordFunctionOff
FOR I= 1 TO Num_faces
    RecordFunctionOff
    Face1= Face_array(I)
    Ref1=  Ref_array(I)
    Facename$= WPKEntityName(Face1)
    Refname$= WPKEntityName(Ref1)
    RecordFunctionOn
    IF I=1 THEN
	PkFaceGrp= WPKCreateGroup(Bodyname$,WsPKFaceEnt,Facename$)
	PkRefGrp= WPKCreateGroup(Bodyname$,0,Refname$)
    ELSE
    	Stat= WPKAddToGroup(PkFaceGrp,Facename$)
    	Stat= WPKAddToGroup(PkRefGrp,Refname$)
    END IF
    RecordFunctionOff
NEXT I
RecordFunctionOn
Stat= WPKTaperFace(PkFaceGrp,PkRefGrp,V2,V1)
Stat= WPKRemoveGroup(PkFaceGrp)
Stat= WPKRemoveGroup(PkRefGrp)
Stat= WPKNameBody(Bodyname$,Bodyname$)
Stat= WPKSetBodyParent(Bodyname$,1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("taper faces")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck
RETURN


Leave:!
V1=CreateVariable("solid taper face method",WsPKSolidsVars,Method)
Stat= DeleteGroup(Mark_group,WsTrue)
Stat= DeleteGroup(Ref_group,WsTrue)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
