Momentary plug-in example
 
 
 

Below is an example of a plug-in which does a viewframe on all picked objects. It is a simple example of a momentary plug-in.

ViewFrameExample - init.scm

;; This file simply gives the defaults for the Scheme variables ;; used in the option box. (ui-symbol "frame" 0) (ui-string "frame"                      "Frame")

ViewFrameExample .scm

;; This file defines the option box (ui-editor "frame.options" (list 'symbols  'frame )          (list 'title "View Frame Options")          (ui-integer-widget "frame" (list 'range 0 65535)         )  )

ViewFrameExample .cpp

#include <AlUniverse.h> 
#include <AlViewFrame.h> 
#include <AlPickList.h> 
#include <AlObject.h>  
#include <AlLiveData.h> 
#include <AlFunction.h> 
#include <AlFunctionHandle.h> 
#include <AlIterator.h>  
#include <strings.h>   
// A simple iterator used to pass the frame number 
// along when viewing an object. 
class viewIterator: public AlIterator 
{ 
	public: 
	viewIterator( double frame = 0.0 )
 				: frameNum( frame ) {};
 		virtual	~viewIterator() {};
  		virtual	int func( AlObject* );
  	private:
 		double frameNum; };
// The iterator's one and only method. Does a viewframe 
// on the passed in object using the frame number in the 
// iterator object.
 int viewIterator::func( AlObject* obj ) {
 	if ( obj == NULL )
 		return 0;
  	AlAnimatable*	animitem;
 	AlCurve*		curve;
 	AlSurface*		surface;
  	AlObjectType	type = obj->type();
  	if( animitem = obj->asAnimatablePtr() )
 		AlViewFrame::viewFrame( animitem, frameNum, AlViewFrame::kObjectAndBelow );
 	else if( curve = obj->asCurvePtr() )
 		AlViewFrame::viewFrame( curve, frameNum );
 	else if( surface = obj->asSurfacePtr() )
 		AlViewFrame::viewFrame( surface, frameNum );
 	else 		AlPrintf( kPrompt, "Sorry, %s is not a viewable object", obj->name() );
 		;	// a non viewable object
 	return 0;
 }
  // The actual plug-in function.
 void my_view()
 {
 	int		frameNum;
  	// Get the frame number from the option box.
 	if( sSuccess == AlGetInteger( "frame", frameNum ) )
 	{
 		// Print the number to the promptline.
 		AlPrintf( kPrompt, "Viewing frame %d.", frameNum );
  		// Instantiate an iterator and apply it to all picked 
		// objects.
 		int rc; 
		viewIterator*	view = new viewIterator( frameNum );
 		if ( sSuccess != AlPickList::applyIteratorToItems( view, rc ) )
 			AlPrintf( kPrompt, "Iterator failed." );
 		if ( rc != 0 )
 			AlPrintf( kPrompt, "Return code is %d.", rc );
  		AlPrintf( kPrompt, "Viewing complete." );
 	} }
// This handle may have to be global if you wish to remove the
// plug-in from the menu using the h.destroy() method in the
// 'momentary_exit' function.
// The menu entry is automatically removed when Alias exits.
// 
static AlFunctionHandle h; 
static AlMomentaryFunction hFunc;
 
extern "C" int 
PLUGINAPI_DECL int plugin_init( const char *dirName ) 
// 
// This routine initializes the plugins and attaches it to the menu. 
// It returns 0 if there is no initialization error. 
// { 	char *dirScm; 
 	// Initialize the universe. This must be done by all
 	// plug-ins. If the universe is not initialized the plug-in
 	// will fail.
 	AlUniverse::initialize( );
  	dirScm = makeAltPath(dirName,"scm");
  	// Invoke the scheme file which sets defaults for the scheme
 	// variables.
 	AlInvokeSchemeFile( "frame_init.scm", dirScm ); 
 	// Allocate a function handle. The first argument is the label on
 	// the menu and the second is the function to invoke when the
 	// menu item is selected.
 	hFunc.create( my_view );
 	h.create( "View Frame", &hFunc );
   	// Define the attribute string for the attribute line below
 	// the prompt line. 	h.setAttributeString( "viewframe" );
  	// Read in the option box. The first argument is the
 	// option box in $ALIAS_WORKENV, and
 	// the second argument is the name of the option box given
 	// in the scheme file. The third argument is a directory
 	// path(possibly NULL) where to look for the scheme file.
 	// If a call to this method is omitted, there will be no option box.
     if ( sSuccess != h.setOptionBox( "frame.scm", "frame.options", dirScm) ) {
         AlPrintf(
  			kPrompt,
  			"Frame plug-in unable to find .scm file for option box"
 		);
         return (1);
     }
   	// Indicate which menu to add the plugin to. addToMenu()
 	// adds the plugin to the bottom of the menu, while
 	// appendToMenu() will add it to the top of the menu.
 	h.addToMenu( "mp_objtools" );
  	h.setIconPath( makeAltPath( dirName, NULL ) );
  	// Return a success code.
 	// Returning a non zero indicates an error.
 	// An error value ( a non-zero ) will be printed on the prompt
 	// line in Alias.
 	AlPrintf( kPrompt, "Frame attached to Palette 'Object Edit'");
 	return 0;
 }  
extern "C" 
PLUGINAPI_DECL int plugin_exit( void )
{
 	// Remove the plugin from the menu and free the FunctionHandle.
 	// Note that h.destroy() implicitly calls h.removeFromMenu()
 	h.deleteObject();
 	hFunc.deleteObject();
 	return 0;
 }