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(); }
private static Position GetCameraPosition(this UIDocument uiDocument, bool isPerspective) { ProjectPosition projectPosition = uiDocument.Document.ActiveProjectLocation.GetProjectPosition(XYZ.Zero); var zoomCorners = uiDocument.GetOpenUIViews()[0].GetZoomCorners(); XYZ bottomLeft = zoomCorners[0]; XYZ topRight = zoomCorners[1]; XYZ viewCenter = uiDocument.ActiveView.Origin; if (!isPerspective) { viewCenter = new XYZ((topRight.X + bottomLeft.X) / 2, (topRight.Y + bottomLeft.Y) / 2, (topRight.Z + bottomLeft.Z) / 2); } return(RevitUtils.TransformCameraPosition( new ProjectPositionWrapper(projectPosition), new Position( viewCenter.ToVector3(), uiDocument.ActiveView.ViewDirection.ToVector3(), uiDocument.ActiveView.UpDirection.ToVector3()))); }
public void TransformCameraPosition_ReturnsExpectedPosition( ProjectPositionWrapper projectBase, Position initialCamera, Position expectedCamera, bool reverse) { // Arrange / Act Position cameraPosition = RevitUtils.TransformCameraPosition(projectBase, initialCamera, reverse); // Assert Assert.Equal(expectedCamera.Center.X, cameraPosition.Center.X, 10); Assert.Equal(expectedCamera.Center.Y, cameraPosition.Center.Y, 10); Assert.Equal(expectedCamera.Center.Z, cameraPosition.Center.Z, 10); Assert.Equal(expectedCamera.Forward.X, cameraPosition.Forward.X, 10); Assert.Equal(expectedCamera.Forward.Y, cameraPosition.Forward.Y, 10); Assert.Equal(expectedCamera.Forward.Z, cameraPosition.Forward.Z, 10); Assert.Equal(expectedCamera.Up.X, cameraPosition.Up.X, 10); Assert.Equal(expectedCamera.Up.Y, cameraPosition.Up.Y, 10); Assert.Equal(expectedCamera.Up.Z, cameraPosition.Up.Z, 10); }