from PSPApp import *
import PSPUtils

def ScriptProperties():
	return {
		'Author': u'Corel Corporation',
		'Copyright': u'Copyright (c) 2002-2006 Corel Corporation. All rights reserved.',
		'Description': "Merge the selected objects into the first object selected rewind the contours.",
		'Host': u'Paint Shop Pro 9',
		'Host Version': u'9.00'
	}


def Do(Environment):
	# get the set of selected objects
	Result = App.Do( Environment, 'ReturnVectorObjectProperties', {'GeneralSettings': {'Version': ((9,0,0),1) }} )
	ObjList = Result['ListOfObjects']

	# use list comprehension to get a list of all the regular objects and all the text
	# objects.  We don't care about the text objects, but if any exist we can't do the
	# merge
	if ObjList is not None:
		MergeableObjects = [ Obj for Obj in ObjList if Obj['ObjectData'] is not None ]
		TextExObjects = [ Obj for Obj in ObjList if Obj['TextExData'] is not None ]
		TextObjects = [ Obj for Obj in ObjList if Obj['TextData'] is not None ]
		RectangleObjects = [ Obj for Obj in ObjList if Obj['RectangleData'] is not None ]
		EllipseObjects = [ Obj for Obj in ObjList if Obj['EllipseData'] is not None ]
		SymmetricShapeObjects = [ Obj for Obj in ObjList if Obj['SymmetricShapeData'] is not None ]
	else:
		App.Do(Environment,  'MsgBox', {
					'Buttons': App.Constants.MsgButtons.OK, 
					'Icon': App.Constants.MsgIcons.Info, 
					'Text': PSPUtils.TwoOrMoreMsg,
                                        'GeneralSettings': {
                                            'Version': ((9,0,0),1)
                                            }
					})
		return

	x = 0
	if len(TextExObjects) > 0:
		x = 1
	elif len(TextObjects) > 0:
		x = 1
	elif len(RectangleObjects) > 0:
		x = 1
	elif len(EllipseObjects) > 0:
		x = 1
	elif len(SymmetricShapeObjects) > 0:
		x = 1
	if x:
		App.Do( Environment, 'ConvertToPath', {'GeneralSettings': {'Version': ((9,0,0),1) }} )
		Result = App.Do( Environment, 'ReturnVectorObjectProperties', {'GeneralSettings': {'Version': ((9,0,0),1) }} )
		ObjList = Result['ListOfObjects']
		if ObjList is not None:
			MergeableObjects = [ Obj for Obj in ObjList if Obj['ObjectData'] is not None ]
	if len(MergeableObjects) < 2:
		App.Do(Environment,  'MsgBox', {
					'Buttons': App.Constants.MsgButtons.OK, 
					'Icon': App.Constants.MsgIcons.Info, 
					'Text': PSPUtils.TwoOrMoreMsg,
                                        'GeneralSettings': {
                                            'Version': ((9,0,0),1)
                                            }
					})
		return

	# delete all the selected objects
	App.Do( Environment, 'ClearSelection', {'GeneralSettings': {'Version': ((9,0,0),1) }} )

	# Create the base object by taking the first thing in the mergeable list
	App.Do( Environment, 'AddGroupsAndObjects', {'ListOfObjects': [MergeableObjects[0]],
                                                     'GeneralSettings': {'Version': ((9,0,0),1) }} )

	# now add all the other objects - we could rewind the contours after every object, but
	# there is a performance penalty to doing so, so we only put the windpath parameter
	# on the last object in the list.
	for Obj in MergeableObjects[1:-1]: # take everything after the first object    
		App.Do( Environment, 'NodeEditAddPath', { 'Path': Obj['ObjectData']['Path'],'WindPath': App.Constants.Boolean.false,
                                                          'GeneralSettings': {'Version': ((9,0,0),1) }})
		
	# on the last object set the winding flag so that nested objects become cutouts
	Obj = MergeableObjects[-1]
	App.Do( Environment, 'NodeEditAddPath', { 'Path': Obj['ObjectData']['Path'],'WindPath': App.Constants.Boolean.true,
                                                  'GeneralSettings': {'Version': ((9,0,0),1) } })

