
! script name: 3dcpaths.d/normdriveslice
! 3dtopic: slices surface using normal lines to a drive curve

!A
!A NAME:
!A
!A 	Gouge: Normal Drive Curve Slice Gouge 
!A
!A DESCRIPTION:
!A
!A 	This script cuts a group of surfaces. Each slice is generated as  
!A 	follows: The cutter center is locked on a line normal to a 
!A	point on the drive curve in a constant Z plane. The cutter is
!A	then moved along the line until it comes in contact with the part.
!A	As the lines sweep around the drive curve the contact points 
!A	generate the slice.
!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.  It is this point which
!B 		    is kept away the part boundary.
!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 	WIDTH OF CUT: Enter the actual width of cut.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	Z START: Start of first pass in Z.
!B
!B 	Z END: Enter the last pass in Z.
!B
!B 	Z STEP: Enter the Z depth increment.
!B
!B 		The Z depth calculation is controlled by the user named
!B 		variable Z Step Method.  There are two methods available:
!B 			WsEqualZSteps - 0: Equalized steps in Z.
!B 			WsFullZDepth -  1: Each Z pass is at full depth
!B 				increment, only the last Z pass is smaller.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!B 	PICK DRIVE CONTOUR : Pick the drive  contour.
!B
!B 	NORMAL EXTENSION: Specify the distance normal to the drive curve.
!B
!B 	PICK WHICH SIDE OF CONTOUR: This pick does two things.  It determines
!B 		the side of the contour the normal lines are created. Also, a
!B 		pick close to the contour means the cutter finds contact from
!B 		the contour out to the end of the normal line.  A pick toward
!B 		the end of the normal means the contour will travel toward the
!B 		contour looking for contact.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!C NOTES:
!C
!C 	Normal Slice Rules:
!C
!C 	It is assumed that it the cutter is placed on the end of the normal
!C 	line it is clear of interference with the part Normal slice does not
!C 	work well with standing bosses and islands.  It leaves uncut areas.
!C 	This routine will not cut areas that are in the shadow of the cutter
!C 	as determined by the normal lines to the drive curve.
!C
!C 	An additional feature is auto edge extension.  If the auto edge extend
!C 	switch is set on an edge, the edge will extend a distance equal to the
!C 	extension dist (a named variable), along the tangent to the surface on
!C 	the edge.  Then the normal vector controlling the cutter will roll
!C 	through 90 deg of arc.  This feature helps insure that the gouge check
!C 	will work in tight corners, where the cutter paths extend beyond the
!C 	edge of the part.
!C
!C 	Circle output is controlled by the named variable "3D Circle Output".
!C
!C 	There are two methods available:
!C
!C 		WsLinearMoves - 0: Outputs straight line moves.
!C 		WsCircleMoves - 1: Outputs circular arc moves.
!C
!C 	Gouge Level:
!C
!C 		WsContactCheck -   0: Fastest- Checks cutter contact point only.
!C 		WsCutterCylinder - 1: Slower- Checks cutter "cylinder".
!C 				The cylinder generated by the cutter diameter.
!C 		WsFullGougeCheck - 2: Slowest- Treats the cutter as a surface
!C 				of revolution and calculates the intersection
!C 				between the part and the cutter surface.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: normal drive slice ")

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

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4
Units=CurrentUnits

Gouge_level= 0
Make_cpaths= WsTrue

IF ARGC<2 THEN
        Gouge_level= WsContactCheck
ELSE
        Gouge_level= VAL(ARGV(1))
END IF


Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0
Tool_assoc=0
Shank_radius=0
Flute_length=1
Total_length= 1

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)
Tool_assoc=NamedVarVal("tool association",WsToolVars,0.0)
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$)

		Flute_length=GetUserValue(Tool,"Flute length:")
		Total_length=GetUserValue(Tool,"Tip to holder:")

		SELECT Tool_type$
		    CASE "ENDMILL","BULLMILL"
			Cutter_type=WsEndMill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
			Shank_dia=GetUserValue(Tool,"Shank diameter:")
			Shank_radius= 0.5*Shank_dia
		    CASE "TAPERMILL"
			Cutter_type=WsEndMill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Taper=GetUserValue(Tool,"Taper angle:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
			Shank_dia=GetUserValue(Tool,"Shank diameter:")
			Shank_radius= 0.5*Shank_dia
		    CASE "TAPERBALL"
                        Cutter_type=WsTaperedBall
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Taper=GetUserValue(Tool,"Taper angle:")
			Shank_dia=GetUserValue(Tool,"Shank diameter:")
			Shank_radius= 0.5*Shank_dia
		    CASE "BALLMILL"
			Cutter_type=WsBallCutter
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Shank_dia=GetUserValue(Tool,"Shank diameter:")
			Shank_radius= 0.5*Shank_dia
		    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,Flute_length,0.0,Shank_radius,Total_length)


Z_level: !

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zend)
Zend=GetValue("Z END",Answer$)
IF ExitScript(Destination$) THEN Leave

IF Zend=Zstart THEN
        Zstep=0
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Zstep)
        Zstep=GetValue("Z STEP",Answer$)
        IF ExitScript(Destination$) THEN Leave
END IF


Pick_loop:!

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,")
Part_group=PickGroupEnt("PICK SURFACE GROUP, Name ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN
        GOSUB Get_named_group
END IF
IF NOT ValidGeometry(Part_group) THEN Pick_loop


Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)

