
! script name: vpkassembly.d/vpkcreateinstance
! var_solids: create an instance of a body in an assembly

!A
!A NAME:
!A
!A 	Create Instance  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will create an instance of a body in an assembly
!A
!B INSTRUCTIONS:
!B
!B 	PICK ASSEMBLY: Pick the assembly.
!B
!B 	PICK BODY: Pick the body to be added to the assembly.
!B
!B 	X AXIS ROTATION: X axis rotation angle about reference point.
!B
!B 	Y AXIS ROTATION: Y axis rotation angle about reference point.
!B
!B 	Z AXIS ROTATION: Z axis rotation angle about reference point.
!B
!B 	REFERENCE POINT: Input the reference point for the transformation.
!B
!B 	DESTINATION POINT: Input the destination point.
!B
!# see also - htdocs/help.d/parasolids.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: create instance")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Body$[256]
DIM Assembly1$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Xangle_var$[256]
DIM Yangle_var$[256]
DIM Zangle_var$[256]

ActivateUndo
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

N1= WPKAssemblyCount
IF NOT N1 THEN Leave

Stat= GLPlotOn
Pick_assembly:!
Pkassembly= WPKCurrentAssembly
IF NOT WPKIsEntity(Pkassembly) THEN 
	Stat=SetInputFormat("n4","")
	E1=WPKPickEntity("PICK ASSEMBLY")
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT WPKIsEntity(E1) THEN Pick_assembly

	Pkbody= WPKParentBody(E1)
	Pkassembly= WPKParentAssembly(Pkbody,1)
END IF
Stat= EntityNameCheck(Pkassembly,Assembly1$)

Pick_body:!
N1= WPKAssemblyCount
IF N1> 1 THEN
	Stat=SetInputFormat("n4a1",",A,")
	E1=WPKPickEntity("PICK BODY:   Add assembly",WsPKPickAll,Answer$)
ELSE
	Stat=SetInputFormat("n4","")
	E1=WPKPickEntity("PICK BODY",WsPKPickAll,Answer$)
END IF
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "A" THEN
Pick_assembly2:!
	Stat=SetInputFormat("n4","")
	E1=WPKPickEntity("PICK ASSEMBLY TO ADD")
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT WPKIsEntity(E1) THEN Pick_assembly2

	Pkbody= WPKParentBody(E1)
	PkAssembly2= WPKParentAssembly(Pkbody,1)
	Pkbody= Pkassembly2
ELSE
	IF NOT WPKIsEntity(E1) THEN Pick_body
	Pkbody= WPKParentBody(E1)
END IF
Stat= EntityNameCheck(Pkbody,Body$)


Xangle:!
Stat=SetInputFormat("a1n4=",",N, ")
Stat=SetEqualsValue(Xangle)
Xangle=GetValue("X AXIS ROTATION, No transform  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "N" THEN
	No_transform=1
	GOTO Create_instance
END IF
No_transform=0
No_xangle_var= LastVariableInput(Xangle_var$)

Stat=SetEqualsValue(Yangle)
Yangle=GetValue("Y AXIS ROTATION  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_yangle_var= LastVariableInput(Yangle_var$)

Stat=SetEqualsValue(Zangle)
Zangle=GetValue("Z AXIS ROTATION  ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_zangle_var= LastVariableInput(Zangle_var$)

Pick_ref:Stat=SetInputFormat("n4=","")
Ps=XyzInput("REFERENCE POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Ps) THEN Pick_ref
E1= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF NOT ValidDependency(E1) THEN
        E1= CopyEntity(Ps)
        Stat= EntityNameCheck(E1,Point1$)
        Ps_created= WsTrue
ELSE
        Stat= EntityNameCheck(E1,Point1$)
        Ps_created= WsFalse
END IF
Stat=SetLastPoint(Ps)


Next_dest:!
Dest_point:Stat=SetInputFormat("n4=","")
Pe=XyzInput("DESTINATION POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Pe) THEN Dest_point
E2= LastPickEnt
Para2= LastPickPara
Pick_return2= CurrPickReturn
IF NOT ValidDependency(E2) THEN
        E2= CopyEntity(Pe)
        Stat= EntityNameCheck(E2,Point2$)
        Pe_created= WsTrue
ELSE
        Stat= EntityNameCheck(E2,Point2$)
        Pe_created= WsFalse
END IF
Stat=SetLastPoint(Pe)
Stat= DeleteEntity(Ps)
Stat= DeleteEntity(Pe)


Create_instance:!
WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("ASSEMBLY: create instance")
IF No_transform THEN
	P1=WPKCreateInstance(Assembly1$,Body$)
ELSE
	SELECT Pick_return1
	CASE WsGeometryEnd
        	Ps= ParameterReturnPt(Point1$,Para1,WsGeometryEnd)
	CASE WsGeometryMid
        	Ps= ParameterReturnPt(Point1$,Para1,WsGeometryMid)
	CASE WsGeomCenter
        	Ps= ParameterReturnPt(Point1$,Para1,WsGeomCenter)
	END SELECT
	SELECT Pick_return2
	CASE WsGeometryEnd
        	Pe= ParameterReturnPt(Point2$,Para2,WsGeometryEnd)
	CASE WsGeometryMid
        	Pe= ParameterReturnPt(Point2$,Para2,WsGeometryMid)
	CASE WsGeomCenter
        	Pe= ParameterReturnPt(Point2$,Para2,WsGeomCenter)
	END SELECT
	IF No_xangle_var THEN
        	V1= FreeParameter("X angle",Xangle)
	ELSE
        	V1= FreeParameter("X angle",Xangle_var$)
	END IF
	IF No_yangle_var THEN
       		V2= FreeParameter("Y angle",Yangle)
	ELSE
        	V2= FreeParameter("Y angle",Yangle_var$)
	END IF
	IF No_zangle_var THEN
        	V3= FreeParameter("Z angle",Zangle)
	ELSE
        	V3= FreeParameter("Z angle",Zangle_var$)
	END IF
	T1=AngleTransform(Ps,0,0,0,WsWorkplaneVals)
        T2= InverseTrans(T1)
        T3=AngleTransform(Pe,V1,V2,V3,WsWorkplaneVals)
        T4= MultTransforms(T3,T2)
        P1=WPKCreateInstance(Assembly1$,Body$,T4)
        Stat= DeleteEntityList(Ps,Pe,T1,T2,T3,T4)
END IF
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("create instance")
IF NOT No_transform THEN
IF Ps_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point1$)
IF Pe_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point2$)
END IF
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck
IF No_transform THEN
        GOTO Pick_body
END IF
GOTO Next_dest

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