
! script name: 3dcpaths.d/cutintersect
! 3dtopic: drives a cutter along the intersection of two surfaces

!A
!A NAME:
!A
!A 	Curves: Intersect
!A
!A DESCRIPTION:
!A
!A 	This script generates the path of intersection of a cutter in contact
!A 	with two surfaces.
!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 	PICK SURFACE # 1: Pick the first surface to be used for generating the
!B 		path of intersection.
!B
!B 	PICK SURFACE # 2: Pick the second surface to be used for generating the
!B 		path of intersection.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: cut intersect surfaces ")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[80]

ActivateUndo
IF ARGC<2 THEN
	Fast_intersect=0
ELSE
        Fast_intersect= VAL(ARGV(1))
END IF

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

Start:!
Units= CurrentUnits
Cutter_type= WsBallCutter
Offset= 0
Taper= 0

StdWorkplane

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)

Tool_assoc=0
Tool_val=FindNamedVar("tool association",WsToolVars)
IF ValidGeometry(Tool_val) THEN Tool_assoc=GetVariableVal(Tool_val)

IF NOT Tool_assoc THEN
	GOSUB Get_mill_type
	GOSUB Get_cutter_dia
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
        IF Stat THEN
		GOSUB Get_mill_type
                GOSUB Get_cutter_dia
                Tool_assoc=0
        ELSE
Tool_ques:!
                Stat=SetInputFormat("a99","")
                Stat=GetValue("CUTTER PATH TOOL",Tool_name$)
                IF (ExitScript(Destination$)) THEN Leave
                IF Tool_name$= "CANCEL" THEN
                        LowerSelectBox
			GOSUB Get_mill_type
                        GOSUB Get_cutter_dia
                        Tool_assoc=0
                        GOTO Past_dia
                END IF
                Tool=FindUserData(Tool_name$,WsToolData)

                Stat=GetUserString(Tool,"Tool type:",Tool_type$)

		 SELECT Tool_type$
                    CASE "ENDMILL","BULLMILL"
                        Cutter_type=WsEndMill
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Corner_rad=GetUserValue(Tool,"Corner radius:")
                    CASE "TAPERMILL"
                        Cutter_type=WsTaperedEndmill
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Taper=GetUserValue(Tool,"Taper angle:")
                        Corner_rad=GetUserValue(Tool,"Corner radius:")
                        Length=GetUserValue(Tool,"Flute length:")
                    CASE "TAPERBALL"
                        Cutter_type=WsTaperedBall
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Taper=GetUserValue(Tool,"Taper angle:")
                        Length=GetUserValue(Tool,"Flute length:")
                    CASE "BALLMILL"
                        Cutter_type=WsBallCutter
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                    CASE ELSE
                        GOTO Tool_ques
                END SELECT

                LowerSelectBox
        END IF
END IF
Past_dia:!
Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

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


VCutter_type=CreateVariable("cutter type",WsSystemVars,Cutter_type)
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VCorner=CreateVariable("corner radius",WsSystemVars,Corner_rad)

stat= Setup3dCutter(Cutter_type,Cutter_rad,Corner_rad,Offset,Finish_allow,Taper,Length)

Intersect2:StdWorkplane

Pick_loop:!

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE #1",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_loop
Surface1=Surface


Surface=PickEntity("PICK SURFACE #2",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_loop
Surface2=Surface

Stat=SetPickMask(WsPickGeometry)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave

Normal_sign1= 1
Normal_sign2= 1
IF Fast_intersect THEN

Uvpoint1= PointXYZ(0.5,0.5,0.0)
Uvpoint2= PointXYZ(0.5,0.5,0.0)

Path_group= FastIntSurfs(Surface1,Normal_sign1,UVpoint1,Surface2,Normal_sign2,UVpoint2,Tol)

Stat= DeleteEntity(Uvpoint1)
Stat= DeleteEntity(Uvpoint2)

ELSE

Path_group=IntersectSurfs(Surface1,Normal_sign1,Surface2,Normal_sign2,Tol)

END IF



IF ValidGeometry(Path_group) THEN
	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	Stat=GroupName(Path_group,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  INTERSECT"&CHR$(10)
	Stat=SurfaceName(Surface1,Answer$)
	Datastr$=Datastr$&"SURFACE 1: "&Answer$&CHR$(10)
	Stat=SurfaceName(Surface2,Answer$)
	Datastr$=Datastr$&"SURFACE 2: "&Answer$&CHR$(10)
	Datastr$=Datastr$&"S1: "&VAL$(S1)&CHR$(10)
	Datastr$=Datastr$&"S2: "&VAL$(S2)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&CHR$(10)
	Datastr$=Datastr$&"OFFSET: "&VAL$(Offset)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CORNER RAD: "&VAL$(Corner_rad)&CHR$(10)
	Datastr$=Datastr$&"TAPER: "&VAL$(Taper)&CHR$(10)
	Datastr$=Datastr$&"CUTTER TYPE: "&VAL$(Cutter_type)&CHR$(10)
	Datastr$=Datastr$&"FAST INTERSECT: "&VAL$(Fast_intersect)&CHR$(10)

	Cpath3d=FindUserData(Name$,Ws3dCpath)
	IF ValidGeometry(Cpath3d) THEN Stat= DeleteEntity(Cpath3d)
	Cpath3d=ReadUserData(Ws3dCpath,Name$,Datastr$)

	Stat=AssocUserData(Path_group,Cpath3d,Ws3dCpath)
	IF Tool_assoc THEN
		Stat=AssociateTool(Path_group,Tool)
		Number=GroupCount(Path_group)
		FOR I=1 TO Number
			GrpTool=GroupElement(Path_group,I)
			Stat=AssociateTool(GrpTool,Tool)
		NEXT I
	END IF

	ActivateUndo
        Stat= AdvanceUndoNumber("cut intersect")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)

END IF

GOTO Intersect2

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

Get_mill_type:!
	SELECT Cutter_type
	    CASE WsBallCutter
		Answer$="B"
	    CASE WsEndMill
		Answer$="E"
	END SELECT
	Stat=SetEqualsValue(0,Answer$)
	Stat=SetInputFormat("a1=",",B,E,")
	Stat=GetValue("CUTTER TYPE Ball, Endmill",Answer$)
	IF ExitScript(Destination$) THEN Leave

	SELECT Answer$
	    CASE "B"
		Cutter_type=WsBallCutter
	    CASE "E"
		Cutter_type=WsEndMill
	END SELECT
RETURN

Get_cutter_dia:!
	Stat=SetInputFormat("n4=","")
	Cutter_dia=NamedVarVal("cutter diameter",WsSystemVars,0.0)
	Stat=SetRadiusEquals(Cutter_dia/2)
	Cutter_dia=InputRadius("CUTTER DIAMETER",WsDiaInput)
	IF ExitScript(Destination$) THEN Leave

	IF Cutter_type=WsEndMill THEN
		Corner_rad=NamedVarVal("corner radius",WsSystemVars,corner_rad)
		Stat=SetEqualsValue(Corner_rad)
		Corner_rad=GetValue("CORNER RADIUS",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Corner_rad=Corner_rad
	END IF
RETURN

END
