
! script name: 3dcpaths.d/engravectext
! 3dtopic: engraves a circular text line on a part

!A
!A NAME:
!A
!A 	Engrave Circular Text
!A
!A DESCRIPTION:
!A
!A 	This script projects text line onto a group of surfaces, along an arc.
!A 	The center of the cutter is constrained to move along the text lines
!A 	while staying in contact with the part.  The corresponding cutter path
!A 	is generated with gouge avoidance.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER TYPE Ball, Endmill, TBall, TEndmill:
!B
!B 		B - Cutter is assumed to be a ball endmill.  Offset is
!B 		    measured from the center of the ball radius.  The bottom
!B 		    of the ball is the programmed point.
!B 		E - Cutter is assumed to be a standard, flat-bottom endmill,
!B 		    with possibly rounded corners.  The programmed point is the
!B 		    center of the bottom of the cutter.  It is this point which
!B 		    is kept away the part boundary.
!B 		TB - Cutter is tapered ball endmill.
!B 		TE - Cutter is tapered endmill.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	CORNER RADIUS: Enter the corner radius of the endmill.
!B 		NOTE: This question is only asked for endmills.
!B
!B 	TAPER ANGLE: Enter the taper angle.
!B 		NOTE: This question is only asked for tapered endmills.
!B
!B 	TAPER LENGTH: Enter the length of the taper.
!B 		NOTE: This question is only asked for tapered endmills.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!B 	PICK CIRCULAR ARC: Pick circular arc defining text location.
!B	
!B 	INPUT TEXT LINE: Input the line of text.
!B
!B 	PICK WHICH SIDE: Pick which side of the circular arc the text should
!B 		be located on.
!B
!B 	Z DEPTH: Enter the Z depth of cut into part surface.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: engrave text")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[256]
DIM Text_line$[256]

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Units=CurrentUnits
Text_centered=1

Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0
Taper_angle=15
StdWorkplane

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)

Surface_dir$="F" 

Tool_assoc=0
Tool_val=FindNamedVar("tool association",WsToolVars)
IF ValidGeometry(Tool_val) THEN Tool_assoc=GetVariableVal(Tool_val)

IF NOT Tool_assoc THEN
	GOSUB Get_mill_type
	GOSUB Get_cutter_dia
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
	IF Stat THEN
		GOSUB Get_mill_type
		GOSUB Get_cutter_dia
		Tool_assoc=0
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("CUTTER PATH TOOL",Tool_name$)
		IF ExitScript(Destination$) THEN Leave
		IF Tool_name$="CANCEL" THEN
			LowerSelectBox
			GOSUB Get_mill_type
			GOSUB Get_cutter_dia
			Tool_assoc=0
			GOTO Past_dia
		END IF
		Tool=FindUserData(Tool_name$,WsToolData)

		Stat=GetUserString(Tool,"Tool type:",Tool_type$)

		SELECT Tool_type$
		    CASE "ENDMILL","BULLMILL"
			Cutter_type=WsEndMill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
		    CASE "TAPERMILL"
			Cutter_type=WsTaperedEndmill
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Taper=GetUserValue(Tool,"Taper angle:")
			Corner_rad=GetUserValue(Tool,"Corner radius:")
			Length=GetUserValue(Tool,"Flute length:")
		    CASE "TAPERBALL"
			Cutter_type=WsTaperedBall
			Cutter_dia=GetUserValue(Tool,"Diameter:")
			Taper=GetUserValue(Tool,"Taper angle:")
			Length=GetUserValue(Tool,"Flute length:")
		    CASE "BALLMILL"
			Cutter_type=WsBallCutter
			Cutter_dia=GetUserValue(Tool,"Diameter:")
		    CASE ELSE
			GOTO Tool_ques
		END SELECT

		LowerSelectBox
	END IF
END IF

Past_dia:!
Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

VCutter_type=CreateVariable("cutter type",WsSystemVars,Cutter_type)
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VCorner=CreateVariable("corner radius",WsSystemVars,Corner_rad)

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

StdWorkplane

Pick_loop:!

Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,")
Part_group=PickGroupEnt("PICK SURFACE GROUP, Name ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN
        GOSUB Get_named_group
END IF

Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Stat=SetPickMask(WsPickGeometry)

Tolerance:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave

Cut_part:!
Ecircle=PickSpecificEnt("PICK CIRCULAR ARC",WsPickCircles)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Ecircle) THEN text_loop
Stat=SetPickMask(WsPickGeometry)

