
! script name: 3drender.d/shade3dcpath
! 3dtopic: syncut for 3D cutter paths

!A
!A NAME:
!A
!A 	Syncut for 3D cutter paths
!A
!A DESCRIPTION:
!A
!A 	This script is for simulating machining of 3D Cutterpaths.
!A
!B INSTRUCTIONS:
!B
!B 	PICK VIEW FOR DISPLAY: Pick the view window in which the shaded image
!B 		will appear.
!B
!B
!B 	INITIAL BLOCK FILE NAME, None, Files: A block which has been previously
!B 		machined and stored away can be recalled at this point. All
!B 		subsequent machining will be done on this block.
!B
!B 		N - None will use a full rectangular block.
!B 		F - Files will popup a file selection box.
!B
!B 	Continue, No stops, Exit: This question is
!B 		encountered during the syncut process.  Syncut will normally
!B 		pause between tools.
!B
!B 		C - Continue will start the next tool processing.		
!B 		The new image will be in the current view orientation.
!B 		N - No stops means Syncut will not stop after each tool.
!B 		It will only stop on the final image.
!B 		M - Re machines the block.
!B		X - slices block parallel to x axis.
!B		Y - slices block parallel to y axis.
!B 		E - Exit gives the option to leave syncut.
!B
!B 	STORED BLOCK FILE NAME, None: Allows the machined block to be
!B 		stored. This is not an image; it is the shape of the block
!B 		after the machining process has occurred. It is not a geometry
!B 		file and can only be recalled by syncut related options.
!B
!C NOTES:
!C
!C 	Syncut simulates the machining of a part by calculating the removal
!C 	of material by each tool.  The resolution of the machining calculation
!C 	is specified when the block is defined.  Images of the machined block
!C 	may be Coarse, Medium, or Fine. The script defaults to Medium images
!C 	for intermediate results and Fine images for final results.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: shade 3d cutterpath ")

DIM Destination$[256]
DIM Answer$[256]
DIM Jobname$[256]
DIM Jobname1$[256]
DIM Stored_block$[256]
DIM Store_dir$[256]
DIM Name1$[256]
DIM Space$[20]
DIM Name$[256]
DIM Dir$[256]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Choice$[256]
DIM Mask$[32]
DIM Block_dir$[256]


IF ARGC< 2 THEN
	Do_stored= 0
ELSE
	IF ARGC> 2 THEN
		Do_stored= VAL(ARGV(2))
	END IF
END IF

DeActivateUndo
Space$=" "
Jobname$="shade"
Stat=NamedVarStr("storage dir",WsSystemVars,"/usr/weber/ST/",Block_dir$)
Stat=NamedVarStr("pics dir",WsSystemVars,"/usr/weber/ST/pics/",Store_dir$)
Start:!

Stat= GLPlotOn
Tool_stops=1
Tol=NamedVarVal("3D Shade Tol",Ws3DSystemVars,0.06)

Cutter_type=WsBallCutter
Offset=0
Taper=0
Finish_allow=0
Corner_rad=0.0000
Cutter_rad=0.25
Frequency= 10

StdWorkplane
Stat=Setup3dCutter(Cutter_type,Cutter_rad,Corner_rad,Offset,Finish_allow,Taper)

