
! script name: pksurfaces.d/pkbsurfgroup
! solids: construct sheet bodies from a synergy surface group

!A
!A NAME:
!A
!A 	Make Sheet Bodies from a group of Surfaces
!A
!A DESCRIPTION:
!A
!A 	construct Sheet Bodies from a  Synergy surface group
!A
!B INSTRUCTIONS:
!B
!B	PICK GROUP: Pick the group of synergy surfaces to be converted.
!B
!B      GAP DISTANCE  No sew : Enter the maximum gap to be closed or
!B				turn off the sewing.
!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.
!C NOTES:
!C
!C	Can be used to convert existing surfaces into a parasolids
!C	sheet body.
!C
!# see also - htdocs/help.d/parasolids.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: make sheets from group")

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]

Title$= "CONVERSION 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

Pick_group:!

Stat=SetInputFormat("n4","")
Surf_group=PickGroupEnt("PICK GROUP")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surf_group) THEN Pick_group

Gap_dist:!
Stat=SetInputFormat("a1n4=",",N,")
Stat=SetEqualsValue(Gap_dist)
Gap_dist=GetValue("GAP DISTANCE  No sew ",Answer$)
IF ExitScript(Destination$) THEN Leave

IF Answer$= "N" THEN
	Sew_bodies= WsFalse
ELSE
	Sew_bodies= WsTrue

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

END IF


WPKClearErrorFlag
PlotOff
Bodies= WPKMakeFromSurfGrp(Surf_group)

IF WPKErrorFlag THEN Sew_bodies= WsFalse
ActivateUndo
Stat= AdvanceUndoNumber("convert trimmed surfaces")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck

IF Sew_bodies THEN
	WPKClearErrorFlag
	Group= WPKSewBodyList(Bodies,Gap_dist,Iterations)
	IF ValidGeometry(Group) THEN
   	   N1= GroupCount(Bodies)
   	   N2= GroupCount(Group)
	   N3= GroupCount(Surf_group)
   	   Label$= "SEWING RESULTS"
   	   Stat= OutputString(File$,Label$&CHR$(10),WsNewFile)
   	   OUTPUT A$ USING "35A,5D";"Starting number of surfaces:",N3
   	   Stat= OutputString(File$,A$&CHR$(10),WsAppend)
   	   OUTPUT A$ USING "35A,5D";"Converted 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)
ELSE
   IF NOT WPKErrorFlag THEN 
   	N1= GroupCount(Bodies)
	N3= GroupCount(Surf_group)
	Label$= "CONVERSION RESULTS"
        Stat= OutputString(File$,Label$&CHR$(10),WsNewFile)
   	OUTPUT A$ USING "35A,5D";"Starting number of surfaces:",N3
        Stat= OutputString(File$,A$&CHR$(10),WsAppend)
        OUTPUT A$ USING "35A,5D";"Converted number of sheet bodies:",N1
        Stat= OutputString(File$,A$&CHR$(10),WsAppend)
   	Stat=MessageFileBox(File$,Title$)
   END IF
END IF
Stat= DeleteEntity(Bodies)
PlotOn

IF NOT WPKErrorFlag THEN
	RefineAll
END IF
ActivateUndo
Stat= AdvanceUndoNumber("sew bodies")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck

GOTO Pick_group

Leave:!
PlotOn
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
