
! script name: v3dinter.d/osintersect
! var_topic: generate path of intersection of two offset surfaces

!A
!A NAME:
!A
!A 	Offset Intersect	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will generate the intersection of two surfaces
!A 	with an offset applied to each surface.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE #1: Pick the first surface.
!B
!B 	OFFSET FOR SURFACE #1: Enter the offset amount for surface #1.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #1 the offset is
!B 		applied on.  The normal vectors will be displayed to allow the
!B 		side to be picked.
!B
!B 	PICK SURFACE #2: Pick the second surface.
!B
!B 	OFFSET FOR SURFACE #2: Enter the offset amount for surface #2.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #2 the offset is
!B 		applied on.  The normal vectors will be displayed to allow the
!B 		side to be picked.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: offset intersect surfaces ")

DIM Destination$[256]
DIM Answer$[256]
DIM Surface_dir$[80]
DIM Name1$[256]
DIM Name2$[256]
DIM Name$[256]
DIM Offset1_var$[256]
DIM Offset2_var$[256]

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


ActivateUndo
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)
Forward_color=3
Reverse_color=4

Start:!
Normal_color=3
Direction_color=4

Intersect2:!

Pick_surf1:Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface1=PickEntity("PICK SURFACE #1",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface1) THEN Pick_surf1
Stat= EntityNameCheck(Surface1,Name1$)
Surface= Surface1
UVpoint1=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint1) THEN Pick_surf1
U=XPointValue(UVpoint1)
V=YPointValue(UVpoint1)

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Offset1)
Offset1=GetValue("OFFSET FOR SURFACE #1")
IF ExitScript(Destination$) THEN Leave
No_offset1_var= LastVariableInput(Offset1_var$)

norm_sign=1
IF ABS(Offset1)>0.0 THEN
	GOSUB Orient_normal
END IF
Surface1=Surface
Normal_sign1=norm_sign

Pick_surf2:Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n0","")
Surface2=PickEntity("PICK SURFACE #2",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface2) THEN Pick_surf2
Stat= EntityNameCheck(Surface2,Name2$)
Surface= Surface2
UVpoint2=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint2) THEN Pick_surf2
U=XPointValue(UVpoint2)
V=YPointValue(UVpoint2)

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Offset2)
Offset2=GetValue("OFFSET FOR SURFACE #2")
IF ExitScript(Destination$) THEN Leave
No_offset2_var= LastVariableInput(Offset2_var$)

norm_sign=1
IF ABS(Offset2)>0.0 THEN
	GOSUB Orient_normal
END IF
Surface2=Surface
Normal_sign2=norm_sign
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CURVE: Offset Intersect")
Stat= SetRecDelLastExec
stat= Set3dInterOffset(0.0)
IF No_offset1_var THEN
        V1= FreeParameter("Offset 1",Offset1)
ELSE
        V1= FreeParameter("Offset 1",Offset1_var$)
END IF
IF No_offset2_var THEN
        V2= FreeParameter("Offset 2",Offset2)
ELSE
        V2= FreeParameter("Offset 2",Offset2_var$)
END IF
V3= FreeParameter("Side 1",Normal_sign1)
V4= FreeParameter("Side 2",Normal_sign2)
V5= FreeParameter("Tolerance",Tol)
G1=IntersectSurfs(Name1$,V3,Name2$,V4,V5,V1,V2)
Stat= AutoNameGroup(G1,Name$)
Stat= SetNoPathFlag(G1,WsTrue)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(G1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("offset untersect")
	Stat= AddGroupToUndo(WsUBCreateMode,G1,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Intersect2

Orient_normal:!
	GOSUB ShowNormalDir
	Stat=SetInputFormat("n4","")
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE OF SURFACE",WsPickLines)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(L1) THEN Line_loop
	Norm_sign=0
	Pt=ParameterPt(L1,1.0)
	d1=DistTwoPts(Pt,Pforward)
	d2=DistTwoPts(Pt,Preverse)
	Norm_sign=1
	IF D2<D1 THEN Norm_sign=-1
	IF NOT Norm_sign THEN Pick_normal
	Stat=DeleteEntity(Forward_normal)
	Stat=DeleteEntity(Reverse_normal)
	Stat=DeleteEntity(Pt)
	Stat=DeleteEntity(Pforward)
	Stat=DeleteEntity(Preverse)
RETURN

ShowNormalDir:!
	Forward_normal=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
	Pforward=ParameterPt(Forward_normal,1.0)
	Stat=SetColor(Forward_normal,Forward_color)
	Stat=SetColor(Pforward,Forward_color)

	Reverse_normal=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
	Preverse=ParameterPt(Reverse_normal,1.0)
	Stat=SetColor(Reverse_normal,Reverse_color)
	Stat=SetColor(Preverse,Reverse_color)

	Stat= SetForceVisible(Forward_normal,WsTrue)
        Stat= SetForceVisible(Pforward,WsTrue)
        Stat= SetForceVisible(Reverse_normal,WsTrue)
        Stat= SetForceVisible(Preverse,WsTrue)
RETURN

Leave:!
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
