Esempio n. 1
0
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            if (!Parameters.Document.GetDataOrDefault(this, DA, "Document", out var doc))
            {
                return;
            }

            var filePath = string.Empty;

            DA.GetData("Path", ref filePath);

            var overrideFile = false;

            if (!DA.GetData("Override File", ref overrideFile))
            {
                return;
            }

            var compact = false;

            if (!DA.GetData("Compact", ref compact))
            {
                return;
            }

            var backups = -1;

            if (!DA.GetData("Backups", ref backups))
            {
                return;
            }

            var view = default(DB.View);

            if (DA.GetData("View", ref view))
            {
                if (!view.Document.Equals(doc))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"View '{view.Title}' is not a valid view in document {doc.Title}");
                    return;
                }
            }

            try
            {
                Guest.Instance.CommitTransactionGroups();

                if (string.IsNullOrEmpty(filePath))
                {
                    if (overrideFile)
                    {
                        using (var saveOptions = new DB.SaveOptions()
                        {
                            Compact = compact
                        })
                        {
                            if (view is object)
                            {
                                saveOptions.PreviewViewId = view.Id;
                            }

                            doc.Save(saveOptions);
                        }

                        DA.SetData("Document", doc);
                    }
                    else
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Failed to collect data from 'Path'.");
                    }
                }
                else
                {
                    if (filePath.Last() == Path.DirectorySeparatorChar)
                    {
                        filePath = Path.Combine(filePath, doc.Title);
                    }

                    if (Path.IsPathRooted(filePath) && filePath.Contains(Path.DirectorySeparatorChar))
                    {
                        if (!Path.HasExtension(filePath))
                        {
                            if (doc.IsFamilyDocument)
                            {
                                filePath += ".rfa";
                            }
                            else
                            {
                                filePath += ".rvt";
                            }
                        }

                        using (var saveAsOptions = new DB.SaveAsOptions()
                        {
                            OverwriteExistingFile = overrideFile, Compact = compact
                        })
                        {
                            if (backups > -1)
                            {
                                saveAsOptions.MaximumBackups = backups;
                            }

                            if (view is object)
                            {
                                saveAsOptions.PreviewViewId = view.Id;
                            }

                            doc.SaveAs(filePath, saveAsOptions);
                            DA.SetData("Document", doc);
                        }
                    }
                    else
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Path should be absolute.");
                    }
                }
            }
            finally
            {
                Guest.Instance.StartTransactionGroups();
            }
        }
Esempio n. 2
0
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            DB.Family family = null;
            if (!DA.GetData("Family", ref family))
            {
                return;
            }

            var filePath = string.Empty;

            DA.GetData("Path", ref filePath);

            var overrideFile = false;

            if (!DA.GetData("OverrideFile", ref overrideFile))
            {
                return;
            }

            var compact = false;

            if (!DA.GetData("Compact", ref compact))
            {
                return;
            }

            var backups = -1;

            if (!DA.GetData("Backups", ref backups))
            {
                return;
            }

            if (Revit.ActiveDBDocument.EditFamily(family) is DB.Document familyDoc)
            {
                using (familyDoc)
                {
                    var view = default(DB.View);
                    if (DA.GetData("PreviewView", ref view))
                    {
                        if (!view.Document.Equals(familyDoc))
                        {
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"View '{view.Title}' is not a valid view in document {familyDoc.Title}");
                            return;
                        }
                    }

                    try
                    {
                        if (string.IsNullOrEmpty(filePath))
                        {
                            if (overrideFile)
                            {
                                using (var saveOptions = new DB.SaveOptions()
                                {
                                    Compact = compact
                                })
                                {
                                    if (view is object)
                                    {
                                        saveOptions.PreviewViewId = view.Id;
                                    }

                                    familyDoc.Save(saveOptions);
                                    DA.SetData("Family", family);
                                }
                            }
                            else
                            {
                                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Failed to collect data from 'Path'.");
                            }
                        }
                        else
                        {
                            bool isFolder = filePath.Last() == Path.DirectorySeparatorChar;
                            if (isFolder)
                            {
                                filePath = Path.Combine(filePath, familyDoc.Title);
                            }

                            if (Path.IsPathRooted(filePath) && filePath.Contains(Path.DirectorySeparatorChar))
                            {
                                if (!Path.HasExtension(filePath))
                                {
                                    filePath += ".rfa";
                                }

                                using (var saveAsOptions = new DB.SaveAsOptions()
                                {
                                    OverwriteExistingFile = overrideFile, Compact = compact
                                })
                                {
                                    if (backups > -1)
                                    {
                                        saveAsOptions.MaximumBackups = backups;
                                    }

                                    if (view is object)
                                    {
                                        saveAsOptions.PreviewViewId = view.Id;
                                    }

                                    familyDoc.SaveAs(filePath, saveAsOptions);
                                    DA.SetData("Family", family);
                                }
                            }
                            else
                            {
                                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Path should be absolute.");
                            }
                        }
                    }
                    catch (Autodesk.Revit.Exceptions.InvalidOperationException e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); }
                    finally
                    {
                        familyDoc.Release();
                    }
                }
            }
        }