
! script name: trim.d/polygonclip
! topic: clip elements in a box to a polygon

!A
!A NAME:
!A
!A 	Polygon Trim - Box
!A
!A DESCRIPTION:
!A
!A 	This script clips elements in a box to a polygon.
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: polygon clip ")

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

ActivateUndo
Boundary_color= 3
Boundary_ltype= 9
Units= CurrentUnits
Delta= 0.0001
IF Units= WsMetric THEN Delta= 0.001

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","")
Group=CreateGroup("TEMP")
Vnum= LastPickView
Stat=PickBox(Group,Vnum,WsPickPen)
IF ExitScript(Destination$) THEN Leave

IF NOT ValidGeometry(Group) THEN
        GOTO Start
ELSE
        IF NOT GroupCount(Group) THEN 
		Stat= DeleteEntity(Group)
		GOTO Start
	END IF
END IF

Line_chain:!
Length1= 1
Contour= CreateGroup("LINE CHAIN",WsContourGroup)

Pick_first:!
Stat=SetInputFormat("n4b"," ")
Ps=XyzInput("STARTING POINT OF CLIP AREA ",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetLastPoint(Ps)
Pstart= ParameterPt(Ps,0.0)

Line_loop:!
PlotOff
Pe= PtIncrXyz(Ps,Length1,0.0,0.0)
L1= LineTwoPoints(Ps,Pe)
Stat= MakeInvisible(Ps)
Stat= MakeInvisible(Pe)
Stat= MakeInvisible(L1)
PlotOn

Vnum= LastPickView
Stat=DynamicEntity(Vnum,L1,Ps,Pe,WsDynamicEnd)
IF (ExitScript(Destination$)) THEN
        Stat=DeleteEntity(L1)
        PlotOff
        Stat=DeleteEntity(Pe)
        Stat=DeleteEntity(Ps)
        PlotOn
        IF Destination$= "BACKUP" THEN
                N1=GroupCount(Contour)
                IF N1=0  THEN Pick_first
                L1= GroupElement(Contour,N1)
                PlotOff
                        Ps= ParameterPt(L1,0.0)
                        Stat= MakeInvisible(Ps)
                PlotOn
                Stat= RemoveFromGrp(Contour,N1)
                Stat= DeleteEntity(L1)
                GOTO Line_loop
        END IF
	Stat=DeleteGroup(Contour,WsTrue)
        GOTO Leave
END IF
Stat= MakeVisible(L1)
Stat= SetColor(L1,Boundary_color)
Stat= SetLineType(L1,Boundary_ltype)
Stat= SetForceVisible(L1,WsTrue)
Stat= PlotEntity(L1)
Stat= AddToGroup(Contour,L1)

PlotOff
Stat=DeleteEntity(Pe)
Pe= ParameterPt(L1,1.0)
Stat=SetLastPoint(Pe)
Stat=DeleteEntity(Ps)
Ps=Pe
PlotOn
Dist= DistTwoPts(Pstart,Ps)
IF Dist<= Delta THEN
        Stat=DeleteEntity(Ps)
        Stat=DeleteEntity(Pstart)
        GOTO Trim_contour
END IF
GOTO Line_loop


Trim_contour:!
Closed=IsContClosed(Contour)
IF NOT Closed THEN 
	Stat=DeleteGroup(Contour,WsTrue)
	GOTO Line_chain
END IF
Islands=CreateGroup("ISLANDS")

Stat= EraseEntity(Group)
PlotOff
Group1= CopyGroup(Group)
ActivateUndo
Stat= AdvanceUndoNumber("polygon clip")
Stat= AddGroupToUndo(WsUBDeleteMode,Group1,0,WsTrue)
Stat= DeleteGroup(Group,WsTrue)

Stat=UnLink(Group1)
Trim_group=BoundaryTrim(Contour,Islands,Group1,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
Stat=DeleteEntity(Islands)
Stat=DeleteGroup(Contour,WsTrue)

GOTO Start


Leave:!
Stat=DeleteEntity(Group)
Stat=DeleteEntity(Islands)
Stat=DeleteEntity(Trim_group)
Stat=DeleteGroup(Contour,WsTrue)

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