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

!A
!A NAME:
!A
!A 	Cut a Wrapped Contour	VARIATIONAL
!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: Enter the radius of the cylinder to be wrapped
!B 		around.
!B
!B 	STARTING ANGLE: ENter the starting angular coordinate of the wrapped
!B 		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 	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 developement of the wrapped contour.
!C

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

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[256]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Rcontour$[256]
DIM Finish_var$[256]
DIM Radius_var$[256]
DIM Astart_var$[256]
DIM Zdist_var$[256]
DIM Name$[256]

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


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

ActivateUndo
Start:!
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")
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

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)
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:!
IF NOT Centerline_cut THEN
        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)
ELSE
        No_finish_var= 1
        Finish_allow=0
END IF

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

Ask_radius:Stat=SetInputFormat("n4=","")
Radius=InputRadius("RADIUS OF CYLINDER")
IF (ExitScript(Destination$)) THEN Leave
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_radius
        No_radius_var=0
        Stat= EntityNameCheck(C1,Radius_var$)
END IF

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

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

Ploop:!

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

Contour=ParentGroup(Ent1,0)
Etype=EntityType(Contour)
IF Etype<>WsGroupEnt THEN Ploop
IF NOT ValidDependency(Contour) THEN Ploop
Stat= EntityNameCheck(Contour,Contour$)
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","")
Rcontour=PickGroupEnt("PICK REVOLUTION PROFILE CONTOUR" )
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Rcontour) THEN Ploop
Stat= EntityNameCheck(Rcontour,Rcontour$)
Stat= SetPickMask(WsPickAll)

Nsign=1
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Revolution Wrap Curve")
Stat= SetRecDelLastExec
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_astart_var THEN
        V2= FreeParameter("Astart",Astart)
ELSE
        V2= FreeParameter("Astart",Astart_var$)
END IF
IF No_zdist_var THEN
        V3= FreeParameter("Z Distance",Zdist)
ELSE
        V3= FreeParameter("Z Distance",Zdist_var$)
END IF
V4= FreeParameter("Side",Side)
V5= FreeParameter("Tolerance",Tol)
V6= FreeParameter("Direction",Direction)
V7= FreeParameter("Normal direction",Nsign)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V4,0.0,V1,0.0,1,V5)
Stat= MillDefZInfo(Mdef1,1,V3)
IF No_radius_var THEN
        V8= FreeParameter("Radius",Radius)
        G1=CutRevWrapCurve(Mdef1,Contour$,Rcontour$,V8,V2,V5,V7)
ELSE
        G1=CutRevWrapCurve(Mdef1,Contour$,Rcontour$,Radius_var$,V2,V5,V7)
END IF
Stat= AutoNameGroup(G1,Name$)
Stat= SetNoPathFlag(G1,WsTrue)
Stat= DeleteEntity(Mdef1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(G1) THEN
        DelLastVarRecord
	Curr_record= 0
ELSE
	Stat= AnimateTool(Tool,G1)
	ActivateUndo
	Stat= AdvanceUndoNumber("cut rev wrap")
	Stat= AddGroupToUndo(WsUBCreateMode,G1,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
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

END


