
! script name: solids.d/cuttersolid
! 3dtopic: Make a solid of revolution out of a tool shape

!A
!A NAME:
!A
!A 	Cutter solid
!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 Datastr$[1256]
DIM Boundary_name$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Mach_def$[256]
DIM Contour$[256]
DIM Finish_var$[256]
DIM Depth_var$[256]
DIM Name$[256]

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

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

Fin_allow:!

CALL DefineTool(Tool_name$,Toolid$)

Pick_point:!
Pc=XyzInput("PLACEMENT POINT",WsXyzInput)
IF ExitScript(Destination$) THEN Leave

IF NOT Axis_orient THEN
	
	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)

ELSE
Pick_axis:!
	A_line=PickSpecificEnt("PICK AXIS LINE",WsPickLines)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(A_line) THEN pick_axis
	T= LastPickPara

	Axis_vector= LineVector(A_line)
	If T> 0.5 THEN
		V1= ScaleVector(Axis_vector,(-1))
		Stat= DeleteEntity(Axis_vector)
		Axis_vector= V1
	END IF
	P2= AddVectors(Pc,Axis_vector)
	Axis_line= LineTwoPoints(Pc,P2)

END IF

Smooth= WsTrue

Path1= CutterSolid(Axis_line,Smooth)
ActivateUndo
Stat= AdvanceUndoNumber("revolution")
Stat= AddItemToUndo(WsUBCreateMode,Path1)

Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Axis_vector)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(Axis_line)

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

SUB DefineTool(Name$,Toolid$)

Tool=FindUserData(Name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN 
	Mach_def$= ""
	SUBEXIT
END IF

Tip_angle=0
Taper= 0
Corner_chamfer=0
Crad=0
Shank_dia=0
D3=0
D4=0

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

SELECT Toolid$
CASE "DRILL"
	Cutter_type= WsDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "CENTER DRILL"
	Cutter_type= WsCenterDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
        Shank_dia=GetUserValue(Tool,"Body diameter:")
	Taper= 90- Tip_angle/2
CASE "TWIST DRILL"
	Cutter_type= WsTwistDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "TAPERED DRILL"
	Cutter_type= WsTaperedDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=GetUserValue(Tool,"Taper angle:")
CASE "COUNTER SINK"
	Cutter_type= WsCounterSink
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Overall_len=GetUserValue(Tool,"Overall length:")
        Shank_len=GetUserValue(Tool,"Shank length:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Flute_length=Overall_len-Shank_len
CASE "COUNTERBORE"
	Cutter_type= WsCounterBore
	Crad=GetUserValue(Tool,"Corner radius:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Pilot_dia=GetUserValue(Tool,"Pilot diameter:")
        Pilot_len=GetUserValue(Tool,"Pilot length:")
	D3= Pilot_dia
	D4= Pilot_len
CASE "TAP"
	Cutter_type= WsTap
	Thread_len=GetUserValue(Tool,"Thread length:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Taper=0
	Tip_angle= 10
	Radius= 0.5*Tdia
	Crad= 0.5*Radius
	Flute_length= Thread_len
CASE "REAMER"
	Cutter_type= WsReamer
        Chamfer_length=GetUserValue(Tool,"Corner chamfer:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=10
	Radius= 0.5*Tdia
	Taper_length= 0.5*Radius
        Chamfer_angle=45
	Crad= Chamfer_length
	Tip_angle= Chamfer_angle
	D3= Taper_length
CASE "BALLMILL"
	Cutter_type= WsBallCutter
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "BULLMILL"
	Cutter_type= WsBullmill
	Crad=GetUserValue(Tool,"Corner radius:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "ENDMILL"
	Cutter_type= WsEndmill
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "TAPERMILL"
	Cutter_type= WsTaperedEndmill
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=GetUserValue(Tool,"Taper angle:")
	Crad=GetUserValue(Tool,"Corner radius:")
CASE "CHAMFERMILL"
	Cutter_type= WsChamferTool
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Radius= 0.5*Tdia
        Tip_radius=GetUserValue(Tool,"Tip diameter:")/2
	Chamfer_angle=GetUserValue(Tool,"Taper angle:")
	Chamfer_length= (Radius- Tip_radius)/TAN(Chamfer_angle)
	Crad= Chamfer_length
	Tip_angle= Chamfer_angle
CASE "THREADER"
	Cutter_type= WsThreadedMill
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Flute_length=GetUserValue(Tool,"Thread length:")
CASE "BORING BAR"
	Cutter_type= WsBoringBar
        Crad=GetUserValue(Tool,"Corner radius:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
END SELECT


Radius= 0.5*Tdia
Shank_radius= 0.5*Shank_dia
Stat=Setup3dCutter(Cutter_type,Radius,Crad,0.0,0.0,Taper,Flute_length,Tip_angle,Shank_radius,Total_length,D3,D4)


SUBEND
