Пример #1
0
        public void CopyElementsBetweenSheets(SheetCopierViewHost sheet)
        {
            IList <ElementId> list = new List <ElementId>();

            using (var collector = new FilteredElementCollector(doc))
            {
                collector.OwnedByView(sheet.SourceSheet.Id);
                foreach (Element e in collector)
                {
                    if (!(e is Viewport))
                    {
                        if (e is CurveElement)
                        {
                            continue;
                        }
                        if (e.IsValidObject && e.ViewSpecific)
                        {
                            list.Add(e.Id);
                        }
                    }
                }
            }
            if (list.Count > 0)
            {
                Transform        transform;
                CopyPasteOptions options;
                ElementTransformUtils.CopyElements(
                    sheet.SourceSheet,
                    list,
                    sheet.DestinationSheet,
                    transform = new Transform(ElementTransformUtils.GetTransformFromViewToView(sheet.SourceSheet, sheet.DestinationSheet)),
                    options   = new CopyPasteOptions());
                if (Settings.Default.DeleteRevisionClouds)
                {
                    DeleteRevisionClouds(sheet.DestinationSheet.Id, doc);
                }
                options.Dispose();
                transform.Dispose();
            }
        }
Пример #2
0
        public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;
            Selection     sel   = uidoc.Selection;
            Transaction   trans = new Transaction(doc, "ExComm");

            trans.Start();

            DWGExportOptions options = new DWGExportOptions();
            //SUPPORT.PARAMETER.PARAMETER Para = new SUPPORT.PARAMETER.PARAMETER();
            View      view  = doc.ActiveView;
            ElementId eleid = view.Id;

            ICollection <ElementId>  views    = new List <ElementId>();
            FilteredElementCollector filter   = new FilteredElementCollector(doc);
            FilteredElementCollector Filter   = filter.OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType();
            IList <Element>          list     = Filter.ToElements();
            IList <Element>          lishname = new List <Element>();
            ViewDrafting             drafting = null;
            ElementId newlegend   = null;
            string    currentview = doc.ActiveView.ViewName;
            string    test        = "Zz_" + currentview + "##";
            ElementId id          = null;
            View      viewlegend  = null;

            foreach (var ele1 in list)
            {
                Parameter      parameter = ele1.get_Parameter(BuiltInParameter.VIEW_NAME);
                string         viewname  = parameter.AsString();
                ViewFamilyType vd        = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).Cast <ViewFamilyType>()
                                           .FirstOrDefault(q => q.ViewFamily == ViewFamily.Drafting);
                views.Add(doc.ActiveView.Id);
                doc.Export(@"C:\Autodesk", "exportdwg.dwg", views, options);
                drafting = ViewDrafting.Create(doc, vd.Id);
                doc.Regenerate();

                DWGImportOptions importOptions = new DWGImportOptions();
                importOptions.ColorMode = ImportColorMode.BlackAndWhite;
                doc.Import(@"C:\Autodesk\\exportdwg.dwg", importOptions, drafting, out id);
                try
                {
                    drafting.Name = test;
                    trans.Commit();
                    commandData.Application.ActiveUIDocument.ActiveView = drafting;
                }
                catch
                {
                    TaskDialog.Show("ERROR", "SECTION NÀY ĐÃ ĐƯỢC TẠO FROZEN");

                    trans.RollBack();
                    break;
                }
                break;
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Transaction Name");

                Element               curElem     = doc.GetElement(id);
                ImportInstance        curLink     = (ImportInstance)curElem;
                List <GeometryObject> curveobject = GetLinkedDWGCurves(curLink, doc);

                foreach (GeometryObject curGeom in curveobject)
                {
                    if (curGeom.GetType() == typeof(PolyLine))
                    {
                        // create polyline in current view
                        PolyLine curPolyline = (PolyLine)curGeom;

                        // get polyline coordinate points
                        IList <XYZ> ptsList = curPolyline.GetCoordinates();

                        for (var i = 0; i <= ptsList.Count - 2; i++)
                        {
                            // create detail curve from polyline coordinates
                            try
                            {
                                DetailCurve newDetailLine = doc.Create.NewDetailCurve(doc.ActiveView, Line.CreateBound(ptsList[i], ptsList[i + 1]));
                            }
                            catch
                            {
                            }
                        }
                    }
                    else
                    {
                        try { DetailCurve newDetailLine = doc.Create.NewDetailCurve(doc.ActiveView, (Curve)curGeom); }
                        catch { }
                    }
                }

                FilteredElementCollector legCollector = new FilteredElementCollector(doc).OfClass(typeof(View)).WhereElementIsNotElementType();
                List <View> alllegends = new List <View>();
                foreach (ElementId eid in legCollector.ToElementIds())
                {
                    View v = doc.GetElement(eid) as View;
                    if (v.ViewType == ViewType.Legend)
                    {
                        alllegends.Add(v);
                    }
                }

                newlegend  = alllegends.Last().Duplicate(ViewDuplicateOption.WithDetailing);
                viewlegend = doc.GetElement(newlegend) as View;
                tx.Commit();
            }

            using (Form_FrozenSection form = new Form_FrozenSection(doc, uiapp))
            {
                form.ShowDialog();

                if (form.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return(Result.Cancelled);
                }

                if (form.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    using (Transaction tx1 = new Transaction(doc))
                    {
                        tx1.Start("Transaction Name");



                        tx1.Commit();
                        uiapp.ActiveUIDocument.ActiveView = form.vsheet();
                    }
                }
                using (Transaction tx2 = new Transaction(doc))
                {
                    tx2.Start("Transaction Name");

                    XYZ pos = uidoc.Selection.PickPoint(ObjectSnapTypes.None, "Chon diem dat legend");
                    Viewport.Create(doc, form.vsheet().Id, newlegend, pos);

                    FilteredElementCollector allElementsInView = new FilteredElementCollector(doc, viewlegend.Id).WhereElementIsNotElementType();
                    ICollection <ElementId>  elementsInView    = allElementsInView.WhereElementIsNotElementType().Where(x => x.Category != null).Select(x => x.Id).ToList();
                    doc.Delete(elementsInView);

                    FilteredElementCollector detailine     = new FilteredElementCollector(doc, drafting.Id);
                    IList <ElementId>        listdetailine = detailine.OfCategory(BuiltInCategory.OST_Lines).WhereElementIsNotElementType().ToElementIds().ToList();

                    Transform tranform = ElementTransformUtils.GetTransformFromViewToView(viewlegend, doc.ActiveView);
                    ElementTransformUtils.CopyElements(drafting, listdetailine, viewlegend, tranform, new CopyPasteOptions());

                    viewlegend.Scale = 16;
                    Parameter viewname = viewlegend.LookupParameter("View Name");
                    string    name     = form.Viewname();
                    viewname.Set(name);
                    doc.Delete(drafting.Id);
                    tx2.Commit();
                }
            }
            return(Result.Succeeded);
        }
Пример #3
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);
        }