Group=CreateGroup("TEMP")
Stat=SetInputFormat("n4a1=",",N,")
Stat=SetEqualsValue(frequency)
Frequency=GetValue("UPDATE FREQUENCY, None ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="N" THEN 
	Frequency=0
END IF

Pick_loop:!
Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4a1",",P,A,")
Path_ent=PickEntity("PICK CUTTERPATH, All, Proceed",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="P" THEN Shade_start
IF Answer$="A" THEN
	Stat=DeleteEntity(Group)
	Group=GrpByEntType(WsCPathFileEnt)

	Stat=SetInputFormat("n4","")
	Vnum=PickView("PICK VIEW FOR DISPLAY")
	IF ExitScript(Destination$) THEN Leave

	GOTO Shade_start
END IF
IF NOT ValidGeometry(Path_ent) THEN Pick_loop

Etype=EntityType(Path_ent)
SELECT Etype
CASE WsCPathFileEnt
	Stat=AddToGroup(Group,Path_ent)
	Stat=Highlight(Path_ent,WsHighlightPen,0)
CASE WsHoleEnt
	Stat=AddToGroup(Group,Path_ent)
	Stat=Highlight(Path_ent,WsHighlightPen,0)
CASE ELSE
	GOTO Pick_loop
END SELECT

Vnum=LastPickView

GOTO Pick_loop


Shade_start:Stat=SetPickMask(WsPickGeometry)
GOSUB Stored_block

IF Stored_block$="N" THEN
	Stat=InitSimulation(WsShadedImage,0,"",Part_group,Fixture_group,WsTrue)
	Stat=Setup3dShade(Vnum,Tol)
ELSE
	Stat=DisplayMessage("IMAGING INITIAL BLOCK SHAPE ")
	Stat=InitSimulation(WsShadedImage,0,Stored_block$)
	Stat=Setup3dShade(Vnum,Tol)
	Stat=ShadeBlock(Name1$,WsFine,Part_group,Fixture_group)
END IF


Num_elements=GroupCount(Group)
Tool_stops=0
FOR I=1 TO Num_elements
	Element1=GroupElement(Group,I)
	Grp1=CreateGroup("TEMP")
	Stat=AddToGroup(Grp1,Element1)
	Stat=DisplayMessage("MACHINING CUTTER PATH "&VAL$(I))
	Stat=MachineBlock(Grp1,Frequency,WsMedium)
	Stat=DisplayMessage("IMAGING CUTTER PATH "&VAL$(I))
	IF I=Num_elements THEN 
		Res1=WsFine
		Name1$=Jobname1$
		Tool_stops=1
		Stat=DisplayMessage("MACHINING COMPLETE ")
	ELSE
		Res1= WsFine
		Name1$=""
	END IF
	Stat=DeleteEntity(Grp1)


IF Tool_stops  THEN
Stat=ShadeBlock("",WsFine,Part_group,Fixture_group,WsAutoFitOff)
Tool_stop:Stat=SetInputFormat("a2=",",C,E,N,X,Y,S,NI,M,")
        Stat=SetEqualsValue(0,"C")
        Stat=GetValue("Continue, No stops, Exit ",Answer$)
        IF ExitScript(Destination$) THEN 
		GOSUB Exit_simulation
                Stat=DeleteEntity(Group)
		GOTO Leave
	END IF
        SELECT Answer$
	CASE "N"
		Tool_stops=0
	CASE "NI"
		Tool_stops=0
	CASE "S"
		Do_stored=1
                GOSUB Exit_simulation
                Stat=DeleteEntity(Group)
                GOTO Leave
	CASE "M"
                GOSUB Exit_simulation
                GOTO Shade_start
        CASE "E"
		GOSUB Exit_simulation
                Stat=DeleteEntity(Group)
                GOTO Leave
        CASE "C"
                Stat=DisplayMessage("   ")
	CASE "X"
		Pt=XyzInput("INPUT",WsXyOnly)
		IF ExitScript(Destination$) THEN Leave
		P1=ExplodeBlockRow(Pt,1)
		Stat=DeleteEntity(Pt)

		Contour1= CreateGroup("X Slice",WsContourGroup)
		Stat= AddToGroup(Contour1,P1)
		Stat= AutoNumberCont(Contour1)

		GOTO Tool_stop
	CASE "Y"
		Pt=XyzInput("INPUT",WsXyOnly)
		P1=ExplodeBlockRow(Pt,0)
		Stat=DeleteEntity(Pt)

		Contour1= CreateGroup("Y Slice",WsContourGroup)
                Stat= AddToGroup(Contour1,P1)
		Stat= AutoNumberCont(Contour1)

		GOTO Tool_stop
        END SELECT
END IF

NEXT I 
GOSUB Exit_simulation
Stat=LowerShade("data")
Stat=DeleteEntity(Group)
GOTO Start


Exit_simulation:!
IF Do_stored THEN
   Stat=SetInputFormat("a99=","")
   Stat=SetEqualsValue(0,Stored_block$)
   Stat=GetValue("STORED BLOCK FILE NAME, None, "&Space$&"(MAX= 10) ",Stored_block$)
   IF ExitScript(Destination$) THEN Leave
   IF POS(Stored_block$," ") THEN
                Space$=" (NO SPACES) "
                GOTO Question
   ELSE
                Space$=" "
   END IF
ELSE
	Stored_block$="N"
END IF
IF Stored_block$="N" OR POS(Stored_block$,"None") THEN
	Stat=EndSimulation
ELSE
	IF NOT POS(Stored_block$,"/") THEN
       		 Stored_block$=FileNameString(Block_dir$,Stored_block$)
	END IF
	Stat=EndSimulation(Stored_block$)
END IF
RETURN



Stored_block:!
IF Do_stored THEN
   Stat=SetInputFormat("a99","")
   Stored_block$="N"
   Stat=SetEqualsValue(0,Stored_block$)
   Stat=GetValue("INITIAL BLOCK FILE NAME, None, Files, "&Space$&"(MAX= 10) ",Stored_block$)
   IF ExitScript(Destination$) THEN Leave
   IF POS(Stored_block$," ") THEN
         Space$=" (NO SPACES) "
         GOTO Stored_block
   ELSE
         Space$=" "
   END IF
   IF Stored_block$="F"  OR POS(Stored_block$,"Files") THEN
	Dir$=Block_dir$
	D_label$="DIRECTORY"
	Sub_label$="SUB DIRECTORIES"
	F_label$="FILES"
	Choice$="FILE TO OPEN"
	Mask$="[A-z]*"
	Stat=UserFileBox(Dir$,Mask$,D_label$,Sub_label$,F_label$,Choice$)
	GOTO Stored_block
   END IF
   LowerFileBox
   IF Stored_block$="N"  OR POS(Stored_block$,"None") THEN Stored_block$="N"
   IF Stored_block$="N" THEN RETURN
   IF NOT POS(Stored_block$,"/") THEN 
   	Stored_block$=FileNameString(Block_dir$,Stored_block$)
   END IF
ELSE 
	Stored_block$="N"
END IF
RETURN






Leave:!
LowerFileBox
Stat=SetInputFormat("n4","")
Stat=LowerShade("data")
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Group)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
