
! script name: vsolids.d/boundedplane
! var_topic: make a plane bounded by  exterior and interior closed contours

!A
!A NAME:
!A
!A 	Bounded Plane	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script make a plane entity trimmed to an exterior and
!A 	the interior closed contours.
!A
!B INSTRUCTIONS:
!B
!B 	PICK EXTERIOR CONTOUR: Pick the exterior boundary of the boundary.
!B 		The contour had to be created by types or by auto link.
!B
!B 	# OF INTERIOR CONTOURS: Enter the number of interior islands.
!B
!B 	PICK INTERIOR CONTOUR # : Pick the interior contours.  The contour(s)
!B 		had to be created by types or by auto link.
!B
!C NOTE:
!C
!C	The exterior and interior contours must be closed.
!C	The construction must take place in the workplane of the contours.
!C	The contours must be flat relative to the plane created.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: bounded plane ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Contour$[256]
DIM Islands$[256]
DIM Isle_name$[256]

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

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)
Tol=NamedVarVal("3D Trimming Tol",Ws3DSystemVars,0.005)

Contour_loop:!
ActivateUndo
Stat=SetPickMask(WsPickNoSurf)
Contour=PickGroupEnt("PICK EXTERIOR CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidDependency(Contour)) THEN Boundary_loop
Stat= EntityNameCheck(Contour,Contour$)
Stat=SetPickMask(WsPickGeometry)

Closed=IsContClosed(Contour)
IF NOT Closed THEN Contour_loop
Stat= SetSelectedFlag(Contour,WsTrue)
Stat= PlotEntity(Contour)

Stat=SetInputFormat("n0=","")
Stat= SetEqualsValue(0)
Nisles=GetValue("# OF INTERIOR CONTOURS  1 2 3 4 5 ",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles
Pick_isle: Stat=SetPickMask(WsPickNoSurf)
	Isle=PickGroupEnt("PICK INTERIOR CONTOUR #"&VAL$(I))
        IF (ExitScript(Destination$)) THEN Leave
        IF (NOT ValidDependency(Isle)) THEN Pick_isle
	Closed=IsContClosed(Isle)
	IF NOT Closed THEN Pick_isle
        Stat=AppendToGroup(Islands,Isle)
        Stat= SetSelectedFlag(Isle,WsTrue)
        Stat= PlotEntity(Isle)
	Stat=SetPickMask(WsPickGeometry)
NEXT I

Stat= SetSelectedFlag(Contour,WsFalse)
Stat= SetSelectedFlag(Islands,WsFalse)

Stat= GenerateVarName(WsPlaneEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("PLANE: Bounded plane")
Stat= NullReference(Contour$)
Isles=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Nisles
        Isle= GroupElement(Islands,I)
        Stat= EntityNameCheck(Isle,Isle_name$)
        RecordFunctionOn
        Stat= AppendToGroup(Isles,Isle_name$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
V1= FreeParameter("Tolerance",Tol)
Plane1= BoundedPlane(Contour$,Isles,V1)
Stat= NameEntity(Plane1,Name$)
Stat= DeleteEntity(Isles)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Plane1) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
	Stat= AdvanceUndoNumber("bounded plane")
	Stat= AddItemToUndo(WsUBCreateMode,Plane1)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

Stat= PlotEntity(Plane1)
Stat= PlotEntity(Contour)
Stat= PlotEntity(Islands)
Stat= DeleteEntity(Islands)
GOTO Contour_loop

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