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

!A
!A NAME:
!A
!A 	Cutter Solid  
!A
!A DESCRIPTION:
!A
!A 	This script will make a solid  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
!B 		coordinates of the center point of the cylinder.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!# see also - points.d/xyzinput.hlp
!# see also - htdocs/help.d/parasolids.htm

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


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

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=","")
Stat=SetInputFormat("n4","")
Pc=XyzInput("PLACEMENT POINT ",WsXyzInput)
IF ExitScript(Destination$) THEN Leave
Stat=SetLastPoint(Pc)

Stat= GLPlotOn
IF NOT Axis_orient THEN
	Pt= PtIncrXyz(Pc,0.0,0.0,1.0)
	Axis_line= LineTwoPoints(Pc,Pt)
	
	Mdef1= ToolMillingDef(Tool,Mill_dir)
	Stat=Setup3dCutter(Tool)

	WPKClearErrorFlag
	C1= WPKCutterSolid(Axis_line,WsForward)

	Stat= DeleteEntity(Pc)
	Stat= DeleteEntity(Pt)
	Stat= DeleteEntity(Axis_line)
	Stat= DeleteEntity(Mdef)

	ActivateUndo
	Stat= AdvanceUndoNumber("cutter solid")
	Stat= AddItemToUndo(WsUBPKSessionMark)
	WPKErrorCheck

ELSE
Pick_axis:!
	Stat=SetInputFormat("n4","")
	Axis_line=PickSpecificEnt("PICK AXIS LINE",WsPickLines)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(Axis_line) THEN Pick_axis

	T=LastPickPara
	IF T<=0.5 THEN
       		 Direction= 1
	ELSE
       		 Direction= -1
	END IF


	V1= LineVector(Axis_line)
        Axis_vector= ScaleVector(V1,Direction)
        Pt= AddVectors(Pc,Axis_vector)
        Axis_line= LineTwoPoints(Pc,Pt)

	WPKClearErrorFlag
	Mdef1= ToolMillingDef(Tool,Mill_dir)
        Stat=Setup3dCutter(Tool)
        C1= WPKCutterSolid(Axis_line)

	Stat= DeleteEntity(Pc)
	Stat= DeleteEntity(Pt)
	Stat= DeleteEntity(Axis_vector)
	Stat= DeleteEntity(V1)
	Stat= DeleteEntity(Axis_line)
	Stat= DeleteEntity(Mdef)

	ActivateUndo
	Stat= AdvanceUndoNumber("cutter solid")
	Stat= AddItemToUndo(WsUBPKSessionMark)
	WPKErrorCheck
END IF

GOTO Pick_point

Leave:!
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(Pt)
LowerSelectBox
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

