
! script name: vcopy.d/dynamiccopybox
! var_topic: Dynamic copy in box group by reference points

!A
!A NAME:
!A
!A      Dynamic Copy In Box  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script graphically copies entities in a box.
!A
!B INSTRUCTIONS:
!B
!B
!B 	REFERENCE POINT ABS X,Y,Z: Enter the X,Y,Z coordinates of the point
!B 		to be used as the reference for the copy.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	PICK DESTINATION POINT : The elements to be moved are displayed
!B		graphically with the cursor. 
!B		Hitting the Select Buttonon the mouse will select the location.
!B		The numeric keypad controls the orientation.
!B
!B			(6) 0   degree orientation.
!B			(9) 45  degree orientation.
!B			(8) 90  degree orientation.
!B			(7) 135 degree orientation.
!B			(4) 180 degree orientation.
!B			(1) 225 degree orientation.
!B			(2) 270 degree orientation.
!B			(3) 305 degree orientation.
!B
!B 		Plus (+) and (-) turn the entities +/- the angular increment.
!B		The Default angular increment is 5 degrees. 
!B		Hitting ( A ) will change the increment to 10 degrees.		
!B		Hitting ( a ) will change the increment to 1 degrees.		
!B		Hitting ( F ) or ( f ) will change the increment to 0.1 degrees.
!B		Hitting ( D ) or ( d ) will return the increment to 5 degrees.
!B
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: dynamic box copy ")

DIM Answer$[256]
DIM Destination$[256]
DIM Name$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Element$[256]
DEG

Single_mode=NamedVarVal("Single place mode",WsSystemVars,0)
Place_min= NamedVarVal("Graph place min",WsSystemVars,50)
Place_max= NamedVarVal("Graph place max",WsSystemVars,500)
Place_density= NamedVarVal("Graph place density",WsSystemVars,3)

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


Start: !
ActivateUndo
Stat=SetPickMask(WsPickAll)
Active_workplane=0
Work=CurrentWorkplane
IF ValidGeometry(Work) THEN Active_workplane=1

stat= SetInputFormat("n4","")
Group1=CreateGroup("TEMP")

Vnum= LastPickView
Stat=PickBox(Group1,Vnum,WsPickPen)
IF ExitScript(Destination$) THEN Leave

IF NOT ValidGeometry(Group1) THEN
        GOTO Start
ELSE
	Group2=CreateGroup("TEMP")
	Num_elements= GroupCount(Group1)
	FOR I=1 TO Num_elements
        	E1= GroupElement(Group1,I)
        	IF ValidGeometry(E1) THEN
              		Stat= EntityNameCheck(E1,Element$)
              		IF ValidDependency(E1,0) THEN
               	    		Stat= AddToGroup(Group2,Element$)
              		END IF
        	END IF
	NEXT I
	Stat= DeleteEntity(Group1)
	Group1= Group2
        IF NOT GroupCount(Group1) THEN 
		Stat= DeleteEntity(Group1)
		GOTO Start
	END IF
END IF

Ref_pt:P1=PickEntity("REFERENCE POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(P1) THEN Ref_pt
Para1= LastPickPara
Pick_return1= CurrPickReturn
Stat= EntityNameCheck(P1,Point1$)
Pref= ParameterReturnPt(P1,Para1,Pick_return1)

Place_group:!
FadeUndoButtons
Angle= 0
Pdest= CopyEntity(Pref)
Axis_line= WorkplaneAxisLine(Work,Pref)

Vnum= LastPickView
stat= SetInputFormat("n4b","")
Group2= MakeSparseGroup(Group1,Place_min,Place_max,Place_density)
Stat= MakeVisible(Group1)
Stat= PlotEntity(Group1)
Rotate_angle= DynamicMoveGroup(Vnum,Group2,Pref,Pdest,Angle,"PICK DESTINATION POINT",WsFalse)
Stat= DeleteEntity(Group2)
IF (ExitScript(Destination$)) THEN 
	Stat= PlotEntity(Group1)
	GOTO Leave
END IF
E2= LastPickEnt
Para2= LastPickPara
Pick_return2= CurrPickReturn
IF NOT ValidDependency(E2) THEN
        E2= CopyEntity(Pdest)
        Stat= MakeVisible(E2)
        Stat= EntityNameCheck(E2,Point2$)
	P2_created= WsTrue
ELSE
        Stat= EntityNameCheck(E2,Point2$)
	P2_created= WsFalse
END IF
Stat= DeleteEntityList(Pdest,Axis_line)

Make_record:!
Stat= SetSelectedFlag(Group1,WsFalse)
Num_elements= GroupCount(Group1)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("COPY: ref copy entities")
Stat= SetRecDelLastExec
G1= CreateGroup("Picked Entities")
FOR I=1 TO Num_elements
RecordFunctionOff
        E1= GroupElement(Group1,I)
        IF ValidGeometry(E1) THEN
              Stat= EntityNameCheck(E1,Element$)
              RecordFunctionOn
              Stat= AddToGroup(G1,Element$)
              RecordFunctionOff
	END IF
NEXT I
RecordFunctionOn
SELECT Pick_return1
CASE WsGeometryEnd
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryEnd)
CASE WsGeometryMid
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryMid)
CASE WsGeomCenter
        Ps= ParameterReturnPt(Point1$,Para1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryEnd)
CASE WsGeometryMid
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryMid)
CASE WsGeomCenter
        Pe= ParameterReturnPt(Point2$,Para2,WsGeomCenter)
END SELECT
Work= CurrentWorkplane
L1= WorkplaneAxisLine(Work,Ps)
Stat=SetWorkplane(0)
V1= FreeParameter("Rotate angle",Rotate_angle)
G2= CopyGroup(G1)
Trans1= RotMoveAxisTrans(Pe,L1,V1)
Stat=TransGroupTrans(G2,Trans1,WsFalse,WsTrue)
Stat= AutoNameGroup(G2,Name$)
Stat=Unlink(G2)
ActivateUndo
Stat= AdvanceUndoNumber("copy")
IF P2_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point2$)
Stat= AddGroupToUndo(WsUBCreateMode,G2,2,WsTrue)
Stat= DeleteEntityList(G1,G2,Ps,Pe,L1)
Stat=SetWorkplane(Work)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord


Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
Stat= PlotEntity(Group1)
Stat= DeleteEntityList(Pdest,Axis_line)
Stat= PlotEntity(Point1$)
Stat= PlotEntity(Point2$)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetPickMask(WsPickAll)
IF Single_mode THEN Leave
GOTO Place_group


Leave: !
ActivateUndo
Stat=SetPickMask(WsPickAll)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
stat= SetInputFormat("n4","")
Stat= DeleteEntityList(Group1,Pref,Pdest,Axis_line)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
        IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END
