#include "meshOpCmd.h"
#include "meshOpNode.h"
#include <maya/MFnDependencyNode.h>
#include <maya/MFnMesh.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MPlug.h>
#include <maya/MArgList.h>
#include <maya/MIOStream.h>
#define MCheckStatus(status,message)    \
    if( MS::kSuccess != status ) {      \
        cerr << message << "\n";        \
        return status;                  \
    }
meshOp::meshOp()
{
    fOperation = (MeshOperation) 0;
}
meshOp::~meshOp()
{}
void* meshOp::creator()
{
    return new meshOp();
}
bool meshOp::isUndoable() const
{
    return true;
}
{
    bool badArgument = false;
    
    
    
    {
        int operationTypeArgument = argList.
asInt(0);
 
        if (operationTypeArgument < 0
            || operationTypeArgument > kMeshOperationCount - 1)
        {
            badArgument = true;
        }
        else
        {
            fOperation = (MeshOperation)operationTypeArgument;
        }
    }
    else badArgument = true;
    if (badArgument)
    {
        cerr << "Expecting one parameter: the operation type." << endl;
        cerr << "Valid types are: " << endl;
        cerr << "   0 - Subdivide edge(s)." << endl;
        cerr << "   1 - Subdivide face(s)." << endl;
        cerr << "   2 - Extrude edge(s)." << endl;
        cerr << "   3 - Extrude face(s)." << endl;
        cerr << "   4 - Collapse edge(s)." << endl;
        cerr << "   5 - Collapse face(s)." << endl;
        cerr << "   6 - Duplicate face(s)." << endl;
        cerr << "   7 - Extract face(s)." << endl;
        cerr << "   8 - Split face(s)." << endl;
        displayError(" Expecting one parameter: the operation type.");
    }
    
    
    MFn::Type componentType = meshOpFty::getExpectedComponentType(fOperation);
 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    bool found = false;
    bool foundMultiple = false;
    for( ; !selListIter.isDone(); selListIter.next() )
    {
        selListIter.getDagPath( dagPath, component );
        
        
        if( component.
apiType() == componentType )
 
        {
            if( !found )
            {
                
                
                
                
                
                compListFn.
add( component );
                
                
                
                fComponentList = compListFn.
object();
                
                
                
                
                
                
                
                setMeshNode( dagPath );
                found = true;
            }
            else
            {
                
                
                
                
                
                foundMultiple = true;
                break;
            }
        }
    }
    if( foundMultiple )
    {
        displayWarning("Found more than one object with selected components.");
        displayWarning("Only operating on first found object.");
    }
    
    
    setModifierNodeType( meshOpNode::id );
    if( found )
    {
        
        
        
        status = doModifyPoly();
        
        {
            setResult( "meshOp command succeeded!" );
        }
        else
        {
            displayError( "meshOp command failed!" );
        }
    }
    else
    {
        displayError(
            "meshOp command failed: Unable to find selected components" );
    }
    
    return status;
}
{
    
    
    status = redoModifyPoly();
    {
        setResult( "meshOp command succeeded!" );
    }
    else
    {
        displayError( "meshOp command failed!" );
    }
    return status;
}
{
    status = undoModifyPoly();
    {
        setResult( "meshOp undo succeeded!" );
    }
    else
    {
        setResult( "meshOp undo failed!" );
    }
    
    return status;
}
{
    
    
    
    
    MObject cpListAttr = depNodeFn.attribute( 
"inputComponents" );
 
    
    
    MPlug cpListPlug( modifierNode, cpListAttr );
 
    status = cpListPlug.setValue( fComponentList );
    
    
    MObject opTypeAttr = depNodeFn.attribute( 
"operationType" );
 
    MPlug opTypePlug( modifierNode, opTypeAttr );
 
    status = opTypePlug.setValue( fOperation );
    return status;
}
{
    fmeshOpFactory.setMesh( mesh );
    fmeshOpFactory.setComponentList( fComponentList );
    fmeshOpFactory.setComponentIDs( fComponentIDs );
    fmeshOpFactory.setMeshOperation( fOperation );
    
    
    status = fmeshOpFactory.doIt();
    return status;
}