
! script name: wscmill.d/replacepara
! var_topic: replace free parameters

!A
!A NAME:
!A
!A 	Record Free Parameters
!A
!A DESCRIPTION:
!A
!A 	Replace free parameters.
!A
!B INSTRUCTIONS:
!B
!B 	PICK VARIATIONAL RECORD,   Number:
!B
!B 	PICK VARIATIONAL OBJECT, Name:
!B
!B 	OBJECT NAME: Enter the VDM object name.
!B 		NOTE: This question is only asked if the Name
!B 		option has been selected in the previous question.
!B
!B 	RECORD # IN OBJECT:   object_name:
!B
!B 	PARAMETER TO BE REPLACED Replace, Calculate:
!B
!B 	REPLACEMENT FOR   parameter_name:
!B
!# see also - htdocs/help.d/vdminfo.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: replace free parameters")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Object_name$[256]
DIM Newpara$[256]
DIM Para_name$[256]
DIM Para_val$[256]
DIM Descr$[256]
DIM Varstr$[256]
DIM Parameters$[5000]
DIM Data$[32000]
DIM Namelist$[16384]

DeActivateUndo
Stat=LineNumberIncr(1)
Start_obj=0
Highlight_color=2

! put here to re-establish workplane for VDM 
Start:!
Quote$=CHR$(34)
Commastr$=","
Spacestr$=" "

