BuildingCoder.CmdPreviewImage.Execute C# (CSharp) Méthode

Execute() public méthode

public Execute ( ExternalCommandData commandData, string &message, ElementSet elements ) : System.Result
commandData ExternalCommandData
message string
elements ElementSet
Résultat System.Result
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
              UIDocument uidoc = uiapp.ActiveUIDocument;
              Document doc = uidoc.Document;

              FilteredElementCollector collector
            = new FilteredElementCollector( doc );

              collector.OfClass( typeof( FamilyInstance ) );

              foreach( FamilyInstance fi in collector )
              {
            Debug.Assert( null != fi.Category,
              "expected family instance to have a valid category" );

            ElementId typeId = fi.GetTypeId();

            ElementType type = doc.GetElement( typeId )
              as ElementType;

            Size imgSize = new Size( 200, 200 );

            Bitmap image = type.GetPreviewImage( imgSize );

            // encode image to jpeg for test display purposes:

            JpegBitmapEncoder encoder
              = new JpegBitmapEncoder();

            encoder.Frames.Add( BitmapFrame.Create(
              ConvertBitmapToBitmapSource( image ) ) );

            encoder.QualityLevel = 25;

            string filename = "a.jpg";

            FileStream file = new FileStream(
              filename, FileMode.Create, FileAccess.Write );

            encoder.Save( file );
            file.Close();

            Process.Start( filename ); // test display
              }
              return Result.Succeeded;
        }