
! script name: contours.d/grindstock
! topic: add grind stock to a contour line.

!A
!A NAME:
!A
!A 	Adds Grinding stock to a contour line.
!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
!A~ image location - images/grindstock.gif
!A
!B INSTRUCTIONS:
!B
!B	GRINDING STOCK: Thickness of stock allowance for grinding.
!B
!B 	MINIMUM RADIUS FOR SHARP CORNER: Input the minimum
!B 		radius to be used on corners. All radii smaller
!B 		than this amount will be made into sharp corners.
!B
!B	PICK AN ELEMENT OF CONTOUR: Pick an element of the
!B 		contour that the stock is to be added to.
!B
!B	PICK WHICH SIDE: Pick the side the 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
!B 	REPLACE ORIGINAL CONTOUR Yes, No, Cancel:
!B
!B 		Y - for yes will replace the original contour with the new
!B 		    contour.
!B 		N - for no will not delete the original but will keep the
!B 		    new contour.
!B 		C - for cancel will delete the new contour but leave
!B 		    the original unchanged.
!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 Name$[256]

ActivateUndo
Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)


Offset:!
Stat= SetInputFormat("n4=","")
Stat=SetEqualsValue(0.0)
Stock=GetValue("GRINDING STOCK",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Radius= InputRadius("MINIMUM RADIUS FOR SHARP CORNERS")
IF (ExitScript(Destination$)) THEN Leave

First_pick:!
Element=PickEntity("PICK AN ELEMENT OF CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
Contour= ParentGroup(Element)
IF NOT ValidGeometry(Contour) THEN First_pick
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 ValidGeometry(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:!
New_contour=OffsetContourLine(Contour,Group,Stock,Radius,Side)
Stat= DeleteEntity(Group)
IF NOT ValidGeometry(New_contour) THEN First_pick

Stat= SetInputFormat("a1=",",Y,N,C,")
Stat= GetValue("REPLACE ORIGINAL CONTOUR Yes, No, Cancel",Answer$)
IF (ExitScript(Destination$)) THEN 
	Tool= GetAssocTool(Contour)
        IF ValidGeometry(Tool) THEN
                Stat= AssociateTool(New_contour,Tool)
        END IF
	Stat= AutoNumberCont(New_contour)
	GOTO Leave
END IF

Pen_color= CurrentColor
Pen_color= Pen_color+ WsPickPen*(NOT Pen_color)
SELECT Answer$
CASE "Y"
	Tool= GetAssocTool(Contour)
        IF ValidGeometry(Tool) THEN
                Stat= AssociateTool(New_contour,Tool)
        END IF
	stat= GroupName(Contour,Name$)
        Stat= AutoNumberCont(New_contour)
	stat= ChangeGrpName(New_contour,Name$)

	ActivateUndo
	Stat= AdvanceUndoNumber("grind stock")
	Stat= AddGroupToUndo(WsUBDeleteMode,Contour,1,WsTrue)
	Stat= AddGroupToUndo(WsUBCreateMode,New_contour,1,WsTrue)

	stat= UnLink(Contour)
	stat= DeleteGroup(Contour,1)
	Stat=Highlight(New_contour,Pen_color,WsDirArrowOn)
CASE "N"
	Tool= GetAssocTool(Contour)
        IF ValidGeometry(Tool) THEN
                Stat= AssociateTool(New_contour,Tool)
        END IF
	Stat= AutoNumberCont(New_contour)

	ActivateUndo
	Stat= AdvanceUndoNumber("grind stock")
	Stat= AddGroupToUndo(WsUBCreateMode,New_contour,1,WsTrue)
CASE "C"
	stat= UnLink(New_contour)
	stat= DeleteGroup(New_contour,1)
	Stat=Highlight(Contour,Pen_color,WsDirArrowOff)
END SELECT

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

