public View ExportDWG(Document document, View view, out string file)
        {
            bool             exported   = false;
            ElementId        outid      = ElementId.InvalidElementId;
            DWGExportOptions dwgOptions = new DWGExportOptions();

            dwgOptions.FileVersion    = ACADVersion.R2007;
            dwgOptions.ExportOfSolids = SolidGeometry.Polymesh;
            dwgOptions.ACAPreference  = ACAObjectPreference.Geometry;
            dwgOptions.MergedViews    = true;
            dwgOptions.PropOverrides  = PropOverrideMode.ByEntity;
            View v = null;
            ICollection <ElementId> views = new List <ElementId>();

            views.Add(view.Id);
            var pathfile = SettingFreeze.Instance.GetFolderPath();

            if (view is View3D)
            {
                List <ElementId> vSet = new List <ElementId>();
                bool             flag = view != null;
                if (flag)
                {
                    View3D    tmp3D     = null;
                    ViewSheet viewSheet = null;
                    bool      proceed   = false;
                    try
                    {
                        FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
                        filteredElementCollector.OfClass(typeof(FamilySymbol));
                        filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
                        IList <Element>     titleBlocks   = filteredElementCollector.ToElements();
                        List <FamilySymbol> familySymbols = new List <FamilySymbol>();
                        foreach (Element element in titleBlocks)
                        {
                            FamilySymbol f     = element as FamilySymbol;
                            bool         flag3 = f != null;
                            if (flag3)
                            {
                                familySymbols.Add(f);
                            }
                        }
                        bool flag4 = titleBlocks.Count != 0;
                        if (flag4)
                        {
                            FamilySymbol fs = null;
                            foreach (FamilySymbol f2 in familySymbols)
                            {
                                bool flag5 = f2 != null;
                                if (flag5)
                                {
                                    fs = f2;
                                    break;
                                }
                            }
                            viewSheet = ViewSheet.Create(doc, fs.Id);
                            bool flag6 = viewSheet != null;
                            if (flag6)
                            {
                                UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2.0, (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2.0);
                                try
                                {
                                    Viewport.Create(doc, viewSheet.Id, view.Id, new XYZ(location.U, location.V, 0.0));
                                }
                                catch
                                {
                                    try
                                    {
                                        XYZ             tmpXYZ           = new XYZ(-view.ViewDirection.X, -view.ViewDirection.Y, -view.ViewDirection.Z);
                                        BoundingBoxXYZ  tmpBoundingBox   = view.CropBox;
                                        bool            tmpCropBoxActive = view.CropBoxActive;
                                        IList <Element> viewTypes        = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).ToElements();
                                        ElementId       viewTypeid       = null;
                                        foreach (Element viewType in viewTypes)
                                        {
                                            ViewFamilyType famType = viewType as ViewFamilyType;
                                            bool           flag7   = famType != null && famType.ViewFamily == ViewFamily.ThreeDimensional;
                                            if (flag7)
                                            {
                                                viewTypeid = famType.Id;
                                                break;
                                            }
                                        }
                                        bool flag8 = viewTypeid != null;
                                        if (flag8)
                                        {
                                            tmp3D = View3D.CreateIsometric(doc, viewTypeid);
                                            tmp3D.ApplyViewTemplateParameters(view);
                                            tmp3D.CropBox = tmpBoundingBox;
                                            Viewport.Create(doc, viewSheet.Id, tmp3D.Id, new XYZ(location.U, location.V, 0.0));
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                                vSet.Add(viewSheet.Id);
                                proceed = true;
                            }
                        }
                    }
                    catch
                    {
                    }
                    bool flag9 = proceed;
                    if (flag9)
                    {
                        exported = document.Export(pathfile, view.Name, vSet, dwgOptions);
                        bool flag10 = viewSheet != null;
                        if (flag10)
                        {
                            ElementId elementId = viewSheet.Id;
                            doc.Delete(elementId);
                        }
                        bool flag11 = tmp3D != null;
                        if (flag11)
                        {
                            ElementId elementId2 = tmp3D.Id;
                            doc.Delete(elementId2);
                        }
                    }
                }
            }
            else
            {
                exported = document.Export(pathfile, view.Name, views, dwgOptions);
            }
            if (tmp3D != null)
            {
                file = Path.Combine(pathfile, tmp3D.Name + ".dwg");
            }
            else
            {
                file = Path.Combine(pathfile, view.Name + ".dwg");
            }
            if (exported)
            {
                TaskDialog taskDialog = new TaskDialog("Freeze View");
                taskDialog.Id                = "Freeze";
                taskDialog.Title             = "Freeze Drawing";
                taskDialog.TitleAutoPrefix   = true;
                taskDialog.MainIcon          = TaskDialogIcon.TaskDialogIconInformation;
                taskDialog.AllowCancellation = true;
                taskDialog.MainInstruction   = ("Select View Type :");
                taskDialog.AddCommandLink((TaskDialogCommandLinkId)1001, "Drafting View");
                taskDialog.AddCommandLink((TaskDialogCommandLinkId)1002, "Legend");
                taskDialog.CommonButtons = TaskDialogCommonButtons.Cancel;
                taskDialog.DefaultButton = ((TaskDialogResult)2);
                TaskDialogResult taskDialogResult = taskDialog.Show();
                var dwgimport = new DWGImportOptions();
                dwgimport.ColorMode    = ImportColorMode.BlackAndWhite;
                dwgimport.ThisViewOnly = true;
                dwgimport.OrientToView = true;
                dwgimport.Placement    = ImportPlacement.Origin;
                if (taskDialogResult == TaskDialogResult.CommandLink2)
                {
                    v = GetLegend(document);
                }
                else if (taskDialogResult == TaskDialogResult.CommandLink1)
                {
                    v = CreateDrafting(document);
                }
                if (v != null)
                {
                    document.Import(file, dwgimport, v, out outid);
                }
                string strPost  = "(Forzen)";
                string newname  = this.ReplaceForbiddenSigns(doc.ActiveView.Name);
                string tempName = newname;
                if (v != null)
                {
                    int j = 1;
                    for (; ;)
                    {
                        try
                        {
                            v.Name = newname + strPost;
                            break;
                        }
                        catch
                        {
                            bool flag2 = j > 10;
                            if (flag2)
                            {
                                try
                                {
                                    v.Name += strPost;
                                }
                                catch
                                {
                                }
                                break;
                            }
                            newname = tempName + "-" + j.ToString();
                            j++;
                        }
                    }
                }
            }
            return(v);
        }
Exemple #2
0
        //******************************************************************************************
        /// <summary>
        /// this function export View from the list
        /// </summary>
        public List <ViewPath> Export(List <View> argListView, DWGExportOptions setDwg, IREXProgress argProgress)
        {
            IREXProgress Progress = argProgress;

            Progress.Steps    = 2 * argListView.Count;
            Progress.Position = 0;
            Progress.Header   = Resources.Strings.Texts.FreezeInProgress + " - " + Resources.Strings.Texts.Export;

            List <ViewPath> nameStr = new List <ViewPath>();

            m_path = getTempPath();

            //reading files from temp directory

            setDwg.LayerMapping = GetLayMap(setDwg.LayerMapping);

            foreach (View v in argListView)
            {
                List <ElementId> vSet = new List <ElementId>();
                if (v != null)
                {
                    View3D tmp3D = null;
                    Autodesk.Revit.DB.ViewSheet viewSheet = null;
                    bool proceed = false;

                    if (v is Autodesk.Revit.DB.View3D)
                    {
                        try
                        {
                            FilteredElementCollector filteredElementCollector = new FilteredElementCollector(ThisExtension.Revit.ActiveDocument);
                            filteredElementCollector.OfClass(typeof(FamilySymbol));
                            filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
                            IList <RevitElement> titleBlocks   = filteredElementCollector.ToElements();
                            List <FamilySymbol>  familySymbols = new List <FamilySymbol>();
                            foreach (Element element in titleBlocks)
                            {
                                FamilySymbol f = element as FamilySymbol;
                                if (f != null)
                                {
                                    familySymbols.Add(f);
                                }
                            }

                            if (titleBlocks.Count != 0)
                            {
                                Autodesk.Revit.DB.FamilySymbol fs = null;
                                foreach (Autodesk.Revit.DB.FamilySymbol f in familySymbols)
                                {
                                    if (null != f)
                                    {
                                        fs = f; break;
                                    }
                                }
                                viewSheet = Autodesk.Revit.DB.ViewSheet.Create(ThisExtension.Revit.ActiveDocument, fs.Id);
                                if (null != viewSheet)
                                {
                                    UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2,
                                                         (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);

                                    try
                                    {
                                        Viewport.Create(ThisExtension.Revit.ActiveDocument, viewSheet.Id, v.Id, new XYZ(location.U, location.V, 0.0));
                                    }
                                    catch
                                    {
                                        try
                                        {
                                            XYZ            tmpXYZ           = new XYZ(-v.ViewDirection.X, -v.ViewDirection.Y, -v.ViewDirection.Z);
                                            BoundingBoxXYZ tmpBoundingBox   = v.CropBox;
                                            bool           tmpCropBoxActive = v.CropBoxActive;

                                            IList <Element> viewTypes = REXGeometryRvt.FilterElements(ThisExtension.Revit.ActiveDocument, typeof(ViewFamilyType));

                                            ElementId viewTypeid = null;

                                            foreach (Element viewType in viewTypes)
                                            {
                                                ViewFamilyType famType = viewType as ViewFamilyType;

                                                if (famType != null && famType.ViewFamily == ViewFamily.ThreeDimensional)
                                                {
                                                    viewTypeid = famType.Id;
                                                    break;
                                                }
                                            }

                                            if (viewTypeid != null)
                                            {
                                                tmp3D = View3D.CreateIsometric(ThisExtension.Revit.ActiveDocument, viewTypeid);

                                                tmp3D.ApplyViewTemplateParameters(v);
                                                tmp3D.CropBox = tmpBoundingBox;

                                                Viewport.Create(ThisExtension.Revit.ActiveDocument, viewSheet.Id, tmp3D.Id, new XYZ(location.U, location.V, 0.0));
                                            }
                                        }
                                        catch
                                        {
                                        }
                                    }

                                    vSet.Add(viewSheet.Id);
                                    proceed = true;
                                }
                            }
                        }
                        catch { }
                    }
                    else
                    {
                        vSet.Add(v.Id);
                        proceed = true;
                    }

                    if (proceed)
                    {
                        string fam = v.get_Parameter(BuiltInParameter.VIEW_FAMILY).AsString();
                        Progress.Step(fam + "-" + v.Name);

                        string vName = ValidateFileName(v.Name);
                        m_CommandData.Application.ActiveUIDocument.Document.Export(m_path, fam + "-" + vName, vSet, setDwg);

                        nameStr.Add(new ViewPath(m_path + "\\" + fam + "-" + vName + ".dwg", v, fam + "-" + v.Name));
                        Progress.Step(fam + "-" + v.Name);

                        if (viewSheet != null)
                        {
                            ElementId elementId = viewSheet.Id;
                            ThisExtension.Revit.ActiveDocument.Delete(elementId);
                        }

                        if (tmp3D != null)
                        {
                            ElementId elementId = tmp3D.Id;
                            ThisExtension.Revit.ActiveDocument.Delete(elementId);
                        }
                    }
                }
            }
            return(nameStr);
        }