
! script name: vtrimming.d/bdytrimpathgroup
! var_topic: trim group of paths to a boundary

!A
!A NAME:
!A
!A 	Boundary Trim Path Group VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script trims a group of paths to a boundary.
!A
!B INSTRUCTIONS:
!B
!B 	PICK EXTERIOR CONTOUR: Pick the contour to
!B 		be used as the boundary of the contour to be trimmed.
!B
!B 	NUMBER OF ISLANDS: Enter the number
!B 		of islands to be included for trimming purposes.
!B
!B 	PICK ISLAND # : Pick the islands to be included.
!B
!B 	PICK PATH GROUP TO BE TRIMMED: Pick an element
!B 		on the contour to be trimmed.
!B
!B NOTE:
!B
!B	This script returns a group of pathgroups
!B
!B 	One difference with standard boundary trim is that the variational
!B 	trim requires that the user establish the workplane which the trim
!B 	is to be done on.
!B
!B 	This variational script may create different numbers of elements
!B 	at different variational conditions. Caution must be exercised when
!B 	using an element created by this script.  It is possible for later
!B 	constructions to fail because a referenced entity no longer exists.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: boundary trim path group")

DIM Destination$[256]
DIM Answer$[256]
DIM Trim_paths$[256]
DIM Contour$[256]
DIM Boundary$[256]
DIM Island$[256]
DIM Element$[256]
DIM Name$[256]


Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Start:!
Stat=SetInputFormat("a1=",",I,E,")
Stat= SetEqualsValue(0,"I")
Stat= GetValue("SAVE:   Interior or Exterior  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Inout= (Answer$= "I")*WsInside+ (Answer$= "E")*WsOutside

Boundary_loop:!
Stat=SetInputFormat("n4a1",",B,")
Contour=PickGroupEnt("PICK EXTERIOR CONTOUR, Boundary ",0,Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "B" THEN
Pick_boundary:!
	Stat=SetInputFormat("n4","")
	Contour=PickGroupEnt("PICK CONTOUR IN BOUNDARY")
	IF (ExitScript(Destination$)) THEN Leave
	Boundary1= FindBoundary(Contour)
	IF (NOT ValidDependency(Boundary1)) THEN Start
	Stat= EntityNameCheck(Boundary1,Boundary$)
	Boundary_pick=1
	GOTO Pick_loop
END IF
Boundary_pick=0
IF (NOT ValidDependency(Contour)) THEN Boundary_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN Boundary_loop
Closed=IsContClosed(Contour)
IF NOT Closed THEN Boundary_loop

IF Inout= WsInside THEN
        Stat=SetInputFormat("n0=","")
        Stat= SetEqualsValue(0)
        Nisles=GetValue("NUMBER OF ISLANDS",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
ELSE
        Nisles= 0
END IF

Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles
Pick_island:Stat=SetInputFormat("n4","")
        Isle=PickGroupEnt("PICK ISLAND #"&VAL$(I))
        IF (ExitScript(Destination$)) THEN Leave
        IF (NOT ValidDependency(Isle)) THEN Pick_island
        Stat= EntityNameCheck(Isle,Island$)
        Stat=AddToGroup(Islands,Isle)
NEXT I

Pick_loop:Path=PickGroupEnt("PICK GROUP TO BE TRIMMED")
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidDependency(Path)) THEN Pick_loop
Stat=EntityNameCheck(Path,Trim_paths$)
IF NOT IsEntVariational(Path) THEN
   Stat=SetInputFormat("a99","")
   Stat= GetValue("NON-VDM ENTITY. CANNOT USE ON NON VDM ENTITY ",Answer$)
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF


Trim_paths= ParentGroup(Path)
IF ValidDependency(Trim_paths) THEN
IF IsGroupOfPaths(Trim_paths) THEN
        Stat=SetInputFormat("a1=",",S,P,G,")
        Stat= SetEqualsValue(0,"S")
        Stat=GetValue("APPLY TO: Single path or Path group ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "P" THEN
                Stat=EntityNameCheck(Trim_paths,Trim_paths$)
        END IF
        IF Answer$= "G" THEN
                Path_groups= ParentGroup(Trim_paths)
                IF ValidDependency(Path_groups) THEN
                   IF IsGroupOfPathGrps(Path_groups) THEN
                        Stat=EntityNameCheck(Path_groups,Trim_paths$)
                   END IF
                ELSE
                        Stat=EntityNameCheck(Trim_paths,Trim_paths$)
                END IF
        END IF
END IF
END IF
Stat=Highlight(Trim_paths,WsHighlightPen,Wsarrowoff)

IF Boundary_pick THEN
	ActivateUndo
        Stat= AdvanceUndoNumber("boundary trim")

	Stat= GenerateVarName(WsGroupEnt,Name$)
	RecordFunctionOn
	Stat= RecordDescr("TRIM: Boundary trim contour")
	V1= FreeParameter("Side",Inout)
	C1= BoundaryExterior(Boundary$)
	Isles= BoundaryInteriors(Boundary$)
	Trim_paths= FindNamedEntity(Trim_paths$)
	Trim_group= CopyGroup(Trim_paths)
	Stat= AddGroupToUndo(WsUBDeleteMode,Trim_paths,1,WsTrue)
	Stat= DeleteGroup(Trim_paths,WsTrue)
	G1=BdyTrimPathGroup(C1,Isles,Trim_group,V1)
	RecordFunctionOff
	Stat= PlotEntity(G1)
	RecordFunctionOn
	Stat= AutoNameGroup(G1,Name$)
	Stat= AddGroupToUndo(WsUBCreateMode,G1,1,WsTrue)
	RecordFunctionOff
	Curr_record= CurrentVarRecord
	CloseVarRecord
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
ELSE
	ActivateUndo
        Stat= AdvanceUndoNumber("boundary trim")

	Stat= GenerateVarName(WsGroupEnt,Name$)
	RecordFunctionOn
	Stat= RecordDescr("TRIM: Boundary trim contour")
	Stat= NullReference(Contour$)
	Isles=CreateGroup("")
	RecordFunctionOff
	FOR I= 1 TO Nisles
       		 Isle= GroupElement(Islands,I)
       		 Stat= EntityNameCheck(Isle,Island$)
       		 RecordFunctionOn
       		 Stat= AppendToGroup(Isles,Island$)
       		 RecordFunctionOff
	NEXT I
	RecordFunctionOn
	Trim_paths= FindNamedEntity(Trim_paths$)
	Trim_group= CopyGroup(Trim_paths)
        Stat= AddGroupToUndo(WsUBDeleteMode,Trim_paths,1,WsTrue)
        Stat= DeleteGroup(Trim_paths,WsTrue)
	V1= FreeParameter("Side",Inout)
	G1=BdyTrimPathGroup(Contour$,Isles,Trim_group,V1)
	Stat=DeleteEntity(Isles)
	RecordFunctionOff
	Stat= PlotEntity(G1)
	RecordFunctionOn
	Stat= AutoNameGroup(G1,Name$)
	Stat= AddGroupToUndo(WsUBCreateMode,G1,1,WsTrue)
	RecordFunctionOff
	Curr_record= CurrentVarRecord
	CloseVarRecord
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_loop

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