
! script name: v3dwrap.d/cutwrapcont
! var_topic: cuts a contour wrapped around axis line

!A
!A NAME:
!A
!A 	Cut Wrap Contour	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script cuts a Contour wrapped about an axis line.
!A
!B INSTRUCTIONS:
!B
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path
!B 		    which is clockwise for an interior cut
!B 		    and counterclockwise for an exterior cut.
!B 		CL - Climb mill will generate a path which is
!B 		     clockwise for an exterior cut and
!B 		     counterclockwise for an interior cut.
!B
!B 	CUT ALONG Wall or Centerline:
!B
!B 		W - Wall cutting positions the cutter along
!B 		    the side of the contour as if it is a wall.
!B 		C - Centerline cutting will place the center
!B 		    of the tool along the wrapped contour.
!B
!B 		The angular orientation of the cutter is determined by the
!B 		normal vector to the cylinder.
!B
!B 	TOLERANCE: Enter the tolerance for the wrapping operation.
!B
!B 	RADIUS OF CYLINDER: Enter the radius
!B 		of cylinder to be wrapped around.
!B
!B 	STARTING ANGLE: Starting angular
!B 		coordinate of the wrapped entity.
!B
!B 	TAPER ANGLE: Enter the taper to be
!B 		applied along axis of wrapping cylinder.
!B
!B 	Z CUTTER DISPLACEMENT: Enter the Z displacement of
!B 		cutter relative to the Z or the wrapping cylinder.
!B
!B 	PICK CONTOUR TO BE WRAPPED: Pick the
!B 		contour to be wrapped.
!B
!C NOTES:
!C
!C 	The wrapping algorithm is as follows:
!C 	A point on the entity 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 entity becomes the Y coordinate
!C 	on the wrapped entity.
!C
!C 	The Z coordinate on the wrapping entity becomes a radial displacement
!C 	from the radius of the wrapping cylinder.
!C 	A positive Z adds to the radius. A negative Z subtracts from the radius.
!C 	The Taper modifies the Radius for increasing Y values.
!C
!C 	The wrapping entity is not a true development of the wrapped entity.
!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 Finish_var$[256]
DIM Radius_var$[256]
DIM Astart_var$[256]
DIM Taper_var$[256]
DIM Zdist_var$[256]
DIM Name$[256]

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


ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
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= SetAngleEquals(Taper)
Taper=InputAngle("TAPER ANGLE")
IF ExitScript(Destination$) THEN Leave
No_taper_var= LastVariableInput(Taper_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)
Nsign=1


Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: 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_taper_var THEN
        V3= FreeParameter("Taper",Taper)
ELSE
        V3= FreeParameter("Taper",Taper_var$)
END IF
IF No_zdist_var THEN
        V4= FreeParameter("Z Distance",Zdist)
ELSE
        V4= FreeParameter("Z Distance",Zdist_var$)
END IF
V5= FreeParameter("Side",Side)
V6= FreeParameter("Tolerance",Tol)
V7= FreeParameter("Direction",Direction)
V8= FreeParameter("Normal direction",Nsign)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V5,0.0,V1,0.0,1,V6)
Stat= MillDefZInfo(Mdef1,1,V4)
IF No_radius_var THEN
	V9= FreeParameter("Radius",Radius)
	G1=CutWrapCurve(Mdef1,Contour$,V9,V2,V3,V6,V8)
ELSE
	G1=CutWrapCurve(Mdef1,Contour$,Radius_var$,V2,V3,V6,V8)
END IF
Stat= AutoNameGroup(G1,Name$)
Stat= DeleteEntity(Mdef1)
Stat= SetNoPathFlag(G1,WsTrue)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(G1) THEN
        DelLastVarRecord
	Curr_record= 0
ELSE
        Stat= AnimateTool(Tool,G1)
	ActivateUndo
	Stat= AdvanceUndoNumber("cut wrap curve")
	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

