
! script name: pksurfaces.d/sewallsheets
! solids: sew all sheet bodies

!A
!A NAME:
!A
!A 	Sew All Sheet Bodies
!A
!A DESCRIPTION:
!A
!A 	This script attempts to sew all sheet bodies together
!A	to form a single sheet body.
!A
!A
!B INSTRUCTIONS:
!B
!B	GAP DISTANCE: Enter the maximum gap to be closed.
!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 all sheet bodies ")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]
DIM Title$[256]
DIM Name$[256]
DIM Label$[256]
DIM File$[256]
DIM Btype$[256]

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

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


WPKClearErrorFlag
G1= WPKPartitionBodies(WsPKSheetBody)
Group= WPKSewBodyList(G1,Gap_dist,Iterations)

IF ValidGeometry(Group) THEN
   N1= GroupCount(G1)
   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)
Stat= DeleteEntity(G1)

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

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