/// <summary> /// /// </summary> /// <param name="modelPath"></param> /// <param name="reset"></param> /// <returns name="bool">Returns true if document successfully closed.</returns> public static bool UpgradeAuditCompact(string modelPath, [DefaultArgument("true")] bool reset) { RevitDoc doc = OpenWithOptions(modelPath, WorksetConfigurationCloseAll(), true, true); RevitDB.SaveAsOptions saveOptions = new RevitDB.SaveAsOptions(); RevitDB.WorksharingSaveAsOptions worksharingSaveAsOptions = saveOptions.GetWorksharingOptions(); worksharingSaveAsOptions.ClearTransmitted = true; worksharingSaveAsOptions.SaveAsCentral = true; saveOptions.SetWorksharingOptions(worksharingSaveAsOptions); doc.SaveAs(modelPath, saveOptions); bool results = doc.Close(true); return(results); }
private string CreateFreeformElementFamily(List <DB.Solid> solids, string name) { // FreeformElements can only be created in a family context. // so we create a temporary family to hold it. var famPath = Path.Combine(Doc.Application.FamilyTemplatePath, @"English\Metric Generic Model.rft"); if (!File.Exists(famPath)) { throw new Exception($"Could not find file Metric Generic Model.rft - {famPath}"); } var famDoc = Doc.Application.NewFamilyDocument(famPath); using (DB.Transaction t = new DB.Transaction(famDoc, "Create Freeform Elements")) { t.Start(); solids.ForEach(s => { DB.FreeFormElement.Create(famDoc, s); }); t.Commit(); } var famName = "SpeckleFreeform_" + name; string tempFamilyPath = Path.Combine(Path.GetTempPath(), famName + ".rfa"); var so = new DB.SaveAsOptions(); so.OverwriteExistingFile = true; famDoc.SaveAs(tempFamilyPath, so); famDoc.Close(); return(tempFamilyPath); }
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(); } }
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(); } } } }
// TODO: fix unit conversions since block geometry is being converted inside a new family document, which potentially has different unit settings from the main doc. // This could be done by passing in an option Document argument for all conversions that defaults to the main doc (annoying) // I suspect this also needs to be fixed for freeform elements private string BlockDefinitionToNative(BlockDefinition definition) { // convert definition geometry to native var solids = new List <DB.Solid>(); var curves = new List <DB.Curve>(); var blocks = new List <BlockInstance>(); foreach (var geometry in definition.geometry) { switch (geometry) { case Brep brep: try { var solid = BrepToNative(geometry as Brep); solids.Add(solid); } catch (Exception e) { ConversionErrors.Add(new SpeckleException($"Could not convert block {definition.id} brep to native, falling back to mesh representation.", e)); var brepMeshSolids = MeshToNative(brep.displayMesh, DB.TessellatedShapeBuilderTarget.Solid, DB.TessellatedShapeBuilderFallback.Abort) .Select(m => m as DB.Solid); solids.AddRange(brepMeshSolids); } break; case Mesh mesh: var meshSolids = MeshToNative(mesh, DB.TessellatedShapeBuilderTarget.Solid, DB.TessellatedShapeBuilderFallback.Abort) .Select(m => m as DB.Solid); solids.AddRange(meshSolids); break; case ICurve curve: try { var modelCurves = CurveToNative(geometry as ICurve); foreach (DB.Curve modelCurve in modelCurves) { curves.Add(modelCurve); } } catch (Exception e) { ConversionErrors.Add(new SpeckleException($"Could not convert block {definition.id} curve to native.", e)); } break; case BlockInstance instance: blocks.Add(instance); break; } } // create a family to represent a block definition // TODO: package our own generic model rft so this path will always work (need to change for freeform elem too) // TODO: match the rft unit to the main doc unit system (ie if main doc is in feet, pick the English Generic Model) // TODO: rename block with stream commit info prefix taken from UI - need to figure out cleanest way of storing this in the doc for retrieval by converter var famPath = Path.Combine(Doc.Application.FamilyTemplatePath, @"Metric Generic Model.rft"); if (!File.Exists(famPath)) { throw new Exception($"Could not find file Metric Generic Model.rft - {famPath}"); } var famDoc = Doc.Application.NewFamilyDocument(famPath); using (DB.Transaction t = new DB.Transaction(famDoc, "Create Block Geometry Elements")) { t.Start(); solids.ForEach(o => { DB.FreeFormElement.Create(famDoc, o); }); curves.ForEach(o => { famDoc.FamilyCreate.NewModelCurve(o, NewSketchPlaneFromCurve(o, famDoc)); }); blocks.ForEach(o => { BlockInstanceToNative(o, famDoc); }); t.Commit(); } var famName = "SpeckleBlock_" + definition.name; string familyPath = Path.Combine(Path.GetTempPath(), famName + ".rfa"); var so = new DB.SaveAsOptions(); so.OverwriteExistingFile = true; famDoc.SaveAs(familyPath, so); famDoc.Close(); return(familyPath); }