convertEdgesToFacesCmd.cpp
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MObject.h>
#include <maya/MDagPath.h>
#include <maya/MItMeshEdge.h>
#include <maya/MItMeshPolygon.h>
class convertEdgesToContainedFacesCommand : public MPxCommand
{
public:
convertEdgesToContainedFacesCommand();
virtual ~convertEdgesToContainedFacesCommand();
static void* creator();
bool isUndoable() const;
MStatus doIt(const MArgList&);
MStatus redoIt();
MStatus undoIt();
private:
MSelectionList previousSelectionList;
};
convertEdgesToContainedFacesCommand::convertEdgesToContainedFacesCommand()
{
}
convertEdgesToContainedFacesCommand::~convertEdgesToContainedFacesCommand()
{
previousSelectionList.clear();
}
void* convertEdgesToContainedFacesCommand::creator()
{
return new convertEdgesToContainedFacesCommand;
}
bool convertEdgesToContainedFacesCommand::isUndoable() const
{
return true;
}
MStatus convertEdgesToContainedFacesCommand::doIt(const MArgList& args)
{
MGlobal::getActiveSelectionList(previousSelectionList);
return redoIt();
}
MStatus convertEdgesToContainedFacesCommand::redoIt()
{
MSelectionList finalFacesSelection;
MDagPath meshDagPath;
MObject multiEdgeComponent, singleEdgeComponent;
int dummyIndex;
for (MItSelectionList edgeComponentIter(previousSelectionList, MFn::kMeshEdgeComponent); !edgeComponentIter.isDone(); edgeComponentIter.next())
{
edgeComponentIter.getDagPath(meshDagPath, multiEdgeComponent);
MString meshName = meshDagPath.fullPathName();
if (!multiEdgeComponent.isNull())
{
for (MItMeshEdge edgeIter(meshDagPath, multiEdgeComponent); !edgeIter.isDone(); edgeIter.next())
{
MIntArray connectedFacesIndices;
edgeIter.getConnectedFaces(connectedFacesIndices);
MItMeshPolygon faceIter(meshDagPath);
for (unsigned i=0; i<connectedFacesIndices.length(); i++)
{
MIntArray faceEdgesIndices;
faceIter.setIndex(connectedFacesIndices[i], dummyIndex);
faceIter.getEdges(faceEdgesIndices);
bool faceIsContained=1;
for (unsigned j=0; j<faceEdgesIndices.length(); j++)
{
MSelectionList singleEdgeList;
singleEdgeList.clear();
MString edgeName = meshName;
edgeName += ".e[";
edgeName += faceEdgesIndices[j];
edgeName += "]";
singleEdgeList.add(edgeName);
singleEdgeList.getDagPath(0, meshDagPath, singleEdgeComponent);
if (!previousSelectionList.hasItem(meshDagPath, singleEdgeComponent))
{
faceIsContained = 0;
break;
}
}
if (faceIsContained)
{
MString faceName = meshName;
faceName += ".f[";
faceName += connectedFacesIndices[i];
faceName += "]";
finalFacesSelection.add(faceName);
}
}
}
}
}
MGlobal::setActiveSelectionList(finalFacesSelection, MGlobal::kReplaceList);
MStringArray containedFacesArray;
finalFacesSelection.getSelectionStrings(containedFacesArray);
MPxCommand::setResult(containedFacesArray);
return MS::kSuccess;
}
MStatus convertEdgesToContainedFacesCommand::undoIt()
{
MGlobal::setActiveSelectionList(previousSelectionList, MGlobal::kReplaceList);
return MS::kSuccess;
}
MStatus initializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj, PLUGIN_COMPANY, "4.0", "Any");
status = plugin.registerCommand("convertEdgesToFaces", convertEdgesToContainedFacesCommand::creator);
if (!status)
{
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj);
status = plugin.deregisterCommand("convertEdgesToFaces");
if (!status)
{
status.perror("deregisterCommand");
return status;
}
return status;
}