
! script name: 3dwrap.d/cutrevwrap
! 3dtopic: cuts a contour wrapped around a surface of revolution

!A
!A NAME:
!A
!A 	Cut Revolution Wrap Contour
!A
!A DESCRIPTION:
!A
!A 	This script cuts a Contour wrapped about a surface of revolution.
!A
!B INSTRUCTIONS:
!B
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path which is
!B 		    clockwise for an interior cut and counterclockwise
!B 		    for an exterior cut.
!B 		CL - Climb mill will generate a path which is clockwise for
!B 		    an exterior cut and counterclockwise for an interior cut
!B
!B 	CUT ALONG Wall or Centerline:
!B
!B 		W - Wall cutting positions the cutter along the side of the
!B 		    contour as if it is a wall.
!B 		C - Centerline cutting will place the center of the tool along
!B 		    the wrapped contour.
!B
!B 		The angular orientation of the cutter is determined by the
!B 		normal vector to the wrapping cylinder.
!B
!B 	TOLERANCE: Enter the tolerance for the wrapping operation.
!B
!B 	RADIUS OF CYLINDER: Radius of cylinder to be wrapped around.
!B
!B 	STARTING ANGLE: Starting angular coordinate of the wrapped entity.
!B
!B 	Z CUTTER DISPLACEMENT: Z Displacement of cutter relative to 
!B 		the z or the wrapping cylinder.
!B
!B 	PICK CONTOUR TO BE WRAPPED: Pick the contour to be wrapped.
!B
!B 	PICK REVOLUTION PROFILE CONTOUR: Pick contour defining the profile
!B 		of the surface of revolution.
!B
!C NOTES:
!C
!C 	The wrapping algorithm is as follows:
!C
!C 	A point on the contour to be wrapped has an X,Y,Z coordinate
!C 	relative to the current workplane. The X coordinate determines
!C 	the angular displacement around the wrapping cylinder.
!C 	X=0 will be mapped into the starting angle relative to the
!C 	wrapping cylinder. X= 2*PI*R will be mapped into one full
!C 	revolution around the wrapping cylinder from the starting angle.
!C 	X= 2*PI*R*A/360 will be mapped into a point at an angle A
!C 	relative to the starting angle.
!C
!C 	The Y coordinate on the wrapping contour becomes the Y coordinate
!C 	on the wrapped contour.
!C
!C 	The Z coordinate on the wrapping contour becomes a radial displacement
!C 	from the radius of the surface of revolution.
!C 	Z=0 corresponds to the surface of revolution.
!C 	A positive Z adds to the radius. A negative Z subtracts from the radius.
!C
!C 	The wrapping contour is not a true development of the wrapped contour.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: cut wrapped contour ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Tool_type$[256]

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Patch_width=NamedVarVal("3D Default Patch",Ws3DSystemVars,0.5)
Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0

Stat=SetInputFormat("a1=",",W,C,")
Stat=SetEqualsValue(0,"W")
Stat=GetValue("CUT ALONG Wall or Centerline",Answer$)
IF ExitScript(Destination$) THEN Leave

Centerline_cut=(Answer$="C")

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

Surface_dir$="F" 

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

Pen_color=CurrentPen
IF NOT Centerline_cut THEN
	Stat=SetInputFormat("a2=",",CL,C,")
	Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
	Stat=SetEqualsValue(0,Answer$)
	Mill_dir=GetValue("MILL CLimb OR Conventional",Answer$)
	IF ExitScript(Destination$) THEN Leave
	IF Answer$="C" THEN
		Mill_dir=WsConventional
		Pen_color=WsConvCutPen
	ELSE
		Mill_dir=WsClimb
		Pen_color=WsClimbCutPen
	END IF
END IF

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=WsEndMill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Taper=GetUserValue(Tool,"Taper angle:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
		    CASE "BALLMILL"
			Cutter_type=WsBallCutter
			Cutter_dia=GetUserValue(Tool,"Diameter:")
		    CASE ELSE
			GOTO Tool_ques
		END SELECT

		LowerSelectBox
	END IF
END IF
Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

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

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)

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

Radius=InputRadius("RADIUS OF CYLINDER")
IF ExitScript(Destination$) THEN Leave

Astart=InputAngle("STARTING ANGLE")
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zdist)
Zdist=GetValue("Z CUTTER DISPLACEMENT")
IF ExitScript(Destination$) THEN Leave

Ploop:!

Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Ent1=PickEntity("PICK CONTOUR TO BE WRAPPED")
IF ExitScript(Destination$) THEN Leave

Contour=ParentGroup(Ent1,0)
Etype=EntityType(Contour)
IF Etype<>WsGroupEnt THEN Ploop
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Stat=SetPickMask(WsPickAll)

Direction=WsForward
Side=0
IF NOT Centerline_cut THEN

	Pt=InputFreePoint("PICK WHICH SIDE")
	IF ExitScript(Destination$) THEN Leave

	Side=OffsetLeftRight(Ent1,Pt)

	Direction_sign=MillDirection(Ent1,Pt)
	Stat=DeleteEntity(Pt)

	IF Direction_sign=1 AND Mill_dir=WsConventional THEN
		Direction=WsReverse
	END IF
	IF Direction_sign=-1 AND Mill_dir=WsClimb THEN
		Direction=WsReverse
	END IF
END IF

Stat=SetCurrentColor(Pen_color)

Stat= SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n4","")
Zcontour=PickGroupEnt("PICK REVOLUTION PROFILE CONTOUR" )
IF ExitScript(Destination$) THEN Leave
Stat= SetPickMask(WsPickAll)


Nsign=1

New_contour=CutRevolutionWrap(Contour,Zcontour,Radius,Astart,Zdist,Nsign,Side,Direction,Tol)

IF ValidGeometry(New_contour) THEN
	Stat=AutoNumberCont(New_contour)
	IF Tool_assoc THEN
		Stat=AssociateTool(New_contour,Tool)
	END IF

        Stat= AnimateTool(Tool,New_contour)

	ActivateUndo
	Stat= AdvanceUndoNumber("cut rev wrap")
	Stat= AddGroupToUndo(WsUBCreateMode,New_contour,1,WsTrue)

END IF

GOTO Ploop

Leave:!
Stat=DeleteEntity(Pt)
Stat=SetPickMask(WsPickAll)
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
