
! script name: vsolids.d/cuttersolid
! var_topic: Make a solid of revolution out of a tool shape

!A
!A NAME:
!A
!A 	Cutter solid   VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will make a solid of revolution out of a tool shape.
!A
!B INSTRUCTIONS:
!B
!B 	TOOL: Select the tool.
!B
!B 	PLACEMENT POINT ABS X,Y,Z: Enter the X,Y,Z coordinates of the center
!B 		point of the cylinder.  NOTE: See XYZ INPUT OPTIONS.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: cutter solid")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Name$[256]
DIM Center$[256]
DIM Axis_line$[256]
DIM Toolid$[256]
DIM Name$[256]

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

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

ActivateUndo
Active_workplane=0
Work=CurrentWorkplane
IF (ValidGeometry(Work)) THEN Active_workplane=1
StdWorkplane

Start:!
Stat=ToolSelectBox("TOOLS","selected tool")
IF Stat THEN Leave
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
		LowerSelectBox
		GOTO Leave
END IF
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox



Pick_point:Stat=SetInputFormat("n4=","")
E1=PickEntity("PLACRMENT POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(E1) THEN Pick_point
IF NOT ValidDependency(E1) THEN Pick_point
T= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Center$)
Pc= ParameterReturnPt(E1,T,Pick_return)
Stat=SetEqualsValue(Radius)
Stat=SetLastPoint(Pc)
Stat= DeleteEntity(Pc)

IF NOT Axis_orient THEN
	
	Stat= GenerateVarName(WsRevolutionEnt,Name$)
	RecordFunctionOn
	Stat= RecordDescr("REVOLUTION: Cutter shape")
	Stat= SetRecDelLastExec
	SELECT Pick_return
	CASE WsGeometryEnd
       		 Pc= ParameterReturnPt(Center$,T,WsGeometryEnd)
	CASE WsGeometryMid
       		 Pc= ParameterReturnPt(Center$,T,WsGeometryMid)
	CASE WsGeomCenter
       		 Pc= ParameterReturnPt(Center$,T,WsGeomCenter)
	END SELECT
	Work= CurrentWorkplane
	Stat= SetWorkplane
	V1= PointXYZ(0.0,0.0,1.0)
	Axis_vector= TransformVector(V1,Work)
	Stat= DeleteEntity(V1)
	P2= AddVectors(Pc,Axis_vector)
	Axis_line= LineTwoPoints(Pc,P2)
	Tool=FindUserData(Tool_name$,WsToolData)
	Mdef1= ToolMillingDef(Tool,Mill_dir)
	Stat=Setup3dCutter(Mdef1)
	C1= CutterSolid(Axis_line,WsTrue)
	Stat= NameEntity(C1,Name$)
	Stat= DeleteEntityList(Mdef1,Axis_vector,Axis_line,Pc,P2)
	Stat= SetWorkplane(Work)
	RecordFunctionOff
	Curr_record= CurrentVarRecord
	CloseVarRecord
	IF NOT ValidGeometry(C1) THEN
       		 DelLastVarRecord
	ELSE
		ActivateUndo
		Stat= AdvanceUndoNumber("cutter solid")
		Stat= AddItemToUndo(WsUBCreateMode,C1)
		Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	END IF
ELSE
Pick_axis:!
	Stat=SetInputFormat("n4","")
	Axis_line=PickSpecificEnt("PICK AXIS LINE",WsPickLines)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidDependency(Axis_line) THEN Pick_axis
	Stat= EntityNameCheck(Axis_line,Axis_line$)

	T=LastPickPara
	IF T<=0.5 THEN
       		 Direction= WsForward
	ELSE
       		 Direction= WsReverse
	END IF

	Stat= GenerateVarName(WsRevolutionEnt,Name$)
	RecordFunctionOn
	Stat= RecordDescr("REVOLUTION: Cutter shape")
	Stat= SetRecDelLastExec
	SELECT Pick_return
	CASE WsGeometryEnd
          Pc= ParameterReturnPt(Center$,T,WsGeometryEnd)
        CASE WsGeometryMid
                 Pc= ParameterReturnPt(Center$,T,WsGeometryMid)
        CASE WsGeomCenter
                 Pc= ParameterReturnPt(Center$,T,WsGeomCenter)
        END SELECT
	V1= LineVector(Axis_line$)
	Axis_vector= ScaleVector(V1,Direction)
        P2= AddVectors(Pc,Axis_vector)
        Axis_line= LineTwoPoints(Pc,P2)
        Tool=FindUserData(Tool_name$,WsToolData)
        Mdef1= ToolMillingDef(Tool,Mill_dir)
        Stat=Setup3dCutter(Mdef1)
        C1= CutterSolid(Axis_line,WsTrue)
        Stat= NameEntity(C1,Name$)
        Stat= DeleteEntityList(Mdef1,Pc,Axis_vector,P2,Axis_line,V1)
        RecordFunctionOff
	Curr_record= CurrentVarRecord
        CloseVarRecord
        IF NOT ValidGeometry(C1) THEN
                 DelLastVarRecord
	ELSE
		ActivateUndo
		Stat= AdvanceUndoNumber("cutter solid")
		Stat= AddItemToUndo(WsUBCreateMode,C1)
		Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	END IF
END IF

GOTO Pick_point

Leave:!
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Axis_vector)
Stat= DeleteEntity(Axis_line)
Stat= DeleteEntity(P2)
LowerSelectBox
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

