Esempio n. 1
0
        public static View View3D(this Document document, string viewName = null, BoundingBoxXYZ boundingBoxXyz = null, ViewOrientation3D viewOrientation3D = null, ElementId viewTemplateId = null, ViewDetailLevel viewDetailLevel = ViewDetailLevel.Coarse)
        {
            //for information about a perspective boundingbox and its orientation see here:
            //https://knowledge.autodesk.com/support/revit-products/learn-explore/caas/CloudHelp/cloudhelp/2014/ENU/Revit/files/GUID-A7FA8DBC-830E-482D-9B66-147399524442-htm.html

            View3D result = null;

            ViewFamilyType vft = Query.ViewFamilyType(document, ViewFamily.ThreeDimensional);

            result = Autodesk.Revit.DB.View3D.CreatePerspective(document, vft.Id);

            Modify.SetViewDetailLevel(result, viewDetailLevel);

            if (!string.IsNullOrEmpty(viewName))
            {
                try
                {
#if (REVIT2018 || REVIT2019)
                    result.ViewName = viewName;
#else
                    result.Name = viewName;
#endif
                }
                catch
                {
                    BH.Engine.Reflection.Compute.RecordWarning("There is already a view named '" + viewName + "'." + " It has been named '" + result.Name + "' instead.");
                }
            }

            if (viewTemplateId != null)
            {
                try
                {
                    result.ViewTemplateId = viewTemplateId;
                }
                catch (Exception)
                {
                    BH.Engine.Reflection.Compute.RecordWarning("Could not apply the View Template of Id " + viewTemplateId + "'." + ". Please check if it's a valid ElementId.");
                }
            }

            if (viewOrientation3D != null)
            {
                try
                {
                    result.SetOrientation(viewOrientation3D);
                }
                catch (Exception)
                {
                    BH.Engine.Reflection.Compute.RecordWarning("Could not set the view's orientation in 3D due to unexpected values, please report this error.");
                }
            }

            if (boundingBoxXyz != null)
            {
                try
                {
                    //transform to view coordinates previously picked points
                    BoundingBoxXYZ bb        = result.get_BoundingBox(result);
                    Transform      transform = bb.Transform;

                    bb.Max = transform.Inverse.OfPoint(boundingBoxXyz.Max);
                    bb.Min = transform.Inverse.OfPoint(boundingBoxXyz.Min);

                    result.CropBox = bb;

                    //changes the view's far clip to the the eye and target line's length (x1.5 offset)
                    result.SetParameter(BuiltInParameter.VIEWER_BOUND_OFFSET_FAR, boundingBoxXyz.Min.DistanceTo(boundingBoxXyz.Max) * 1.5);
                }
                catch (Exception)
                {
                    BH.Engine.Reflection.Compute.RecordWarning("Could not set the view's bounding box due to unexpected values, please report this error.");
                }
            }

            return(result);
        }