I was looking for a printing method for Away3D scenes, and found this post on printing. I changed some of the design ideas to make it more extensible, and made it into a final (static) class to allow printing in one line.

Further, since in Away3D the View3D object extends the Sprite object, I had to add a method for printing Sprites, too.

This is a general printing class, and not specifically designed for Away3d.

Important note for Away3d users: you may have to do some fiddling with the objects in your scene. Even though your camera/view may be centered on the stage, this has no bearing in printing. At this point, the objects themselves have to be centered to the stage in order to be printed where you are expecting.

package
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.printing.PrintJob;
    /**
     *
     * Code derived in part from: http://stackoverflow.com/questions/1422259/as3-using-printjob-to-print-a-movieclip
     *
     * @author Delfeld, copyright 2010.
     */
    public final class Printing
    {
        public function Printing(){throw(new Error("Printing.Printing: Do not instantiate this class."));}

        public static function printSprite(clip:Sprite):void
        {
            if ( !clip )
                return;

            var printJob:PrintJob=new PrintJob();

            if ( !printJob.start() )
                return;

            // Resize movie clip to fit within page width
            var origWid:Number = clip.width;
            clip.width = printJob.pageWidth;
            clip.scaleY = clip.scaleX;

            // Add page to print job, set the clipping to the page dimensions.
            printJob.addPage(clip, new Rectangle(0, 0, printJob.pageWidth, printJob.pageHeight));

            // Send print job to printer
            // Note: If the job is cancelled at this point, it will send a blank page to the printer on my system.  Have not tracked down the bug yet.
            printJob.send();

            // Delete job from memory
            printJob = null;

            // Reset size.
            clip.width = origWid;
            clip.scaleY = clip.scaleX;
        }

        public static function printMovieClip(clip:MovieClip, frameToPrint:int = 1):void
        {
            if ( !clip )
                return;

            var printJob:PrintJob=new PrintJob();

            if ( !printJob.start() )
                return;

            // Print all frames of the MovieClip
            if ( frameToPrint == 0 )
            {
                for (var i:int=1; i <= clip.totalFrames; i++)
                    addMCFrame2PJ(printJob, clip, i);
            }
                // Print the selected frame
            else
            {
                addMCFrame2PJ(printJob, clip, frameToPrint);
            }

            // Send print job to printer
            // Note: If the job is cancelled at this point, it will send a blank page to the printer on my system.  Have not tracked down the bug yet.
            printJob.send();

            // Delete job from memory
            printJob = null;

            // Note: There may be a need to re-resize the MC.  Untested.
        }

        protected static function addMCFrame2PJ(printJob:PrintJob, clip:MovieClip, curFrame:int):void
        {
            // Goto the frame to be printed
            clip.gotoAndStop(curFrame);

            // Resize movie clip to fit within page width
            clip.width = printJob.pageWidth;
            clip.scaleY = clip.scaleX;

            // Add page to print job, set the clipping to the page dimensions.
            printJob.addPage(clip, new Rectangle(0, 0, printJob.pageWidth, printJob.pageHeight));
        }
    }
}

– 30 –

Advertisements