导出图片

class CmdExportImage : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;
            bool use_old_code = false;

            Result r = use_old_code
              ? ExportToImage2(doc)
              : ExportToImage3(doc);
            return r;
        }

        // 设置白色背景
        void SetWhiteRenderBackground(View3D view)
        {
            RenderingSettings rs = view.GetRenderingSettings();
            rs.BackgroundStyle = BackgroundStyle.Color;
            ColorBackgroundSettings cbs = (ColorBackgroundSettings)rs.GetBackgroundSettings();
            cbs.Color = new Color(255, 0, 0);
            rs.SetBackgroundSettings(cbs);
            view.SetRenderingSettings(rs);
        }

        static string ExportToImage(Document doc)
        {
            var tempFileName = Path.ChangeExtension(Path.GetRandomFileName(), "png");
            string tempImageFile;

            try
            {
                tempImageFile = Path.Combine(Path.GetTempPath(), tempFileName);
            }
            catch (IOException)
            {
                return null;
            }

            IList<ElementId> views = new List<ElementId>();
            try
            {
                var collector = new FilteredElementCollector(
                  doc);

                var viewFamilyType = collector.OfClass(typeof(ViewFamilyType)).OfType<ViewFamilyType>().FirstOrDefault(x =>x.ViewFamily == ViewFamily.ThreeDimensional);
                var view3D = (viewFamilyType != null)
                ? View3D.CreateIsometric(doc, viewFamilyType.Id)
                : null;

                if (view3D != null)
                {
                    // Ensure white background.
                    Color white = new Color(255, 255, 255);
                    view3D.SetBackground(ViewDisplayBackground.CreateGradient(white, white, white));
                    views.Add(view3D.Id);
                    var graphicDisplayOptions= view3D.get_Parameter(BuiltInParameter.MODEL_GRAPHICS_STYLE);

                    // Settings for best quality

                    graphicDisplayOptions.Set(6);
                }
            }
            catch (Autodesk.Revit.Exceptions.InvalidOperationException)
            {
            }

            var ieo = new ImageExportOptions
            {
                FilePath = tempImageFile,
                FitDirection = FitDirectionType.Horizontal,
                HLRandWFViewsFileType = ImageFileType.PNG,
                ImageResolution = ImageResolution.DPI_150,
                ShouldCreateWebSite = false
            };

            if (views.Count > 0)
            {
                ieo.SetViewsAndSheets(views);
                ieo.ExportRange = ExportRange.SetOfViews;
            }
            else
            {
                ieo.ExportRange = ExportRange.VisibleRegionOfCurrentView;
            }

            ieo.ZoomType = ZoomFitType.FitToPage;
            ieo.ViewName = "tmp";

            if (ImageExportOptions.IsValidFileName(tempImageFile))
            {
                // If ExportRange = ExportRange.SetOfViews 
                // and document is not active, then image 
                // exports successfully, but throws
                // Autodesk.Revit.Exceptions.InternalException

                try
                {
                    doc.ExportImage(ieo);
                }
                catch
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }

            // File name has format like "tempFileName - view type - view name", e.g.
            // "luccwjkz - 3D View - {3D}.png".Get the first image (we only listed one view
            // in views).

            var files = Directory.GetFiles(Path.GetTempPath(),string.Format("{0}*.*", Path.GetFileNameWithoutExtension(tempFileName)));

            return files.Length > 0
              ? files[0]
              : string.Empty;
        }

        /// <summary>
        /// Wrapper for old sample code.
        /// </summary>
        static Result ExportToImage2(Document doc)
        {
            Result r = Result.Failed;

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Export Image");
                string filepath = ExportToImage(doc);
                tx.RollBack();

                if (0 < filepath.Length)
                {
                    Process.Start(filepath);
                    r = Result.Succeeded;
                }
            }
            return r;
        }        
        static Result ExportToImage3(Document doc)
        {
            Result r = Result.Failed;

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Export Image");
                string desktop_path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                View view = doc.ActiveView;
                string filepath = Path.Combine(desktop_path,view.Name);
                ImageExportOptions img = new ImageExportOptions();
                img.ZoomType = ZoomFitType.FitToPage;
                img.PixelSize = 32;
                img.ImageResolution = ImageResolution.DPI_600;
                img.FitDirection = FitDirectionType.Horizontal;
                img.ExportRange = ExportRange.CurrentView;
                img.HLRandWFViewsFileType = ImageFileType.PNG;
                img.FilePath = filepath;
                img.ShadowViewsFileType = ImageFileType.PNG;
                doc.ExportImage(img);
                tx.RollBack();
                filepath = Path.ChangeExtension(filepath, "png");
                Process.Start(filepath);
                r = Result.Succeeded;
            }
            return r;
        }        
    }

猜你喜欢

转载自blog.csdn.net/weixin_42479664/article/details/81909723