Stat=SetInputFormat("a99","",0)
Stat=GetValue("INPUT TEXT LINE",Answer$)
IF ExitScript(Destination$) THEN Leave
Text_line$=Answer$

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

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zdepth)
Zdepth=GetValue("Z DEPTH",Answer$)
IF ExitScript(Destination$) THEN Leave

Pcenter=CircleCenter(Ecircle)
Theight=GetDefault(WsTextHeight)
Twidth=GetDefault(WsTextWidth)
Tangle=GetDefault(WsTextAngle)
Radius=CircleRadius(Ecircle)
Radius=ABS(Radius)
Clength=EntityLength(Ecircle)
Astart=EntityValue(Ecircle,WsStartAngle)
Ang_ext=EntityValue(Ecircle,WsAngleExtent)
Tspace=0.0

R1=DistTwoPts(Pt,Pcenter)
Flip=(R1<Radius)

P1=PointXYZ(-Twidth/2,0,0)

Num1=LEN(Text_line$)

IF Text_centered THEN
	Text_length=Num1*Twidth+(Num1-1)*Tspace
	Start_length=(Clength-Text_length+Twidth)/2
ELSE
	Start_length=Twidth/2
END IF

Grp1=CreateGroup("TEMP")
FOR I=1 TO Num1
	C$=Text_line$[I;1]
	Text1=TextLine(P1,C$)
	L1=Start_length+(I-1)*(Twidth+Tspace)
	T=L1/Clength
	P2=ParameterPt(Ecircle,T)
	Line1=LineTwoPoints(Pcenter,P2,WsTrimmed)
	Angle=LineAngle(Line1)-90+Flip*180
	T1=AngleTransform(P2,0,0,Angle,WsWorkplaneVals)
	Text2=PTransformEntity(Text1,T1)

	Grp2=ExplodeText(Text2)
	IF ValidGeometry(Grp2) THEN
		Stat=CombineGroups(Grp1,Grp2)
		Stat=DeleteEntity(Grp2)
	END IF

	Stat=DeleteEntity(P2)
	Stat=DeleteEntity(Text1)
	Stat=DeleteEntity(Text2)
	Stat=DeleteEntity(Line1)
NEXT I
Stat=DeleteEntity(P1)
Xc=XPointValue(Pcenter)
Yc=YPointValue(Pcenter)
Zc=ZPointValue(Pcenter)
Stat=DeleteEntity(Pcenter)
Stat=DeleteEntity(Pt)

Group=JoinEntities(Grp1)
Stat=DeleteEntity(Grp1)

Cutterpath=ContGrpGouge(0,Part_group,Group,0.0,Tol,1)
Stat=DeleteGroup(Group,1)

Pr=PointXYZ(0,0,-Zdepth)
T1=AngleTransform(Pr,0,0,0,WsActualVals)
Temp_group=CreateGroup("TEMP")
Stat=AddToGroup(Temp_group,Cutterpath)
Stat=TransGroupTrans(Temp_group,T1)
Stat=DeleteEntity(Temp_group)
Stat=DeleteEntity(Pr)

