Esempio n. 1
0
        private void ApplyViewOrientationAndVisibility(UIDocument uiDocument, View3D view, Camera camera)
        {
            using var trans = new Transaction(uiDocument.Document);
            if (trans.Start($"Apply view orientation and visibility in '{view.Name}'") != TransactionStatus.Started)
            {
                return;
            }

            StatusBarService.SetStatusText("Loading view point data ...");
            Log.Information("Calculating view orientation from camera position ...");
            ProjectPosition projectPosition   = uiDocument.Document.ActiveProjectLocation.GetProjectPosition(XYZ.Zero);
            var             viewOrientation3D = RevitUtils.TransformCameraPosition(
                new ProjectPositionWrapper(projectPosition),
                camera.Position.ToInternalUnits(),
                true)
                                                .ToViewOrientation3D();

            if (camera.Type == CameraType.Perspective)
            {
                Log.Information("Setting active far viewer bound to zero ...");
                Parameter farClip = view.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR);
                if (farClip.HasValue)
                {
                    farClip.Set(0);
                }
            }

            Log.Information("Applying new view orientation ...");
            view.SetOrientation(viewOrientation3D);

            Log.Information("Applying element visibility ...");
            var currentlyVisibleElements = uiDocument.Document.GetVisibleElementsOfView(view);
            var map = uiDocument.Document.GetIfcGuidElementIdMap(currentlyVisibleElements);
            var exceptionElements = GetViewpointVisibilityExceptions(map);
            var selectedElements  = GetViewpointSelection(map);

            if (exceptionElements.Any())
            {
                if (_bcfViewpoint.GetVisibilityDefault())
                {
                    view.HideElementsTemporary(exceptionElements);
                    selectedElements = selectedElements.Where(id => !exceptionElements.Contains(id)).ToList();
                }
                else
                {
                    view.IsolateElementsTemporary(exceptionElements);
                    selectedElements = selectedElements.Where(id => exceptionElements.Contains(id)).ToList();
                }
            }

            view.ConvertTemporaryHideIsolateToPermanent();

            if (selectedElements.Any())
            {
                Log.Information("Select {n} elements ...", selectedElements.Count);
                uiDocument.Selection.SetElementIds(selectedElements);
            }

            trans.Commit();
        }