Пример #1
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document   doc   = commandData.Application.ActiveUIDocument.Document;

            ViewPlan activeview = uiDoc.ActiveView as ViewPlan;

            if (activeview == null)
            {
                TaskDialog.Show("wrong", "请在平面视图内运行."); return(Result.Failed);
            }
            Parameter para = activeview.LookupParameter("VIEW-Content");

            if (para == null)
            {
                TaskDialog.Show("wrong", "未找到VIEW-Content参数"); return(Result.Failed);
            }

            //找到提资视图样板
            ViewPlan template = null;
            FilteredElementCollector collector_template = new FilteredElementCollector(doc);

            collector_template.OfClass(typeof(ViewPlan));
            IEnumerable <ViewPlan> templates =
                from ele in collector_template
                let view = ele as ViewPlan
                           where view.Name.Equals("A-PLAN-MECH-INTF")
                           where view.IsTemplate
                           select view;

            if (templates != null && templates.Count() != 0)
            {
                template = templates.First();
            }


            //找到所有的非建筑链接文件
            FilteredElementCollector linkcollector = new FilteredElementCollector(doc);

            linkcollector.OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType();
            IEnumerable <RevitLinkInstance> links = from ele in linkcollector
                                                    let ins                       = ele as RevitLinkInstance
                                                                         let name = ins.Name
                                                                                    where name[0] != 'A'
                                                                                    select ins;

            //获取所有PLOT视图
            List <ViewPlan> plots = GetPLOT(doc);

            if (plots.Count() == 0)
            {
                TaskDialog.Show("wrong", "未找到PLOT视图.确认视图名称内含PLOT,并且VIEW-Content参数设置为02.PLOT"); return(Result.Failed);
            }

            //让用户选择哪几个PLOT视图需要生成INTF视图.
            List <ViewPlan> plots_selected = new List <ViewPlan>();
            MyWindow        window         = new MyWindow();

            window.listbox.ItemsSource = plots;
            if (window.ShowDialog() == true)
            {
                var objs = window.listbox.SelectedItems;
                foreach (Object obj in objs)
                {
                    plots_selected.Add(obj as ViewPlan);
                }
            }
            if (plots_selected.Count == 0)
            {
                return(Result.Failed);
            }

            //记录对应的intf视图.
            List <ViewPlan> intfs = new List <ViewPlan>();
            //记录没有对应intf视图的plot视图.
            List <ViewPlan> plots_withoutintf = new List <ViewPlan>();
            string          name_intfs        = " ";

            foreach (ViewPlan plot in plots_selected)
            {
                ViewPlan intf = FindINTFByPLOT(plot);
                if (intf == null)
                {
                    plots_withoutintf.Add(plot);
                }
                else
                {
                    intfs.Add(intf);

                    name_intfs = name_intfs + "\n" + intf.Name;
                }
            }
            name_intfs += "\n以上INTF视图已生成,将删除现有视图并重新生成.其间可能出现Revit未响应的情况,请不要惊慌,去倒杯茶回来就好了.";
            if (intfs.Count != 0)
            {
                TaskDialog.Show("goa", name_intfs);
            }

            //预先设置视图样板,以保证后面copy 2D图元的时候减少错误的发生.
            if (template != null)
            {
                using (Transaction transaction_settemplate = new Transaction(doc))
                {
                    transaction_settemplate.Start("set template");
                    foreach (View intf in intfs)
                    {
                        intf.ViewTemplateId = template.Id;
                    }
                    transaction_settemplate.Commit();
                }
            }


            //开启事务 将PLOT的2D图元更新到INTF
            using (Transaction transaction_delete2D = new Transaction(doc))
            {
                transaction_delete2D.Start("delete 2d ");
                List <ElementId> deleteids = new List <ElementId>();
                foreach (ViewPlan intf in intfs)
                {
                    deleteids.AddRange(FindAll2DElmentIds(intf));
                }
                doc.Delete(deleteids);
                transaction_delete2D.Commit();
            }
            using (Transaction transaction_copy2D = new Transaction(doc))
            {
                transaction_copy2D.Start("copy 2d ");
                string errinfo = "以下图元在无法复制:";
                foreach (ViewPlan intf in intfs)
                {
                    ViewPlan plot = FindPLOTByINTF(intf);
                    if (plot == null)
                    {
                        continue;
                    }
                    List <ElementId> ids_2D    = FindAll2DElmentIds(plot);
                    Transform        transform = ElementTransformUtils.GetTransformFromViewToView(plot, intf);
                    //ElementTransformUtils.CopyElements(plot, ids_2D, intf, transform, new CopyPasteOptions());
                    foreach (ElementId id in ids_2D)
                    {
                        List <ElementId> ids = new List <ElementId>();
                        ids.Add(id);
                        try
                        {
                            ElementTransformUtils.CopyElements(plot, ids, intf, transform, new CopyPasteOptions());
                        }
                        catch
                        {
                            errinfo = errinfo + "\n ID:" + id.IntegerValue + "类别:" + doc.GetElement(id).Category.Name;
                            continue;
                        }
                    }
                }
                TaskDialog.Show("goa", errinfo);
                transaction_copy2D.Commit();
            }

            //开启事务 带细节复制plot视图
            using (Transaction transaction_duplicate = new Transaction(doc))
            {
                transaction_duplicate.Start("带细节复制plot视图");
                foreach (ViewPlan plot in plots_withoutintf)
                {
                    ElementId id_newintf = plot.Duplicate(ViewDuplicateOption.WithDetailing);
                    ViewPlan  newintf    = doc.GetElement(id_newintf) as ViewPlan;
                    //重命名视图名字
                    newintf.Name = plot.Name.Replace("PLOT", "INTF");
                    //共享参数VIEW-Content中 PLOT修改为INTF
                    Parameter view_content = newintf.LookupParameter("VIEW-Content");
                    view_content.Set("03.INTF");
                }
                transaction_duplicate.Commit();
            }

            string wrong = "以下PLOT视图似乎存在问题,请手动进行提资操作.";

            using (Transaction transaction = new Transaction(doc))
            {
                transaction.Start("deal");
                foreach (ViewPlan plot in plots_selected)
                {
                    ViewPlan intf = FindINTFByPLOT(plot);
                    if (intf == null)
                    {
                        wrong = wrong + "\n" + plot.Name; continue;
                    }

                    HideHDDNTextNote(intf);
                    HideHDDNAnnotation(intf);
                    HideANNODimension(intf);

                    //应用视图样板
                    if (template != null)
                    {
                        intf.ViewTemplateId = template.Id;
                    }

                    //关闭非建筑链接
                    List <ElementId> links_tohidden = new List <ElementId>();
                    foreach (RevitLinkInstance rli in links)
                    {
                        if (rli == null)
                        {
                            continue;
                        }
                        if (rli.CanBeHidden(intf))
                        {
                            links_tohidden.Add(rli.Id);
                        }
                    }
                    intf.HideElements(links_tohidden);
                }

                transaction.Commit();
            }

            return(Result.Succeeded);
        }
