
! script name: v3dcpaths.d/threesurfpt
! var_topic: three surface contact point

!A
!A NAME:
!A
!A 	Curves: Three Surface Contact Point    VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will place a cutter in contact with three surfaces.
!A 	The pick location on each surface will determine the point generated.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER TYPE Ball, Endmill:
!B
!B 		B - Cutter is assumed to be a ball endmill.  Offset is
!B 		    measured from the center of the ball radius.  The bottom
!B 		    of the ball is the programmed point.
!B		E - Cutter is assumed to be a standard, flat-bottom endmill,
!B 		    with possibly rounded corners.  The programmed point is the
!B 		    center of the bottom of the cutter.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	CORNER RADIUS: Enter the corner radius of the endmill.
!B 		NOTE: This question is only asked for endmills.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!B 	PICK SURFACE #1: Pick the first surface.
!B 		NOTE: The pick location is important.
!B
!B 	PICK SURFACE #2: Pick the the second surface.
!B 		NOTE: The pick location is important.
!B
!B 	PICK SURFACE #3: Pick the third surface.
!B 		NOTE: The pick location is important.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: Three surface contact point")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Surface1$[256]
DIM Surface2$[256]
DIM Surface3$[256]
DIM Finish_var$[256]
DIM Name$[256]

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


ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)

Start:!
StdWorkplane
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CUTTER PATH TOOL",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave

Tool=FindUserData(Tool_name$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
LowerSelectBox

Past_dia:!
Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Finishr)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Finishr=Finish_allow
No_finish_var= LastVariableInput(Finish_var$)


Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)


Intersect2: StdWorkplane

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


Pick_surf2:!
Surface=PickEntity("PICK SURFACE #2",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_surf2
Stat= EntityNameCheck(Surface,Surface2$)
UVpoint2= LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint2) THEN Pick_surf2
Uvalue2= XPointValue(UVpoint2)
Vvalue2= YPointValue(UVpoint2)
stat= DeleteEntity(UVpoint2)
Surface2= Surface


Pick_surf3:!
Surface=PickEntity("PICK SURFACE #3",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_surf3
Stat= EntityNameCheck(Surface,Surface3$)
UVpoint3= LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint3) THEN Pick_surf3
Uvalue3= XPointValue(UVpoint3)
Vvalue3= YPointValue(UVpoint3)
stat= DeleteEntity(UVpoint3)
Surface3= Surface


Stat= GenerateVarName(WsPointEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("POINT: Three surface cutter contact point")
Stat= SetRecDelLastExec
Stat= NullReference(Surface1$)
Stat= NullReference(Surface2$)
Stat= NullReference(Surface3$)
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V4= FreeParameter("Tolerance",Tol)
UVpoint1= PointXYZ(Uvalue1,Vvalue1,0)
UVpoint2= PointXYZ(Uvalue2,Vvalue2,0)
UVpoint3= PointXYZ(Uvalue3,Vvalue3,0)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0,V1)
Stat=Setup3dCutter(Mdef1)
P1=IntThreeSurfs(Surface1$,UVpoint1,Surface2$,UVpoint2,Surface3$,UVpoint3,V4)
Stat= NameEntity(P1,Name$)
Stat=AssociateTool(P1,Tool)
Stat= DeleteEntityList(Mdef1,UVpoint1,UVpoint2,UVpoint3)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(P1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("3 surface pt")
	Stat= AddItemToUndo(WsUBCreateMode,P1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Intersect2



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

END
