
! script name: v3dinter.d/entintoffsurf
! var_topic: entity intersect offset surface

!A
!A NAME:
!A
!A 	Entity Intersect Surface with offset  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will generate a point at the intersection of an entity and
!A 	a surface. The pick location will determine which intersection is to be
!A 	generated.
!A
!B INSTRUCTIONS:
!B
!B      PICK ENTITY: Pick the entity.  Note: The pick location is important.
!B
!B      PICK SURFACE : Pick the the surface.  Note: The pick location is
!B              important.
!B
!B      OFFSET FOR SURFACE : Enter the offset amount for the surface.
!B
!B
!C NOTES:
!C
!C 	The pick location should be near the desired intersection, failure
!C 	to do this may result in no point generated.
!C

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

DIM Destination$[256]
DIM Answer$[256]
DIM Offset_var$[256]
DIM Name1$[256]
DIM Name2$[256]
DIM Name$[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:!

Intersect2: !

Pick_entity:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n0","")
E1=PickEntity("PICK ENTITY ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_entity
Stat= EntityNameCheck(E1,Name1$)
T=LastPickPara

Pick_surf:!
Stat=SetPickMask(WsPickSurfOnly)
Surface=PickEntity("PICK SURFACE",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_surf
Stat= EntityNameCheck(Surface,Name2$)
UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surf

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Offset)
Offset=GetValue("OFFSET FOR SURFACE")
IF ExitScript(Destination$) THEN Leave
No_offset_var= LastVariableInput(Offset_var$)

norm_sign=1
IF ABS(Offset)>0.0 THEN
        GOSUB Orient_normal
END IF
Normal_sign=norm_sign
Stat=SetPickMask(WsPickGeometry)
U= XPointValue(UVPoint)
V= YPointValue(UVPoint)
Stat=DeleteEntity(UVpoint)

Stat= GenerateVarName(WsPointEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("POINT: Entity intersect surface")
Stat= SetRecDelLastExec
stat= Set3dInterOffset(0.0)
Stat= NullReference(Name1$)
Stat= NullReference(Name2$)
V1= FreeParameter("Tolerance",Tol)
IF No_offset_var THEN
	V2= FreeParameter("Offset",Offset)
ELSE
	V2= FreeParameter("Offset",Offset_var$)
END IF
V3= FreeParameter("Normal sign",Normal_sign)
G1=EntityIntSurf(Name1$,T,Name2$,U,V,V1,V2,V3)
Stat= AutoNameGroup(G1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(G1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("intersect pts")
	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,UVPoint,Normal_len,WsForward)
        Pforward=ParameterPt(Forward_normal,1.0)

        Reverse_normal=SurfNormalLine(Surface,UVPoint,Normal_len,WsReverse)
        Preverse=ParameterPt(Reverse_normal,1.0)

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

        Stat=SetColor(Forward_normal,Forward_color,WsTrue)
        Stat=SetColor(Pforward,Forward_color,WsTrue)
        Stat=SetColor(Reverse_normal,Reverse_color,WsTrue)
        Stat=SetColor(Preverse,Reverse_color,WsTrue)

RETURN

Leave:!
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(UVpoint)
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
