Exemple #1
0
        private List <string> ExportDraftingView(Document recipientDoc, List <ElementId> viewIds)
        {
            List <string> fileNames = new List <string>();

            try
            {
                string tempFileName  = System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(), "png");
                string tempImageFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), tempFileName);

                ImageExportOptions option = new ImageExportOptions();
                option.FilePath = tempImageFile;
                option.HLRandWFViewsFileType = ImageFileType.PNG;
                option.ImageResolution       = ImageResolution.DPI_300;
                option.ShouldCreateWebSite   = false;
                option.SetViewsAndSheets(viewIds);
                option.ExportRange = ExportRange.SetOfViews;

                if (ImageExportOptions.IsValidFileName(tempImageFile))
                {
                    recipientDoc.ExportImage(option);
                }

                fileNames = Directory.GetFiles(System.IO.Path.GetTempPath(), string.Format("{0}*.*", System.IO.Path.GetFileNameWithoutExtension(tempFileName))).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to export draftingView to image.\n" + ex.Message, "ExportDraftingView", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(fileNames);
        }
Exemple #2
0
        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //export image
                ImageExportOptions option = new ImageExportOptions();
                option.HLRandWFViewsFileType = ImageFileType.PNG;
                option.ImageResolution       = ImageResolution.DPI_300;
                option.ShouldCreateWebSite   = false;
                option.ExportRange           = ExportRange.SetOfViews;

                List <ElementId> viewIds = new List <ElementId>();
                int        index         = pageNum - 1;
                PreviewMap preview       = previewMapList[index];
                if (null != preview.RecipientViewProperties)
                {
                    if (preview.RecipientViewProperties.ViewId > 0)
                    {
                        string tempFileName  = System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(), "png");
                        string tempImageFile = System.IO.Path.Combine(tempFolder, tempFileName);

                        viewIds.Add(new ElementId(preview.RecipientViewProperties.ViewId));
                        option.FilePath = tempImageFile;
                        option.SetViewsAndSheets(viewIds);
                        if (ImageExportOptions.IsValidFileName(tempImageFile))
                        {
                            string imageFileName = ExportDraftingView(preview.RecipientModelInfo.Doc, option);
                            if (!string.IsNullOrEmpty(preview.ViewLinkInfo.DestImagePath1))
                            {
                                if (File.Exists(preview.ViewLinkInfo.DestImagePath1))
                                {
                                    File.Delete(preview.ViewLinkInfo.DestImagePath1);
                                }
                            }
                            if (!string.IsNullOrEmpty(preview.ViewLinkInfo.DestImagePath2))
                            {
                                preview.ViewLinkInfo.DestImagePath1 = preview.ViewLinkInfo.DestImagePath2;
                            }
                            preview.ViewLinkInfo.DestImagePath2 = imageFileName;
                            imageRecipient.Source = new BitmapImage(new Uri(preview.ViewLinkInfo.DestImagePath2));
                        }
                    }
                    previewMapList.RemoveAt(index);
                    previewMapList.Insert(index, preview);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to refresh the preview image.\n" + ex.Message, "Refresh Preview Image", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
        public static void GetImagesFromDocument(Document familyDoc, string targetDirectory)
        {
            //IL_000f: Unknown result type (might be due to invalid IL or missing references)
            //IL_0014: Unknown result type (might be due to invalid IL or missing references)
            //IL_0015: Unknown result type (might be due to invalid IL or missing references)
            //IL_001c: Unknown result type (might be due to invalid IL or missing references)
            //IL_0023: Unknown result type (might be due to invalid IL or missing references)
            //IL_002a: Unknown result type (might be due to invalid IL or missing references)
            //IL_0031: Unknown result type (might be due to invalid IL or missing references)
            //IL_0038: Unknown result type (might be due to invalid IL or missing references)
            //IL_003f: Unknown result type (might be due to invalid IL or missing references)
            //IL_004a: Unknown result type (might be due to invalid IL or missing references)
            //IL_0051: Unknown result type (might be due to invalid IL or missing references)
            //IL_0052: Unknown result type (might be due to invalid IL or missing references)
            //IL_0057: Unknown result type (might be due to invalid IL or missing references)
            //IL_0058: Unknown result type (might be due to invalid IL or missing references)
            //IL_0063: Unknown result type (might be due to invalid IL or missing references)
            //IL_0080: Unknown result type (might be due to invalid IL or missing references)
            //IL_0085: Unknown result type (might be due to invalid IL or missing references)
            //IL_0087: Unknown result type (might be due to invalid IL or missing references)
            //IL_0089: Expected O, but got Unknown
            //IL_008e: Unknown result type (might be due to invalid IL or missing references)
            //IL_0090: Unknown result type (might be due to invalid IL or missing references)
            //IL_0094: Unknown result type (might be due to invalid IL or missing references)
            //IL_009d: Unknown result type (might be due to invalid IL or missing references)
            //IL_00a7: Unknown result type (might be due to invalid IL or missing references)
            //IL_00a9: Unknown result type (might be due to invalid IL or missing references)
            //IL_00ca: Unknown result type (might be due to invalid IL or missing references)
            //IL_0118: Unknown result type (might be due to invalid IL or missing references)
            //IL_0119: Unknown result type (might be due to invalid IL or missing references)
            if (!Directory.Exists(targetDirectory))
            {
                Util_Helper.CreateDirectory(targetDirectory);
            }
            ImageExportOptions val = new ImageExportOptions();

            val.set_ExportRange(2);
            val.set_FilePath(targetDirectory);
            val.set_HLRandWFViewsFileType(2);
            val.set_ImageResolution(0);
            val.set_ShadowViewsFileType(2);
            val.set_ZoomType(0);
            val.set_PixelSize(512);
            val.set_FitDirection(0);
            FilteredElementCollector val2  = new FilteredElementCollector(familyDoc);
            IList <Element>          list  = val2.OfClass(typeof(View)).ToElements();
            IList <ElementId>        list2 = new List <ElementId>();

            foreach (Element item in list)
            {
                View val3 = item as View;
                if ((int)val3 != 0 && !val3.get_IsTemplate() && val3.get_CanBePrinted())
                {
                    list2.Add(val3.get_Id());
                }
            }
            val.SetViewsAndSheets(list2);
            if (Directory.Exists(targetDirectory))
            {
                string[] files = Directory.GetFiles(targetDirectory, "*.jpg");
                foreach (string path in files)
                {
                    File.Delete(path);
                }
            }
            if (ImageExportOptions.IsValidFileName(targetDirectory) && list2.Count > 0)
            {
                try
                {
                    familyDoc.ExportImage(val);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Exemple #4
0
        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
            {
#if !VERSION2014
                var direction = new XYZ(-1, 1, -1);
                var view3D    = doc.IsFamilyDocument
      ? doc.FamilyCreate.NewView3D(direction)
      : doc.Create.NewView3D(direction);
#else
                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;
#endif // VERSION2014

                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);
        }
Exemple #5
0
        private OrthogonalCamera GetOrthogonalCamera(Dictionary <int, ElementProperties> elementDictionary, string imagePath)
        {
            OrthogonalCamera orthoCamera = new OrthogonalCamera();

            try
            {
                BoundingBoxXYZ boundingBox = new BoundingBoxXYZ();
                boundingBox.Enabled = true;
                for (int i = 0; i < 3; i++)
                {
                    boundingBox.set_MinEnabled(i, true);
                    boundingBox.set_MaxEnabled(i, true);
                    boundingBox.set_BoundEnabled(0, i, true);
                    boundingBox.set_BoundEnabled(1, i, true);
                }

                BoundingBoxXYZ tempBoundingBox = elementDictionary.First().Value.RevitElement.get_BoundingBox(null);
                tempBoundingBox.Enabled = true;

                double maxX = tempBoundingBox.Max.X;
                double maxY = tempBoundingBox.Max.Y;
                double maxZ = tempBoundingBox.Max.Z;
                double minX = tempBoundingBox.Min.X;
                double minY = tempBoundingBox.Min.Y;
                double minZ = tempBoundingBox.Min.Z;

                List <ElementId>           elementIds = new List <ElementId>();
                Dictionary <int, Category> categories = new Dictionary <int, Category>();
                foreach (ElementProperties ep in elementDictionary.Values)
                {
                    Element element = ep.RevitElement;
                    if (null != element)
                    {
                        try
                        {
                            if (!categories.ContainsKey(element.Category.Id.IntegerValue))
                            {
                                categories.Add(element.Category.Id.IntegerValue, element.Category);
                            }
                            BoundingBoxXYZ bbBox = element.get_BoundingBox(null);
                            bbBox.Enabled = true;
                            elementIds.Add(element.Id);
                            if (null != boundingBox)
                            {
                                if (bbBox.Max.X > maxX)
                                {
                                    maxX = bbBox.Max.X;
                                }
                                if (bbBox.Max.Y > maxY)
                                {
                                    maxY = bbBox.Max.Y;
                                }
                                if (bbBox.Max.Z > maxZ)
                                {
                                    maxZ = bbBox.Max.Z;
                                }
                                if (bbBox.Min.X < minX)
                                {
                                    minX = bbBox.Min.X;
                                }
                                if (bbBox.Min.Y < minY)
                                {
                                    minY = bbBox.Min.Y;
                                }
                                if (bbBox.Min.Z < minZ)
                                {
                                    minZ = bbBox.Min.Z;
                                }
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }

                XYZ xyzMax = new XYZ(maxX, maxY, maxZ);
                XYZ xyzMin = new XYZ(minX, minY, minZ);

                boundingBox.set_Bounds(0, xyzMin);
                boundingBox.set_Bounds(1, xyzMax);


                ViewFamilyType           view3dFamilyType = null;
                FilteredElementCollector collector        = new FilteredElementCollector(m_doc);
                List <Element>           elements         = collector.OfClass(typeof(ViewFamilyType)).ToElements().ToList();
                foreach (Element element in elements)
                {
                    ViewFamilyType viewfamilytype = element as ViewFamilyType;
                    if (viewfamilytype.ViewFamily == ViewFamily.ThreeDimensional)
                    {
                        view3dFamilyType = viewfamilytype; break;
                    }
                }

                if (null != view3dFamilyType)
                {
                    using (TransactionGroup transGroup = new TransactionGroup(m_doc))
                    {
                        transGroup.Start("Start Creating View 3D");
                        using (Transaction trans = new Transaction(m_doc))
                        {
                            trans.Start("Create View");

                            View3D view3d = View3D.CreateIsometric(m_doc, view3dFamilyType.Id);
                            view3d.SetSectionBox(boundingBox);
                            view3d.GetSectionBox().Enabled = true;
                            view3d.DetailLevel = ViewDetailLevel.Fine;

                            foreach (Category category in categories.Values)
                            {
                                if (category.get_AllowsVisibilityControl(view3d))
                                {
#if RELEASE2017 || RELEASE2018
                                    view3d.SetCategoryHidden(category.Id, false);
#else
                                    view3d.SetVisibility(category, true);
#endif
                                }
                            }

                            view3d.get_Parameter(BuiltInParameter.MODEL_GRAPHICS_STYLE).Set(4);

                            //m_app.ActiveUIDocument.ActiveView = view3d;
                            //m_app.ActiveUIDocument.RefreshActiveView();

                            XYZ   eyePostion = view3d.GetOrientation().EyePosition;
                            Point viewPoint  = new Point();
                            viewPoint.X = eyePostion.X; viewPoint.Y = eyePostion.Y; viewPoint.Z = eyePostion.Z;
                            orthoCamera.CameraViewPoint = viewPoint;

                            XYZ       forwardDirection = view3d.GetOrientation().ForwardDirection;
                            Direction fDirection       = new Direction();
                            fDirection.X = forwardDirection.X; fDirection.Y = forwardDirection.Y; fDirection.Z = forwardDirection.Z;
                            orthoCamera.CameraDirection = fDirection;

                            XYZ       upDirection = view3d.GetOrientation().UpDirection;
                            Direction uDirection  = new Direction();
                            uDirection.X = upDirection.X; uDirection.Y = upDirection.Y; uDirection.Z = upDirection.Z;
                            orthoCamera.CameraUpVector = uDirection;

                            orthoCamera.ViewToWorldScale = view3d.Scale;
                            m_app.ActiveUIDocument.RefreshActiveView();
                            trans.Commit();

                            trans.Start("Export Image");
                            //create snapshot.png
                            ImageExportOptions option = new ImageExportOptions();
                            option.HLRandWFViewsFileType = ImageFileType.PNG;
                            option.ImageResolution       = ImageResolution.DPI_300;
                            option.ShouldCreateWebSite   = false;
                            option.ExportRange           = ExportRange.SetOfViews;
                            option.FilePath = imagePath;
                            List <ElementId> viewIds = new List <ElementId>();
                            viewIds.Add(view3d.Id);
                            option.SetViewsAndSheets(viewIds);

                            if (ImageExportOptions.IsValidFileName(option.FilePath))
                            {
                                m_doc.ExportImage(option);
                            }
                            trans.Commit();
                        }
                        transGroup.RollBack();
                    }

                    if (File.Exists(imagePath))
                    {
                        File.Delete(imagePath);
                    }
                    string[] fileNames = Directory.GetFiles(Path.GetDirectoryName(imagePath), "snapshot*");
                    foreach (string fName in fileNames)
                    {
                        if (Path.GetExtension(fName) == ".png" || Path.GetExtension(fName) == ".jpg")
                        {
                            File.Move(fName, imagePath);
                            if (File.Exists(fName))
                            {
                                File.Delete(fName);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to get the orthogonal camera.\n" + ex.Message, "Get Orthogonal Camera", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(orthoCamera);
        }
Exemple #6
0
        private bool UpdateViews()
        {
            bool result = false;

            try
            {
                progressBar.Visibility = System.Windows.Visibility.Visible;
                statusLable.Visibility = System.Windows.Visibility.Visible;
                statusLable.Text       = "Duplicating Views . . .";

                progressBar.Minimum = 0;
                progressBar.Maximum = previewMapList.Count;
                progressBar.Value   = 0;

                double value = 0;

                UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar.SetValue);
                DuplicateUtils.errorMessage = new StringBuilder();
                List <PreviewMap> updatedList = new List <PreviewMap>();
                for (int i = 0; i < previewMapList.Count; i++)
                {
                    value += 1;
                    if (previewMapList[i].IsEnabled)
                    {
                        PreviewMap previewMap = DuplicateUtils.DuplicateView(previewMapList[i], createSheet);
                        updatedList.Add(previewMap);
                    }
                    Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
                }

                if (DuplicateUtils.errorMessage.Length > 0)
                {
                    MessageBox.Show("Following drafting views contains problems.\n\n" + DuplicateUtils.errorMessage.ToString(), "Errors in Duplicating Views", MessageBoxButton.OK, MessageBoxImage.Information);
                }

                previewMapList = new List <PreviewMap>();
                previewMapList = updatedList;
                //exportView one by one
                ImageExportOptions option = new ImageExportOptions();
                option.HLRandWFViewsFileType = ImageFileType.PNG;
                option.ImageResolution       = ImageResolution.DPI_300;
                option.ShouldCreateWebSite   = false;
                option.ExportRange           = ExportRange.SetOfViews;

                List <ElementId> viewIds = new List <ElementId>();
                foreach (PreviewMap preview in previewMapList)
                {
                    viewIds.Clear();
                    if (null != preview.RecipientViewProperties)
                    {
                        if (preview.RecipientViewProperties.ViewId > 0)
                        {
                            string tempFileName  = System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(), "png");
                            string tempImageFile = System.IO.Path.Combine(tempFolder, tempFileName);

                            viewIds.Add(new ElementId(preview.RecipientViewProperties.ViewId));
                            option.FilePath = tempImageFile;
                            option.SetViewsAndSheets(viewIds);
                            if (ImageExportOptions.IsValidFileName(tempImageFile))
                            {
                                string imageFileName = ExportDraftingView(preview.RecipientModelInfo.Doc, option);

                                if (!string.IsNullOrEmpty(preview.ViewLinkInfo.DestImagePath1))
                                {
                                    if (File.Exists(preview.ViewLinkInfo.DestImagePath1))
                                    {
                                        File.Delete(preview.ViewLinkInfo.DestImagePath1);
                                    }
                                }
                                if (!string.IsNullOrEmpty(preview.ViewLinkInfo.DestImagePath2))
                                {
                                    preview.ViewLinkInfo.DestImagePath1 = preview.ViewLinkInfo.DestImagePath2;
                                }
                                preview.ViewLinkInfo.DestImagePath2 = imageFileName;
                            }
                        }
                    }
                }

                result = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to update drafting views.\n" + ex.Message, "Update Views", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(result);
        }