
! script name: vpksolids.d/vpkoffsetfaces
! var_solids: offset a list of faces to make a new sheet body

!A
!A NAME:
!A
!A 	Offset Faces  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script offsets a list of faces to form a new sheet body.
!A
!A~ image location - images/pkoffsetfaces.gif
!A
!B INSTRUCTIONS:
!B
!B	PICK FACES: Make body, Unpick ,Clear
!B
!B      NORMAL OFFSET: Enter the offset + positive normal, - negative normal.
!B
!B NOTES:
!B
!B	If the faces to be offset are connected in such a way that they
!B	form an outside corner then a gap will appear between the 
!B	edges. Either offset each surface separately and use a face fillet
!B	or fillet the connecting edge first then include the fillet with
!B	the rest of the faces.
!B
!# see also - htdocs/help.d/parasolids.htm

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

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

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


Pick_color=2

Stat= GLPlotOn
Start:!
Stat=SetInputFormat("n4=","")
Stat=SetDepthEquals(Offset)
Offset=InputDepth("NORMAL OFFSET")
IF ExitScript(Destination$) THEN Leave
No_offset_var= LastVariableInput(Offset_var$)

Num_faces= 0
Mark_group=CreateGroup("TEMPGROUP")

Face_loop:!
Stat=SetInputFormat("a1",",M,U,C,")
E1=WPKPickEntity("PICK FACES: Make body, Unpick ,Clear ",WsPKPickFace,Answer$)
IF (ExitScript(Destination$)) THEN Leave

SELECT Answer$
CASE "M"
	GOSUB Make_body
	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(Mark_group)
	IF N1= 0 THEN 
		Stat= DeleteEntity(Ps)
	END IF
	PlotAll
	GOTO Face_loop
CASE "C"
	Stat= DeleteGroup(Mark_group,WsTrue)
	Stat= DeleteEntity(Ps)
	PlotAll
	GOTO Start
END SELECT

IF Num_faces= 99 THEN Start
IF NOT ValidGeometry(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)
GOTO Face_loop

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


WPKClearErrorFlag
Stat= GenerateVarName("Body",Name$)
RecordFunctionOn
Stat= RecordDescr("SHEET: offset faces")
IF No_offset_var THEN
        V1= FreeParameter("Normal offset",Offset)
ELSE
        V1= FreeParameter("Normal offset",Offset_var$)
END IF
RecordFunctionOff
FOR I= 1 TO Num_faces
    RecordFunctionOff
    Face1= Face_array(I)
    Facename$= WPKEntityName(Face1)
    RecordFunctionOn
    IF I=1 THEN
        PkFaceGrp= WPKCreateGroup(Bodyname$,WsPKFaceEnt,Facename$)
    ELSE
        Stat= WPKAddToGroup(PkFaceGrp,Facename$)
    END IF
    RecordFunctionOff
NEXT I
RecordFunctionOn
Pksheet= WPKCopyFaceGroup(PkFaceGrp)
Pkbody=  WPKOffsetBody(Pksheet,V1)
Stat= WPKRemoveGroup(PkFaceGrp)
Stat= WPKNameBody(Pkbody,Name$)
Stat= WPKSetBodyParent(Pkbody)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

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

RETURN


Leave:!
PlotOn
Stat= DeleteGroup(Mark_group,WsTrue)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
