public static View3D GetMatching3DView(this View view, Document doc) { ViewFamilyType viewFamilyType = (from v in new FilteredElementCollector(doc). OfClass(typeof(ViewFamilyType)). Cast <ViewFamilyType>() where v.ViewFamily == ViewFamily.ThreeDimensional select v).First(); View3D view3d = View3D.CreateIsometric(doc, viewFamilyType.Id); view3d.Name = view.Name + " 3D temp view"; ViewBox myviewbox = GetViewBox(view); if (myviewbox.bbox == null) { BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ(); boundingBoxXYZ.Min = myviewbox.P1; boundingBoxXYZ.Max = myviewbox.P2; view3d.SetSectionBox(boundingBoxXYZ); } else { view3d.SetSectionBox(myviewbox.bbox); } view3d.SetOrientation(new ViewOrientation3D(myviewbox.EyePosition, myviewbox.DirectionUp, myviewbox.DirectionView)); foreach (Category cat in doc.Settings.Categories) { try { if (cat.CategoryType == CategoryType.Model && cat.get_AllowsVisibilityControl(view3d)) { view3d.SetVisibility(cat, view.GetVisibility(cat)); } } catch (System.Exception e) { } } doc.Regenerate(); return(view3d); }
static void HideCADFiles(Document doc, View3D view) { var collection = new FilteredElementCollector(doc).WherePasses(new ElementClassFilter(typeof(ImportInstance))).ToElements().ToList(); foreach (var ele in collection) { if (ele.Category != null) { if (ele.Category.Name.Contains(".dwg") || (doc.GetElement(ele.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsElementId()) as CADLinkType) != null) { try { view.SetVisibility(ele.Category, false); } catch { continue; } } } } }
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); }