IF ValidGeometry(Cutterpath) THEN
	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	Stat=CPathFileName(Cutterpath,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  ENGRAVE CIRCLE"&CHR$(10)
	Stat=GroupName(Part_group,Answer$)
	Datastr$=Datastr$&"SURFACE GROUP: "&Answer$&CHR$(10)
	Datastr$=Datastr$&"TEXT LINE: "&Text_line$&CHR$(10)
	Datastr$=Datastr$&"TEXT CIRCLE CENTER X: "&VAL$(Xc)&CHR$(10)	
	Datastr$=Datastr$&"TEXT CIRCLE CENTER Y: "&VAL$(Yc)&CHR$(10)	
	Datastr$=Datastr$&"TEXT CIRCLE CENTER Z: "&VAL$(Zc)&CHR$(10)	
	Datastr$=Datastr$&"CIRCLE RADIUS: "&VAL$(Radius)&CHR$(10)	
	Datastr$=Datastr$&"ANGULAR EXTENT: "&VAL$(Ang_ext)&CHR$(10)	
	Datastr$=Datastr$&"START ANGLE: "&VAL$(Astart)&CHR$(10)	
	Datastr$=Datastr$&"INSIDE CIRCLE: "&VAL$(Flip)&CHR$(10)	
	Datastr$=Datastr$&"Z DEPTH: "&VAL$(Zdepth)&CHR$(10)
	Datastr$=Datastr$&"CENTER TEXT: "&VAL$(Text_centered)&CHR$(10)
	Datastr$=Datastr$&"TEXT SPACING: "&VAL$(Tspace)&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"CORNER RAD: "&VAL$(Corner_rad)&CHR$(10)
	Datastr$=Datastr$&"TAPER: "&VAL$(Taper)&CHR$(10)
	Datastr$=Datastr$&"CUTTER TYPE: "&VAL$(Cutter_type)&CHR$(10)

	Cpath3d=FindUserData(Name$,Ws3dCpath)
	IF ValidGeometry(Cpath3d) THEN Stat= DeleteEntity(Cpath3d)
	Cpath3d=ReadUserData(Ws3dCpath,Name$,Datastr$)

	Stat=AssocUserData(Cutterpath,Cpath3d,Ws3dCpath)
	IF Tool_assoc THEN Stat=AssociateTool(Cutterpath,Tool)

	ActivateUndo
        Stat= AdvanceUndoNumber("engrave text")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutterpath,1,WsTrue)

END IF
Stat=DeleteEntity(Pc)

GOTO Start

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

Get_mill_type:!
	SELECT Cutter_type
	    CASE WsTaperedBall
		Answer$="TB"
	    CASE WsTaperedEndmill
		Answer$="TE"
	    CASE WsBallCutter
		Answer$="B"
	    CASE WsEndMill
		Answer$="E"
	END SELECT
	Stat=SetEqualsValue(0,Answer$)
	Stat=SetInputFormat("a2=",",B,E,TB,TE,")
	Stat=GetValue("CUTTER TYPE Ball, Endmill, TBall, TEndmill",Answer$)
	IF ExitScript(Destination$) THEN Leave

	SELECT Answer$
	    CASE "TB"
		Cutter_type=WsTaperedBall
	    CASE "TE"
		Cutter_type=WsTaperedEndmill
	    CASE "B"
		Cutter_type=WsBallCutter
	    CASE "E"
		Cutter_type=WsEndMill
	END SELECT
RETURN

Get_cutter_dia:!
	Stat=SetInputFormat("n4=","")
	Cutter_dia=NamedVarVal("cutter diameter",WsSystemVars,0.0)
	Stat=SetRadiusEquals(Cutter_dia/2)
	Cutter_dia=InputRadius("CUTTER DIAMETER",WsDiaInput)
	IF ExitScript(Destination$) THEN Leave

	IF Cutter_type=WsEndMill OR Cutter_type=WsTaperedEndmill THEN
		Corner_rad=NamedVarVal("corner radius",WsSystemVars,corner_rad)
		Stat=SetEqualsValue(Corner_rad)
		Corner_rad=GetValue("CORNER RADIUS",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Corner_rad=Corner_rad
	END IF
	IF Cutter_type=WsTaperedBall OR Cutter_type=WsTaperedEndmill THEN
		Taper_angle=NamedVarVal("taper angle",WsSystemVars,Taper_angle)
		Stat=SetEqualsValue(Taper_angle)
		Taper_angle=GetValue("TAPER ANGLE",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Taper=Taper_angle/2

		Length=NamedVarVal("Flute length",WsSystemVars,Length)
		Stat=SetEqualsValue(Length)
		Length=GetValue("TAPER LENGTH",Answer$)
		IF ExitScript(Destination$) THEN Leave
	END IF
RETURN

Get_named_group:!
Stat=ListGrpNames("SURFACE GROUPS","selected group","",WsSurfaceGroup)
IF Stat=1 THEN Leave_name

Stat=SetInputFormat("a99","")
Stat=GetValue("SURFACE GROUP NAME",Answer$)

IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave_name
Part_group=FindNamedGrp(Answer$)
IF NOT ValidGeometry(Part_group) THEN Get_named_group
Grp_type= GroupType(Part_group)
IF Grp_type <> WsSurfaceGroup THEN Get_named_group
LowerSelectBox

Leave_name:!
Stat=SetInputFormat("n4","")

END
