
! script name: vcontours.d/grindstock
! var_topic: add grind stock to a contour line.

!A
!A NAME:
!A
!A 	Add Grinding Stock		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script adds grinding stock to selected elements of a contour.
!A	It has provisions to remove small corner radii from the contour
!A	so that they don't interfere with this proceedure.
!A
!A~ image location - images/grindstock.gif
!A
!B INSTRUCTIONS:
!B
!B	GRINDING STOCK: Thickness of stock
!B 		allowance for grinding.
!B
!B 	MINIMUM RADIUS FOR SHARP CORNER: Input
!B 		the minimum radius to be used on corners.  All radii
!B 		smaller than this amount will be made into sharp corners.
!B
!B	PICK AN ELEMENT OF CONTOUR: Pick an element
!B 		of the contour that the stock is to be added to.
!B
!B	PICK WHICH SIDE:   Pick the side the
!B 		stock is to be added.
!B
!B	PICK ELEMENT, Backup, Proceed: Pick all the
!B 		elements which are to have the same grinding stock.
!B
!C NOTES:
!C
!C 	This script is designed to be used when the contour to be machined
!C 	may have been designed externally to synergy or it is necessary
!C 	for drafting purposes the first draw the finished shape.
!C 	This script allows the grinding stock needed for machining to
!C 	be added without having to reconstruct the geometry of the 
!C 	contour.
!C


stat=ChangeLabelStr("ScriptNameStatus","Function: grinding stock ")

DIM Destination$[256]
DIM Answer$[256]
DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Contour$[256]
DIM Element$[256]
DIM Radius_var$[256]
DIM Stock_var$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)


ActivateUndo
Offset:!
Stat= SetInputFormat("n4=","")
Stat=SetEqualsValue(0.025)
Stock=GetValue("GRINDING STOCK",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_stock_var=LastVariableInput(Stock_var$)

Min_radius:!
Stat=SetEqualsValue(0.05)
Radius=InputRadius("MINIMUM RADIUS FOR SHARP CORNERS")
IF ExitScript(Destination$) THEN Leave
No_radius_var=LastVariableInput(Radius_var$)
C1=LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Min_radius
        No_radius_var=0
        Stat=EntityNameCheck(C1,Radius_var$)
END IF


First_pick:!
Element=PickEntity("PICK THE ELEMENT OF CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
Contour= ParentGroup(Element)
IF NOT ValidDependency(Contour) THEN First_pick
Stat=EntityNameCheck(Contour,Contour$)
IF NOT IsEntVariational(Contour) THEN
   Stat=SetInputFormat("a99","")
   Stat= GetValue("NON-VDM ENTITY. CANNOT USE ON NON VDM ENTITY ",Answer$)
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF
Stat= SetSelectedFlag(Element,WsTrue)
Stat= PlotEntity(Element)
Group= CreateGroup("TEMPGROUP")
Stat= AddToGroup(Group,Element)

Pt=InputFreePoint("PICK WHICH SIDE")
IF (ExitScript(Destination$)) THEN Leave

Side=OffsetLeftRight(Element,Pt)
Side= -Side
Stat= DeleteEntity(Pt)


Ploop:!
Stat=SetInputFormat("a1",",B,P,")
E1=PickEntity("PICK ELEMENT, Backup, Proceed",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "B"
	Element_num=GroupCount(Group)
      	Temp_e1=GroupElement(Group,Element_num)
      	IF NOT ValidGeometry(Temp_e1) THEN Ploop
      	Stat= SetSelectedFlag(Temp_e1,WsFalse)
      	Stat=PlotEntity(Temp_e1)
      	Stat=RemoveFromGrp(Group,Element_num)
CASE "P"
      	Stat= SetSelectedFlag(Group,WsFalse)
      	Stat=PlotEntity(Group)
	GOTO Add_stock
END SELECT
IF NOT ValidDependency(E1) THEN Ploop
Etype= EntityType(E1)
SELECT Etype
CASE WsLineEnt
	N1= ElementGrpNum(Contour,E1)
	IF N1 THEN
		Stat= AppendToGroup(Group,E1)
		Stat= SetSelectedFlag(E1,WsTrue)
	END IF
CASE WsPointEnt
	Stat= AppendToGroup(Group,E1)
	Stat= SetSelectedFlag(E1,WsTrue)
END SELECT
Stat=PlotEntity(E1)
GOTO Ploop


Add_stock:!

Num_elements= GroupCount(Group)
Stat=GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Add Grind Stock")
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
G1= CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Num_elements
        E1= GroupElement(Group,I)
        IF ValidGeometry(E1) THEN
                Stat= EntityNameCheck(E1,Element$)
                RecordFunctionOn
                Stat= AppendToGroup(G1,Element$)
                RecordFunctionOff
        END IF
NEXT I
RecordFunctionOn
IF No_radius_var THEN
        V1=FreeParameter("Min Corner Radius",Radius)
ELSE
        V1=FreeParameter("Min Corner Radius",Radius_var$)
END IF
IF No_stock_var THEN
        V2=FreeParameter("Grind stock",Stock)
ELSE
        V2=FreeParameter("Grind stock",Stock_var$)
END IF
V3=FreeParameter("Side",Side)
C1=OffsetContourLine(Contour$,G1,V2,V1,V3)
Stat= DeleteEntity(G1)
Stat=AutoNameGroup(C1,Name$)
RecordFunctionOff

CloseVarRecord
IF NOT ValidGeometry(C1) THEN
        DelLastVarRecord
ELSE
        Stat=PlotEntity(C1)
	ActivateUndo
	Stat= AdvanceUndoNumber("grind stock")
	Stat= AddGroupToUndo(WsUBCreateMode,C1,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat= SetSelectedFlag(Group,WsFalse)
Stat= DeleteEntity(Group)
GOTO First_pick

Leave:!
IF ValidGeometry(Group) THEN
	Stat= SetSelectedFlag(Group,WsFalse)
	Stat= PlotEntity(Group)
END IF
Stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
