
! script name: pksurfaces.d/sewbodylist
! solids: sew multiple sheet bodies

!A
!A NAME:
!A
!A 	Sew Multiple Sheet Bodies
!A
!A DESCRIPTION:
!A
!A 	This script sews multiple sheet bodies together along common
!A	edges to form a single sheet body.
!A
!B INSTRUCTIONS:
!B
!B	GAP DISTANCE: Enter the maximum gap to be closed.
!B
!B	PICK SHEET BODY: Pick the sheet bodies.
!B
!B	NUMBER OF ITERATIONS: Number of trials in the sewing process.
!B			      If the number of iterations is 1, only one trial
!B			      will be made using the input gap distance.
!B			      If the number of iterations is 2, then
!B			      the first trial will be at 1/2 the gap
!B			      distance, the second trial at the full
!B			      gap distance.
!B
!B NOTE:
!B
!B	Sewing multiple bodies can be a very unstable operation. Real
!B	problems can result if a large gap distance results in unwanted
!B	connections of edges. The parasolids kernel has been known to
!B	fail in an unrecoverable way during sewing, also sometimes
!B	sewing will produced bodies which are unworkable. It is important
!B	to remember that once sheets have been sewn together, problems
!B	may arise which can only be fixed by undoing the sew operation.
!B	The user should save the current file before sewing.
!B
!B	The user must check that the bodies have been joined along
!B	the common edges. Carefully examine the wire frame of the sheet
!B	looking for double edges. The sheets may join in some areas and
!B	not in others. If the join is imperfect, undo and use a larger
!B	gap distance.
!B
!B	Using too large a gap distance may result in undesired merging.
!B
!B	Too small a gap distance will result in an imperfect join.
!B	
!B	If a series of sheet bodies is sewn together to form a closed
!B	volume a solid body will result if possible.
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: sew multiple sheet bodies ")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]
DIM Title$[256]
DIM Name$[256]
DIM Label$[256]
DIM File$[256]
DIM Btype$[256]
REAL Bodies(500)

Title$= "SEWING RESULTS"
File$= "tempdata"
Stat= RemoveFile(File$)


ActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
        Gap_dist= 1.25
ELSE
        Gap_dist= .050
END IF
Stat= GLPlotOn

Start:!
Gap_dist:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Gap_dist)
Gap_dist=GetValue("GAP DISTANCE ")
IF ExitScript(Destination$) THEN Leave

Iterations:! Stat=SetInputFormat("n0=","")
Stat=SetEqualsValue(1)
Iterations=GetValue("NUMBER OF ITERATIONS")
IF ExitScript(Destination$) THEN Leave

Num_bodies= 0
Body_loop:!
Stat=SetInputFormat("a1",",S,U,C,")
E1=WPKPickEntity("PICK SHEET BODY: Sew, Unpick ,Clear ",WsPKPickAll,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "S"
        GOSUB Sew_bodies
        GOTO Start
CASE "U"
        Num_bodies= Num_bodies-1
        IF Num_bodies<0 THEN Num_bodies=0
        PlotAll
        GOTO Body_loop
CASE "C"
        PlotAll
        GOTO Start
END SELECT
IF Num_bodies>= 499 THEN Start

IF NOT WPKIsEntity(E1) THEN Body_loop
Body= WPKParentBody(E1)
Body_type= WPKBodyType(Body)
IF Body_type<> WsPKSheetBody THEN Body_loop
Body_tag= WPKEntityTag(Body)
FOR I= 1 TO Num_bodies
	IF Bodies(I)= Body_tag THEN Body_loop
NEXT I
Num_bodies= Num_bodies+1
Bodies(Num_bodies)= Body_tag
GOTO Body_loop


Sew_bodies:!
WPKClearErrorFlag
Group= WPKSewBodyList(Num_bodies,Bodies,Gap_dist,Iterations)
IF ValidGeometry(Group) THEN
   N1= Num_bodies
   N2= GroupCount(Group)
   Label$= "SEWING RESULTS"
   Stat= OutputString(File$,Label$&CHR$(10),WsNewFile)
   OUTPUT A$ USING "35A,5D";"Starting number of sheet bodies:",N1
   Stat= OutputString(File$,A$&CHR$(10),WsAppend)
   OUTPUT A$ USING "35A,5D";"Resulting number of bodies:",N2
   Stat= OutputString(File$,A$&CHR$(10),WsAppend)
   IF N2= 1 THEN
        Pkbody= GroupElement(Group,1)
        body_type= WPKBodyType(Pkbody)
        SELECT body_type
        CASE WsPKSolidBody
                 Btype$= "SOLID BODY"
        CASE WsPKSheetBody
                Btype$= "SHEET BODY"
        CASE WsPKWireBody
                Btype$= "WIRE BODY"
        CASE ELSE
                Btype$= "GENERAL BODY"
        END SELECT
        OUTPUT A$ USING "35A,10A";"Resulting body type:",Btype$
        Stat= OutputString(File$,A$&CHR$(10),WsAppend)
   END IF
   Stat=MessageFileBox(File$,Title$)
END IF
Stat= DeleteEntity(Group)

ActivateUndo
Stat= AdvanceUndoNumber("sew bodies")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck
RETURN


Leave:!
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
