
! script name: 3drender.d/syncut2
! 3dtopic: fast syncut for simulated machining

!A
!A NAME:
!A
!A 	Fast Syncut
!A
!A DESCRIPTION:
!A
!A 	Simulated machining of a rectangular block.
!A 	Fast syncut does not pause or reimage between tools.
!A
!B INSTRUCTIONS:
!B
!B 	PICK VIEW FOR DISPLAY: Pick the view window in which the shaded image
!B 		will appear.
!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
!B 		the block after the machining process has occurred. It is
!B 		not a geometry file and can only be recalled by syncut
!B 		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: fast syn-cut ")

DIM Destination$[256]
DIM Answer$[256]
DIM Jobname$[256]
DIM Jobname1$[256]
DIM Toolname$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Name$[256]
DIM Dir$[256]
DIM Data_name$[256]
DIM Space$[20]
DIM Operations$[4000]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Choice$[256]
DIM Mask$[32]
DIM Stored_block$[256]
DIM Store_dir$[256]
DIM Block_dir$[256]

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


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:!

Tool_stops=0
Stat= GLPlotOn

Tol=NamedVarVal("3D Shade Tol",Ws3DSystemVars,0.06)

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

Tool_group=CreateGroup("TEMP")
Task_ent=FindUserData("AMO OPERATIONS",WsTaskPacket)
IF ValidGeometry(Task_ent) THEN
	Amo_group=PropGrpFromFields(Task_ent,"Number tools:","Tool ",1,WsToolData)
ELSE
	Amo_group=FindNamedGrp("AMO OPERATIONS")
END IF
IF ValidGeometry(Amo_group) THEN
	Num_tools=GroupCount(Amo_group)
	FOR I=1 TO Num_tools
		Tool=GroupElement(Amo_group,I)
		IF ValidGeometry(Tool) THEN 
			Stat=GetUserDataName(Tool,Data_name$)
			Is_tool=FindUserData(Data_name$,WsToolData)
			IF ValidGeometry(Is_tool) THEN 
				Stat=AddToGroup(Tool_group,Tool)
			END IF
		END IF
Next_entry:!
	NEXT I
	IF ValidGeometry(Task_ent) THEN Stat= DeleteEntity(Amo_group)

	IF NOT GroupCount(Tool_group) THEN Pick_loop
ELSE

Pick_loop:!
	Stat=ToolSelectBox("TOOLS","selected tool")
	IF Stat=1 THEN Leave

	Stat=SetInputFormat("a99","")
	Stat=GetValue("INPUT TOOL ID NAME, Proceed ",Answer$)
	IF ExitScript(Destination$) THEN Leave
	IF Answer$="CANCEL" THEN Leave
	IF Answer$="P" THEN Shade_start
	IF Answer$="p" THEN Shade_start
	IF Answer$="Proceed" THEN Shade_start
	Name$=Answer$

	Tool=FindUserData(Name$,WsToolData)
	IF NOT ValidGeometry(Tool) THEN Pick_loop

	Stat=AppendToGroup(Tool_group,Tool)

	GOTO Pick_loop
END IF


Shade_start:Stat=SetPickMask(WsPickGeometry)
LowerSelectBox
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$,Fixture_group,WsTrue)
        Stat=Setup3dShade(Vnum,Tol)
        Stat=ShadeBlock(Name$,WsMedium,Part_group,Fixture_group)
END IF

Tool_stops=0
Frequency= 1

Num_tools=GroupCount(Tool_group)
FOR I=1 TO Num_tools

	Tool=GroupElement(Tool_group,I)
	IF NOT ValidGeometry(Tool) THEN Next_tool

	Group1= GrpByTool(Tool)
        Holes_group= GrpByEntType(group1,WsHoleEnt)
        Cpath_group= GrpByEntType(group1,WsCPathFileEnt)
	IF ValidGeometry(Cpath_group) THEN
                Frequency= 10
        ELSE
                Frequency= 1
        END IF
        IF ValidGeometry(Holes_group) THEN
                 Group2= SubtractGrps(Group1,Holes_group)
                 Stat= DeleteEntity(Group1)
                 IF ValidGeometry(Group2) THEN
                           Data_group= HighestParentGroup(Group2)
                           Stat= DeleteEntity(Group2)

                           Stat= CombineGroups(Data_group,Cpath_group)
                           Stat= CombineGroups(Data_group,Holes_group)
                           Stat= DeleteEntity(Holes_group)
                           Stat= DeleteEntity(Cpath_group)
                 ELSE
                        Data_group= Holes_group
                        Stat= CombineGroups(Data_group,Cpath_group)
                        Stat= DeleteEntity(Cpath_group)
                 END IF
        ELSE
                Data_group= HighestParentGroup(Group1)
                IF ValidGeometry(Data_group) THEN
                    Stat= CombineGroups(Data_group,Cpath_group)
                    Stat= DeleteEntity(Group1)
                    Stat= DeleteEntity(Cpath_group)
                ELSE
                        Data_group= Cpath_group
                END IF
        END IF

	IF NOT ValidGeometry(Data_group) THEN Next_tool

	Stat=Setup3dCutter(Tool)

	Stat=DisplayMessage("MACHINING TOOL "&Toolname$)
	Stat=MachineBlock(Data_group,Frequency,WsFine)
	Stat=DisplayMessage("IMAGING TOOL "&Toolname$)
	IF I>=Num_tools THEN 
		Res1=WsFine
		Name$=Jobname1$
		Tool_stops=1
		Stat=DisplayMessage("MACHINING COMPLETE ")
		Stat=Setup3dShade(Vnum,Tol)
		Stat=ShadeBlock(Name$,Res1,Part_group,Fixture_group,WsAutoFitOff,WsTrue)
	END IF
	Stat=DeleteEntity(Data_group)

IF Tool_stops  THEN
Stat=ShadeBlock("",WsFine,Part_group,Fixture_group,WsAutoFitOff)
Tool_stop:Stat=SetInputFormat("a1=",",C,E,N,S,X,Y,M,")
	Stat=SetEqualsValue(0,"C")
        Stat=GetValue("Continue, No stops, Exit ",Answer$)
	IF ExitScript(Destination$) THEN
                GOSUB Exit_simulation
                Stat=DeleteEntity(Tool_group)
                GOTO Leave
        END IF
	SELECT Answer$
	CASE "N"
		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(Tool_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_tool: !
NEXT I 
GOSUB Exit_simulation
Stat=LowerShade("data")
Stat=DeleteEntity(Tool_group)
GOTO Leave


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

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




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