
! script name: voffsets.d/engravetext
! var_topic: engrave center line of text

!A
!A NAME:
!A
!A 	Engrave Text
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path along a line of text.
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the engraving tool.
!B
!B 	FINISH DEPTH: Enter the finish depth.
!B
!B	PICK TEXT LINE: Pick the line of text.
!B

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

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Depth_var$[256]
DIM Text_line$[256]
DIM Name$[256]


Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

ActivateUndo
Current_pen=CurrentColor

IF ARGC>1 THEN
	Answer$= ARGV(1)
	GOTO Have_tool
END IF

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:voffset.d/engravetext"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF

Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("SELECT TOOL, New_tool ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
	LowerSelectBox
	GOTO Leave
END IF
IF Answer$="New_tool" THEN
	LowerSelectBox
	LOAD "vtools.d/readmilltool:voffset.d/engravetext"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox

Finish_depth:!
Finish_depth=NamedVarVal("finish depth",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_depth)
Stat=SetInputFormat("n4=","")
Finish_depth=GetValue("FINISH DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Vdepth=CreateVariable("finish depth",WsSystemVars,Finish_depth)

Pick_loop:!
Stat=SetPickMask(WsPickNoSurf)
Text_ent=PickEntity("PICK TEXT LINE")
IF (ExitScript(Destination$)) THEN Leave
!Following line removed due to 2D-Text lines.
!IF NOT ValidDependency(Text_ent) THEN Pick_loop
Etype= EntityType(Text_ent)
IF Etype<> WsTextEnt THEN Pick_loop
Stat= EntityNameCheck(Text_ent,Text_line$)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Engrave text "&Tool_name$)
Stat= SetRecDelLastExec
IF No_depth_var THEN
	V1= FreeParameter("Finish depth",Finish_depth)
ELSE
	V1= FreeParameter("Finish depth",Depth_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
G1=ExplodeText(Text_line$,WsFalse)
G2=JoinEntities(G1)
Stat=DeleteEntity(G1)
Move_vector= PointXYZ(0.0,0.0,-VariableValue(V1))
Trans=AngleTransform(Move_vector,0,0,0,WsWorkplaneVals)
Stat=DeleteEntity(Move_vector)
Stat=TransGroupTrans(G2,Trans,1,1)
Stat=AssociateTool(G2,Tool)
Stat= AutoNameGroup(G2,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(G2) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,G2)
	ActivateUndo
	Stat= AdvanceUndoNumber("engrave text")
	Stat= AddGroupToUndo(WsUBCreateMode,G2,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF


GOTO Pick_loop

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