Pick_record:!
Has_pt_loc= 0
LowerSelectBox
Stat=SetInputFormat("a1",",N,")
E1=PickEntity("PICK FEATURE TO EDIT ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="N" THEN
	GOSUB Pick_object
Record_number:!
	Stat=SetInputFormat("n0","")
	Rec_number=GetValue("RECORD # IN OBJECT: "&Object_name$&"   ")
	IF ExitScript(Destination$) THEN Leave
	Var_record=VObjectRecord(Var_object,Rec_number)
	IF NOT ValidGeometry(Var_record) THEN Record_number
	Start_obj=VarObjectNumber(Var_object)
	Stat= VarRecordData(Var_record,Data$)
	Stat=GetDescription(Var_record,Descr$)
ELSE
	IF EntityType(E1)= WsPointEnt OR EntityType(E1)= WsHoleEnt THEN
		Namelist$= ""
		Pc= ParameterPt(E1,0.0)
		Stat=MakeInvisible(Pc)
		Has_pt_loc= 0
		Loc_group=CreateGroup("LOCATION GROUP")
		Lgc= 1

		PlotOff
		G1= GrpByEntTypeVsble(WsPointEnt)
		IF ValidGeometry(G1) THEN
		   FOR K= 1 TO GroupCount(G1)
			E2= GroupElement(G1,K)
			IF DistTwoPts(Pc,E2)<.0001 THEN
			   Var_record=VarRecordParent(E2)
			   IF ValidGeometry(Var_record) THEN
				Var_object=VarObjectParent(E2)
				IF ValidGeometry(Var_object) THEN
					Stat= AddToGroup(Loc_group,E2)
					Stat= GetDescription(Var_record,Descr$)
					Namelist$= Namelist$&VAL$(Lgc)&") "&Descr$&","
					Lgc= Lgc+1
					GOTO Next_pt
				END IF
			   END IF
			   IF NOT Has_pt_loc THEN
				Has_pt_loc= 1
				E1= E2
			   END IF
			END IF
Next_pt:	   NEXT K
		END IF
Find_holes:!
		Stat= DeleteEntity(G1)

		G1= GrpByEntTypeVsble(WsHoleEnt)
		IF ValidGeometry(G1) THEN
		   FOR K= 1 TO GroupCount(G1)
			E2= GroupElement(G1,K)
			P1= ParameterPt(E2,0.0)
			IF DistTwoPts(Pc,P1)<.0001 THEN
			   Var_record=VarRecordParent(E2)
			   IF ValidGeometry(Var_record) THEN
				Var_object=VarObjectParent(E2)
				IF ValidGeometry(Var_object) THEN
					Stat= AddToGroup(Loc_group,E2)
					Stat= GetDescription(Var_record,Descr$)
					Namelist$= Namelist$&VAL$(Lgc)&") "&Descr$&","
					Lgc= Lgc+1
				END IF
			   END IF
			END IF
			Stat= DeleteEntity(P1)
		   NEXT K
		END IF
		Stat= DeleteEntity(G1)
		Stat= DeleteEntity(Pc)
		PlotOn

		Num_holes= GroupCount(Loc_group)
		SELECT Num_holes
		CASE 0
			Stat= DeleteEntity(Loc_group)
			IF Has_pt_loc THEN GOTO Edit_pt_loc
			GOTO Pick_record
		CASE 1
			E1= GroupElement(Loc_group,1)
		CASE ELSE
			IF Has_pt_loc THEN Namelist$="EDIT POINT LOCATION,"&Namelist$
Para_ques: 		OFF ERROR
			Stat=GeneralListBox("CHOICES","Choices",Namelist$)
			Stat=SetInputFormat("a99","")
			Stat=GetValue("WHICH FEATURE TO EDIT",Answer$)
			IF ExitScript(Destination$) THEN
				Stat= DeleteEntity(Loc_group)
				GOTO Leave
			END IF
			SELECT UPC$(Answer$)
			CASE "CANCEL"
				Stat= DeleteEntity(Loc_group)
				GOTO Pick_record
			CASE "EDIT POINT LOCATION"
				Stat= DeleteEntity(Loc_group)
				GOTO Edit_pt_loc
			CASE ELSE
				ON ERROR GOTO Para_ques
				Choice= VAL(Answer$)
				IF Choice<1 THEN Para_ques
				IF Choice>GroupCount(Loc_group) THEN Para_ques
				E1= GroupElement(Loc_group,Choice)
				Var_record=VarRecordParent(E1)
				IF NOT ValidGeometry(Var_record) THEN Pick_record
				Var_object=VarObjectParent(E1)
				IF NOT ValidGeometry(Var_object) THEN Pick_record
				Start_obj=VarObjectNumber(Var_object)
				Stat=VarRecordData(Var_record,Data$)
				Stat=GetDescription(Var_record,Descr$)
				Has_pt_loc= 0
			END SELECT
		END SELECT
		Stat= DeleteEntity(Loc_group)
	ELSE
		IF NOT ValidGeometry(E1) THEN Pick_record
		Var_record=VarRecordParent(E1)
		IF NOT ValidGeometry(Var_record) THEN Pick_record
		Var_object=VarObjectParent(E1)
		IF NOT ValidGeometry(Var_object) THEN Pick_record
		Start_obj=VarObjectNumber(Var_object)
		Stat= VarRecordData(Var_record,Data$)
		Stat=GetDescription(Var_record,Descr$)
	END IF

END IF

List_para:!
Num_para=VRecordParaList(Var_record,Parameters$)
IF Num_para=0 THEN Pick_record
IF LEN(Parameters$)=0 THEN Pick_record

IF Has_pt_loc THEN Parameters$="EDIT POINT LOCATION,"&Parameters$
Parameters$=Parameters$&",DELETE"
Parameters$=Parameters$&",APPLY"
Stat=GeneralListBox(Descr$,"Parameters",Parameters$)
Stat=SetInputFormat("a99","")
Stat=GetValue("VALUE TO EDIT, Apply ",Answer$)
IF ExitScript(Destination$) THEN 
	IF LEN(Data$)>1 THEN
		Stat= ReplaceRecordData(Var_record,Data$)
	END IF
	GOTO Leave
END IF

SELECT UPC$(Answer$)
CASE "EDIT POINT LOCATION"
	GOTO Edit_pt_loc
CASE "D","DELETE","DELETE,"
	GOTO Del_record
CASE "B","BACK","BACK,"
	GOTO Pick_record
CASE "C","CALCULATE","CALCULATE,"
	GOTO Calc_record
CASE "E","EXECUTE","EXECUTE,"
	GOTO Calc_record
CASE "REPLACE","REPLACE,"
	Has_pt_loc= 0
	GOTO Replace_rec
CASE "A","APPLY","APPLY," 
	GOTO Calc_object
CASE "CANCEL"
	Has_pt_loc= 0
	IF LEN(Data$)>1 THEN
		Stat= ReplaceRecordData(Var_record,Data$)
	END IF
	GOTO Pick_record
CASE ELSE
	Answer$=TRIM$(Answer$)
	IF LEN(Answer$)<2 THEN Pick_record
	Name$=Answer$
	Add_on$=", Back, Cancel"
END SELECT


LowerSelectBox
Stat=StringFieldStr(Name$,"=",1,Para_name$)
IF Stat=1 THEN Pick_record
Stat=StringFieldStr(Name$,"=",2,Para_val$)
IF Stat=1 THEN Pick_record
Para_value=StringFieldVal(Name$,"=",2)

Replace_ques: Stat=SetEqualsValue(Para_value)
Stat=SetEditValue(Para_value)
Stat=SetInputFormat("n4a1=",",V,R,B,C,A,")
Replace_value=GetValue("NEW VALUE FOR "&Para_name$&" ",Answer$)
IF ExitScript(Destination$) THEN
       IF LEN(Data$)>1 THEN
             Stat= ReplaceRecordData(Var_record,Data$)
       END IF
       GOTO Leave
END IF
SELECT Answer$
CASE "V"
	Stat=UserSelectBox("VARIABLES","variable chosen",WsVariationVars,WsVarInput)
	GOTO Replace_ques
CASE "CANCEL"
       IF LEN(Data$)>1 THEN
             Stat= ReplaceRecordData(Var_record,Data$)
       END IF
       GOTO Pick_record
END SELECT

No_var=LastVariableInput(Varstr$)
IF No_var THEN
	Newpara$=VAL$(Replace_value)
ELSE
	Newpara$=Quote$&Varstr$&Quote$
END IF

Stat=ReplaceParameter(Var_record,Para_name$,Newpara$)

GOTO List_para

Replace_rec:!
LowerSelectBox
GOTO Start

Calc_object:!
LowerSelectBox
Stat= CleanNonVarGrps
Output_script=0
Stat=VarObjectName(Var_object,Object_name$)
Stat= DisplayMessage("CALCULATING CHANGES")
Stat=OutputVarObject("tempscript",Object_name$,"wscmill.d/replacepara",1,Output_script)
IF Stat=1 THEN Start
LOAD "tempscript"

Calc_record:!
LowerSelectBox
Output_script=0
Stat= DisplayMessage("CALCULATING CHANGES")
Stat=OutputAllObjects("tempscript","wscmill.d/replacepara",Start_obj,Output_script)
IF Stat=1 THEN Start

LOAD "tempscript"

GOTO Raise_box

Del_record:!
Var_record=VarRecordParent(E1)
Var_object=VarObjectParent(E1)
No_delete=VRecDeleteCheck(Var_record)
IF No_delete THEN
	Execute_on= WsTrue
	Stat=SetInputFormat("a1",",D,E,")
	Stat=GetValue("BROKEN DEPENDENCY, Delete anyway, Exit",Answer$)
	IF ExitScript(Destination$) THEN Leave
	IF Answer$="D" THEN Delete_record
	GOTO Pick_record
ELSE
	Execute_on= IsDelLastExecOn(Var_record)
END IF

Delete_record:!
Stat=Highlight(Var_record,Highlight_color,0)
Stat=VarObjectName(Var_object,Name$)
IF NOT LEN(Name$) THEN Pick_record
Rec_number= VarRecordNumber(Var_record)
Vgroup=VRecordRefGroup(Var_record,WsVariableRef)
Stat=DeleteEntity(Var_record,WsTrue,2)
Stat= DelStrgyVarsInGrp(Vgroup)
Stat= DeleteEntity(Vgroup)
Stat= DisplayMessage("CALCULATING CHANGES")
Stat=OutputVarObject("tempscript",Name$,"wscmill.d/replacepara",1,Output_script)
IF Stat=1 THEN Start
LOAD "tempscript"

Pick_object:!
Nobjects= EntityTypeCount(WsVarObjectEnt)
IF Nobjects=1 THEN
	Var_object= CurrentVarObject
	IF NOT ValidGeometry(Var_object) THEN Leave
ELSE
	Var_object=PickVarObject("PICK VARIATIONAL OBJECT")
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(Var_object) THEN Pick_object
END IF
Stat=VarObjectName(Var_object,Object_name$)
Stat=Highlight(Var_object,Highlight_color,0)
RETURN

Edit_pt_loc:!
	LowerSelectBox
	Pc= ParameterPt(E1,0.0)
	Stat=SetLastPoint(Pc)
	Stat=DeleteEntity(Pc,WsTrue)
	Stat=SetPickMask(WsPickNoSurf)
	Pt=XyzInput("INPUT NEW",WsXyzInput)
	IF ExitScript(Destination$) THEN Leave
	Stat=SetPickMask(WsPickAll)
	Pc= ParameterPt(E1,0.0)
        Stat=ChConnectEnts(Pc,Pt,WsStart)
	Stat=DeleteEntity(Pc)
	Stat=DeleteEntity(Group1)
	Stat=OutputAllObjects("tempscript","wscmill.d/replacepara",1,0)
	IF Stat=1 THEN Leave
	LOAD "tempscript"
RETURN

Leave:!
LowerSelectBox
Stat=SetPickMask(WsPickAll)

IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