Cut_part:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Element=PickEntity("PICK DRIVE CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
Para_value= LastPickPara
Contour= ParentGroup(Element,0)
IF NOT ValidGeometry(Contour) THEN Cut_part
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Norm_ext:Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(ABS(Norm_length))
Norm_length=GetValue("NORMAL EXTENSION")
IF ExitScript(Destination$) THEN Leave
Norm_length= ABS(Norm_length)
IF Norm_length< 0.0001 THEN Norm_ext
Normal_len= Norm_length

GOSUB Which_side
Norm_length= Norm_sign*Norm_length

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

Z_Group=ZStepsPtGrp(ZStepMethod,Zstart,Zend,Zstep)

Work= CurrentWorkplane
Cutterpath=NormalDriveGouge(Work,Part_group,Contour,Z_group,Norm_length,Tol,Norm_dir,Circle_output,Gouge_level,Make_cpaths)

Stat= DeleteGroup(Z_group,WsTrue)

IF ValidGeometry(Cutterpath) THEN
	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	IF Make_cpaths THEN
		Stat=CPathFileName(Cutterpath,Name$)
	ELSE
		Stat=GroupName(Cutterpath,Name$)
	END IF
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  Z NORMAL CURVE SLICE"&CHR$(10)
	Stat=GroupName(Part_group,Answer$)
	Datastr$=Datastr$&"SURFACE GROUP: "&Answer$&CHR$(10)
	Stat=GroupName(Contour,Answer$)
	Datastr$=Datastr$&"CONTOUR : "&Answer$&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"OFFSET: "&VAL$(Offset)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
	Datastr$=Datastr$&"CORNER RAD: "&VAL$(Corner_rad)&CHR$(10)
	Datastr$=Datastr$&"NORMAL LENGTH: "&VAL$(Norm_length)&CHR$(10)
	Datastr$=Datastr$&"NORMAL DIR: "&VAL$(Norm_dir)&CHR$(10)
	Datastr$=Datastr$&"TAPER: "&VAL$(Taper)&CHR$(10)
	Datastr$=Datastr$&"CUTTER TYPE: "&VAL$(Cutter_type)&CHR$(10)
	Datastr$=Datastr$&"CIRCLES: "&VAL$(Circle_output)&CHR$(10)
	Datastr$=Datastr$&"GOUGE LEVEL: "&VAL$(Gouge_level)&CHR$(10)
	Datastr$=Datastr$&"MAKE CPATHS: "&VAL$(Make_cpaths)&CHR$(10)
	Datastr$=Datastr$&"Z START: "&VAL$(Zstart)&CHR$(10)
        Datastr$=Datastr$&"Z END: "&VAL$(Zend)&CHR$(10)
        Datastr$=Datastr$&"Z STEP: "&VAL$(Zstep)&CHR$(10)
        Datastr$=Datastr$&"Z STEP METHOD: "&VAL$(Zstepmethod)&CHR$(10)
        Datastr$=Datastr$&"Z ROUGH METHOD: "&VAL$(Zroughmethod)&CHR$(10)

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

	Stat=AssocUserData(Cutterpath,Cpath3d,Ws3dCpath)
	IF Tool_assoc THEN Stat=AssociateTool(Cutterpath,Tool)

	ActivateUndo
        Stat= AdvanceUndoNumber("normal drive slice")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutterpath,1,WsTrue)

END IF

GOTO Start

Leave:!
LowerSelectBox
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
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

ShowNormalDir:!
	Forward_normal= ParameterLine(Element,Para_value,Normal_len,WsNormal)
        Pforward=ParameterPt(Forward_normal,1.0)

	Reverse_normal= ParameterLine(Element,Para_value,-Normal_len,WsNormal)
        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)
        Stat=SetColor(Pforward,Forward_color)
        Stat=SetColor(Reverse_normal,Reverse_color)
        Stat=SetColor(Preverse,Reverse_color)
RETURN

Which_side:!
        GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE OF CONTOUR",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
	T1= LastPickPara
        Norm_sign=0
        Pc=ParameterPt(Forward_normal,0.0)
        Pt=ParameterPt(L1,T1)
        D1=DistTwoPts(Pt,Pforward)
        D2=DistTwoPts(Pt,Preverse)
        D3=DistTwoPts(Pt,Pc)
	IF D1<= D2 THEN
        	Norm_sign=-1
		IF IsDirReversed(Element) THEN Norm_sign= 1
		Norm_dir= WsInside
		IF D1< D3 THEN Norm_dir= WsOutside
	ELSE
        	Norm_sign=1
		IF IsDirReversed(Element) THEN Norm_sign= -1
		Norm_dir= WsInside
		IF D2< D3 THEN Norm_dir= WsOutside
	END IF
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
RETURN
	
Get_named_group:!
Stat=ListGrpNames("SURFACE GROUPS","selected group","",WsSurfaceGroup)
IF Stat=1 THEN Leave_name

Stat=SetInputFormat("a99","")
Stat=GetValue("SURFACE GROUP NAME",Answer$)

IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave_name
Part_group=FindNamedGrp(Answer$)
IF NOT ValidGeometry(Part_group) THEN Get_named_group
Grp_type= GroupType(Part_group)
IF Grp_type <> WsSurfaceGroup THEN Get_named_group
LowerSelectBox

Leave_name:!
Stat=SetInputFormat("n4","")

RETURN


END
