/// <summary>
        /// Create perspective view with camera settings
        /// matching the Forge Viewer.
        /// </summary>
        void CreatePerspectiveViewMatchingCamera(
            Document doc,
            XYZ camera_position,
            XYZ target)
        {
            using (var trans = new Transaction(doc))
            {
                trans.Start("Map Forge Viewer Camera");

                ViewFamilyType typ
                    = new FilteredElementCollector(doc)
                      .OfClass(typeof(ViewFamilyType))
                      .Cast <ViewFamilyType>()
                      .First <ViewFamilyType>(
                          x => x.ViewFamily.Equals(
                              ViewFamily.ThreeDimensional));

                // Create a new perspective 3D view

                View3D view3D = View3D.CreatePerspective(
                    doc, typ.Id);

                Random rnd = new Random();
                view3D.Name = string.Format("Camera{0}",
                                            rnd.Next());

                // By default, the 3D view uses a default
                // orientation. Change that by creating and
                // setting up a suitable ViewOrientation3D.

                //var position = new XYZ( -15.12436009332275,
                //  -8.984616232971192, 4.921260089050291 );

                var up = XYZ.BasisZ;

                //var target = new XYZ( -15.02436066552734,
                //  -8.984211875061035, 4.921260089050291 );

                var sightDir = target.Subtract(camera_position).Normalize();

                var orientation = new ViewOrientation3D(
                    camera_position, up, sightDir);

                view3D.SetOrientation(orientation);

                // Turn off the far clip plane, etc.

                view3D.LookupParameter("Far Clip Active")
                .Set(0);

                view3D.LookupParameter("Crop Region Visible")
                .Set(1);

                view3D.LookupParameter("Crop View")
                .Set(1);

                trans.Commit();
            }
        }
Ejemplo n.º 2
0
        private bool DuplicateCameraView(ModelInfo sModel, ModelInfo rModel, CameraViewInfo cameraInfo, ViewFamilyType vFamilyType, out CameraViewInfo createdViewInfo)
        {
            bool duplicated = false;

            createdViewInfo = null;
            try
            {
                Document sourceDoc    = sModel.ModelDoc;
                Document recipientDoc = rModel.ModelDoc;

                using (Transaction trans = new Transaction(recipientDoc))
                {
                    trans.Start("Create Camera View");
                    try
                    {
                        View3D createdView = View3D.CreatePerspective(recipientDoc, vFamilyType.Id);
                        if (CanHaveViewName(rModel, cameraInfo.ViewName))
                        {
                            createdView.Name = cameraInfo.ViewName;
                        }
                        createdView.SetOrientation(cameraInfo.Orientation);
                        createdView.CropBoxActive      = cameraInfo.IsCropBoxOn;
                        createdView.CropBox            = cameraInfo.CropBox;
                        createdView.IsSectionBoxActive = cameraInfo.IsSectionBoxOn;
                        createdView.SetSectionBox(cameraInfo.SectionBox);
                        createdView.DisplayStyle = cameraInfo.Display;
                        //createdView.SetRenderingSettings(cameraInfo.Rendering);

                        foreach (string paramName in cameraInfo.ViewParameters.Keys)
                        {
                            Parameter sourceParam    = cameraInfo.ViewParameters[paramName];
                            Parameter recipientParam = createdView.LookupParameter(paramName);
                            if (parametersToSkip.Contains(sourceParam.Id.IntegerValue))
                            {
                                continue;
                            }

                            if (null != recipientParam && sourceParam.HasValue)
                            {
                                if (!recipientParam.IsReadOnly)
                                {
                                    switch (sourceParam.StorageType)
                                    {
                                    case StorageType.Double:
                                        try { recipientParam.Set(sourceParam.AsDouble()); }
                                        catch { }
                                        break;

                                    case StorageType.ElementId:
                                        /*
                                         * try { recipientParam.Set(sourceParam.AsElementId()); }
                                         * catch { }
                                         */
                                        break;

                                    case StorageType.Integer:
                                        try { recipientParam.Set(sourceParam.AsInteger()); }
                                        catch { }
                                        break;

                                    case StorageType.String:
                                        try { recipientParam.Set(sourceParam.AsString()); }
                                        catch { }
                                        break;
                                    }
                                }
                            }
                        }

                        if (cameraInfo.ViewTemplateId != ElementId.InvalidElementId)
                        {
                            ElementId templateId = GetLinkedItem(sModel, rModel, MapType.ViewTemplate, cameraInfo.ViewTemplateId);
                            if (templateId != ElementId.InvalidElementId && createdView.IsValidViewTemplate(templateId))
                            {
                                createdView.ViewTemplateId = templateId;
                            }
                            else
                            {
                                MissingItem missingItem = new MissingItem(cameraInfo.ViewName, "View Template", "");
                                missingItems.Add(missingItem);
                            }
                        }

                        if (cameraInfo.PhaseId != ElementId.InvalidElementId)
                        {
                            ElementId phaseId = GetLinkedItem(sModel, rModel, MapType.Phase, cameraInfo.PhaseId);
                            if (phaseId != ElementId.InvalidElementId)
                            {
                                Parameter param = createdView.get_Parameter(BuiltInParameter.VIEW_PHASE);
                                if (null != param)
                                {
                                    param.Set(phaseId);
                                }
                            }
                            else
                            {
                                MissingItem missingItem = new MissingItem(cameraInfo.ViewName, "Phase", "");
                                missingItems.Add(missingItem);
                            }
                        }

                        if (viewConfig.ApplyWorksetVisibility && cameraInfo.WorksetVisibilities.Count > 0)
                        {
                            bool worksetFound = true;
                            foreach (WorksetId wsId in cameraInfo.WorksetVisibilities.Keys)
                            {
                                WorksetVisibility wsVisibility = cameraInfo.WorksetVisibilities[wsId];
                                WorksetId         worksetId    = GetLinkedWorkset(sModel, rModel, wsId);
                                if (worksetId != WorksetId.InvalidWorksetId)
                                {
                                    createdView.SetWorksetVisibility(worksetId, wsVisibility);
                                }
                                else
                                {
                                    worksetFound = false;
                                }
                            }
                            if (!worksetFound)
                            {
                                MissingItem missingItem = new MissingItem(cameraInfo.ViewName, "Workset", "");
                                missingItems.Add(missingItem);
                            }
                        }

                        createdViewInfo = new CameraViewInfo(createdView);
                        createdViewInfo.LinkedViewId = cameraInfo.ViewId;
                        createdViewInfo.Linked       = true;
                        duplicated = true;
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.RollBack();
                        string message = ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to duplicate camera views.\n" + ex.Message, "Duplicate Camera Views", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(duplicated);
        }