
! script name: trim.d/boundarytrim
! topic: trim contour with boundary

!A
!A NAME:
!A
!A 	Boundary Trim
!A
!A DESCRIPTION:
!A
!A 	This script trims a contour with a boundary.
!A
!B INSTRUCTIONS:
!B
!B 	PICK EXTERIOR CONTOUR: Pick the contour to be used as the boundary
!B 		of the contour to be trimmed.
!B
!B 	NUMBER OF ISLANDS: Enter the number of islands to be included for
!B 		trimming purposes.
!B
!B 	PICK ISLAND # : Pick the islands to be included.
!B
!B 	PICK CONTOUR TO BE TRIMMED: Pick an element on the contour to be
!B 		trimmed.
!B

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

DIM Destination$[256]
DIM Answer$[256]

ActivateUndo
Active_workplane=0
Work=CurrentWorkplane
IF (ValidGeometry(Work)) THEN Active_workplane=1


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

Stat=SetInputFormat("n4","")
Boundary_loop:!
Contour=PickEntity("PICK EXTERIOR CONTOUR",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidGeometry(Contour)) THEN Boundary_loop

Stat=SetWorkplane(Contour)

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

	Stat=SetInputFormat("n4","")
	Isle=PickEntity("PICK ISLAND #"&VAL$(I))
	IF (ExitScript(Destination$)) THEN Leave

	Stat=AddToGroup(Islands,Isle)

NEXT I

Pick_loop:Trim_contour= PickGroupEnt("PICK CONTOUR TO BE TRIMMED",0,Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (NOT ValidGeometry(Trim_contour)) THEN Pick_loop

Stat=Highlight(Trim_contour,WsHighlightPen,Wsarrowoff)

Stat=SetWorkplane(Contour)

Stat= EraseEntity(Trim_contour)
PlotOff
Trim_contour1= CopyGroup(Trim_contour)
ActivateUndo
Stat= AdvanceUndoNumber("boundary trim")
Stat= AddGroupToUndo(WsUBDeleteMode,Trim_contour,1,WsTrue)
Stat= DeleteGroup(Trim_contour,WsTrue)

Trim_group=BoundaryTrim(Contour,Islands,Trim_contour1,Inout)

PlotOn
IF (ValidGeometry(Trim_group)) THEN
	Stat= PlotEntity(Trim_group)
	Stat=UnLink(Trim_group)
	Stat= AddGroupToUndo(WsUBCreateMode,Trim_group,0,WsTrue)
	Stat=DeleteEntity(Trim_group)
END IF

GOTO Pick_loop

Leave:!
Stat=DeleteEntity(Islands)
Stat=DeleteEntity(Trim_group)

IF Active_workplane THEN
	Stat=SetWorkplane(Work)
ELSE
	StdWorkplane
END IF
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