Пример #2
0
        private bool DuplicatePlanView(ModelInfo sModel, ModelInfo rModel, PlanViewInfo planInfo, ViewFamilyType vFamilyType, out PlanViewInfo createdPlanInfo)
        {
            bool duplicated = false;

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

                using (Transaction trans = new Transaction(recipientDoc))
                {
                    trans.Start("Create Plan View");
                    try
                    {
                        ViewPlan  createdView = null;
                        ElementId levelId     = GetLinkedItem(sModel, rModel, MapType.Level, planInfo.LevelId);
                        if (levelId != ElementId.InvalidElementId)
                        {
                            if (planInfo.PlanViewType == ViewType.AreaPlan)
                            {
                                ElementId areaSchemeId = GetLinkedItem(sModel, rModel, MapType.AreaScheme, planInfo.AreaSchemeId);
                                if (areaSchemeId != ElementId.InvalidElementId)
                                {
                                    createdView = ViewPlan.CreateAreaPlan(recipientDoc, areaSchemeId, levelId);
                                }
                                else
                                {
                                    MissingItem missingItem = new MissingItem(planInfo.ViewName, "Area Scheme", "");
                                    missingItems.Add(missingItem);
                                }
                            }
                            else
                            {
                                createdView = ViewPlan.Create(recipientDoc, vFamilyType.Id, levelId);
                            }

                            if (null != createdView)
                            {
                                if (CanHaveViewName(rModel, planInfo.ViewName))
                                {
                                    createdView.Name = planInfo.ViewName;
                                }
                                createdView.CropBoxActive = planInfo.IsCropBoxOn;
                                createdView.CropBox       = planInfo.CropBox;
                                createdView.DisplayStyle  = planInfo.Display;

                                foreach (string paramName in planInfo.ViewParameters.Keys)
                                {
                                    Parameter sourceParam    = planInfo.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 (planInfo.ViewTemplateId != ElementId.InvalidElementId)
                                {
                                    ElementId templateId = GetLinkedItem(sModel, rModel, MapType.ViewTemplate, planInfo.ViewTemplateId);
                                    if (templateId != ElementId.InvalidElementId && createdView.IsValidViewTemplate(templateId))
                                    {
                                        createdView.ViewTemplateId = templateId;
                                    }
                                    else
                                    {
                                        MissingItem missingItem = new MissingItem(planInfo.ViewName, "View Template", "");
                                        missingItems.Add(missingItem);
                                    }
                                }

                                if (planInfo.ScopeBoxId != ElementId.InvalidElementId)
                                {
                                    ElementId scopeboxId = GetLinkedItem(sModel, rModel, MapType.ScopeBox, planInfo.ScopeBoxId);
                                    if (scopeboxId != ElementId.InvalidElementId)
                                    {
                                        Parameter param = createdView.get_Parameter(BuiltInParameter.VIEWER_VOLUME_OF_INTEREST_CROP);
                                        if (null != param)
                                        {
                                            param.Set(scopeboxId);
                                        }
                                    }
                                    else
                                    {
                                        MissingItem missingItem = new MissingItem(planInfo.ViewName, "Scope Box", "");
                                        missingItems.Add(missingItem);
                                    }
                                }

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

                                if (viewConfig.ApplyWorksetVisibility && planInfo.WorksetVisibilities.Count > 0)
                                {
                                    bool worksetFound = true;
                                    foreach (WorksetId wsId in planInfo.WorksetVisibilities.Keys)
                                    {
                                        WorksetVisibility wsVisibility = planInfo.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(planInfo.ViewName, "Workset", "");
                                        missingItems.Add(missingItem);
                                    }
                                }

                                createdPlanInfo = new PlanViewInfo(createdView);
                                createdPlanInfo.LinkedViewId = planInfo.ViewId;
                                createdPlanInfo.Linked       = true;
                                duplicated = true;
                            }
                        }
                        else
                        {
                            MissingItem missingItem = new MissingItem(planInfo.ViewName, "Level", planInfo.LevelName);
                            missingItems.Add(missingItem);
                        }

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.RollBack();
                        string message = ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(planInfo.ViewName + ": Failed to duplicate a plan view.\n" + ex.Message, "Duplicate Plan View", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(duplicated);
        }